Dataset Viewer
Auto-converted to Parquet Duplicate
fact
stringlengths
9
10.6k
type
stringclasses
19 values
library
stringclasses
6 values
imports
listlengths
0
12
filename
stringclasses
101 values
symbolic_name
stringlengths
1
48
docstring
stringclasses
1 value
invH := inversion H; subst; clear H.
Ltac
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
inv
ceval_deterministic: forall c st st1 st2, c / st \\ st1 -> c / st \\ st2 -> st1 = st2. Proof. intros c st st1 st2 E1 E2; generalize dependent st2; induction E1; intros st2 E2; inv E2. - (* E_Skip *) reflexivity. - (* E_Ass *) reflexivity. - (* E_Seq *) assert (st' = st'0) as EQ1. { (* Proof of assertion *) apply IHE1_1; assumption. } subst st'0. apply IHE1_2. assumption. - (* b evaluates to true *) apply IHE1. assumption. - (* b evaluates to false (contradiction) *) rewrite H in H5. inversion H5. - (* b evaluates to true (contradiction) *) rewrite H in H5. inversion H5. - (* b evaluates to false *) apply IHE1. assumption. - (* b evaluates to false *) reflexivity. - (* b evaluates to true (contradiction) *) rewrite H in H2. inversion H2. - (* b evaluates to false (contradiction) *) rewrite H in H4. inversion H4. - (* b evaluates to true *) assert (st' = st'0) as EQ1. { (* Proof of assertion *) apply IHE1_1; assumption. } subst st'0. apply IHE1_2. assumption. Qed. (** Thus far, our proof scripts mostly apply relevant hypotheses or lemmas by name, and one at a time. *)
Theorem
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval_deterministic
auto_example_1: forall (P Q R: Prop), (P -> Q) -> (Q -> R) -> P -> R. Proof. intros P Q R H1 H2 H3. apply H2. apply H1. assumption. Qed. (** The [auto] tactic frees us from this drudgery by _searching_ for a sequence of applications that will prove the goal *)
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
auto_example_1
auto_example_1': forall (P Q R: Prop), (P -> Q) -> (Q -> R) -> P -> R. Proof. intros P Q R H1 H2 H3. auto. Qed. (** The [auto] tactic solves goals that are solvable by any combination of - [intros] and - [apply] (of hypotheses from the local context, by default). *) (** Using [auto] is always "safe" in the sense that it will never fail and will never change the proof state: either it completely solves the current goal, or it does nothing. *)
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
auto_example_1'
auto_example_2: forall P Q R S T U : Prop, (P -> Q) -> (P -> R) -> (T -> R) -> (S -> T -> U) -> ((P->Q) -> (P->S)) -> T -> P -> U. Proof. auto. Qed. (** Proof search could, in principle, take an arbitrarily long time, so there are limits to how far [auto] will search by default. *)
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
auto_example_2
auto_example_3: forall (P Q R S T U: Prop), (P -> Q) -> (Q -> R) -> (R -> S) -> (S -> T) -> (T -> U) -> P -> U. Proof. auto. auto 6. Qed. (** When searching for potential proofs of the current goal, [auto] considers the hypotheses in the current context together with a _hint database_ of other lemmas and constructors. Some common lemmas about equality and logical operators are installed in this hint database by default. *)
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
auto_example_3
auto_example_4: forall P Q R : Prop, Q -> (Q -> R) -> P \/ (Q /\ R). Proof. auto. Qed. (** We can extend the hint database just for the purposes of one application of [auto] by writing [auto using ...]. *)
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
auto_example_4
le_antisym: forall n m: nat, (n <= m /\ m <= n) -> n = m. Proof. intros. omega. Qed.
Lemma
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
le_antisym
auto_example_6: forall n m p : nat, (n <= p -> (n <= m /\ m <= n)) -> n <= p -> n = m. Proof. intros. auto. (* does nothing: auto doesn't destruct hypotheses! *) auto using le_antisym. Qed. (** Of course, in any given development there will probably be some specific constructors and lemmas that are used very often in proofs. We can add these to the global hint database by writing Hint Resolve T. at the top level, where [T] is a top-level theorem or a constructor of an inductively defined proposition (i.e., anything whose type is an implication). As a shorthand, we can write Hint Constructors c. to tell Coq to do a [Hint Resolve] for _all_ of the constructors from the inductive definition of [c]. It is also sometimes necessary to add Hint Unfold d. where [d] is a defined symbol, so that [auto] knows to expand uses of [d], thus enabling further possibilities for applying lemmas that it knows about. *) Hint Resolve le_antisym.
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
auto_example_6
auto_example_6': forall n m p : nat, (n<= p -> (n <= m /\ m <= n)) -> n <= p -> n = m. Proof. intros. auto. (* picks up hint from database *) Qed.
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
auto_example_6'
is_fortytwox := x = 42.
Definition
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
is_fortytwo
auto_example_7: forall x, (x <= 42 /\ 42 <= x) -> is_fortytwo x. Proof. auto. (* does nothing *) Abort. Hint Unfold is_fortytwo.
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
auto_example_7
auto_example_7': forall x, (x <= 42 /\ 42 <= x) -> is_fortytwo x. Proof. auto. Qed. (** Now let's take a first pass over [ceval_deterministic] to simplify the proof script. *)
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
auto_example_7'
ceval_deterministic': forall c st st1 st2, c / st \\ st1 -> c / st \\ st2 -> st1 = st2. Proof. intros c st st1 st2 E1 E2. generalize dependent st2; induction E1; intros st2 E2; inv E2; auto. - (* E_Seq *) assert (st' = st'0) as EQ1 by auto. subst st'0. auto. - (* E_IfTrue *) + (* b evaluates to false (contradiction) *) rewrite H in H5. inversion H5. - (* E_IfFalse *) + (* b evaluates to true (contradiction) *) rewrite H in H5. inversion H5. - (* E_WhileFalse *) + (* b evaluates to true (contradiction) *) rewrite H in H2. inversion H2. - (* b evaluates to false (contradiction) *) rewrite H in H4. inversion H4. - (* b evaluates to true *) assert (st' = st'0) as EQ1 by auto. subst st'0. auto. Qed. (** When we are using a particular tactic many times in a proof, we can use a variant of the [Proof] command to make that tactic into a default within the proof. Saying [Proof with t] (where [t] is an arbitrary tactic) allows us to use [t1...] as a shorthand for [t1;t] within the proof. As an illustration, here is an alternate version of the previous proof, using [Proof with auto]. *)
Theorem
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval_deterministic'
ceval_deterministic'_alt: forall c st st1 st2, c / st \\ st1 -> c / st \\ st2 -> st1 = st2. Proof with auto. intros c st st1 st2 E1 E2; generalize dependent st2; induction E1; intros st2 E2; inv E2... - (* E_Seq *) assert (st' = st'0) as EQ1... subst st'0... - (* E_IfTrue *) + (* b evaluates to false (contradiction) *) rewrite H in H5. inversion H5. - (* E_IfFalse *) + (* b evaluates to true (contradiction) *) rewrite H in H5. inversion H5. - (* E_WhileFalse *) + (* b evaluates to true (contradiction) *) rewrite H in H2. inversion H2. - (* b evaluates to false (contradiction) *) rewrite H in H4. inversion H4. - (* b evaluates to true *) assert (st' = st'0) as EQ1... subst st'0... Qed. (** The proof has become simpler, but there is still an annoying amount of repetition. Let's start by tackling the contradiction cases. Each of them occurs in a situation where we have both H1: beval st b = false and H2: beval st b = true as hypotheses. The contradiction is evident, but demonstrating it is a little complicated: we have to locate the two hypotheses [H1] and [H2] and do a [rewrite] following by an [inversion]. We'd like to automate this process. (In fact, Coq has a built-in tactic [congruence] that will do the job in this case. But we'll ignore the existence of this tactic for now, in order to demonstrate how to build forward search tactics by hand.) As a first step, we can abstract out the piece of script in question by writing a little function in Coq's tactic programming language, Ltac. *)
Theorem
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval_deterministic'_alt
rwinvH1 H2 := rewrite H1 in H2; inv H2.
Ltac
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
rwinv
ceval_deterministic'': forall c st st1 st2, c / st \\ st1 -> c / st \\ st2 -> st1 = st2. Proof. intros c st st1 st2 E1 E2. generalize dependent st2; induction E1; intros st2 E2; inv E2; auto. - (* E_Seq *) assert (st' = st'0) as EQ1 by auto. subst st'0. auto. - (* E_IfTrue *) + (* b evaluates to false (contradiction) *) rwinv H H5. - (* E_IfFalse *) + (* b evaluates to true (contradiction) *) rwinv H H5. - (* E_WhileFalse *) + (* b evaluates to true (contradiction) *) rwinv H H2. - (* b evaluates to false (contradiction) *) rwinv H H4. - (* b evaluates to true *) assert (st' = st'0) as EQ1 by auto. subst st'0. auto. Qed. (** That was is a bit better, but not much. We really want Coq to discover the relevant hypotheses for us. We can do this by using the [match goal] facility of Ltac. *)
Theorem
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval_deterministic''
find_rwinv:= match goal with H1: ?E = true, H2: ?E = false |- _ => rwinv H1 H2 end. (** The [match goal] tactic looks for two distinct hypotheses that have the form of equalities, with the same arbitrary expression [E] on the left and with conflicting boolean values on the right. If such hypotheses are found, it binds [H1] and [H2] to their names and applies the [rwinv] tactic to [H1] and [H2]. Adding this tactic to the ones that we invoke in each case of the induction handles all of the contradictory cases. *)
Ltac
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
find_rwinv
ceval_deterministic''': forall c st st1 st2, c / st \\ st1 -> c / st \\ st2 -> st1 = st2. Proof. intros c st st1 st2 E1 E2. generalize dependent st2; induction E1; intros st2 E2; inv E2; try find_rwinv; auto. - (* E_Seq *) assert (st' = st'0) as EQ1 by auto. subst st'0. auto. - (* E_WhileTrue *) + (* b evaluates to true *) assert (st' = st'0) as EQ1 by auto. subst st'0. auto. Qed. (** Let's see about the remaining cases. Each of them involves applying a conditional hypothesis to extract an equality. Currently we have phrased these as assertions, so that we have to predict what the resulting equality will be (although we can then use [auto] to prove it). An alternative is to pick the relevant hypotheses to use and then rewrite with them, as follows: *)
Theorem
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval_deterministic'''
ceval_deterministic'''': forall c st st1 st2, c / st \\ st1 -> c / st \\ st2 -> st1 = st2. Proof. intros c st st1 st2 E1 E2. generalize dependent st2; induction E1; intros st2 E2; inv E2; try find_rwinv; auto. - (* E_Seq *) rewrite (IHE1_1 st'0 H1) in *. auto. - (* E_WhileTrue *) + (* b evaluates to true *) rewrite (IHE1_1 st'0 H3) in *. auto. Qed. (** Now we can automate the task of finding the relevant hypotheses to rewrite with. *)
Theorem
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval_deterministic''''
find_eqn:= match goal with H1: forall x, ?P x -> ?L = ?R, H2: ?P ?X |- _ => rewrite (H1 X H2) in * end. (** The pattern [forall x, ?P x -> ?L = ?R] matches any hypothesis of the form "for all [x], _some property of [x]_ implies _some equality_." The property of [x] is bound to the pattern variable [P], and the left- and right-hand sides of the equality are bound to [L] and [R]. The name of this hypothesis is bound to [H1]. Then the pattern [?P ?X] matches any hypothesis that provides evidence that [P] holds for some concrete [X]. If both patterns succeed, we apply the [rewrite] tactic (instantiating the quantified [x] with [X] and providing [H2] as the required evidence for [P X]) in all hypotheses and the goal. One problem remains: in general, there may be several pairs of hypotheses that have the right general form, and it seems tricky to pick out the ones we actually need. A key trick is to realize that we can _try them all_! Here's how this works: - each execution of [match goal] will keep trying to find a valid pair of hypotheses until the tactic on the RHS of the match succeeds; if there are no such pairs, it fails; - [rewrite] will fail given a trivial equation of the form [X = X]; - we can wrap the whole thing in a [repeat], which will keep doing useful rewrites until only trivial ones are left. *)
Ltac
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
find_eqn
ceval_deterministic''''': forall c st st1 st2, c / st \\ st1 -> c / st \\ st2 -> st1 = st2. Proof. intros c st st1 st2 E1 E2. generalize dependent st2; induction E1; intros st2 E2; inv E2; try find_rwinv; repeat find_eqn; auto. Qed. (** The big payoff in this approach is that our proof script should be robust in the face of modest changes to our language. For example, we can add a [REPEAT] command to the language. *)
Theorem
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval_deterministic'''''
com: Type := | CSkip : com | CAsgn : id -> aexp -> com | CSeq : com -> com -> com | CIf : bexp -> com -> com -> com | CWhile : bexp -> com -> com | CRepeat : com -> bexp -> com. (** [REPEAT] behaves like [WHILE], except that the loop guard is checked _after_ each execution of the body, with the loop repeating as long as the guard stays _false_. Because of this, the body will always execute at least once. *) Notation "'SKIP'" := CSkip. Notation "c1 ; c2" := (CSeq c1 c2) (at level 80, right associativity). Notation "X '::=' a" := (CAsgn X a) (at level 60). Notation "'WHILE' b 'DO' c 'END'" := (CWhile b c) (at level 80, right associativity). Notation "'IFB' e1 'THEN' e2 'ELSE' e3 'FI'" := (CIf e1 e2 e3) (at level 80, right associativity). Notation "'REPEAT' e1 'UNTIL' b2 'END'" := (CRepeat e1 b2) (at level 80, right associativity).
Inductive
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
com
ceval: state -> com -> state -> Prop := | E_Skip : forall st, ceval st SKIP st | E_Ass : forall st a1 n X, aeval st a1 = n -> ceval st (X ::= a1) (t_update st X n) | E_Seq : forall c1 c2 st st' st'', ceval st c1 st' -> ceval st' c2 st'' -> ceval st (c1 ; c2) st'' | E_IfTrue : forall st st' b1 c1 c2, beval st b1 = true -> ceval st c1 st' -> ceval st (IFB b1 THEN c1 ELSE c2 FI) st' | E_IfFalse : forall st st' b1 c1 c2, beval st b1 = false -> ceval st c2 st' -> ceval st (IFB b1 THEN c1 ELSE c2 FI) st' | E_WhileFalse : forall b1 st c1, beval st b1 = false -> ceval st (WHILE b1 DO c1 END) st | E_WhileTrue : forall st st' st'' b1 c1, beval st b1 = true -> ceval st c1 st' -> ceval st' (WHILE b1 DO c1 END) st'' -> ceval st (WHILE b1 DO c1 END) st'' | E_RepeatEnd : forall st st' b1 c1, ceval st c1 st' -> beval st' b1 = true -> ceval st (CRepeat c1 b1) st' | E_RepeatLoop : forall st st' st'' b1 c1, ceval st c1 st' -> beval st' b1 = false -> ceval st' (CRepeat c1 b1) st'' -> ceval st (CRepeat c1 b1) st''. Notation "c1 '/' st '\\' st'" := (ceval st c1 st') (at level 40, st at level 39). (** Our first attempt at the proof is disappointing: the [E_RepeatEnd] and [E_RepeatLoop] cases are not handled by our previous automation. *)
Inductive
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval
ceval_deterministic: forall c st st1 st2, c / st \\ st1 -> c / st \\ st2 -> st1 = st2. Proof. intros c st st1 st2 E1 E2. generalize dependent st2; induction E1; intros st2 E2; inv E2; try find_rwinv; repeat find_eqn; auto. - (* E_RepeatEnd *) + (* b evaluates to false (contradiction) *) find_rwinv. (* oops: why didn't [find_rwinv] solve this for us already? answer: we did things in the wrong order. *) - (* E_RepeatLoop *) + (* b evaluates to true (contradiction) *) find_rwinv. Qed. (** To fix this, we just have to swap the invocations of [find_eqn] and [find_rwinv]. *)
Theorem
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval_deterministic
ceval_deterministic': forall c st st1 st2, c / st \\ st1 -> c / st \\ st2 -> st1 = st2. Proof. intros c st st1 st2 E1 E2. generalize dependent st2; induction E1; intros st2 E2; inv E2; repeat find_eqn; try find_rwinv; auto. Qed.
Theorem
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval_deterministic'
ceval_example1: (X ::= ANum 2;; IFB BLe (AId X) (ANum 1) THEN Y ::= ANum 3 ELSE Z ::= ANum 4 FI) / empty_state \\ (t_update (t_update empty_state X 2) Z 4). Proof. apply E_Seq with (t_update empty_state X 2). - apply E_Ass. reflexivity. - apply E_IfFalse. reflexivity. apply E_Ass. reflexivity. Qed. (** In the first step of the proof, we had to explicitly provide a longish expression to help Coq instantiate a "hidden" argument to the [E_Seq] constructor. This was needed because the definition of [E_Seq]... E_Seq : forall c1 c2 st st' st'', c1 / st \\ st' -> c2 / st' \\ st'' -> (c1 ;; c2) / st \\ st'' is quantified over a variable, [st'], that does not appear in its conclusion, so unifying its conclusion with the goal state doesn't help Coq find a suitable value for this variable. If we leave out the [with], this step fails ("Error: Unable to find an instance for the variable [st']"). What's silly about this error is that the appropriate value for [st'] will actually become obvious in the very next step, where we apply [E_Ass]. If Coq could just wait until we get to this step, there would be no need to give the value explicitly. This is exactly what the [eapply] tactic gives us: *)
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval_example1
ceval'_example1: (X ::= ANum 2;; IFB BLe (AId X) (ANum 1) THEN Y ::= ANum 3 ELSE Z ::= ANum 4 FI) / empty_state \\ (t_update (t_update empty_state X 2) Z 4). Proof. eapply E_Seq. (* 1 *) - apply E_Ass. (* 2 *) reflexivity. (* 3 *) - (* 4 *) apply E_IfFalse. reflexivity. apply E_Ass. reflexivity. Qed. (** The tactic [eapply H] tactic behaves just like [apply H] except that, after it finishes unifying the goal state with the conclusion of [H], it does not bother to check whether all the variables that were introduced in the process have been given concrete values during unification. If you step through the proof above, you'll see that the goal state at position [1] mentions the _existential variable_ [?st'] in both of the generated subgoals. The next step (which gets us to position [2]) replaces [?st'] with a concrete value. This new value contains a new existential variable [?n], which is instantiated in its turn by the following [reflexivity] step, position [3]. When we start working on the second subgoal (position [4]), we observe that the occurrence of [?st'] in this subgoal has been replaced by the value that it was given during the first subgoal. *) (** Several of the tactics that we've seen so far, including [exists], [constructor], and [auto], have [e...] variants. For example, here's a proof using [eauto]: *) Hint Constructors ceval. Hint Transparent state. Hint Transparent total_map.
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
ceval'_example1
st12:= t_update (t_update empty_state X 1) Y 2.
Definition
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
st12
st21:= t_update (t_update empty_state X 2) Y 1.
Definition
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
st21
auto_example_8: exists s', (IFB (BLe (AId X) (AId Y)) THEN (Z ::= AMinus (AId Y) (AId X)) ELSE (Y ::= APlus (AId X) (AId Z)) FI) / st21 \\ s'. Proof. eauto. Qed. (** The [eauto] tactic works just like [auto], except that it uses [eapply] instead of [apply]. *)
Example
sf-experiment
[ "Require Import Coq.omega.Omega", "Require Import Maps", "Require Import Imp" ]
sf-experiment/Auto.v
auto_example_8
day: Type := | monday : day | tuesday : day | wednesday : day | thursday : day | friday : day | saturday : day | sunday : day.
Inductive
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
day
next_weekday(d:day) : day := match d with | monday => tuesday | tuesday => wednesday | wednesday => thursday | thursday => friday | friday => monday | saturday => monday | sunday => monday end.
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
next_weekday
test_next_weekday:= (next_weekday (next_weekday saturday)) = tuesday. (* BCP: Needs an equality test. QuickCheck test_next_weekday. *) (* BCP: QC needs the native one. (Unless we make this Checkable somehow.)
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
test_next_weekday
bool: Type := | true : bool | false : bool. *)
Inductive
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
bool
negb(b:bool) : bool := match b with | true => false | false => true end.
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
negb
andb(b1:bool) (b2:bool) : bool := match b1 with | true => b2 | false => false end.
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
andb
orb(b1:bool) (b2:bool) : bool := match b1 with | true => true | false => b2 end.
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
orb
nandb(b1:bool) (b2:bool) : bool (* REPLACE THIS LINE WITH ":= _your_definition_ ." *) := false.
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
nandb
test_nandb1:= (nandb true false).
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
test_nandb1
minustwo(n : nat) : nat := match n with | O => O | S O => O | S (S n') => n' end.
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
minustwo
evenb(n:nat) : bool := match n with | O => true | S O => false | S (S n') => evenb n' end.
Fixpoint
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
evenb
oddb(n:nat) : bool := negb (evenb n).
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
oddb
test_oddb1:= oddb 1.
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
test_oddb1
plus(n : nat) (m : nat) : nat := match n with | O => m | S n' => S (plus n' m) end.
Fixpoint
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
plus
mult(n m : nat) : nat := match n with | O => O | S n' => plus m (mult n' m) end.
Fixpoint
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
mult
test_mult1:= (mult 3 3) =? 9.
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
test_mult1
minus(n m:nat) : nat := match n, m with | O , _ => O | S _ , O => n | S n', S m' => minus n' m' end.
Fixpoint
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
minus
exp(base power : nat) : nat := match power with | O => S O | S p => mult base (exp base p) end.
Fixpoint
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
exp
factorial(n:nat) : nat (* REPLACE THIS LINE WITH ":= _your_definition_ ." *) := 0.
Fixpoint
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
factorial
test_factorial1:= (factorial 3) =? 6. Notation "x + y" := (plus x y) (at level 50, left associativity) : nat_scope. Notation "x - y" := (minus x y) (at level 50, left associativity) : nat_scope. Notation "x * y" := (mult x y) (at level 40, left associativity) : nat_scope. Check ((0 + 1) + 1).
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
test_factorial1
beq_nat(n m : nat) : bool := match n with | O => match m with | O => true | S m' => false end | S n' => match m with | O => false | S m' => beq_nat n' m' end end.
Fixpoint
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
beq_nat
leb(n m : nat) : bool := match n with | O => true | S n' => match m with | O => false | S m' => leb n' m' end end. Notation "'FORALLX' x : T , c" := (forAllShrink (@arbitrary T _) shrink (fun x => c)) (at level 200, x ident, T at level 200, c at level 200, right associativity (* , format "'[' 'exists' '/ ' x .. y , '/ ' p ']'" *) ) : type_scope.
Fixpoint
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
leb
plus_O_n:= FORALL n:nat, 0 + n =? n.
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
plus_O_n
bool_eq(x y : bool) : Dec (x = y). constructor. unfold ssrbool.decidable. repeat (decide equality). Defined.
Instance
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
bool_eq
negb_involutive(b: bool) := (negb (negb b) = b)?. Check negb_involutive. QuickChick negb_involutive.
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
negb_involutive
negb_involutive2(b: bool) := Bool.eqb (negb (negb b)) b.
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
negb_involutive2
andb_commutative:= fun b c => Bool.eqb (andb b c) (andb c b). (* BCP: Don't know what to do with this one!
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
andb_commutative
identity_fn_applied_twice: forall (f : bool -> bool), (forall (x : bool), f x = x) -> forall (b : bool), f (f b) = b. *)
Theorem
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
identity_fn_applied_twice
andb_eq_orb:= fun (b c : bool) => (Bool.eqb (andb b c) (orb b c)) ==> (Bool.eqb b c).
Definition
sf-experiment
[ "From QuickChick Require Import QuickChick", "Require Import List ZArith", "From mathcomp Require Import ssreflect ssrfun ssrbool", "From mathcomp Require Import seq ssrnat eqtype" ]
sf-experiment/Basics.v
andb_eq_orb
aexp: Type := | ANum : nat -> aexp | APlus : aexp -> aexp -> aexp | AMinus : aexp -> aexp -> aexp | AMult : aexp -> aexp -> aexp.
Inductive
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aexp
bexp: Type := | BTrue : bexp | BFalse : bexp | BEq : aexp -> aexp -> bexp | BLe : aexp -> aexp -> bexp | BNot : bexp -> bexp | BAnd : bexp -> bexp -> bexp. (** In this chapter, we'll elide the translation from the concrete syntax that a programmer would actually write to these abstract syntax trees -- the process that, for example, would translate the string ["1+2*3"] to the AST APlus (ANum 1) (AMult (ANum 2) (ANum 3)). The optional chapter [ImpParser] develops a simple implementation of a lexical analyzer and parser that can perform this translation. You do _not_ need to understand that chapter to understand this one, but if you haven't taken a course where these techniques are covered (e.g., a compilers course) you may want to skim it. *) (** For comparison, here's a conventional BNF (Backus-Naur Form) grammar defining the same abstract syntax: a ::= nat | a + a | a - a | a * a b ::= true | false | a = a | a <= a | not b | b and b *) (** Compared to the Coq version above... - The BNF is more informal -- for example, it gives some suggestions about the surface syntax of expressions (like the fact that the addition operation is written [+] and is an infix symbol) while leaving other aspects of lexical analysis and parsing (like the relative precedence of [+], [-], and [*], the use of parens to explicitly group subexpressions, etc.) unspecified. Some additional information (and human intelligence) would be required to turn this description into a formal definition, for example when implementing a compiler. The Coq version consistently omits all this information and concentrates on the abstract syntax only. - On the other hand, the BNF version is lighter and easier to read. Its informality makes it flexible, a big advantage in situations like discussions at the blackboard, where conveying general ideas is more important than getting every detail nailed down precisely. Indeed, there are dozens of BNF-like notations and people switch freely among them, usually without bothering to say which form of BNF they're using because there is no need to: a rough-and-ready informal understanding is all that's important. It's good to be comfortable with both sorts of notations: informal ones for communicating between humans and formal ones for carrying out implementations and proofs. *)
Inductive
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
bexp
aeval(a : aexp) : nat := match a with | ANum n => n | APlus a1 a2 => (aeval a1) + (aeval a2) | AMinus a1 a2 => (aeval a1) - (aeval a2) | AMult a1 a2 => (aeval a1) * (aeval a2) end.
Fixpoint
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aeval
test_aeval1: aeval (APlus (ANum 2) (ANum 2)) = 4. Proof. reflexivity. Qed.
Example
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
test_aeval1
beval(b : bexp) : bool := match b with | BTrue => true | BFalse => false | BEq a1 a2 => beq_nat (aeval a1) (aeval a2) | BLe a1 a2 => leb (aeval a1) (aeval a2) | BNot b1 => negb (beval b1) | BAnd b1 b2 => andb (beval b1) (beval b2) end. (** We haven't defined very much yet, but we can already get some mileage out of the definitions. Suppose we define a function that takes an arithmetic expression and slightly simplifies it, changing every occurrence of [0+e] (i.e., [(APlus (ANum 0) e]) into just [e]. *)
Fixpoint
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
beval
optimize_0plus(a:aexp) : aexp := match a with | ANum n => ANum n | APlus (ANum 0) e2 => optimize_0plus e2 | APlus e1 e2 => APlus (optimize_0plus e1) (optimize_0plus e2) | AMinus e1 e2 => AMinus (optimize_0plus e1) (optimize_0plus e2) | AMult e1 e2 => AMult (optimize_0plus e1) (optimize_0plus e2) end. (** To make sure our optimization is doing the right thing we can test it on some examples and see if the output looks OK. *)
Fixpoint
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
optimize_0plus
test_optimize_0plus: optimize_0plus (APlus (ANum 2) (APlus (ANum 0) (APlus (ANum 0) (ANum 1)))) = APlus (ANum 2) (ANum 1). Proof. reflexivity. Qed. (** But if we want to be sure the optimization is correct -- i.e., that evaluating an optimized expression gives the same result as the original -- we should prove it. *)
Example
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
test_optimize_0plus
optimize_0plus_sound: forall a, aeval (optimize_0plus a) = aeval a. Proof. intros a. induction a. - (* ANum *) reflexivity. - (* APlus *) destruct a1. + (* a1 = ANum n *) destruct n. * (* n = 0 *) simpl. apply IHa2. * (* n <> 0 *) simpl. rewrite IHa2. reflexivity. + (* a1 = APlus a1_1 a1_2 *) simpl. simpl in IHa1. rewrite IHa1. rewrite IHa2. reflexivity. + (* a1 = AMinus a1_1 a1_2 *) simpl. simpl in IHa1. rewrite IHa1. rewrite IHa2. reflexivity. + (* a1 = AMult a1_1 a1_2 *) simpl. simpl in IHa1. rewrite IHa1. rewrite IHa2. reflexivity. - (* AMinus *) simpl. rewrite IHa1. rewrite IHa2. reflexivity. - (* AMult *) simpl. rewrite IHa1. rewrite IHa2. reflexivity. Qed. (** The amount of repetition in this last proof is a little annoying. And if either the language of arithmetic expressions or the optimization being proved sound were significantly more complex, it would start to be a real problem. So far, we've been doing all our proofs using just a small handful of Coq's tactics and completely ignoring its powerful facilities for constructing parts of proofs automatically. This section introduces some of these facilities, and we will see more over the next several chapters. Getting used to them will take some energy -- Coq's automation is a power tool -- but it will allow us to scale up our efforts to more complex definitions and more interesting properties without becoming overwhelmed by boring, repetitive, low-level details. *) (** _Tacticals_ is Coq's term for tactics that take other tactics as arguments -- "higher-order tactics," if you will. *) (** If [T] is a tactic, then [try T] is a tactic that is just like [T] except that, if [T] fails, [try T] _successfully_ does nothing at all (instead of failing). *)
Theorem
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
optimize_0plus_sound
silly1: forall ae, aeval ae = aeval ae. Proof. try reflexivity. (* this just does [reflexivity] *) Qed.
Theorem
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
silly1
silly2: forall (P : Prop), P -> P. Proof. intros P HP. try reflexivity. (* just [reflexivity] would have failed *) apply HP. (* we can still finish the proof in some other way *) Qed. (** There is no real reason to use [try] in completely manual proofs like these, but it is very useful for doing automated proofs in conjunction with the [;] tactical, which we show next. *) (** In its most common form, the [;] tactical takes two tactics as arguments. The compound tactic [T;T'] first performs [T] and then performs [T'] on _each subgoal_ generated by [T]. *)
Theorem
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
silly2
foo: forall n, leb 0 n = true. Proof. intros. destruct n. - (* n=0 *) simpl. reflexivity. - (* n=Sn' *) simpl. reflexivity. Qed.
Lemma
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
foo
foo': forall n, leb 0 n = true. Proof. intros. destruct n; simpl; reflexivity. Qed. (** Using [try] and [;] together, we can get rid of the repetition in the proof that was bothering us a little while ago. *)
Lemma
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
foo'
optimize_0plus_sound': forall a, aeval (optimize_0plus a) = aeval a. Proof. intros a. induction a; try (simpl; rewrite IHa1; rewrite IHa2; reflexivity). (* ... but the remaining cases -- ANum and APlus -- are different: *) - (* ANum *) reflexivity. - (* APlus *) destruct a1; try (simpl; simpl in IHa1; rewrite IHa1; rewrite IHa2; reflexivity). (* The interesting case, on which the [try...] does nothing, is when [e1 = ANum n]. In this case, we have to destruct [n] (to see whether the optimization applies) and rewrite with the induction hypothesis. *) + (* a1 = ANum n *) destruct n; simpl; rewrite IHa2; reflexivity. Qed. (** Coq experts often use this "[...; try... ]" idiom after a tactic like [induction] to take care of many similar cases all at once. Naturally, this practice has an analog in informal proofs. For example, here is an informal proof of the optimization theorem that matches the structure of the formal one: _Theorem_: For all arithmetic expressions [a], aeval (optimize_0plus a) = aeval a. _Proof_: By induction on [a]. Most cases follow directly from the IH. The remaining cases are as follows: - Suppose [a = ANum n] for some [n]. We must show aeval (optimize_0plus (ANum n)) = aeval (ANum n). This is immediate from the definition of [optimize_0plus]. - Suppose [a = APlus a1 a2] for some [a1] and [a2]. We must show aeval (optimize_0plus (APlus a1 a2)) = aeval (APlus a1 a2). Consider the possible forms of [a1]. For most of them, [optimize_0plus] simply calls itself recursively for the subexpressions and rebuilds a new expression of the same form as [a1]; in these cases, the result follows directly from the IH. The interesting case is when [a1 = ANum n] for some [n]. If [n = ANum 0], then optimize_0plus (APlus a1 a2) = optimize_0plus a2 and the IH for [a2] is exactly what we need. On the other hand, if [n = S n'] for some [n'], then again [optimize_0plus] simply calls itself recursively, and the result follows from the IH. [] *) (** However, this proof can still be improved: the first case (for [a = ANum n]) is very trivial -- even more trivial than the cases that we said simply followed from the IH -- yet we have chosen to write it out in full. It would be better and clearer to drop it and just say, at the top, "Most cases are either immediate or direct from the IH. The only interesting case is the one for [APlus]..." We can make the same improvement in our formal proof too. Here's how it looks: *)
Theorem
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
optimize_0plus_sound'
optimize_0plus_sound'': forall a, aeval (optimize_0plus a) = aeval a. Proof. intros a. induction a; try (simpl; rewrite IHa1; rewrite IHa2; reflexivity); try reflexivity. - (* APlus *) destruct a1; try (simpl; simpl in IHa1; rewrite IHa1; rewrite IHa2; reflexivity). + (* a1 = ANum n *) destruct n; simpl; rewrite IHa2; reflexivity. Qed. (** The [;] tactical also has a more general form than the simple [T;T'] we've seen above. If [T], [T1], ..., [Tn] are tactics, then T; [T1 | T2 | ... | Tn] is a tactic that first performs [T] and then performs [T1] on the first subgoal generated by [T], performs [T2] on the second subgoal, etc. So [T;T'] is just special notation for the case when all of the [Ti]'s are the same tactic; i.e., [T;T'] is shorthand for: T; [T' | T' | ... | T'] *) (** The [repeat] tactical takes another tactic and keeps applying this tactic until it fails. Here is an example showing that [10] is in a long list using repeat. *)
Theorem
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
optimize_0plus_sound''
In10: In 10 [1;2;3;4;5;6;7;8;9;10]. Proof. repeat (try (left; reflexivity); right). Qed. (** The tactic [repeat T] never fails: if the tactic [T] doesn't apply to the original goal, then repeat still succeeds without changing the original goal (i.e., it repeats zero times). *)
Theorem
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
In10
In10': In 10 [1;2;3;4;5;6;7;8;9;10]. Proof. repeat (left; reflexivity). repeat (right; try (left; reflexivity)). Qed. (** The tactic [repeat T] also does not have any upper bound on the number of times it applies [T]. If [T] is a tactic that always succeeds, then repeat [T] will loop forever (e.g., [repeat simpl] loops forever, since [simpl] always succeeds). While evaluation in Coq's term language, Gallina, is guaranteed to terminate, tactic evaluation is not! This does not affect Coq's logical consistency, however, since the job of [repeat] and other tactics is to guide Coq in constructing proofs; if the construction process diverges, this simply means that we have failed to construct a proof, not that we have constructed a wrong one. *) (** Since the [optimize_0plus] transformation doesn't change the value of [aexp]s, we should be able to apply it to all the [aexp]s that appear in a [bexp] without changing the [bexp]'s value. Write a function which performs that transformation on [bexp]s, and prove it is sound. Use the tacticals we've just seen to make the proof as elegant as possible. *)
Theorem
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
In10'
optimize_0plus_b(b : bexp) : bexp (* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
Fixpoint
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
optimize_0plus_b
optimize_0plus_b_sound: forall b, beval (optimize_0plus_b b) = beval b. Proof. (* FILL IN HERE *) Admitted. (** _Design exercise_: The optimization implemented by our [optimize_0plus] function is only one of many possible optimizations on arithmetic and boolean expressions. Write a more sophisticated optimizer and prove it correct. (You will probably find it easiest to start small -- add just a single, simple optimization and prove it correct -- and build up to something more interesting incrementially.) *) (** Coq also provides several ways of "programming" tactic scripts. - The [Tactic Notation] idiom illustrated below gives a handy way to define "shorthand tactics" that bundle several tactics into a single command. - For more sophisticated programming, Coq offers a built-in programming language called [Ltac] with primitives that can examine and modify the proof state. The details are a bit too complicated to get into here (and it is generally agreed that [Ltac] is not the most beautiful part of Coq's design!), but they can be found in the reference manual and other books on Coq, and there are many examples of [Ltac] definitions in the Coq standard library that you can use as examples. - There is also an OCaml API, which can be used to build tactics that access Coq's internal structures at a lower level, but this is seldom worth the trouble for ordinary Coq users. The [Tactic Notation] mechanism is the easiest to come to grips with, and it offers plenty of power for many purposes. Here's an example. *) Tactic Notation "simpl_and_try" tactic(c) := simpl; try c. (** This defines a new tactical called [simpl_and_try] that takes one tactic [c] as an argument and is defined to be equivalent to the tactic [simpl; try c]. Now writing "[simpl_and_try reflexivity.]" in a proof will be the same as writing "[simpl; try reflexivity.]" *) (** The [omega] tactic implements a decision procedure for a subset of first-order logic called _Presburger arithmetic_. It is based on the Omega algorithm invented in 1991 by William Pugh [Pugh 1991]. If the goal is a universally quantified formula made out of - numeric constants, addition ([+] and [S]), subtraction ([-] and [pred]), and multiplication by constants (this is what makes it Presburger arithmetic), - equality ([=] and [<>]) and inequality ([<=]), and - the logical connectives [/\], [\/], [~], and [->], then invoking [omega] will either solve the goal or tell you that it is actually false. *)
Theorem
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
optimize_0plus_b_sound
silly_presburger_example: forall m n o p, m + n <= n + o /\ o + 3 = p + 3 -> m <= p. Proof. intros. omega. Qed. (** Finally, here are some miscellaneous tactics that you may find convenient. - [clear H]: Delete hypothesis [H] from the context. - [subst x]: Find an assumption [x = e] or [e = x] in the context, replace [x] with [e] throughout the context and current goal, and clear the assumption. - [subst]: Substitute away _all_ assumptions of the form [x = e] or [e = x]. - [rename... into...]: Change the name of a hypothesis in the proof context. For example, if the context includes a variable named [x], then [rename x into y] will change all occurrences of [x] to [y]. - [assumption]: Try to find a hypothesis [H] in the context that exactly matches the goal; if one is found, behave like [apply H]. - [contradiction]: Try to find a hypothesis [H] in the current context that is logically equivalent to [False]. If one is found, solve the goal. - [constructor]: Try to find a constructor [c] (from some [Inductive] definition in the current environment) that can be applied to solve the current goal. If one is found, behave like [apply c]. We'll see examples below. *) (** We have presented [aeval] and [beval] as functions defined by [Fixpoint]s. Another way to think about evaluation -- one that we will see is often more flexible -- is as a _relation_ between expressions and their values. This leads naturally to [Inductive] definitions like the following one for arithmetic expressions... *)
Example
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
silly_presburger_example
aevalR: aexp -> nat -> Prop := | E_ANum : forall (n: nat), aevalR (ANum n) n | E_APlus : forall (e1 e2: aexp) (n1 n2: nat), aevalR e1 n1 -> aevalR e2 n2 -> aevalR (APlus e1 e2) (n1 + n2) | E_AMinus: forall (e1 e2: aexp) (n1 n2: nat), aevalR e1 n1 -> aevalR e2 n2 -> aevalR (AMinus e1 e2) (n1 - n2) | E_AMult : forall (e1 e2: aexp) (n1 n2: nat), aevalR e1 n1 -> aevalR e2 n2 -> aevalR (AMult e1 e2) (n1 * n2). (** It will be convenient to have an infix notation for [aevalR]. We'll write [e \\ n] to mean that arithmetic expression [e] evaluates to value [n]. (This notation is one place where the limitation to ASCII symbols becomes a little bothersome. The standard notation for the evaluation relation is a double down-arrow. We'll typeset it like this in the HTML version of the notes and use a double slash as the closest approximation in [.v] files.) *) Notation "e '\\' n" := (aevalR e n) (at level 50, left associativity) : type_scope.
Inductive
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aevalR
aevalR: aexp -> nat -> Prop := | E_ANum : forall (n:nat), (ANum n) \\ n | E_APlus : forall (e1 e2: aexp) (n1 n2 : nat), (e1 \\ n1) -> (e2 \\ n2) -> (APlus e1 e2) \\ (n1 + n2) | E_AMinus : forall (e1 e2: aexp) (n1 n2 : nat), (e1 \\ n1) -> (e2 \\ n2) -> (AMinus e1 e2) \\ (n1 - n2) | E_AMult : forall (e1 e2: aexp) (n1 n2 : nat), (e1 \\ n1) -> (e2 \\ n2) -> (AMult e1 e2) \\ (n1 * n2) where "e '\\' n" := (aevalR e n) : type_scope. (** In informal discussions, it is convenient to write the rules for [aevalR] and similar relations in the more readable graphical form of _inference rules_, where the premises above the line justify the conclusion below the line (we have already seen them in the [IndProp] chapter). *) (** For example, the constructor [E_APlus]... | E_APlus : forall (e1 e2: aexp) (n1 n2: nat), aevalR e1 n1 -> aevalR e2 n2 -> aevalR (APlus e1 e2) (n1 + n2) ...would be written like this as an inference rule: e1 \\ n1 e2 \\ n2 -------------------- (E_APlus) APlus e1 e2 \\ n1+n2 *) (** Formally, there is nothing deep about inference rules: they are just implications. You can read the rule name on the right as the name of the constructor and read each of the linebreaks between the premises above the line (as well as the line itself) as [->]. All the variables mentioned in the rule ([e1], [n1], etc.) are implicitly bound by universal quantifiers at the beginning. (Such variables are often called _metavariables_ to distinguish them from the variables of the language we are defining. At the moment, our arithmetic expressions don't include variables, but we'll soon be adding them.) The whole collection of rules is understood as being wrapped in an [Inductive] declaration. In informal prose, this is either elided or else indicated by saying something like "Let [aevalR] be the smallest relation closed under the following rules...". *) (** For example, [\\] is the smallest relation closed under these rules: ----------- (E_ANum) ANum n \\ n e1 \\ n1 e2 \\ n2 -------------------- (E_APlus) APlus e1 e2 \\ n1+n2 e1 \\ n1 e2 \\ n2 --------------------- (E_AMinus) AMinus e1 e2 \\ n1-n2 e1 \\ n1 e2 \\ n2 -------------------- (E_AMult) AMult e1 e2 \\ n1*n2 *) (** It is straightforward to prove that the relational and functional definitions of evaluation agree: *)
Inductive
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aevalR
aeval_iff_aevalR: forall a n, (a \\ n) <-> aeval a = n. Proof. split. - (* -> *) intros H. induction H; simpl. + (* E_ANum *) reflexivity. + (* E_APlus *) rewrite IHaevalR1. rewrite IHaevalR2. reflexivity. + (* E_AMinus *) rewrite IHaevalR1. rewrite IHaevalR2. reflexivity. + (* E_AMult *) rewrite IHaevalR1. rewrite IHaevalR2. reflexivity. - (* <- *) generalize dependent n. induction a; simpl; intros; subst. + (* ANum *) apply E_ANum. + (* APlus *) apply E_APlus. apply IHa1. reflexivity. apply IHa2. reflexivity. + (* AMinus *) apply E_AMinus. apply IHa1. reflexivity. apply IHa2. reflexivity. + (* AMult *) apply E_AMult. apply IHa1. reflexivity. apply IHa2. reflexivity. Qed. (** We can make the proof quite a bit shorter by making more use of tacticals. *)
Theorem
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aeval_iff_aevalR
aeval_iff_aevalR': forall a n, (a \\ n) <-> aeval a = n. Proof. split. - (* -> *) intros H; induction H; subst; reflexivity. - (* <- *) generalize dependent n. induction a; simpl; intros; subst; constructor; try apply IHa1; try apply IHa2; reflexivity. Qed. (** Write a relation [bevalR] in the same style as [aevalR], and prove that it is equivalent to [beval].*)
Theorem
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aeval_iff_aevalR'
bevalR: bexp -> bool -> Prop := .
Inductive
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
bevalR
beval_iff_bevalR: forall b bv, bevalR b bv <-> beval b = bv. Proof. (* FILL IN HERE *) Admitted.
Lemma
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
beval_iff_bevalR
aexp: Type := | ANum : nat -> aexp | APlus : aexp -> aexp -> aexp | AMinus : aexp -> aexp -> aexp | AMult : aexp -> aexp -> aexp | ADiv : aexp -> aexp -> aexp. (* <--- new *) (** Extending the definition of [aeval] to handle this new operation would not be straightforward (what should we return as the result of [ADiv (ANum 5) (ANum 0)]?). But extending [aevalR] is straightforward. *) Reserved Notation "e '\\' n" (at level 50, left associativity).
Inductive
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aexp
aevalR: aexp -> nat -> Prop := | E_ANum : forall (n:nat), (ANum n) \\ n | E_APlus : forall (a1 a2: aexp) (n1 n2 : nat), (a1 \\ n1) -> (a2 \\ n2) -> (APlus a1 a2) \\ (n1 + n2) | E_AMinus : forall (a1 a2: aexp) (n1 n2 : nat), (a1 \\ n1) -> (a2 \\ n2) -> (AMinus a1 a2) \\ (n1 - n2) | E_AMult : forall (a1 a2: aexp) (n1 n2 : nat), (a1 \\ n1) -> (a2 \\ n2) -> (AMult a1 a2) \\ (n1 * n2) | E_ADiv : forall (a1 a2: aexp) (n1 n2 n3: nat), (a1 \\ n1) -> (a2 \\ n2) -> (n2 > 0) -> (mult n2 n3 = n1) -> (ADiv a1 a2) \\ n3 where "a '\\' n" := (aevalR a n) : type_scope.
Inductive
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aevalR
aexp: Type := | AAny : aexp (* <--- NEW *) | ANum : nat -> aexp | APlus : aexp -> aexp -> aexp | AMinus : aexp -> aexp -> aexp | AMult : aexp -> aexp -> aexp. (** Again, extending [aeval] would be tricky, since now evaluation is _not_ a deterministic function from expressions to numbers, but extending [aevalR] is no problem: *)
Inductive
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aexp
aevalR: aexp -> nat -> Prop := | E_Any : forall (n:nat), AAny \\ n (* <--- new *) | E_ANum : forall (n:nat), (ANum n) \\ n | E_APlus : forall (a1 a2: aexp) (n1 n2 : nat), (a1 \\ n1) -> (a2 \\ n2) -> (APlus a1 a2) \\ (n1 + n2) | E_AMinus : forall (a1 a2: aexp) (n1 n2 : nat), (a1 \\ n1) -> (a2 \\ n2) -> (AMinus a1 a2) \\ (n1 - n2) | E_AMult : forall (a1 a2: aexp) (n1 n2 : nat), (a1 \\ n1) -> (a2 \\ n2) -> (AMult a1 a2) \\ (n1 * n2) where "a '\\' n" := (aevalR a n) : type_scope.
Inductive
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aevalR
state:= total_map nat.
Definition
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
state
empty_state: state := t_empty 0. (** We can add variables to the arithmetic expressions we had before by simply adding one more constructor: *)
Definition
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
empty_state
aexp: Type := | ANum : nat -> aexp | AId : id -> aexp (* <----- NEW *) | APlus : aexp -> aexp -> aexp | AMinus : aexp -> aexp -> aexp | AMult : aexp -> aexp -> aexp. (** Defining a few variable names as notational shorthands will make examples easier to read: *)
Inductive
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aexp
W: id := Id "W".
Definition
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
W
X: id := Id "X".
Definition
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
X
Y: id := Id "Y".
Definition
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
Y
Z: id := Id "Z". (** (This convention for naming program variables ([X], [Y], [Z]) clashes a bit with our earlier use of uppercase letters for types. Since we're not using polymorphism heavily in the chapters devoped to Imp, this overloading should not cause confusion.) *) (** The definition of [bexp]s is unchanged (except for using the new [aexp]s): *)
Definition
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
Z
bexp: Type := | BTrue : bexp | BFalse : bexp | BEq : aexp -> aexp -> bexp | BLe : aexp -> aexp -> bexp | BNot : bexp -> bexp | BAnd : bexp -> bexp -> bexp. (** The arith and boolean evaluators are extended to handle variables in the obvious way, taking a state as an extra argument: *)
Inductive
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
bexp
aeval(st : state) (a : aexp) : nat := match a with | ANum n => n | AId x => st x (* <----- NEW *) | APlus a1 a2 => (aeval st a1) + (aeval st a2) | AMinus a1 a2 => (aeval st a1) - (aeval st a2) | AMult a1 a2 => (aeval st a1) * (aeval st a2) end.
Fixpoint
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aeval
beval(st : state) (b : bexp) : bool := match b with | BTrue => true | BFalse => false | BEq a1 a2 => beq_nat (aeval st a1) (aeval st a2) | BLe a1 a2 => leb (aeval st a1) (aeval st a2) | BNot b1 => negb (beval st b1) | BAnd b1 b2 => andb (beval st b1) (beval st b2) end.
Fixpoint
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
beval
aexp1: aeval (t_update empty_state X 5) (APlus (ANum 3) (AMult (AId X) (ANum 2))) = 13. Proof. reflexivity. Qed.
Example
sf-experiment
[ "Require Import Coq.Bool.Bool", "Require Import Coq.Arith.Arith", "Require Import Coq.Arith.EqNat", "Require Import Coq.omega.Omega", "Require Import Coq.Lists.List", "Require Import Maps" ]
sf-experiment/Imp.v
aexp1
End of preview. Expand in Data Studio

Coq-QuickChick

Randomized property-based testing plugin for Coq, inspired by QuickCheck.

Source

Statistics

Property Value
Total Entries 2,371
Files Processed 107

Type Distribution

Type Count
Definition 627
Lemma 533
Inductive 238
Fixpoint 201
Theorem 188
Instance 188
Ltac2 134
Class 76
Example 74
Axiom 32
Ltac 31
Program 24
Record 9
Parameter 9
Coercion 3

Schema

Column Type Description
fact string Declaration body (name, signature, proof)
type string Declaration type
library string Library component
imports list[string] Import statements
filename string Source file path
symbolic_name string Declaration identifier
docstring string Documentation comment (if present)

Creator

Charles Norton (phanerozoic)

Downloads last month
18

Collection including phanerozoic/Coq-QuickChick