1 (*********************************************************************************************************************************)
2 (* NaturalDeduction: structurally explicit proofs in Coq *)
3 (*********************************************************************************************************************************)
5 Generalizable All Variables.
6 Require Import Preamble.
7 Require Import General.
8 Require Import Coq.Strings.Ascii.
9 Require Import Coq.Strings.String.
14 * Unlike most formalizations, this library offers TWO different ways
15 * to represent a natural deduction proof. To demonstrate this,
16 * consider the signature of the propositional calculus:
18 * Variable PropositionalVariable : Type.
20 * Inductive Formula : Prop :=
21 * | formula_var : PropositionalVariable -> Formula (* every propositional variable is a formula *)
22 * | formula_and : Formula -> Formula -> Formula (* the conjunction of any two formulae is a formula *)
23 * | formula_or : Formula -> Formula -> Formula (* the disjunction of any two formulae is a formula *)
25 * And couple this with the theory of conjunction and disjunction:
26 * φ\/ψ is true if either φ is true or ψ is true, and φ/\ψ is true
27 * if both φ and ψ are true.
29 * 1) Structurally implicit proofs
31 * This is what you would call the "usual" representation –- it's
32 * what most people learn when they first start programming in Coq:
34 * Inductive IsTrue : Formula -> Prop :=
35 * | IsTrue_or1 : forall f1 f2, IsTrue f1 -> IsTrue (formula_or f1 f2)
36 * | IsTrue_or2 : forall f1 f2, IsTrue f2 -> IsTrue (formula_or f1 f2)
37 * | IsTrue_and : forall f1 f2, IsTrue f2 -> IsTrue f2 -> IsTrue (formula_and f1 f2)
39 * Here each judgment (such as "φ is true") is represented by a Coq
42 * 1. A proof of a judgment is any inhabitant of that Coq type.
44 * 2. A proof of a judgment "J2" from hypothesis judgment "J1"
45 * is any Coq function from the Coq type for J1 to the Coq
46 * type for J2; Composition of (hypothetical) proofs is
47 * represented by composition of Coq functions.
49 * 3. A pair of judgments is represented by their product (Coq
50 * type [prod]) in Coq; a pair of proofs is represented by
51 * their pair (Coq function [pair]) in Coq.
53 * 4. Duplication of hypotheses is represented by the Coq
54 * function (fun x => (x,x)). Dereliction of hypotheses is
55 * represented by the coq function (fun (x,y) => x) or (fun
56 * (x,y) => y). Exchange of the order of hypotheses is
57 * represented by the Coq function (fun (x,y) => (y,x)).
59 * The above can be done using lists instead of tuples.
61 * The advantage of this approach is that it requires a minimum
62 * amount of syntax, and takes maximum advantage of Coq's
63 * automation facilities.
65 * The disadvantage is that one cannot reason about proof-theoretic
66 * properties *generically* across different signatures and
67 * theories. Each signature has its own type of judgments, and
68 * each theory has its own type of proofs. In the present
69 * development we will want to prove –– in this generic manner --
70 * that various classes of natural deduction calculi form various
71 * kinds of categories. So we will need this ability to reason
72 * about proofs independently of the type used to represent
73 * judgments and (more importantly) of the set of basic inference
76 * 2) Structurally explicit proofs
78 * Structurally explicit proofs are formalized in this file
79 * (NaturalDeduction.v) and are designed specifically in order to
80 * circumvent the problem in the previous paragraph.
85 * REGARDING LISTS versus TREES:
87 * You'll notice that this formalization uses (Tree (option A)) in a
88 * lot of places where you might find (list A) more natural. If this
89 * bothers you, see the end of the file for the technical reasons why.
90 * In short, it lets us avoid having to mess about with JMEq or EqDep,
91 * which are not as well-supported by the Coq core as the theory of
95 Section Natural_Deduction.
97 (* any Coq Type may be used as the set of judgments *)
98 Context {Judgment : Type}.
100 (* any Coq Type –- indexed by the hypothesis and conclusion judgments -- may be used as the set of basic inference rules *)
101 Context {Rule : forall (hypotheses:Tree ??Judgment)(conclusion:Tree ??Judgment), Type}.
104 * This type represents a valid Natural Deduction proof from a list
105 * (tree) of hypotheses; the notation H/⋯⋯/C is meant to look like
106 * a proof tree with the middle missing if you tilt your head to
107 * the left (yeah, I know it's a stretch). Note also that this
108 * type is capable of representing proofs with multiple
109 * conclusions, whereas a Rule may have only one conclusion.
112 forall hypotheses:Tree ??Judgment,
113 forall conclusions:Tree ??Judgment,
116 (* natural deduction: you may infer anything from itself -- "identity proof" *)
117 | nd_id0 : [ ] /⋯⋯/ [ ]
118 | nd_id1 : forall h, [ h ] /⋯⋯/ [ h ]
120 (* natural deduction: you may discard conclusions *)
121 | nd_weak : forall h, [ h ] /⋯⋯/ [ ]
123 (* natural deduction: you may duplicate conclusions *)
124 | nd_copy : forall h, h /⋯⋯/ (h,,h)
126 (* natural deduction: you may write two proof trees side by side on a piece of paper -- "proof product" *)
127 | nd_prod : forall {h1 h2 c1 c2}
130 ( h1 ,, h2 /⋯⋯/ c1 ,, c2)
132 (* natural deduction: given a proof of every hypothesis, you may discharge them -- "proof composition" *)
139 (* structural rules on lists of judgments *)
140 | nd_cancell : forall {a}, [] ,, a /⋯⋯/ a
141 | nd_cancelr : forall {a}, a ,, [] /⋯⋯/ a
142 | nd_llecnac : forall {a}, a /⋯⋯/ [] ,, a
143 | nd_rlecnac : forall {a}, a /⋯⋯/ a ,, []
144 | nd_assoc : forall {a b c}, (a,,b),,c /⋯⋯/ a,,(b,,c)
145 | nd_cossa : forall {a b c}, a,,(b,,c) /⋯⋯/ (a,,b),,c
147 (* any Rule by itself counts as a proof *)
148 | nd_rule : forall {h c} (r:Rule h c), h /⋯⋯/ c
150 where "H /⋯⋯/ C" := (ND H C).
152 Notation "H /⋯⋯/ C" := (ND H C) : pf_scope.
153 Notation "a ;; b" := (nd_comp a b) : nd_scope.
154 Notation "a ** b" := (nd_prod a b) : nd_scope.
158 (* a proof is "structural" iff it does not contain any invocations of nd_rule *)
159 Inductive Structural : forall {h c}, h /⋯⋯/ c -> Prop :=
160 | nd_structural_id0 : Structural nd_id0
161 | nd_structural_id1 : forall h, Structural (nd_id1 h)
162 | nd_structural_weak : forall h, Structural (nd_weak h)
163 | nd_structural_copy : forall h, Structural (nd_copy h)
164 | nd_structural_prod : forall `(pf1:h1/⋯⋯/c1)`(pf2:h2/⋯⋯/c2), Structural pf1 -> Structural pf2 -> Structural (pf1**pf2)
165 | nd_structural_comp : forall `(pf1:h1/⋯⋯/x) `(pf2: x/⋯⋯/c2), Structural pf1 -> Structural pf2 -> Structural (pf1;;pf2)
166 | nd_structural_cancell : forall {a}, Structural (@nd_cancell a)
167 | nd_structural_cancelr : forall {a}, Structural (@nd_cancelr a)
168 | nd_structural_llecnac : forall {a}, Structural (@nd_llecnac a)
169 | nd_structural_rlecnac : forall {a}, Structural (@nd_rlecnac a)
170 | nd_structural_assoc : forall {a b c}, Structural (@nd_assoc a b c)
171 | nd_structural_cossa : forall {a b c}, Structural (@nd_cossa a b c)
174 (* multi-judgment generalization of nd_id0 and nd_id1; making nd_id0/nd_id1 primitive and deriving all other *)
175 Fixpoint nd_id (sl:Tree ??Judgment) : sl /⋯⋯/ sl :=
177 | T_Leaf None => nd_id0
178 | T_Leaf (Some x) => nd_id1 x
179 | T_Branch a b => nd_prod (nd_id a) (nd_id b)
182 Hint Constructors Structural.
183 Lemma nd_id_structural : forall sl, Structural (nd_id sl).
185 induction sl; simpl; auto.
189 (* An equivalence relation on proofs which is sensitive only to the logical content of the proof -- insensitive to
190 * structural variations *)
192 { ndr_eqv : forall {h c }, h /⋯⋯/ c -> h /⋯⋯/ c -> Prop where "pf1 === pf2" := (@ndr_eqv _ _ pf1 pf2)
193 ; ndr_eqv_equivalence : forall h c, Equivalence (@ndr_eqv h c)
195 (* the relation must respect composition, be associative wrt composition, and be left and right neutral wrt the identity proof *)
196 ; ndr_comp_respects : forall {a b c}(f f':a/⋯⋯/b)(g g':b/⋯⋯/c), f === f' -> g === g' -> f;;g === f';;g'
197 ; ndr_comp_associativity : forall `(f:a/⋯⋯/b)`(g:b/⋯⋯/c)`(h:c/⋯⋯/d), (f;;g);;h === f;;(g;;h)
198 ; ndr_comp_left_identity : forall `(f:a/⋯⋯/c), nd_id _ ;; f === f
199 ; ndr_comp_right_identity : forall `(f:a/⋯⋯/c), f ;; nd_id _ === f
201 (* the relation must respect products, be associative wrt products, and be left and right neutral wrt the identity proof *)
202 ; ndr_prod_respects : forall {a b c d}(f f':a/⋯⋯/b)(g g':c/⋯⋯/d), f===f' -> g===g' -> f**g === f'**g'
203 ; ndr_prod_associativity : forall `(f:a/⋯⋯/a')`(g:b/⋯⋯/b')`(h:c/⋯⋯/c'), (f**g)**h === nd_assoc ;; f**(g**h) ;; nd_cossa
204 ; ndr_prod_left_identity : forall `(f:a/⋯⋯/b), (nd_id0 ** f ) === nd_cancell ;; f ;; nd_llecnac
205 ; ndr_prod_right_identity : forall `(f:a/⋯⋯/b), (f ** nd_id0) === nd_cancelr ;; f ;; nd_rlecnac
207 (* products and composition must distribute over each other *)
208 ; ndr_prod_preserves_comp : forall `(f:a/⋯⋯/b)`(f':a'/⋯⋯/b')`(g:b/⋯⋯/c)`(g':b'/⋯⋯/c'), (f;;g)**(f';;g') === (f**f');;(g**g')
210 (* any two _structural_ proofs with the same hypotheses/conclusions must be considered equal *)
211 ; ndr_structural_indistinguishable : forall `(f:a/⋯⋯/b)(g:a/⋯⋯/b), Structural f -> Structural g -> f===g
215 * Single-conclusion proofs; this is an alternate representation
216 * where each inference has only a single conclusion. These have
217 * worse compositionality properties than ND's, but are easier to
218 * emit as LaTeX code.
220 Inductive SCND : Tree ??Judgment -> Tree ??Judgment -> Type :=
221 | scnd_comp : forall ht ct c , SCND ht ct -> Rule ct [c] -> SCND ht [c]
222 | scnd_weak : forall c , SCND c []
223 | scnd_leaf : forall ht c , SCND ht [c] -> SCND ht [c]
224 | scnd_branch : forall ht c1 c2, SCND ht c1 -> SCND ht c2 -> SCND ht (c1,,c2)
226 Hint Constructors SCND.
228 (* Any ND whose primitive Rules have at most one conclusion (note that nd_prod is allowed!) can be turned into an SCND. *)
229 Definition mkSCND (all_rules_one_conclusion : forall h c1 c2, Rule h (c1,,c2) -> False)
230 : forall h x c, ND x c -> SCND h x -> SCND h c.
232 induction nd; intro k.
236 eapply scnd_branch; apply k.
238 apply (scnd_branch _ _ _ (IHnd1 X) (IHnd2 X0)).
242 inversion k; subst; auto.
243 inversion k; subst; auto.
244 apply scnd_branch; auto.
245 apply scnd_branch; auto.
246 inversion k; subst; inversion X; subst; auto.
247 inversion k; subst; inversion X0; subst; auto.
250 apply scnd_leaf. eapply scnd_comp. apply k. apply r.
252 set (all_rules_one_conclusion _ _ _ r) as bogus.
256 (* a "ClosedND" is a proof with no open hypotheses and no multi-conclusion rules *)
257 Inductive ClosedND : Tree ??Judgment -> Type :=
258 | cnd_weak : ClosedND []
259 | cnd_rule : forall h c , ClosedND h -> Rule h c -> ClosedND c
260 | cnd_branch : forall c1 c2, ClosedND c1 -> ClosedND c2 -> ClosedND (c1,,c2)
263 (* we can turn an SCND without hypotheses into a ClosedND *)
264 Definition closedFromSCND h c (pn2:SCND h c)(cnd:ClosedND h) : ClosedND c.
265 refine ((fix closedFromPnodes h c (pn2:SCND h c)(cnd:ClosedND h) {struct pn2} :=
266 (match pn2 in SCND H C return H=h -> C=c -> _ with
267 | scnd_weak c => let case_weak := tt in _
268 | scnd_leaf ht z pn' => let case_leaf := tt in let qq := closedFromPnodes _ _ pn' in _
269 | scnd_comp ht ct c pn' rule => let case_comp := tt in let qq := closedFromPnodes _ _ pn' in _
270 | scnd_branch ht c1 c2 pn' pn'' => let case_branch := tt in
271 let q1 := closedFromPnodes _ _ pn' in
272 let q2 := closedFromPnodes _ _ pn'' in _
274 end (refl_equal _) (refl_equal _))) h c pn2 cnd).
295 destruct case_branch.
298 apply q1. subst. apply cnd0.
299 apply q2. subst. apply cnd0.
302 Close Scope nd_scope.
305 End Natural_Deduction.
307 Implicit Arguments ND [ Judgment ].
308 Hint Constructors Structural.
309 Hint Extern 1 => apply nd_id_structural.
310 Hint Extern 1 => apply ndr_structural_indistinguishable.
312 (* This first notation gets its own scope because it can be confusing when we're working with multiple different kinds
313 * of proofs. When only one kind of proof is in use, it's quite helpful though. *)
314 Notation "H /⋯⋯/ C" := (@ND _ _ H C) : pf_scope.
315 Notation "a ;; b" := (nd_comp a b) : nd_scope.
316 Notation "a ** b" := (nd_prod a b) : nd_scope.
317 Notation "[# a #]" := (nd_rule a) : nd_scope.
318 Notation "a === b" := (@ndr_eqv _ _ _ _ _ a b) : nd_scope.
320 (* enable setoid rewriting *)
324 Add Parametric Relation {jt rt ndr h c} : (h/⋯⋯/c) (@ndr_eqv jt rt ndr h c)
325 reflexivity proved by (@Equivalence_Reflexive _ _ (ndr_eqv_equivalence h c))
326 symmetry proved by (@Equivalence_Symmetric _ _ (ndr_eqv_equivalence h c))
327 transitivity proved by (@Equivalence_Transitive _ _ (ndr_eqv_equivalence h c))
328 as parametric_relation_ndr_eqv.
329 Add Parametric Morphism {jt rt ndr h x c} : (@nd_comp jt rt h x c)
330 with signature ((ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)))
331 as parametric_morphism_nd_comp.
332 intros; apply ndr_comp_respects; auto.
334 Add Parametric Morphism {jt rt ndr a b c d} : (@nd_prod jt rt a b c d)
335 with signature ((ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)))
336 as parametric_morphism_nd_prod.
337 intros; apply ndr_prod_respects; auto.
340 (* a generalization of the procedure used to build (nd_id n) from nd_id0 and nd_id1 *)
341 Definition nd_replicate
347 (forall (o:Ob), @ND Judgment Rule [h o] [c o]) ->
348 @ND Judgment Rule (mapOptionTree h j) (mapOptionTree c j).
357 (* "map" over natural deduction proofs, where the result proof has the same judgments (but different rules) *)
360 {Judgment}{Rule0}{Rule1}
361 (r:forall h c, Rule0 h c -> @ND Judgment Rule1 h c)
363 (pf:@ND Judgment Rule0 h c)
365 @ND Judgment Rule1 h c.
366 intros Judgment Rule0 Rule1 r.
368 refine ((fix nd_map h c pf {struct pf} :=
372 @ND Judgment Rule1 H C
374 | nd_id0 => let case_nd_id0 := tt in _
375 | nd_id1 h => let case_nd_id1 := tt in _
376 | nd_weak h => let case_nd_weak := tt in _
377 | nd_copy h => let case_nd_copy := tt in _
378 | nd_prod _ _ _ _ lpf rpf => let case_nd_prod := tt in _
379 | nd_comp _ _ _ top bot => let case_nd_comp := tt in _
380 | nd_rule _ _ rule => let case_nd_rule := tt in _
381 | nd_cancell _ => let case_nd_cancell := tt in _
382 | nd_cancelr _ => let case_nd_cancelr := tt in _
383 | nd_llecnac _ => let case_nd_llecnac := tt in _
384 | nd_rlecnac _ => let case_nd_rlecnac := tt in _
385 | nd_assoc _ _ _ => let case_nd_assoc := tt in _
386 | nd_cossa _ _ _ => let case_nd_cossa := tt in _
387 end))) ); simpl in *.
389 destruct case_nd_id0. apply nd_id0.
390 destruct case_nd_id1. apply nd_id1.
391 destruct case_nd_weak. apply nd_weak.
392 destruct case_nd_copy. apply nd_copy.
393 destruct case_nd_prod. apply (nd_prod (nd_map _ _ lpf) (nd_map _ _ rpf)).
394 destruct case_nd_comp. apply (nd_comp (nd_map _ _ top) (nd_map _ _ bot)).
395 destruct case_nd_cancell. apply nd_cancell.
396 destruct case_nd_cancelr. apply nd_cancelr.
397 destruct case_nd_llecnac. apply nd_llecnac.
398 destruct case_nd_rlecnac. apply nd_rlecnac.
399 destruct case_nd_assoc. apply nd_assoc.
400 destruct case_nd_cossa. apply nd_cossa.
404 (* "map" over natural deduction proofs, where the result proof has different judgments *)
407 {Judgment0}{Rule0}{Judgment1}{Rule1}
408 (f:Judgment0->Judgment1)
409 (r:forall h c, Rule0 h c -> @ND Judgment1 Rule1 (mapOptionTree f h) (mapOptionTree f c))
411 (pf:@ND Judgment0 Rule0 h c)
413 @ND Judgment1 Rule1 (mapOptionTree f h) (mapOptionTree f c).
414 intros Judgment0 Rule0 Judgment1 Rule1 f r.
416 refine ((fix nd_map' h c pf {struct pf} :=
420 @ND Judgment1 Rule1 (mapOptionTree f H) (mapOptionTree f C)
422 | nd_id0 => let case_nd_id0 := tt in _
423 | nd_id1 h => let case_nd_id1 := tt in _
424 | nd_weak h => let case_nd_weak := tt in _
425 | nd_copy h => let case_nd_copy := tt in _
426 | nd_prod _ _ _ _ lpf rpf => let case_nd_prod := tt in _
427 | nd_comp _ _ _ top bot => let case_nd_comp := tt in _
428 | nd_rule _ _ rule => let case_nd_rule := tt in _
429 | nd_cancell _ => let case_nd_cancell := tt in _
430 | nd_cancelr _ => let case_nd_cancelr := tt in _
431 | nd_llecnac _ => let case_nd_llecnac := tt in _
432 | nd_rlecnac _ => let case_nd_rlecnac := tt in _
433 | nd_assoc _ _ _ => let case_nd_assoc := tt in _
434 | nd_cossa _ _ _ => let case_nd_cossa := tt in _
435 end))) ); simpl in *.
437 destruct case_nd_id0. apply nd_id0.
438 destruct case_nd_id1. apply nd_id1.
439 destruct case_nd_weak. apply nd_weak.
440 destruct case_nd_copy. apply nd_copy.
441 destruct case_nd_prod. apply (nd_prod (nd_map' _ _ lpf) (nd_map' _ _ rpf)).
442 destruct case_nd_comp. apply (nd_comp (nd_map' _ _ top) (nd_map' _ _ bot)).
443 destruct case_nd_cancell. apply nd_cancell.
444 destruct case_nd_cancelr. apply nd_cancelr.
445 destruct case_nd_llecnac. apply nd_llecnac.
446 destruct case_nd_rlecnac. apply nd_rlecnac.
447 destruct case_nd_assoc. apply nd_assoc.
448 destruct case_nd_cossa. apply nd_cossa.
452 (* witnesses the fact that every Rule in a particular proof satisfies the given predicate *)
453 Inductive nd_property {Judgment}{Rule}(P:forall h c, @Rule h c -> Prop) : forall {h}{c}, @ND Judgment Rule h c -> Prop :=
454 | nd_property_structural : forall h c pf, Structural pf -> @nd_property _ _ P h c pf
455 | nd_property_prod : forall h0 c0 pf0 h1 c1 pf1,
456 @nd_property _ _ P h0 c0 pf0 -> @nd_property _ _ P h1 c1 pf1 -> @nd_property _ _ P _ _ (nd_prod pf0 pf1)
457 | nd_property_comp : forall h0 c0 pf0 c1 pf1,
458 @nd_property _ _ P h0 c0 pf0 -> @nd_property _ _ P c0 c1 pf1 -> @nd_property _ _ P _ _ (nd_comp pf0 pf1)
459 | nd_property_rule : forall h c r, P h c r -> @nd_property _ _ P h c (nd_rule r).
460 Hint Constructors nd_property.
462 Close Scope pf_scope.
463 Close Scope nd_scope.