comparison src/Match.v @ 234:82eae7bc91ea

Working with evars
author Adam Chlipala <adamc@hcoop.net>
date Mon, 30 Nov 2009 15:41:51 -0500
parents 15501145d696
children b653e6b19b6d
comparison
equal deleted inserted replaced
233:f15f7c4eebfe 234:82eae7bc91ea
849 (ex_conc (fun x0 : nat => P x0) x 849 (ex_conc (fun x0 : nat => P x0) x
850 (Match (P:=P x) (imp_True (P:=True)))))))) 850 (Match (P:=P x) (imp_True (P:=True))))))))
851 : forall (P : nat -> Prop) (Q : Prop), 851 : forall (P : nat -> Prop) (Q : Prop),
852 (exists x : nat, P x /\ Q) --> Q /\ (exists x : nat, P x) 852 (exists x : nat, P x /\ Q) --> Q /\ (exists x : nat, P x)
853 ]] *) 853 ]] *)
854
855
856 (** * Creating Unification Variables *)
857
858 (** A final useful ingredient in tactic crafting is the ability to allocate new unification variables explicitly. Tactics like [eauto] introduce unification variable internally to support flexible proof search. While [eauto] and its relatives do %\textit{%#<i>#backward#</i>#%}% reasoning, we often want to do similar %\textit{%#<i>#forward#</i>#%}% reasoning, where unification variables can be useful for similar reasons.
859
860 For example, we can write a tactic that instantiates the quantifiers of a universally-quantified hypothesis. The tactic should not need to know what the appropriate instantiantiations are; rather, we want these choices filled with placeholders. We hope that, when we apply the specialized hypothesis later, syntactic unification will determine concrete values.
861
862 Before we are ready to write a tactic, we can try out its ingredients one at a time. *)
863
864 Theorem t5 : (forall x : nat, S x > x) -> 2 > 1.
865 intros.
866
867 (** [[
868 H : forall x : nat, S x > x
869 ============================
870 2 > 1
871
872 ]]
873
874 To instantiate [H] generically, we first need to name the value to be used for [x]. *)
875
876 evar (y : nat).
877
878 (** [[
879 H : forall x : nat, S x > x
880 y := ?279 : nat
881 ============================
882 2 > 1
883
884 ]]
885
886 The proof context is extended with a new variable [y], which has been assigned to be equal to a fresh unification variable [?279]. We want to instantiate [H] with [?279]. To get ahold of the new unification variable, rather than just its alias [y], we perform a trivial call-by-value reduction in the expression [y]. In particular, we only request the use of one reduction rule, [delta], which deals with definition unfolding. We pass a flag further stipulating that only the definition of [y] be unfolded. This is a simple trick for getting at the value of a synonym variable. *)
887
888 let y' := eval cbv delta [y] in y in
889 clear y; generalize (H y').
890
891 (** [[
892 H : forall x : nat, S x > x
893 ============================
894 S ?279 > ?279 -> 2 > 1
895
896 ]]
897
898 Our instantiation was successful. We can finish by using the refined formula to replace the original. *)
899
900 clear H; intro H.
901
902 (** [[
903 H : S ?281 > ?281
904 ============================
905 2 > 1
906
907 ]]
908
909 We can finish the proof by using [apply]'s unification to figure out the proper value of [?281]. (The original unification variable was replaced by another, as often happens in the internals of the various tactics' implementations.) *)
910
911 apply H.
912 Qed.
913
914 (** Now we can write a tactic that encapsulates the pattern we just employed, instantiating all quantifiers of a particular hypothesis. *)
915
916 Ltac insterU H :=
917 repeat match type of H with
918 | forall x : ?T, _ =>
919 let x := fresh "x" in
920 evar (x : T);
921 let x' := eval cbv delta [x] in x in
922 clear x; generalize (H x'); clear H; intro H
923 end.
924
925 Theorem t5' : (forall x : nat, S x > x) -> 2 > 1.
926 intro H; insterU H; apply H.
927 Qed.
928
929 (** This particular example is somewhat silly, since [apply] by itself would have solved the goal originally. Separate forward reasoning is more useful on hypotheses that end in existential quantifications. Before we go through an example, it is useful to define a variant of [insterU] that does not clear the base hypothesis we pass to it. *)
930
931 Ltac insterKeep H :=
932 let H' := fresh "H'" in
933 generalize H; intro H'; insterU H'.
934
935 Section t6.
936 Variables A B : Type.
937 Variable P : A -> B -> Prop.
938 Variable f : A -> A -> A.
939 Variable g : B -> B -> B.
940
941 Hypothesis H1 : forall v, exists u, P v u.
942 Hypothesis H2 : forall v1 u1 v2 u2,
943 P v1 u1
944 -> P v2 u2
945 -> P (f v1 v2) (g u1 u2).
946
947 Theorem t6 : forall v1 v2, exists u1, exists u2, P (f v1 v2) (g u1 u2).
948 intros.
949
950 (** Neither [eauto] nor [firstorder] is clever enough to prove this goal. We can help out by doing some of the work with quantifiers ourselves. *)
951
952 do 2 insterKeep H1.
953
954 (** Our proof state is extended with two generic instances of [H1].
955
956 [[
957 H' : exists u : B, P ?4289 u
958 H'0 : exists u : B, P ?4288 u
959 ============================
960 exists u1 : B, exists u2 : B, P (f v1 v2) (g u1 u2)
961
962 ]]
963
964 [eauto] still cannot prove the goal, so we eliminate the two new existential quantifiers. *)
965
966 repeat match goal with
967 | [ H : ex _ |- _ ] => destruct H
968 end.
969
970 (** Now the goal is simple enough to solve by logic programming. *)
971
972 eauto.
973 Qed.
974 End t6.
975
976 (** Our [insterU] tactic does not fare so well with quantified hypotheses that also contain implications. We can see the problem in a slight modification of the last example. We introduce a new unary predicate [Q] and use it to state an additional requirement of our hypothesis [H1]. *)
977
978 Section t7.
979 Variables A B : Type.
980 Variable Q : A -> Prop.
981 Variable P : A -> B -> Prop.
982 Variable f : A -> A -> A.
983 Variable g : B -> B -> B.
984
985 Hypothesis H1 : forall v, Q v -> exists u, P v u.
986 Hypothesis H2 : forall v1 u1 v2 u2,
987 P v1 u1
988 -> P v2 u2
989 -> P (f v1 v2) (g u1 u2).
990
991 Theorem t6 : forall v1 v2, Q v1 -> Q v2 -> exists u1, exists u2, P (f v1 v2) (g u1 u2).
992 intros; do 2 insterKeep H1;
993 repeat match goal with
994 | [ H : ex _ |- _ ] => destruct H
995 end; eauto.
996
997 (** This proof script does not hit any errors until the very end, when an error message like this one is displayed.
998
999 [[
1000 No more subgoals but non-instantiated existential variables :
1001 Existential 1 =
1002 ?4384 : [A : Type
1003 B : Type
1004 Q : A -> Prop
1005 P : A -> B -> Prop
1006 f : A -> A -> A
1007 g : B -> B -> B
1008 H1 : forall v : A, Q v -> exists u : B, P v u
1009 H2 : forall (v1 : A) (u1 : B) (v2 : A) (u2 : B),
1010 P v1 u1 -> P v2 u2 -> P (f v1 v2) (g u1 u2)
1011 v1 : A
1012 v2 : A
1013 H : Q v1
1014 H0 : Q v2
1015 H' : Q v2 -> exists u : B, P v2 u |- Q v2]
1016
1017 ]]
1018
1019 There is another similar line about a different existential variable. Here, "existential variable" means what we have also called "unification variable." In the course of the proof, some unification variable [?4384] was introduced but never unified. Unification variables are just a device to structure proof search; the language of Gallina proof terms does not include them. Thus, we cannot produce a proof term without instantiating the variable.
1020
1021 The error message shows that [?4384] is meant to be a proof of [Q v2] in a particular proof state, whose variables and hypotheses are displayed. It turns out that [?4384] was created by [insterU], as the value of a proof to pass to [H1]. Recall that, in Gallina, implication is just a degenerate case of [forall] quantification, so the [insterU] code to match against [forall] also matched the implication. Since any proof of [Q v2] is as good as any other in this context, there was never any opportunity to use unification to determine exactly which proof is appropriate. We expect similar problems with any implications in arguments to [insterU]. *)
1022
1023 Abort.
1024 End t7.
1025
1026 Reset insterU.
1027
1028 (** We can redefine [insterU] to treat implications differently. In particular, we pattern-match on the type of the type [T] in [forall x : ?T, ...]. If [T] has type [Prop], then [x]'s instantiation should be thought of as a proof. Thus, instead of picking a new unification variable for it, we instead apply a user-supplied tactic [tac]. It is important that we end this special [Prop] case with [|| fail 1], so that, if [tac] fails to prove [T], we abort the instantiation, rather than continuing on to the default quantifier handling. *)
1029
1030 Ltac insterU tac H :=
1031 repeat match type of H with
1032 | forall x : ?T, _ =>
1033 match type of T with
1034 | Prop =>
1035 (let H' := fresh "H'" in
1036 assert (H' : T); [
1037 solve [ tac ]
1038 | generalize (H H'); clear H H'; intro H ])
1039 || fail 1
1040 | _ =>
1041 let x := fresh "x" in
1042 evar (x : T);
1043 let x' := eval cbv delta [x] in x in
1044 clear x; generalize (H x'); clear H; intro H
1045 end
1046 end.
1047
1048 Ltac insterKeep tac H :=
1049 let H' := fresh "H'" in
1050 generalize H; intro H'; insterU tac H'.
1051
1052 Section t7.
1053 Variables A B : Type.
1054 Variable Q : A -> Prop.
1055 Variable P : A -> B -> Prop.
1056 Variable f : A -> A -> A.
1057 Variable g : B -> B -> B.
1058
1059 Hypothesis H1 : forall v, Q v -> exists u, P v u.
1060 Hypothesis H2 : forall v1 u1 v2 u2,
1061 P v1 u1
1062 -> P v2 u2
1063 -> P (f v1 v2) (g u1 u2).
1064
1065 Theorem t6 : forall v1 v2, Q v1 -> Q v2 -> exists u1, exists u2, P (f v1 v2) (g u1 u2).
1066
1067 (** We can prove the goal by calling [insterKeep] with a tactic that tries to find and apply a [Q] hypothesis over a variable about which we do not yet know any [P] facts. We need to begin this tactic code with [idtac; ] to get around a strange limitation in Coq's proof engine, where a first-class tactic argument may not begin with a [match]. *)
1068
1069 intros; do 2 insterKeep ltac:(idtac; match goal with
1070 | [ H : Q ?v |- _ ] =>
1071 match goal with
1072 | [ _ : context[P v _] |- _ ] => fail 1
1073 | _ => apply H
1074 end
1075 end) H1;
1076 repeat match goal with
1077 | [ H : ex _ |- _ ] => destruct H
1078 end; eauto.
1079 Qed.
1080 End t7.
1081
1082 (** It is often useful to instantiate existential variables explicitly. A built-in tactic provides one way of doing so. *)
1083
1084 Theorem t8 : exists p : nat * nat, fst p = 3.
1085 econstructor; instantiate (1 := (3, 2)); reflexivity.
1086 Qed.
1087
1088 (** The [1] above is identifying an existential variable appearing in the current goal, with the last existential appearing assigned number 1, the second last assigned number 2, and so on. The named existential is replaced everywhere by the term to the right of the [:=].
1089
1090 The [instantiate] tactic can be convenient for exploratory proving, but it leads to very brittle proof scripts that are unlikely to adapt to changing theorem statements. It is often more helpful to have a tactic that can be used to assign a value to a term that is known to be an existential. By employing a roundabout implementation technique, we can build a tactic that generalizes this functionality. In particular, our tactic [equate] will assert that two terms are equal. If one of the terms happens to be an existential, then it will be replaced everywhere with the other term. *)
1091
1092 Ltac equate x y :=
1093 let H := fresh "H" in
1094 assert (H : x = y); [ reflexivity | clear H ].
1095
1096 (** [equate] fails if it is not possible to prove [x = y] by [reflexivity]. We perform the proof only for its unification side effects, clearing the fact [x = y] afterward. With [equate], we can build a less brittle version of the prior example. *)
1097
1098 Theorem t9 : exists p : nat * nat, fst p = 3.
1099 econstructor; match goal with
1100 | [ |- fst ?x = 3 ] => equate x (3, 2)
1101 end; reflexivity.
1102 Qed.