comparison src/Reflection.v @ 289:4662b6f099b0

PC comments on Reflection and Large
author Adam Chlipala <adam@chlipala.net>
date Wed, 10 Nov 2010 15:00:37 -0500
parents 54e8ecb5ec88
children b441010125d4
comparison
equal deleted inserted replaced
288:b653e6b19b6d 289:4662b6f099b0
1 (* Copyright (c) 2008-2009, Adam Chlipala 1 (* Copyright (c) 2008-2010, Adam Chlipala
2 * 2 *
3 * This work is licensed under a 3 * This work is licensed under a
4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
5 * Unported License. 5 * Unported License.
6 * The license text is available at: 6 * The license text is available at:
45 (Even_SS 45 (Even_SS
46 (Even_SS 46 (Even_SS
47 47
48 ]] 48 ]]
49 49
50 %\noindent%...and so on. This procedure always works (at least on machines with infinite resources), but it has a serious drawback, which we see when we print the proof it generates that 256 is even. The final proof term has length linear in the input value. This seems like a shame, since we could write a trivial and trustworthy program to verify evenness of constants. The proof checker could simply call our program where needed. 50 %\noindent%...and so on. This procedure always works (at least on machines with infinite resources), but it has a serious drawback, which we see when we print the proof it generates that 256 is even. The final proof term has length super-linear in the input value. (Coq's implicit arguments mechanism is hiding the values given for parameter [n] of [Even_SS], which is why the proof term only appears linear here.) This seems like a shame, since we could write a trivial and trustworthy program to verify evenness of constants. The proof checker could simply call our program where needed.
51 51
52 It is also unfortunate not to have static typing guarantees that our tactic always behaves appropriately. Other invocations of similar tactics might fail with dynamic type errors, and we would not know about the bugs behind these errors until we happened to attempt to prove complex enough goals. 52 It is also unfortunate not to have static typing guarantees that our tactic always behaves appropriately. Other invocations of similar tactics might fail with dynamic type errors, and we would not know about the bugs behind these errors until we happened to attempt to prove complex enough goals.
53 53
54 The techniques of proof by reflection address both complaints. We will be able to write proofs like this with constant size overhead beyond the size of the input, and we will do it with verified decision procedures written in Gallina. 54 The techniques of proof by reflection address both complaints. We will be able to write proofs like this with constant size overhead beyond the size of the input, and we will do it with verified decision procedures written in Gallina.
55 55
88 end) with 88 end) with
89 | Proved pf => pf 89 | Proved pf => pf
90 | Uncertain => I 90 | Uncertain => I
91 end. 91 end.
92 92
93 (** It may seem strange to define a function like this. However, it turns out to be very useful in writing a reflective verison of our earlier [prove_even] tactic: *) 93 (** It may seem strange to define a function like this. However, it turns out to be very useful in writing a reflective version of our earlier [prove_even] tactic: *)
94 94
95 Ltac prove_even_reflective := 95 Ltac prove_even_reflective :=
96 match goal with 96 match goal with
97 | [ |- isEven ?N] => exact (partialOut (check_even N)) 97 | [ |- isEven ?N] => exact (partialOut (check_even N))
98 end. 98 end.
99 (* end thide *) 99 (* end thide *)
100 100
101 (** We identify which natural number we are considering, and we "prove" its evenness by pulling the proof out of the appropriate [check_even] call. *) 101 (** We identify which natural number we are considering, and we %``%#"#prove#"#%''% its evenness by pulling the proof out of the appropriate [check_even] call. *)
102 102
103 Theorem even_256' : isEven 256. 103 Theorem even_256' : isEven 256.
104 prove_even_reflective. 104 prove_even_reflective.
105 Qed. 105 Qed.
106 106
109 even_256' = partialOut (check_even 256) 109 even_256' = partialOut (check_even 256)
110 : isEven 256 110 : isEven 256
111 111
112 ]] 112 ]]
113 113
114 We can see a constant wrapper around the object of the proof. For any even number, this form of proof will suffice. What happens if we try the tactic with an odd number? *) 114 We can see a constant wrapper around the object of the proof. For any even number, this form of proof will suffice. The size of the proof term is now linear in the number being checked, containing two repetitions of the unary form of that number, one of which is hidden above within the implicit argument to [partialOut].
115
116 What happens if we try the tactic with an odd number? *)
115 117
116 Theorem even_255 : isEven 255. 118 Theorem even_255 : isEven 255.
117 (** [[ 119 (** [[
118 prove_even_reflective. 120 prove_even_reflective.
119 121
156 158
157 ]] 159 ]]
158 160
159 As we might expect, the proof that [tauto] builds contains explicit applications of natural deduction rules. For large formulas, this can add a linear amount of proof size overhead, beyond the size of the input. 161 As we might expect, the proof that [tauto] builds contains explicit applications of natural deduction rules. For large formulas, this can add a linear amount of proof size overhead, beyond the size of the input.
160 162
161 To write a reflective procedure for this class of goals, we will need to get into the actual "reflection" part of "proof by reflection." It is impossible to case-analyze a [Prop] in any way in Gallina. We must %\textit{%#<i>#reflect#</i>#%}% [Prop] into some type that we %\textit{%#<i>#can#</i>#%}% analyze. This inductive type is a good candidate: *) 163 To write a reflective procedure for this class of goals, we will need to get into the actual %``%#"#reflection#"#%''% part of %``%#"#proof by reflection.#"#%''% It is impossible to case-analyze a [Prop] in any way in Gallina. We must %\textit{%#<i>#reflect#</i>#%}% [Prop] into some type that we %\textit{%#<i>#can#</i>#%}% analyze. This inductive type is a good candidate: *)
162 164
163 (* begin thide *) 165 (* begin thide *)
164 Inductive taut : Set := 166 Inductive taut : Set :=
165 | TautTrue : taut 167 | TautTrue : taut
166 | TautAnd : taut -> taut -> taut 168 | TautAnd : taut -> taut -> taut
167 | TautOr : taut -> taut -> taut 169 | TautOr : taut -> taut -> taut
168 | TautImp : taut -> taut -> taut. 170 | TautImp : taut -> taut -> taut.
169 171
170 (** We write a recursive function to "unreflect" this syntax back to [Prop]. *) 172 (** We write a recursive function to %``%#"#unreflect#"#%''% this syntax back to [Prop]. *)
171 173
172 Fixpoint tautDenote (t : taut) : Prop := 174 Fixpoint tautDenote (t : taut) : Prop :=
173 match t with 175 match t with
174 | TautTrue => True 176 | TautTrue => True
175 | TautAnd t1 t2 => tautDenote t1 /\ tautDenote t2 177 | TautAnd t1 t2 => tautDenote t1 /\ tautDenote t2
227 (TautOr TautTrue (TautAnd TautTrue (TautImp TautTrue TautTrue)))) 229 (TautOr TautTrue (TautAnd TautTrue (TautImp TautTrue TautTrue))))
228 : True /\ True -> True \/ True /\ (True -> True) 230 : True /\ True -> True \/ True /\ (True -> True)
229 231
230 ]] 232 ]]
231 233
232 It is worth considering how the reflective tactic improves on a pure-Ltac implementation. The formula reflection process is just as ad-hoc as before, so we gain little there. In general, proofs will be more complicated than formula translation, and the "generic proof rule" that we apply here %\textit{%#<i>#is#</i>#%}% on much better formal footing than a recursive Ltac function. The dependent type of the proof guarantees that it "works" on any input formula. This is all in addition to the proof-size improvement that we have already seen. *) 234 It is worth considering how the reflective tactic improves on a pure-Ltac implementation. The formula reflection process is just as ad-hoc as before, so we gain little there. In general, proofs will be more complicated than formula translation, and the %``%#"#generic proof rule#"#%''% that we apply here %\textit{%#<i>#is#</i>#%}% on much better formal footing than a recursive Ltac function. The dependent type of the proof guarantees that it %``%#"#works#"#%''% on any input formula. This is all in addition to the proof-size improvement that we have already seen. *)
233 235
234 236
235 (** * A Monoid Expression Simplifier *) 237 (** * A Monoid Expression Simplifier *)
236 238
237 (** Proof by reflection does not require encoding of all of the syntax in a goal. We can insert "variables" in our syntax types to allow injection of arbitrary pieces, even if we cannot apply specialized reasoning to them. In this section, we explore that possibility by writing a tactic for normalizing monoid equations. *) 239 (** Proof by reflection does not require encoding of all of the syntax in a goal. We can insert %``%#"#variables#"#%''% in our syntax types to allow injection of arbitrary pieces, even if we cannot apply specialized reasoning to them. In this section, we explore that possibility by writing a tactic for normalizing monoid equations. *)
238 240
239 Section monoid. 241 Section monoid.
240 Variable A : Set. 242 Variable A : Set.
241 Variable e : A. 243 Variable e : A.
242 Variable f : A -> A -> A. 244 Variable f : A -> A -> A.
247 Hypothesis identl : forall a, e + a = a. 249 Hypothesis identl : forall a, e + a = a.
248 Hypothesis identr : forall a, a + e = a. 250 Hypothesis identr : forall a, a + e = a.
249 251
250 (** We add variables and hypotheses characterizing an arbitrary instance of the algebraic structure of monoids. We have an associative binary operator and an identity element for it. 252 (** We add variables and hypotheses characterizing an arbitrary instance of the algebraic structure of monoids. We have an associative binary operator and an identity element for it.
251 253
252 It is easy to define an expression tree type for monoid expressions. A [Var] constructor is a "catch-all" case for subexpressions that we cannot model. These subexpressions could be actual Gallina variables, or they could just use functions that our tactic is unable to understand. *) 254 It is easy to define an expression tree type for monoid expressions. A [Var] constructor is a %``%#"#catch-all#"#%''% case for subexpressions that we cannot model. These subexpressions could be actual Gallina variables, or they could just use functions that our tactic is unable to understand. *)
253 255
254 (* begin thide *) 256 (* begin thide *)
255 Inductive mexp : Set := 257 Inductive mexp : Set :=
256 | Ident : mexp 258 | Ident : mexp
257 | Var : A -> mexp 259 | Var : A -> mexp
258 | Op : mexp -> mexp -> mexp. 260 | Op : mexp -> mexp -> mexp.
259 261
260 (** Next, we write an "un-reflect" function. *) 262 (** Next, we write an %``%#"#un-reflect#"#%''% function. *)
261 263
262 Fixpoint mdenote (me : mexp) : A := 264 Fixpoint mdenote (me : mexp) : A :=
263 match me with 265 match me with
264 | Ident => e 266 | Ident => e
265 | Var v => v 267 | Var v => v