Debian Codegraph @

dev/doc/unification.txt

raw
1 Some notes about the use of unification in Rocq
2 -----------------------------------------------
3
4 There are several applications of unification and pattern-matching
5
6 ** Unification of types **
7
8 - For type inference, inference of implicit arguments
9 * this basically amounts to solve problems of the form T <= U or T = U
10 where T and U are types coming from a given typing problem
11 * this kind of problem has to succeed and all the power of unification is
12 a priori expected (full beta/delta/iota/zeta/nu/mu, pattern-unification,
13 pruning, imitation/projection heuristics, ...)
14
15 - For lemma application (apply, auto, ...)
16 * these are also problems of the form T <= U on types but with T
17 coming from a lemma and U from the goal
18 * it is not obvious that we always want unification and not matching
19 * it is not clear which amounts of delta one wants to use
20
21 ** Looking for subterms **
22
23 - For tactics applying on subterms: induction, destruct, rewrite
24
25 - As part of unification of types in the presence of higher-order
26 evars (e.g. when applying a lemma of conclusion "?P t")
27
28
29 ----------------------------------------------------------------------
30 Here are examples of features one may want or not when looking for subterms
31
32 A- REWRITING
33
34 1- Full conversion on closed terms
35
36 1a- Full conversion on closed terms in the presence of at least one evars (meta)
37
38 Section A1.
39 Variable y: nat.
40 Hypothesis H: forall x, x+2 = 0.
41 Goal y+(1+1) = 0.
42 rewrite H.
43 (* 0 = 0 *)
44 Abort.
45
46 Goal 2+(1+1) = 0.
47 rewrite H.
48 (* 0 = 0 *)
49 Abort.
50
51 (* This exists since the very beginning of Chet's unification for tactics *)
52 (* But this fails for setoid rewrite *)
53
54 1b- Full conversion on closed terms without any evars in the lemma
55
56 1b.1- Fails on rewrite (because Unification.w_unify_to_subterm_list replaces
57 unification by check for a syntactic subterm if terms has no evar/meta)
58
59 Goal 0+1 = 0 -> 0+(1+0) = 0.
60 intros H; rewrite H.
61 (* fails *)
62 Abort.
63
64 1b.2- Works with setoid rewrite
65
66 Require Import Setoid.
67 Goal 0+1 = 0 -> 0+(1+0) = 0.
68 intros H; rewrite H at 1.
69 (* 0 = 0 *)
70 Abort.
71
72 2- Using known instances in full conversion on closed terms
73
74 Section A2.
75 Hypothesis H: forall x, x+(2+x) = 0.
76 Goal 1+(1+2) = 0.
77 rewrite H.
78 Abort.
79 End A2.
80
81 (* This exists since 8.2 (HH) *)
82
83 3- Pattern-unification on Rels
84
85 Section A3a.
86 Variable F: (nat->nat->nat)->nat.
87 Goal exists f, F (fun x y => f x y) = 0 -> F (fun x y => plus y x) = 0.
88 eexists. intro H; rewrite H.
89 (* 0 = 0 *)
90 Abort.
91 End A3a.
92
93 (* Works since pattern unification on Meta applied to Rel was introduced *)
94 (* in unification.ml (8.1, Sep 2006, HH) *)
95
96 Section A3b.
97 Variables x y: nat.
98 Variable H: forall f, f x y = 0.
99 Goal plus y x = 0.
100 rewrite H.
101 (* 0 = 0 *)
102 Abort.
103 End A3b.
104
105 (* Works since pattern unification on all Meta was supported *)
106 (* in unification.ml (8.4, Jun 2011, HH) *)
107
108 4- Unification with open terms
109
110 Section A4.
111 Hypothesis H: forall x, S x = 0.
112 Goal S 0 = 0.
113 rewrite (H _).
114 (* 0 = 0 *)
115 Abort.
116 End A4.
117
118 (* Works since unification on Evar was introduced so as to support rewriting *)
119 (* with open terms (8.2, MS, r11543, Unification.w_unify_to_subterm_list ) *)
120
121 5- Unification of pre-existing evars
122
123 5a- Basic unification of pre-existing evars
124
125 Section A4.
126 Variables x y: nat.
127 Goal exists z, S z = 0 -> S (plus y x) = 0.
128 eexists. intro H; rewrite H.
129 (* 0 = 0 *)
130 Abort.
131 End A4.
132
133 (* This worked in 8.2 and 8.3 as a side-effect of support for rewriting *)
134 (* with open terms (8.2, MS, r11543) *)
135
136 5b- Pattern-unification of pre-existing evars in rewriting lemma
137
138 Goal exists f, forall x y, f x y = 0 -> plus y x = 0.
139 eexists. intros x y H; rewrite H.
140 (* 0 = 0 *)
141 Abort.
142
143 (* Works since pattern-unification on Evar was introduced *)
144 (* in unification.ml (8.3, HH, r12229) *)
145 (* currently governed by a flag: use_evars_pattern_unification *)
146
147 5c- Pattern-unification of pre-existing evars in goal
148
149 Goal exists f, forall x y, plus x y = 0 -> f y x = 0.
150 eexists. intros x y H; rewrite H.
151 (* 0 = 0 *)
152 Abort.
153
154 (* This worked in 8.2 and 8.3 but was removed for autorewrite in 8.4 *)
155
156 5d- Mixing pattern-unification of pre-existing evars in goal and evars in lemma
157
158 Goal exists f, forall x, (forall y, plus x y = 0) -> forall y:nat, f y x = 0.
159 eexists. intros x H y. rewrite H.
160 (* 0 = 0 *)
161 Abort.
162
163 (* This worked in 8.2 and 8.3 but was removed for autorewrite in 8.4 *)
164
165 6- Multiple non-identical but convertible occurrences
166
167 Tactic rewrite only considers the first one, from left-to-right, e.g.:
168
169 Section A6.
170 Variable y: nat.
171 Hypothesis H: forall x, x+2 = 0.
172 Goal (y+(2+0))+(y+(1+1)) = (y+(1+1))+(y+(2+0)).
173 rewrite H.
174 (* 0+(y+(1+1)) = y+(1+1)+0 *)
175 Abort.
176 End A6.
177
178 Tactic setoid rewrite first looks for syntactically equal terms and if
179 not uses the leftmost occurrence modulo delta.
180
181 Require Import Setoid.
182 Section A6.
183 Variable y: nat.
184 Hypothesis H: forall x, x+2 = 0.
185 Goal (y+(2+0))+(y+2) = (y+2)+(y+(2+0)).
186 rewrite H at 1 2 3 4.
187 (* (y+(2+0))+0 = 0+(y+(2+0)) *)
188 Abort.
189
190 Goal (y+(2+0))+(y+(1+1)) = (y+(1+1))+(y+(2+0)).
191 rewrite H at 1 2 3 4.
192 (* 0+(y+(1+1)) = y+(1+1)+0 *)
193 Abort.
194 End A6.
195
196 7- Conversion
197
198 Section A6.
199 Variable y: nat.
200 Hypothesis H: forall x, S x = 0.
201 Goal id 1 = 0.
202 rewrite H.
203
204
205 B- ELIMINATION (INDUCTION / CASE ANALYSIS)
206
207 This is simpler because open terms are not allowed and no unification
208 is involved (8.3).