1 (*********************************************************************************************************************************)
2 (* NaturalDeduction: *)
4 (* Structurally explicit natural deduction proofs. *)
6 (*********************************************************************************************************************************)
8 Generalizable All Variables.
9 Require Import Preamble.
10 Require Import General.
11 Require Import Coq.Strings.Ascii.
12 Require Import Coq.Strings.String.
17 * Unlike most formalizations, this library offers TWO different ways
18 * to represent a natural deduction proof. To demonstrate this,
19 * consider the signature of the propositional calculus:
21 * Variable PropositionalVariable : Type.
23 * Inductive Formula : Prop :=
24 * | formula_var : PropositionalVariable -> Formula (* every propositional variable is a formula *)
25 * | formula_and : Formula -> Formula -> Formula (* the conjunction of any two formulae is a formula *)
26 * | formula_or : Formula -> Formula -> Formula (* the disjunction of any two formulae is a formula *)
28 * And couple this with the theory of conjunction and disjunction:
29 * φ\/ψ is true if either φ is true or ψ is true, and φ/\ψ is true
30 * if both φ and ψ are true.
32 * 1) Structurally implicit proofs
34 * This is what you would call the "usual" representation –- it's
35 * what most people learn when they first start programming in Coq:
37 * Inductive IsTrue : Formula -> Prop :=
38 * | IsTrue_or1 : forall f1 f2, IsTrue f1 -> IsTrue (formula_or f1 f2)
39 * | IsTrue_or2 : forall f1 f2, IsTrue f2 -> IsTrue (formula_or f1 f2)
40 * | IsTrue_and : forall f1 f2, IsTrue f2 -> IsTrue f2 -> IsTrue (formula_and f1 f2)
42 * Here each judgment (such as "φ is true") is represented by a Coq
45 * 1. A proof of a judgment is any inhabitant of that Coq type.
47 * 2. A proof of a judgment "J2" from hypothesis judgment "J1"
48 * is any Coq function from the Coq type for J1 to the Coq
49 * type for J2; Composition of (hypothetical) proofs is
50 * represented by composition of Coq functions.
52 * 3. A pair of judgments is represented by their product (Coq
53 * type [prod]) in Coq; a pair of proofs is represented by
54 * their pair (Coq function [pair]) in Coq.
56 * 4. Duplication of hypotheses is represented by the Coq
57 * function (fun x => (x,x)). Dereliction of hypotheses is
58 * represented by the coq function (fun (x,y) => x) or (fun
59 * (x,y) => y). Exchange of the order of hypotheses is
60 * represented by the Coq function (fun (x,y) => (y,x)).
62 * The above can be done using lists instead of tuples.
64 * The advantage of this approach is that it requires a minimum
65 * amount of syntax, and takes maximum advantage of Coq's
66 * automation facilities.
68 * The disadvantage is that one cannot reason about proof-theoretic
69 * properties *generically* across different signatures and
70 * theories. Each signature has its own type of judgments, and
71 * each theory has its own type of proofs. In the present
72 * development we will want to prove –– in this generic manner --
73 * that various classes of natural deduction calculi form various
74 * kinds of categories. So we will need this ability to reason
75 * about proofs independently of the type used to represent
76 * judgments and (more importantly) of the set of basic inference
79 * 2) Structurally explicit proofs
81 * Structurally explicit proofs are formalized in this file
82 * (NaturalDeduction.v) and are designed specifically in order to
83 * circumvent the problem in the previous paragraph.
88 * REGARDING LISTS versus TREES:
90 * You'll notice that this formalization uses (Tree (option A)) in a
91 * lot of places where you might find (list A) more natural. If this
92 * bothers you, see the end of the file for the technical reasons why.
93 * In short, it lets us avoid having to mess about with JMEq or EqDep,
94 * which are not as well-supported by the Coq core as the theory of
98 Section Natural_Deduction.
100 (* any Coq Type may be used as the set of judgments *)
101 Context {Judgment : Type}.
103 (* any Coq Type –- indexed by the hypothesis and conclusion judgments -- may be used as the set of basic inference rules *)
104 Context {Rule : forall (hypotheses:Tree ??Judgment)(conclusion:Tree ??Judgment), Type}.
107 * This type represents a valid Natural Deduction proof from a list
108 * (tree) of hypotheses; the notation H/⋯⋯/C is meant to look like
109 * a proof tree with the middle missing if you tilt your head to
110 * the left (yeah, I know it's a stretch). Note also that this
111 * type is capable of representing proofs with multiple
112 * conclusions, whereas a Rule may have only one conclusion.
115 forall hypotheses:Tree ??Judgment,
116 forall conclusions:Tree ??Judgment,
119 (* natural deduction: you may infer anything from itself -- "identity proof" *)
120 | nd_id0 : [ ] /⋯⋯/ [ ]
121 | nd_id1 : forall h, [ h ] /⋯⋯/ [ h ]
123 (* natural deduction: you may discard conclusions *)
124 | nd_weak : forall h, [ h ] /⋯⋯/ [ ]
126 (* natural deduction: you may duplicate conclusions *)
127 | nd_copy : forall h, h /⋯⋯/ (h,,h)
129 (* natural deduction: you may write two proof trees side by side on a piece of paper -- "proof product" *)
130 | nd_prod : forall {h1 h2 c1 c2}
133 ( h1 ,, h2 /⋯⋯/ c1 ,, c2)
135 (* natural deduction: given a proof of every hypothesis, you may discharge them -- "proof composition" *)
142 (* structural rules on lists of judgments *)
143 | nd_cancell : forall {a}, [] ,, a /⋯⋯/ a
144 | nd_cancelr : forall {a}, a ,, [] /⋯⋯/ a
145 | nd_llecnac : forall {a}, a /⋯⋯/ [] ,, a
146 | nd_rlecnac : forall {a}, a /⋯⋯/ a ,, []
147 | nd_assoc : forall {a b c}, (a,,b),,c /⋯⋯/ a,,(b,,c)
148 | nd_cossa : forall {a b c}, a,,(b,,c) /⋯⋯/ (a,,b),,c
150 (* any Rule by itself counts as a proof *)
151 | nd_rule : forall {h c} (r:Rule h c), h /⋯⋯/ c
153 where "H /⋯⋯/ C" := (ND H C).
155 Notation "H /⋯⋯/ C" := (ND H C) : pf_scope.
156 Notation "a ;; b" := (nd_comp a b) : nd_scope.
157 Notation "a ** b" := (nd_prod a b) : nd_scope.
161 (* a proof is "structural" iff it does not contain any invocations of nd_rule *)
162 Inductive Structural : forall {h c}, h /⋯⋯/ c -> Prop :=
163 | nd_structural_id0 : Structural nd_id0
164 | nd_structural_id1 : forall h, Structural (nd_id1 h)
165 | nd_structural_weak : forall h, Structural (nd_weak h)
166 | nd_structural_copy : forall h, Structural (nd_copy h)
167 | nd_structural_prod : forall `(pf1:h1/⋯⋯/c1)`(pf2:h2/⋯⋯/c2), Structural pf1 -> Structural pf2 -> Structural (pf1**pf2)
168 | nd_structural_comp : forall `(pf1:h1/⋯⋯/x) `(pf2: x/⋯⋯/c2), Structural pf1 -> Structural pf2 -> Structural (pf1;;pf2)
169 | nd_structural_cancell : forall {a}, Structural (@nd_cancell a)
170 | nd_structural_cancelr : forall {a}, Structural (@nd_cancelr a)
171 | nd_structural_llecnac : forall {a}, Structural (@nd_llecnac a)
172 | nd_structural_rlecnac : forall {a}, Structural (@nd_rlecnac a)
173 | nd_structural_assoc : forall {a b c}, Structural (@nd_assoc a b c)
174 | nd_structural_cossa : forall {a b c}, Structural (@nd_cossa a b c)
177 (* multi-judgment generalization of nd_id0 and nd_id1; making nd_id0/nd_id1 primitive and deriving all other *)
178 Fixpoint nd_id (sl:Tree ??Judgment) : sl /⋯⋯/ sl :=
180 | T_Leaf None => nd_id0
181 | T_Leaf (Some x) => nd_id1 x
182 | T_Branch a b => nd_prod (nd_id a) (nd_id b)
185 Hint Constructors Structural.
186 Lemma nd_id_structural : forall sl, Structural (nd_id sl).
188 induction sl; simpl; auto.
192 (* An equivalence relation on proofs which is sensitive only to the logical content of the proof -- insensitive to
193 * structural variations *)
195 { ndr_eqv : forall {h c }, h /⋯⋯/ c -> h /⋯⋯/ c -> Prop where "pf1 === pf2" := (@ndr_eqv _ _ pf1 pf2)
196 ; ndr_eqv_equivalence : forall h c, Equivalence (@ndr_eqv h c)
198 (* the relation must respect composition, be associative wrt composition, and be left and right neutral wrt the identity proof *)
199 ; ndr_comp_respects : forall {a b c}(f f':a/⋯⋯/b)(g g':b/⋯⋯/c), f === f' -> g === g' -> f;;g === f';;g'
200 ; ndr_comp_associativity : forall `(f:a/⋯⋯/b)`(g:b/⋯⋯/c)`(h:c/⋯⋯/d), (f;;g);;h === f;;(g;;h)
201 ; ndr_comp_left_identity : forall `(f:a/⋯⋯/c), nd_id _ ;; f === f
202 ; ndr_comp_right_identity : forall `(f:a/⋯⋯/c), f ;; nd_id _ === f
204 (* the relation must respect products, be associative wrt products, and be left and right neutral wrt the identity proof *)
205 ; ndr_prod_respects : forall {a b c d}(f f':a/⋯⋯/b)(g g':c/⋯⋯/d), f===f' -> g===g' -> f**g === f'**g'
206 ; ndr_prod_associativity : forall `(f:a/⋯⋯/a')`(g:b/⋯⋯/b')`(h:c/⋯⋯/c'), (f**g)**h === nd_assoc ;; f**(g**h) ;; nd_cossa
207 ; ndr_prod_left_identity : forall `(f:a/⋯⋯/b), (nd_id0 ** f ) === nd_cancell ;; f ;; nd_llecnac
208 ; ndr_prod_right_identity : forall `(f:a/⋯⋯/b), (f ** nd_id0) === nd_cancelr ;; f ;; nd_rlecnac
210 (* products and composition must distribute over each other *)
211 ; 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')
213 (* any two _structural_ proofs with the same hypotheses/conclusions must be considered equal *)
214 ; ndr_structural_indistinguishable : forall `(f:a/⋯⋯/b)(g:a/⋯⋯/b), Structural f -> Structural g -> f===g
218 * Single-conclusion proofs; this is an alternate representation
219 * where each inference has only a single conclusion. These have
220 * worse compositionality properties than ND's, but are easier to
221 * emit as LaTeX code.
223 Inductive SCND : Tree ??Judgment -> Tree ??Judgment -> Type :=
224 | scnd_comp : forall ht ct c , SCND ht ct -> Rule ct [c] -> SCND ht [c]
225 | scnd_weak : forall c , SCND c []
226 | scnd_leaf : forall ht c , SCND ht [c] -> SCND ht [c]
227 | scnd_branch : forall ht c1 c2, SCND ht c1 -> SCND ht c2 -> SCND ht (c1,,c2)
229 Hint Constructors SCND.
231 (* Any ND whose primitive Rules have at most one conclusion (note that nd_prod is allowed!) can be turned into an SCND. *)
232 Definition mkSCND (all_rules_one_conclusion : forall h c1 c2, Rule h (c1,,c2) -> False)
233 : forall h x c, ND x c -> SCND h x -> SCND h c.
235 induction nd; intro k.
239 eapply scnd_branch; apply k.
241 apply (scnd_branch _ _ _ (IHnd1 X) (IHnd2 X0)).
245 inversion k; subst; auto.
246 inversion k; subst; auto.
247 apply scnd_branch; auto.
248 apply scnd_branch; auto.
249 inversion k; subst; inversion X; subst; auto.
250 inversion k; subst; inversion X0; subst; auto.
253 apply scnd_leaf. eapply scnd_comp. apply k. apply r.
255 set (all_rules_one_conclusion _ _ _ r) as bogus.
259 (* a "ClosedND" is a proof with no open hypotheses and no multi-conclusion rules *)
260 Inductive ClosedND : Tree ??Judgment -> Type :=
261 | cnd_weak : ClosedND []
262 | cnd_rule : forall h c , ClosedND h -> Rule h c -> ClosedND c
263 | cnd_branch : forall c1 c2, ClosedND c1 -> ClosedND c2 -> ClosedND (c1,,c2)
266 (* we can turn an SCND without hypotheses into a ClosedND *)
267 Definition closedFromSCND h c (pn2:SCND h c)(cnd:ClosedND h) : ClosedND c.
268 refine ((fix closedFromPnodes h c (pn2:SCND h c)(cnd:ClosedND h) {struct pn2} :=
269 (match pn2 in SCND H C return H=h -> C=c -> _ with
270 | scnd_weak c => let case_weak := tt in _
271 | scnd_leaf ht z pn' => let case_leaf := tt in let qq := closedFromPnodes _ _ pn' in _
272 | scnd_comp ht ct c pn' rule => let case_comp := tt in let qq := closedFromPnodes _ _ pn' in _
273 | scnd_branch ht c1 c2 pn' pn'' => let case_branch := tt in
274 let q1 := closedFromPnodes _ _ pn' in
275 let q2 := closedFromPnodes _ _ pn'' in _
277 end (refl_equal _) (refl_equal _))) h c pn2 cnd).
298 destruct case_branch.
301 apply q1. subst. apply cnd0.
302 apply q2. subst. apply cnd0.
306 Fixpoint closedNDtoNormalND {c}(cnd:ClosedND c) : ND [] c :=
307 match cnd in ClosedND C return ND [] C with
309 | cnd_rule h c cndh rhc => closedNDtoNormalND cndh ;; nd_rule rhc
310 | cnd_branch c1 c2 cnd1 cnd2 => nd_llecnac ;; nd_prod (closedNDtoNormalND cnd1) (closedNDtoNormalND cnd2)
313 Close Scope nd_scope.
316 End Natural_Deduction.
318 Implicit Arguments ND [ Judgment ].
319 Hint Constructors Structural.
320 Hint Extern 1 => apply nd_id_structural.
321 Hint Extern 1 => apply ndr_structural_indistinguishable.
323 (* This first notation gets its own scope because it can be confusing when we're working with multiple different kinds
324 * of proofs. When only one kind of proof is in use, it's quite helpful though. *)
325 Notation "H /⋯⋯/ C" := (@ND _ _ H C) : pf_scope.
326 Notation "a ;; b" := (nd_comp a b) : nd_scope.
327 Notation "a ** b" := (nd_prod a b) : nd_scope.
328 Notation "[# a #]" := (nd_rule a) : nd_scope.
329 Notation "a === b" := (@ndr_eqv _ _ _ _ _ a b) : nd_scope.
331 (* enable setoid rewriting *)
335 Add Parametric Relation {jt rt ndr h c} : (h/⋯⋯/c) (@ndr_eqv jt rt ndr h c)
336 reflexivity proved by (@Equivalence_Reflexive _ _ (ndr_eqv_equivalence h c))
337 symmetry proved by (@Equivalence_Symmetric _ _ (ndr_eqv_equivalence h c))
338 transitivity proved by (@Equivalence_Transitive _ _ (ndr_eqv_equivalence h c))
339 as parametric_relation_ndr_eqv.
340 Add Parametric Morphism {jt rt ndr h x c} : (@nd_comp jt rt h x c)
341 with signature ((ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)))
342 as parametric_morphism_nd_comp.
343 intros; apply ndr_comp_respects; auto.
345 Add Parametric Morphism {jt rt ndr a b c d} : (@nd_prod jt rt a b c d)
346 with signature ((ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)))
347 as parametric_morphism_nd_prod.
348 intros; apply ndr_prod_respects; auto.
351 (* a generalization of the procedure used to build (nd_id n) from nd_id0 and nd_id1 *)
352 Definition nd_replicate
358 (forall (o:Ob), @ND Judgment Rule [h o] [c o]) ->
359 @ND Judgment Rule (mapOptionTree h j) (mapOptionTree c j).
368 (* "map" over natural deduction proofs, where the result proof has the same judgments (but different rules) *)
371 {Judgment}{Rule0}{Rule1}
372 (r:forall h c, Rule0 h c -> @ND Judgment Rule1 h c)
374 (pf:@ND Judgment Rule0 h c)
376 @ND Judgment Rule1 h c.
377 intros Judgment Rule0 Rule1 r.
379 refine ((fix nd_map h c pf {struct pf} :=
383 @ND Judgment Rule1 H C
385 | nd_id0 => let case_nd_id0 := tt in _
386 | nd_id1 h => let case_nd_id1 := tt in _
387 | nd_weak h => let case_nd_weak := tt in _
388 | nd_copy h => let case_nd_copy := tt in _
389 | nd_prod _ _ _ _ lpf rpf => let case_nd_prod := tt in _
390 | nd_comp _ _ _ top bot => let case_nd_comp := tt in _
391 | nd_rule _ _ rule => let case_nd_rule := tt in _
392 | nd_cancell _ => let case_nd_cancell := tt in _
393 | nd_cancelr _ => let case_nd_cancelr := tt in _
394 | nd_llecnac _ => let case_nd_llecnac := tt in _
395 | nd_rlecnac _ => let case_nd_rlecnac := tt in _
396 | nd_assoc _ _ _ => let case_nd_assoc := tt in _
397 | nd_cossa _ _ _ => let case_nd_cossa := tt in _
398 end))) ); simpl in *.
400 destruct case_nd_id0. apply nd_id0.
401 destruct case_nd_id1. apply nd_id1.
402 destruct case_nd_weak. apply nd_weak.
403 destruct case_nd_copy. apply nd_copy.
404 destruct case_nd_prod. apply (nd_prod (nd_map _ _ lpf) (nd_map _ _ rpf)).
405 destruct case_nd_comp. apply (nd_comp (nd_map _ _ top) (nd_map _ _ bot)).
406 destruct case_nd_cancell. apply nd_cancell.
407 destruct case_nd_cancelr. apply nd_cancelr.
408 destruct case_nd_llecnac. apply nd_llecnac.
409 destruct case_nd_rlecnac. apply nd_rlecnac.
410 destruct case_nd_assoc. apply nd_assoc.
411 destruct case_nd_cossa. apply nd_cossa.
415 (* "map" over natural deduction proofs, where the result proof has different judgments *)
418 {Judgment0}{Rule0}{Judgment1}{Rule1}
419 (f:Judgment0->Judgment1)
420 (r:forall h c, Rule0 h c -> @ND Judgment1 Rule1 (mapOptionTree f h) (mapOptionTree f c))
422 (pf:@ND Judgment0 Rule0 h c)
424 @ND Judgment1 Rule1 (mapOptionTree f h) (mapOptionTree f c).
425 intros Judgment0 Rule0 Judgment1 Rule1 f r.
427 refine ((fix nd_map' h c pf {struct pf} :=
431 @ND Judgment1 Rule1 (mapOptionTree f H) (mapOptionTree f C)
433 | nd_id0 => let case_nd_id0 := tt in _
434 | nd_id1 h => let case_nd_id1 := tt in _
435 | nd_weak h => let case_nd_weak := tt in _
436 | nd_copy h => let case_nd_copy := tt in _
437 | nd_prod _ _ _ _ lpf rpf => let case_nd_prod := tt in _
438 | nd_comp _ _ _ top bot => let case_nd_comp := tt in _
439 | nd_rule _ _ rule => let case_nd_rule := tt in _
440 | nd_cancell _ => let case_nd_cancell := tt in _
441 | nd_cancelr _ => let case_nd_cancelr := tt in _
442 | nd_llecnac _ => let case_nd_llecnac := tt in _
443 | nd_rlecnac _ => let case_nd_rlecnac := tt in _
444 | nd_assoc _ _ _ => let case_nd_assoc := tt in _
445 | nd_cossa _ _ _ => let case_nd_cossa := tt in _
446 end))) ); simpl in *.
448 destruct case_nd_id0. apply nd_id0.
449 destruct case_nd_id1. apply nd_id1.
450 destruct case_nd_weak. apply nd_weak.
451 destruct case_nd_copy. apply nd_copy.
452 destruct case_nd_prod. apply (nd_prod (nd_map' _ _ lpf) (nd_map' _ _ rpf)).
453 destruct case_nd_comp. apply (nd_comp (nd_map' _ _ top) (nd_map' _ _ bot)).
454 destruct case_nd_cancell. apply nd_cancell.
455 destruct case_nd_cancelr. apply nd_cancelr.
456 destruct case_nd_llecnac. apply nd_llecnac.
457 destruct case_nd_rlecnac. apply nd_rlecnac.
458 destruct case_nd_assoc. apply nd_assoc.
459 destruct case_nd_cossa. apply nd_cossa.
463 (* witnesses the fact that every Rule in a particular proof satisfies the given predicate *)
464 Inductive nd_property {Judgment}{Rule}(P:forall h c, @Rule h c -> Prop) : forall {h}{c}, @ND Judgment Rule h c -> Prop :=
465 | nd_property_structural : forall h c pf, Structural pf -> @nd_property _ _ P h c pf
466 | nd_property_prod : forall h0 c0 pf0 h1 c1 pf1,
467 @nd_property _ _ P h0 c0 pf0 -> @nd_property _ _ P h1 c1 pf1 -> @nd_property _ _ P _ _ (nd_prod pf0 pf1)
468 | nd_property_comp : forall h0 c0 pf0 c1 pf1,
469 @nd_property _ _ P h0 c0 pf0 -> @nd_property _ _ P c0 c1 pf1 -> @nd_property _ _ P _ _ (nd_comp pf0 pf1)
470 | nd_property_rule : forall h c r, P h c r -> @nd_property _ _ P h c (nd_rule r).
471 Hint Constructors nd_property.
473 Close Scope pf_scope.
474 Close Scope nd_scope.