add support for hetmet_unflatten
[coq-hetmet.git] / src / NaturalDeduction.v
1 (*********************************************************************************************************************************)
2 (* NaturalDeduction:                                                                                                             *)
3 (*                                                                                                                               *)
4 (*   Structurally explicit natural deduction proofs.                                                                             *)
5 (*                                                                                                                               *)
6 (*********************************************************************************************************************************)
7
8 Generalizable All Variables.
9 Require Import Preamble.
10 Require Import General.
11 Require Import Coq.Strings.Ascii.
12 Require Import Coq.Strings.String.
13
14 (*
15  * Unlike most formalizations, this library offers two different ways
16  * to represent a natural deduction proof.  To demonstrate this,
17  * consider the signature of the propositional calculus:
18  *
19  *   Variable  PropositionalVariable : Type.
20  *
21  *   Inductive Formula : Prop :=
22  *   | formula_var : PropositionalVariable -> Formula   (* every propositional variable is a formula *)
23  *   | formula_and :   Formula ->  Formula -> Formula   (* the conjunction of any two formulae is a formula *)
24  *   | formula_or  :   Formula ->  Formula -> Formula   (* the disjunction of any two formulae is a formula *)
25  *
26  * And couple this with the theory of conjunction and disjunction:
27  * φ\/ψ is true if either φ is true or ψ is true, and φ/\ψ is true
28  * if both φ and ψ are true.
29  *
30  * 1) Structurally implicit proofs
31  *
32  *    This is what you would call the "usual" representation –- it's
33  *    what most people learn when they first start programming in Coq:
34  *
35  *    Inductive IsTrue : Formula -> Prop :=
36  *    | IsTrue_or1 : forall f1 f2, IsTrue f1 ->              IsTrue (formula_or  f1 f2) 
37  *    | IsTrue_or2 : forall f1 f2,              IsTrue f2 -> IsTrue (formula_or  f1 f2) 
38  *    | IsTrue_and : forall f1 f2, IsTrue f2 -> IsTrue f2 -> IsTrue (formula_and f1 f2) 
39  *
40  *    Here each judgment (such as "φ is true") is represented by a Coq
41  *    type; furthermore:
42  *
43  *       1. A proof of a judgment is any inhabitant of that Coq type.
44  *
45  *       2. A proof of a judgment "J2" from hypothesis judgment "J1"
46  *          is any Coq function from the Coq type for J1 to the Coq
47  *          type for J2; Composition of (hypothetical) proofs is
48  *          represented by composition of Coq functions.
49  *
50  *       3. A pair of judgments is represented by their product (Coq
51  *          type [prod]) in Coq; a pair of proofs is represented by
52  *          their pair (Coq function [pair]) in Coq.
53  *
54  *       4. Duplication of hypotheses is represented by the Coq
55  *          function (fun x => (x,x)).  Dereliction of hypotheses is
56  *          represented by the coq function (fun (x,y) => x) or (fun
57  *          (x,y) => y).  Exchange of the order of hypotheses is
58  *          represented by the Coq function (fun (x,y) => (y,x)).
59  *
60  *    The above can be done using lists instead of tuples.
61  *
62  *    The advantage of this approach is that it requires a minimum
63  *    amount of syntax, and takes maximum advantage of Coq's
64  *    automation facilities.
65  *
66  *    The disadvantage is that one cannot reason about proof-theoretic
67  *    properties *generically* across different signatures and
68  *    theories.  Each signature has its own type of judgments, and
69  *    each theory has its own type of proofs.  In the present
70  *    development we will want to prove –– in this generic manner --
71  *    that various classes of natural deduction calculi form various
72  *    kinds of categories.  So we will need this ability to reason
73  *    about proofs independently of the type used to represent
74  *    judgments and (more importantly) of the set of basic inference
75  *    rules.
76  *
77  * 2) Structurally explicit proofs
78  *
79  *    Structurally explicit proofs are formalized in this file
80  *    (NaturalDeduction.v) and are designed specifically in order to
81  *    circumvent the problem in the previous paragraph.
82  *
83  *    These proofs are actually structurally explicit on (potentially)
84  *    two different levels.  The beginning of this file formalizes
85  *    natural deduction proofs with explicit structural operations for
86  *    manipulating lists of judgments – for example, the open
87  *    hypotheses of an incomplete proof.  The class
88  *    TreeStructuralRules further down in the file instantiates ND
89  *    such that Judgments is actually a pair of trees of propositions,
90  *    and there will be a whole *other* set of rules for manipulating
91  *    the structure of a tree of propositions *within* a single
92  *    judgment.
93  *
94  *    The flattening functor ends up mapping the first kind of
95  *    structural operation (moving around judgments) onto the second
96  *    kind (moving around propositions/types).  That's why everything
97  *    is so laboriously explicit - there's important information in
98  *    those structural operations.
99  *)
100
101 (*
102  * REGARDING LISTS versus TREES:
103  *
104  * You'll notice that this formalization uses (Tree (option A)) in a
105  * lot of places where you might find (list A) more natural.  If this
106  * bothers you, see the end of the file for the technical reasons why.
107  * In short, it lets us avoid having to mess about with JMEq or EqDep,
108  * which are not as well-supported by the Coq core as the theory of
109  * CiC proper.
110  *)
111
112 Section Natural_Deduction.
113
114   (* any Coq Type may be used as the set of judgments *)
115   Context {Judgment : Type}.
116
117   (* any Coq Type –- indexed by the hypothesis and conclusion judgments -- may be used as the set of basic inference rules *)
118   Context {Rule     : forall (hypotheses:Tree ??Judgment)(conclusion:Tree ??Judgment), Type}.
119
120   (*
121    *  This type represents a valid Natural Deduction proof from a list
122    *  (tree) of hypotheses; the notation H/⋯⋯/C is meant to look like
123    *  a proof tree with the middle missing if you tilt your head to
124    *  the left (yeah, I know it's a stretch).  Note also that this
125    *  type is capable of representing proofs with multiple
126    *  conclusions, whereas a Rule may have only one conclusion.
127    *) 
128   Inductive ND :
129     forall hypotheses:Tree ??Judgment,
130       forall conclusions:Tree ??Judgment,
131         Type :=
132
133     (* natural deduction: you may infer nothing from nothing *)
134     | nd_id0    :             [   ] /⋯⋯/ [   ]
135
136     (* natural deduction: you may infer anything from itself -- "identity proof" *)
137     | nd_id1    : forall  h,  [ h ] /⋯⋯/ [ h ]
138   
139     (* natural deduction: you may discard conclusions *)
140     | nd_weak1  : forall  h,  [ h ] /⋯⋯/ [   ]
141   
142     (* natural deduction: you may duplicate conclusions *)
143     | nd_copy   : forall  h,    h   /⋯⋯/ (h,,h)
144   
145     (* natural deduction: you may write two proof trees side by side on a piece of paper -- "proof product" *)
146     | nd_prod : forall {h1 h2 c1 c2}
147        (pf1: h1       /⋯⋯/ c1      )
148        (pf2:       h2 /⋯⋯/       c2),
149        (     h1 ,, h2 /⋯⋯/ c1 ,, c2)
150   
151     (* natural deduction: given a proof of every hypothesis, you may discharge them -- "proof composition" *)
152     | nd_comp :
153       forall {h x c}
154       `(pf1: h /⋯⋯/ x)
155       `(pf2: x /⋯⋯/ c),
156        (     h /⋯⋯/ c)
157   
158     (* Structural rules on lists of judgments - note that this is completely separate from the structural
159      * rules for *contexts* within a sequent.  The rules below manipulate lists of *judgments* rather than
160      * lists of *propositions*. *)
161     | nd_cancell : forall {a},       [] ,, a /⋯⋯/ a
162     | nd_cancelr : forall {a},       a ,, [] /⋯⋯/ a
163     | nd_llecnac : forall {a},             a /⋯⋯/ [] ,, a
164     | nd_rlecnac : forall {a},             a /⋯⋯/ a ,, []
165     | nd_assoc   : forall {a b c}, (a,,b),,c /⋯⋯/ a,,(b,,c)
166     | nd_cossa   : forall {a b c}, a,,(b,,c) /⋯⋯/ (a,,b),,c
167
168     (* any Rule by itself counts as a proof *)
169     | nd_rule    : forall {h c} (r:Rule h c), h /⋯⋯/ c
170   
171     where  "H /⋯⋯/ C" := (ND H C).
172
173     Notation "H /⋯⋯/ C" := (ND H C) : pf_scope.
174     Notation "a ;; b"   := (nd_comp a b) : nd_scope.
175     Notation "a ** b"   := (nd_prod a b) : nd_scope.
176     Open Scope nd_scope.
177     Open Scope pf_scope.
178
179   (* a predicate on proofs *)
180   Definition NDPredicate := forall h c, h /⋯⋯/ c -> Prop.
181
182   (* the structural inference rules are those which do not change, add, remove, or re-order the judgments *)
183   Inductive Structural : forall {h c}, h /⋯⋯/ c -> Prop :=
184   | nd_structural_id0     :                                                                            Structural nd_id0
185   | nd_structural_id1     : forall h,                                                                  Structural (nd_id1 h)
186   | nd_structural_cancell : forall {a},                                                                Structural (@nd_cancell a)
187   | nd_structural_cancelr : forall {a},                                                                Structural (@nd_cancelr a)
188   | nd_structural_llecnac : forall {a},                                                                Structural (@nd_llecnac a)
189   | nd_structural_rlecnac : forall {a},                                                                Structural (@nd_rlecnac a)
190   | nd_structural_assoc   : forall {a b c},                                                            Structural (@nd_assoc a b c)
191   | nd_structural_cossa   : forall {a b c},                                                            Structural (@nd_cossa a b c)
192   .
193
194   (* the closure of an NDPredicate under nd_comp and nd_prod *)
195   Inductive NDPredicateClosure (P:NDPredicate) : forall {h c}, h /⋯⋯/ c -> Prop :=
196   | ndpc_p       : forall h c f, P h c f                                                   -> NDPredicateClosure P f
197   | ndpc_prod    : forall `(pf1:h1/⋯⋯/c1)`(pf2:h2/⋯⋯/c2),
198     NDPredicateClosure P pf1 -> NDPredicateClosure P pf2 -> NDPredicateClosure P (pf1**pf2)
199   | ndpc_comp    : forall `(pf1:h1/⋯⋯/x) `(pf2: x/⋯⋯/c2),
200     NDPredicateClosure P pf1 -> NDPredicateClosure P pf2 -> NDPredicateClosure P (pf1;;pf2).
201
202   (* proofs built up from structural rules via comp and prod *)
203   Definition StructuralND {h}{c} f := @NDPredicateClosure (@Structural) h c f.
204
205   (* The Predicate (BuiltFrom f P h) asserts that "h" was built from a single occurrence of "f" and proofs which satisfy P *)
206   Inductive BuiltFrom {h'}{c'}(f:h'/⋯⋯/c')(P:NDPredicate) : forall {h c}, h/⋯⋯/c -> Prop :=
207   | builtfrom_refl  : BuiltFrom f P f
208   | builtfrom_P     : forall h c g, @P h c g -> BuiltFrom f P g
209   | builtfrom_prod1 : forall h1 c1 f1 h2 c2 f2, P h1 c1 f1 -> @BuiltFrom _ _ f P h2 c2 f2 -> BuiltFrom f P (f1 ** f2)
210   | builtfrom_prod2 : forall h1 c1 f1 h2 c2 f2, P h1 c1 f1 -> @BuiltFrom _ _ f P h2 c2 f2 -> BuiltFrom f P (f2 ** f1)
211   | builtfrom_comp1 : forall h x c       f1 f2, P h  x  f1 -> @BuiltFrom _ _ f P x  c  f2 -> BuiltFrom f P (f1 ;; f2)
212   | builtfrom_comp2 : forall h x c       f1 f2, P x  c  f1 -> @BuiltFrom _ _ f P h  x  f2 -> BuiltFrom f P (f2 ;; f1).
213
214   (* multi-judgment generalization of nd_id0 and nd_id1; making nd_id0/nd_id1 primitive and deriving all other *)
215   Fixpoint nd_id (sl:Tree ??Judgment) : sl /⋯⋯/ sl :=
216     match sl with
217       | T_Leaf None      => nd_id0
218       | T_Leaf (Some x)  => nd_id1 x
219       | T_Branch a b     => nd_prod (nd_id a) (nd_id b)
220     end.
221
222   Fixpoint nd_weak (sl:Tree ??Judgment) : sl /⋯⋯/ [] :=
223     match sl as SL return SL /⋯⋯/ [] with
224       | T_Leaf None      => nd_id0
225       | T_Leaf (Some x)  => nd_weak1 x
226       | T_Branch a b     => nd_prod (nd_weak a) (nd_weak b) ;; nd_cancelr
227     end.
228
229   Hint Constructors Structural.
230   Hint Constructors BuiltFrom.
231   Hint Constructors NDPredicateClosure.
232   Hint Unfold StructuralND.
233
234   Lemma nd_id_structural : forall sl, StructuralND (nd_id sl).
235     intros.
236     induction sl; simpl; auto.
237     destruct a; auto.
238     Defined.
239
240   (* An equivalence relation on proofs which is sensitive only to the logical content of the proof -- insensitive to
241    * structural variations  *)
242   Class ND_Relation :=
243   { ndr_eqv                  : forall {h c  }, h /⋯⋯/ c -> h /⋯⋯/ c -> Prop where "pf1 === pf2" := (@ndr_eqv _ _  pf1 pf2)
244   ; ndr_eqv_equivalence      : forall h c, Equivalence (@ndr_eqv h c)
245
246   (* the relation must respect composition, be associative wrt composition, and be left and right neutral wrt the identity proof *)
247   ; ndr_comp_respects        : forall {a b c}(f f':a/⋯⋯/b)(g g':b/⋯⋯/c),      f === f' -> g === g' -> f;;g === f';;g'
248   ; ndr_comp_associativity   : forall `(f:a/⋯⋯/b)`(g:b/⋯⋯/c)`(h:c/⋯⋯/d),                         (f;;g);;h === f;;(g;;h)
249
250   (* the relation must respect products, be associative wrt products, and be left and right neutral wrt the identity proof *)
251   ; ndr_prod_respects        : forall {a b c d}(f f':a/⋯⋯/b)(g g':c/⋯⋯/d),     f===f' -> g===g' ->    f**g === f'**g'
252   ; ndr_prod_associativity   : forall `(f:a/⋯⋯/a')`(g:b/⋯⋯/b')`(h:c/⋯⋯/c'),       (f**g)**h === nd_assoc ;; f**(g**h) ;; nd_cossa
253
254   (* products and composition must distribute over each other *)
255   ; 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')
256
257   (* Given a proof f, any two proofs built from it using only structural rules are indistinguishable.  Keep in mind that
258    * nd_weak and nd_copy aren't considered structural, so the hypotheses and conclusions of such proofs will be an identical
259    * list, differing only in the "parenthesization" and addition or removal of empty leaves. *)
260   ; ndr_builtfrom_structural : forall `(f:a/⋯⋯/b){a' b'}(g1 g2:a'/⋯⋯/b'),
261     BuiltFrom f (@StructuralND) g1 ->
262     BuiltFrom f (@StructuralND) g2 ->
263     g1 === g2
264
265   (* proofs of nothing are not distinguished from each other *)
266   ; ndr_void_proofs_irrelevant : forall `(f:a/⋯⋯/[])(g:a/⋯⋯/[]), f === g
267
268   (* products and duplication must distribute over each other *)
269   ; ndr_prod_preserves_copy  : forall `(f:a/⋯⋯/b),                                        nd_copy a;; f**f === f ;; nd_copy b
270
271   (* duplicating a hypothesis and discarding it is irrelevant *)
272   ; ndr_copy_then_weak_left   : forall a,                            nd_copy a;; (nd_weak _ ** nd_id _) ;; nd_cancell === nd_id _
273   ; ndr_copy_then_weak_right  : forall a,                            nd_copy a;; (nd_id _ ** nd_weak _) ;; nd_cancelr === nd_id _
274   }.
275
276   (* 
277    * Natural Deduction proofs which are Structurally Implicit on the
278    * level of judgments.  These proofs have poor compositionality
279    * properties (vertically, they look more like lists than trees) but
280    * are easier to do induction over.
281    *)
282   Inductive SIND : Tree ??Judgment -> Tree ??Judgment -> Type :=
283   | scnd_weak   : forall c       , SIND c  []
284   | scnd_comp   : forall ht ct c , SIND ht ct -> Rule ct [c] -> SIND ht [c]
285   | scnd_branch : forall ht c1 c2, SIND ht c1 -> SIND ht c2 -> SIND ht (c1,,c2)
286   .
287   Hint Constructors SIND.
288
289   (* Any ND whose primitive Rules have at most one conclusion (note that nd_prod is allowed!) can be turned into an SIND. *)
290   Definition mkSIND (all_rules_one_conclusion : forall h c1 c2, Rule h (c1,,c2) -> False)
291     : forall h x c,  ND x c -> SIND h x -> SIND h c.
292     intros h x c nd.
293     induction nd; intro k.
294       apply k.
295       apply k.
296       apply scnd_weak.
297       eapply scnd_branch; apply k.
298       inversion k; subst.
299         apply (scnd_branch _ _ _ (IHnd1 X) (IHnd2 X0)).
300       apply IHnd2.
301         apply IHnd1.
302         apply k.
303       inversion k; subst; auto.
304       inversion k; subst; auto.
305       apply scnd_branch; auto.
306       apply scnd_branch; auto.
307       inversion k; subst; inversion X; subst; auto.
308       inversion k; subst; inversion X0; subst; auto.
309       destruct c.
310         destruct o.
311         eapply scnd_comp. apply k. apply r.
312         apply scnd_weak.
313         set (all_rules_one_conclusion _ _ _ r) as bogus.
314           inversion bogus.
315           Defined.
316
317   (* Natural Deduction systems whose judgments happen to be pairs of the same type *)
318   Section SequentND.
319     Context {S:Type}.                   (* type of sequent components *)
320     Context {sequent:S->S->Judgment}.   (* pairing operation which forms a sequent from its halves *)
321     Notation "a |= b" := (sequent a b).
322
323     (* a SequentND is a natural deduction whose judgments are sequents, has initial sequents, and has a cut rule *)
324     Class SequentND :=
325     { snd_initial : forall a,                           [ ] /⋯⋯/ [ a |= a ]
326     ; snd_cut     : forall a b c,  [ a |= b ] ,, [ b |= c ] /⋯⋯/ [ a |= c ]
327     }.
328
329     Context (sequentND:SequentND).
330     Context (ndr:ND_Relation).
331
332     (*
333      * A predicate singling out structural rules, initial sequents,
334      * and cut rules.
335      *
336      * Proofs using only structural rules cannot add or remove
337      * judgments - their hypothesis and conclusion judgment-trees will
338      * differ only in "parenthesization" and the presence/absence of
339      * empty leaves.  This means that a proof involving only
340      * structural rules, cut, and initial sequents can ADD new
341      * non-empty judgment-leaves only via snd_initial, and can only
342      * REMOVE non-empty judgment-leaves only via snd_cut.  Since the
343      * initial sequent is a left and right identity for cut, and cut
344      * is associative, any two proofs (with the same hypotheses and
345      * conclusions) using only structural rules, cut, and initial
346      * sequents are logically indistinguishable - their differences
347      * are logically insignificant.
348      *
349      * Note that it is important that nd_weak and nd_copy aren't
350      * considered to be "structural".
351      *)
352     Inductive SequentND_Inert : forall h c, h/⋯⋯/c -> Prop :=
353     | snd_inert_initial   : forall a,                     SequentND_Inert _ _ (snd_initial a)
354     | snd_inert_cut       : forall a b c,                 SequentND_Inert _ _ (snd_cut a b c)
355     | snd_inert_structural: forall a b f, Structural f -> SequentND_Inert a b f
356     .
357
358     (* An ND_Relation for a sequent deduction should not distinguish between two proofs having the same hypotheses and conclusions
359      * if those proofs use only initial sequents, cut, and structural rules (see comment above) *)
360     Class SequentND_Relation :=
361     { sndr_ndr   := ndr
362     ; sndr_inert : forall a b (f g:a/⋯⋯/b),
363       NDPredicateClosure SequentND_Inert f -> 
364       NDPredicateClosure SequentND_Inert g -> 
365       ndr_eqv f g }.
366
367   End SequentND.
368
369   (* Deductions on sequents whose antecedent is a tree of propositions (i.e. a context) *)
370   Section ContextND.
371     Context {P:Type}{sequent:Tree ??P -> Tree ??P -> Judgment}.
372     Context {snd:SequentND(sequent:=sequent)}.
373     Notation "a |= b" := (sequent a b).
374
375     (* Note that these rules mirror nd_{cancell,cancelr,rlecnac,llecnac,assoc,cossa} but are completely separate from them *)
376     Class ContextND :=
377     { cnd_ant_assoc     : forall x a b c, ND [((a,,b),,c) |= x]     [(a,,(b,,c)) |= x   ]
378     ; cnd_ant_cossa     : forall x a b c, ND [(a,,(b,,c)) |= x]     [((a,,b),,c) |= x   ]
379     ; cnd_ant_cancell   : forall x a    , ND [  [],,a     |= x]     [        a   |= x   ]
380     ; cnd_ant_cancelr   : forall x a    , ND [a,,[]       |= x]     [        a   |= x   ]
381     ; cnd_ant_llecnac   : forall x a    , ND [      a     |= x]     [    [],,a   |= x   ]
382     ; cnd_ant_rlecnac   : forall x a    , ND [      a     |= x]     [    a,,[]   |= x   ]
383     ; cnd_expand_left   : forall a b c  , ND [          a |= b]     [ c,,a       |= c,,b]
384     ; cnd_expand_right  : forall a b c  , ND [          a |= b]     [ a,,c       |= b,,c]
385     ; cnd_snd           := snd
386     }.
387
388     Context `(ContextND).
389
390     (*
391      * A predicate singling out initial sequents, cuts, context expansion,
392      * and structural rules.
393      *
394      * Any two proofs (with the same hypotheses and conclusions) whose
395      * non-structural rules do nothing other than expand contexts,
396      * re-arrange contexts, or introduce additional initial-sequent
397      * conclusions are indistinguishable.  One important consequence
398      * is that asking for a small initial sequent and then expanding
399      * it using cnd_expand_{right,left} is no different from simply
400      * asking for the larger initial sequent in the first place.
401      *
402      *)
403     Inductive ContextND_Inert : forall h c, h/⋯⋯/c -> Prop :=
404     | cnd_inert_initial           : forall a,                     ContextND_Inert _ _ (snd_initial a)
405     | cnd_inert_cut               : forall a b c,                 ContextND_Inert _ _ (snd_cut a b c)
406     | cnd_inert_structural        : forall a b f, Structural f -> ContextND_Inert a b f
407     | cnd_inert_cnd_ant_assoc     : forall x a b c, ContextND_Inert _ _ (cnd_ant_assoc   x a b c)
408     | cnd_inert_cnd_ant_cossa     : forall x a b c, ContextND_Inert _ _ (cnd_ant_cossa   x a b c)
409     | cnd_inert_cnd_ant_cancell   : forall x a    , ContextND_Inert _ _ (cnd_ant_cancell x a)
410     | cnd_inert_cnd_ant_cancelr   : forall x a    , ContextND_Inert _ _ (cnd_ant_cancelr x a)
411     | cnd_inert_cnd_ant_llecnac   : forall x a    , ContextND_Inert _ _ (cnd_ant_llecnac x a)
412     | cnd_inert_cnd_ant_rlecnac   : forall x a    , ContextND_Inert _ _ (cnd_ant_rlecnac x a)
413     | cnd_inert_se_expand_left    : forall t g s  , ContextND_Inert _ _ (@cnd_expand_left  _ t g s)
414     | cnd_inert_se_expand_right   : forall t g s  , ContextND_Inert _ _ (@cnd_expand_right _ t g s).
415
416     Class ContextND_Relation {ndr}{sndr:SequentND_Relation _ ndr} :=
417     { cndr_inert : forall {a}{b}(f g:a/⋯⋯/b),
418                            NDPredicateClosure ContextND_Inert f -> 
419                            NDPredicateClosure ContextND_Inert g -> 
420                            ndr_eqv f g
421     ; cndr_sndr  := sndr
422     }.
423
424     (* a proof is Analytic if it does not use cut *)
425     (*
426     Definition Analytic_Rule : NDPredicate :=
427       fun h c f => forall c, not (snd_cut _ _ c = f).
428     Definition AnalyticND := NDPredicateClosure Analytic_Rule.
429
430     (* a proof system has cut elimination if, for every proof, there is an analytic proof with the same conclusion *)
431     Class CutElimination :=
432     { ce_eliminate : forall h c, h/⋯⋯/c -> h/⋯⋯/c
433     ; ce_analytic  : forall h c f, AnalyticND (ce_eliminate h c f)
434     }.
435
436     (* cut elimination is strong if the analytic proof is furthermore equivalent to the original proof *)
437     Class StrongCutElimination :=
438     { sce_ce       <: CutElimination
439     ; ce_strong     : forall h c f, f === ce_eliminate h c f
440     }.
441     *)
442
443   End ContextND.
444
445   Close Scope nd_scope.
446   Open Scope pf_scope.
447
448 End Natural_Deduction.
449
450 Coercion snd_cut   : SequentND >-> Funclass.
451 Coercion cnd_snd   : ContextND >-> SequentND.
452 Coercion sndr_ndr  : SequentND_Relation >-> ND_Relation.
453 Coercion cndr_sndr : ContextND_Relation >-> SequentND_Relation.
454
455 Implicit Arguments ND [ Judgment ].
456
457 (* This first notation gets its own scope because it can be confusing when we're working with multiple different kinds
458  * of proofs.  When only one kind of proof is in use, it's quite helpful though. *)
459 Notation "H /⋯⋯/ C" := (@ND _ _ H C)             : pf_scope.
460 Notation "a ;; b"   := (nd_comp a b)             : nd_scope.
461 Notation "a ** b"   := (nd_prod a b)             : nd_scope.
462 Notation "[# a #]"  := (nd_rule a)               : nd_scope.
463 Notation "a === b"  := (@ndr_eqv _ _ _ _ _ a b)  : nd_scope.
464
465 Hint Constructors Structural.
466 Hint Constructors ND_Relation.
467 Hint Constructors BuiltFrom.
468 Hint Constructors NDPredicateClosure.
469 Hint Constructors ContextND_Inert.
470 Hint Constructors SequentND_Inert.
471 Hint Unfold StructuralND.
472
473 (* enable setoid rewriting *)
474 Open Scope nd_scope.
475 Open Scope pf_scope.
476
477 Hint Extern 2 (StructuralND (nd_id _)) => apply nd_id_structural.
478 Hint Extern 2 (NDPredicateClosure _ ( _ ;; _ ) ) => apply ndpc_comp.
479 Hint Extern 2 (NDPredicateClosure _ ( _ ** _ ) ) => apply ndpc_prod.
480 Hint Extern 2 (NDPredicateClosure (@Structural _ _) (nd_id _)) => apply nd_id_structural.
481 Hint Extern 2 (BuiltFrom _ _ ( _ ;; _ ) ) => apply builtfrom_comp1.
482 Hint Extern 2 (BuiltFrom _ _ ( _ ;; _ ) ) => apply builtfrom_comp2.
483 Hint Extern 2 (BuiltFrom _ _ ( _ ** _ ) ) => apply builtfrom_prod1.
484 Hint Extern 2 (BuiltFrom _ _ ( _ ** _ ) ) => apply builtfrom_prod2.
485
486 (* Hint Constructors has failed me! *)
487 Hint Extern 2 (@Structural _ _ _ _ (@nd_id0 _ _))         => apply nd_structural_id0.
488 Hint Extern 2 (@Structural _ _ _ _ (@nd_id1 _ _ _))       => apply nd_structural_id1.
489 Hint Extern 2 (@Structural _ _ _ _ (@nd_cancell _ _ _))   => apply nd_structural_cancell.
490 Hint Extern 2 (@Structural _ _ _ _ (@nd_cancelr _ _ _))   => apply nd_structural_cancelr.
491 Hint Extern 2 (@Structural _ _ _ _ (@nd_llecnac _ _ _))   => apply nd_structural_llecnac.
492 Hint Extern 2 (@Structural _ _ _ _ (@nd_rlecnac _ _ _))   => apply nd_structural_rlecnac.
493 Hint Extern 2 (@Structural _ _ _ _ (@nd_assoc _ _ _ _ _)) => apply nd_structural_assoc.
494 Hint Extern 2 (@Structural _ _ _ _ (@nd_cossa _ _ _ _ _)) => apply nd_structural_cossa.
495
496 Hint Extern 4 (NDPredicateClosure _ _) => apply ndpc_p.
497
498 Add Parametric Relation {jt rt ndr h c} : (h/⋯⋯/c) (@ndr_eqv jt rt ndr h c)
499   reflexivity proved by  (@Equivalence_Reflexive  _ _ (ndr_eqv_equivalence h c))
500   symmetry proved by     (@Equivalence_Symmetric  _ _ (ndr_eqv_equivalence h c))
501   transitivity proved by (@Equivalence_Transitive _ _ (ndr_eqv_equivalence h c))
502     as parametric_relation_ndr_eqv.
503   Add Parametric Morphism {jt rt ndr h x c} : (@nd_comp jt rt h x c)
504   with signature ((ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)))
505     as parametric_morphism_nd_comp.
506     intros; apply ndr_comp_respects; auto.
507     Defined.
508   Add Parametric Morphism {jt rt ndr a b c d} : (@nd_prod jt rt a b c d)
509   with signature ((ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)) ==> (ndr_eqv(ND_Relation:=ndr)))
510     as parametric_morphism_nd_prod.
511     intros; apply ndr_prod_respects; auto.
512     Defined.
513
514 Section ND_Relation_Facts.
515   Context `{ND_Relation}.
516
517   (* useful *)
518   Lemma ndr_comp_right_identity : forall h c (f:h/⋯⋯/c), ndr_eqv (f ;; nd_id c) f.
519     intros; apply (ndr_builtfrom_structural f). auto.
520     auto.
521     Defined.
522
523   (* useful *)
524   Lemma ndr_comp_left_identity : forall h c (f:h/⋯⋯/c), ndr_eqv (nd_id h ;; f) f.
525     intros; apply (ndr_builtfrom_structural f); auto.
526     Defined.
527
528   Ltac nd_prod_preserves_comp_ltac P EQV :=
529     match goal with
530       [ |- context [ (?A ** ?B) ;; (?C ** ?D) ] ] => 
531         set (@ndr_prod_preserves_comp _ _ EQV _ _ A _ _ B _ C _ D) as P
532     end.
533
534   Lemma nd_swap A B C D (f:ND _ A B) (g:ND _ C D) :
535     (f ** nd_id C) ;; (nd_id B ** g) ===
536     (nd_id A ** g) ;; (f ** nd_id D).
537     setoid_rewrite <- ndr_prod_preserves_comp.
538     setoid_rewrite ndr_comp_left_identity.
539     setoid_rewrite ndr_comp_right_identity.
540     reflexivity.
541     Qed.
542
543   (* this tactical searches the environment; setoid_rewrite doesn't seem to be able to do that properly sometimes *)
544   Ltac nd_swap_ltac P EQV :=
545     match goal with
546       [ |- context [ (?F ** nd_id _) ;; (nd_id _ ** ?G) ] ] => 
547         set (@nd_swap _ _ EQV _ _ _ _ F G) as P
548     end.
549
550   Lemma nd_prod_split_left A B C D (f:ND _ A B) (g:ND _ B C) :
551     nd_id D ** (f ;; g) ===
552     (nd_id D ** f) ;; (nd_id D ** g).
553     setoid_rewrite <- ndr_prod_preserves_comp.
554     setoid_rewrite ndr_comp_left_identity.
555     reflexivity.
556     Qed.
557
558   Lemma nd_prod_split_right A B C D (f:ND _ A B) (g:ND _ B C) :
559     (f ;; g) ** nd_id D ===
560     (f ** nd_id D) ;; (g ** nd_id D).
561     setoid_rewrite <- ndr_prod_preserves_comp.
562     setoid_rewrite ndr_comp_left_identity.
563     reflexivity.
564     Qed.
565
566 End ND_Relation_Facts.
567
568 (* a generalization of the procedure used to build (nd_id n) from nd_id0 and nd_id1 *)
569 Definition nd_replicate
570   : forall
571     {Judgment}{Rule}{Ob}
572     (h:Ob->Judgment)
573     (c:Ob->Judgment)
574     (j:Tree ??Ob),
575     (forall (o:Ob), @ND Judgment Rule [h o] [c o]) ->
576     @ND Judgment Rule (mapOptionTree h j) (mapOptionTree c j).
577   intros.
578   induction j; simpl.
579     destruct a; simpl.
580     apply X.
581     apply nd_id0.
582     apply nd_prod; auto.
583     Defined.
584
585 (* "map" over natural deduction proofs, where the result proof has the same judgments (but different rules) *)
586 Definition nd_map
587   : forall
588     {Judgment}{Rule0}{Rule1}
589     (r:forall h c, Rule0 h c -> @ND Judgment Rule1 h c)
590     {h}{c}
591     (pf:@ND Judgment Rule0 h c)
592     ,
593     @ND Judgment Rule1 h c.
594     intros Judgment Rule0 Rule1 r.
595
596     refine ((fix nd_map h c pf {struct pf} :=
597      ((match pf
598          in @ND _ _ H C
599           return
600           @ND Judgment Rule1 H C
601         with
602         | nd_id0                     => let case_nd_id0     := tt in _
603         | nd_id1     h               => let case_nd_id1     := tt in _
604         | nd_weak1   h               => let case_nd_weak    := tt in _
605         | nd_copy    h               => let case_nd_copy    := tt in _
606         | nd_prod    _ _ _ _ lpf rpf => let case_nd_prod    := tt in _
607         | nd_comp    _ _ _   top bot => let case_nd_comp    := tt in _
608         | nd_rule    _ _     rule    => let case_nd_rule    := tt in _
609         | nd_cancell _               => let case_nd_cancell := tt in _
610         | nd_cancelr _               => let case_nd_cancelr := tt in _
611         | nd_llecnac _               => let case_nd_llecnac := tt in _
612         | nd_rlecnac _               => let case_nd_rlecnac := tt in _
613         | nd_assoc   _ _ _           => let case_nd_assoc   := tt in _
614         | nd_cossa   _ _ _           => let case_nd_cossa   := tt in _
615       end))) ); simpl in *.
616
617     destruct case_nd_id0.      apply nd_id0.
618     destruct case_nd_id1.      apply nd_id1.
619     destruct case_nd_weak.     apply nd_weak.
620     destruct case_nd_copy.     apply nd_copy.
621     destruct case_nd_prod.     apply (nd_prod (nd_map _ _ lpf) (nd_map _ _ rpf)).
622     destruct case_nd_comp.     apply (nd_comp (nd_map _ _ top) (nd_map _ _ bot)).
623     destruct case_nd_cancell.  apply nd_cancell.
624     destruct case_nd_cancelr.  apply nd_cancelr.
625     destruct case_nd_llecnac.  apply nd_llecnac.
626     destruct case_nd_rlecnac.  apply nd_rlecnac.
627     destruct case_nd_assoc.    apply nd_assoc.
628     destruct case_nd_cossa.    apply nd_cossa.
629     apply r. apply rule.
630     Defined.
631
632 (* "map" over natural deduction proofs, where the result proof has different judgments *)
633 Definition nd_map'
634   : forall
635     {Judgment0}{Rule0}{Judgment1}{Rule1}
636     (f:Judgment0->Judgment1)
637     (r:forall h c, Rule0 h c -> @ND Judgment1 Rule1 (mapOptionTree f h) (mapOptionTree f c))
638     {h}{c}
639     (pf:@ND Judgment0 Rule0 h c)
640     ,
641     @ND Judgment1 Rule1 (mapOptionTree f h) (mapOptionTree f c).
642     intros Judgment0 Rule0 Judgment1 Rule1 f r.
643     
644     refine ((fix nd_map' h c pf {struct pf} :=
645      ((match pf
646          in @ND _ _ H C
647           return
648           @ND Judgment1 Rule1 (mapOptionTree f H) (mapOptionTree f C)
649         with
650         | nd_id0                     => let case_nd_id0     := tt in _
651         | nd_id1     h               => let case_nd_id1     := tt in _
652         | nd_weak1   h               => let case_nd_weak    := tt in _
653         | nd_copy    h               => let case_nd_copy    := tt in _
654         | nd_prod    _ _ _ _ lpf rpf => let case_nd_prod    := tt in _
655         | nd_comp    _ _ _   top bot => let case_nd_comp    := tt in _
656         | nd_rule    _ _     rule    => let case_nd_rule    := tt in _
657         | nd_cancell _               => let case_nd_cancell := tt in _
658         | nd_cancelr _               => let case_nd_cancelr := tt in _
659         | nd_llecnac _               => let case_nd_llecnac := tt in _
660         | nd_rlecnac _               => let case_nd_rlecnac := tt in _
661         | nd_assoc   _ _ _           => let case_nd_assoc   := tt in _
662         | nd_cossa   _ _ _           => let case_nd_cossa   := tt in _
663       end))) ); simpl in *.
664
665     destruct case_nd_id0.      apply nd_id0.
666     destruct case_nd_id1.      apply nd_id1.
667     destruct case_nd_weak.     apply nd_weak.
668     destruct case_nd_copy.     apply nd_copy.
669     destruct case_nd_prod.     apply (nd_prod (nd_map' _ _ lpf) (nd_map' _ _ rpf)).
670     destruct case_nd_comp.     apply (nd_comp (nd_map' _ _ top) (nd_map' _ _ bot)).
671     destruct case_nd_cancell.  apply nd_cancell.
672     destruct case_nd_cancelr.  apply nd_cancelr.
673     destruct case_nd_llecnac.  apply nd_llecnac.
674     destruct case_nd_rlecnac.  apply nd_rlecnac.
675     destruct case_nd_assoc.    apply nd_assoc.
676     destruct case_nd_cossa.    apply nd_cossa.
677     apply r. apply rule.
678     Defined.
679
680 (* witnesses the fact that every Rule in a particular proof satisfies the given predicate *)
681 Inductive nd_property {Judgment}{Rule}(P:forall h c, @Rule h c -> Prop) : forall {h}{c}, @ND Judgment Rule h c -> Prop :=
682   | nd_property_structural      : forall h c pf, Structural pf -> @nd_property _ _ P h c pf
683   | nd_property_prod            : forall h0 c0 pf0 h1 c1 pf1,
684     @nd_property _ _ P h0 c0 pf0 -> @nd_property _ _ P h1 c1 pf1 -> @nd_property _ _ P _ _ (nd_prod pf0 pf1)
685   | nd_property_comp            : forall h0 c0 pf0  c1 pf1,
686     @nd_property _ _ P h0 c0 pf0 -> @nd_property _ _ P c0 c1 pf1 -> @nd_property _ _ P _ _ (nd_comp pf0 pf1)
687   | nd_property_rule            : forall h c r, P h c r -> @nd_property _ _ P h c (nd_rule r).
688   Hint Constructors nd_property.
689
690 (* witnesses the fact that every Rule in a particular proof satisfies the given predicate (for SIND) *)
691 Inductive scnd_property {Judgment}{Rule}(P:forall h c, @Rule h c -> Prop) : forall {h c}, @SIND Judgment Rule h c -> Prop :=
692 | scnd_property_weak            : forall c, @scnd_property _ _ P _ _ (scnd_weak c)
693 | scnd_property_comp            : forall h x c r cnd',
694   P x [c] r ->
695   @scnd_property _ _ P h x cnd' ->
696   @scnd_property _ _ P h _ (scnd_comp _ _ _ cnd' r)
697 | scnd_property_branch          :
698   forall x c1 c2 cnd1 cnd2,
699   @scnd_property _ _ P x c1 cnd1 ->
700   @scnd_property _ _ P x c2 cnd2 ->
701   @scnd_property _ _ P x _  (scnd_branch _ _ _ cnd1 cnd2).
702
703 (* renders a proof as LaTeX code *)
704 Section ToLatex.
705
706   Context {Judgment : Type}.
707   Context {Rule     : forall (hypotheses:Tree ??Judgment)(conclusion:Tree ??Judgment), Type}.
708   Context {JudgmentToLatexMath : ToLatexMath Judgment}.
709   Context {RuleToLatexMath     : forall h c, ToLatexMath (Rule h c)}.
710   
711   Open Scope string_scope.
712
713   Definition judgments2latex (j:Tree ??Judgment) := treeToLatexMath (mapOptionTree toLatexMath j).
714
715   Definition eolL : LatexMath := rawLatexMath eol.
716
717   (* invariant: each proof shall emit its hypotheses visibly, except nd_id0 *)
718   Section SIND_toLatex.
719
720     (* indicates which rules should be hidden (omitted) from the rendered proof; useful for structural operations *)
721     Context (hideRule : forall h c (r:Rule h c), bool).
722
723     Fixpoint SIND_toLatexMath {h}{c}(pns:SIND(Rule:=Rule) h c) : LatexMath :=
724       match pns with
725         | scnd_branch ht c1 c2 pns1 pns2 => SIND_toLatexMath pns1 +++ rawLatexMath " \hspace{1cm} " +++ SIND_toLatexMath pns2
726         | scnd_weak     c                => rawLatexMath ""
727         | scnd_comp ht ct c pns rule     => if hideRule _ _ rule
728                                             then SIND_toLatexMath pns
729                                             else rawLatexMath "\trfrac["+++ toLatexMath rule +++ rawLatexMath "]{" +++ eolL +++
730                                               SIND_toLatexMath pns +++ rawLatexMath "}{" +++ eolL +++
731                                               toLatexMath c +++ rawLatexMath "}" +++ eolL
732       end.
733   End SIND_toLatex.
734
735   (* this is a work-in-progress; please use SIND_toLatexMath for now *)
736   Fixpoint nd_toLatexMath {h}{c}(nd:@ND _ Rule h c)(indent:string) :=
737     match nd with
738       | nd_id0                      => rawLatexMath indent +++
739                                        rawLatexMath "% nd_id0 " +++ eolL
740       | nd_id1  h'                  => rawLatexMath indent +++
741                                        rawLatexMath "% nd_id1 "+++ judgments2latex h +++ eolL
742       | nd_weak1 h'                 => rawLatexMath indent +++
743                                        rawLatexMath "\inferrule*[Left=ndWeak]{" +++ toLatexMath h' +++ rawLatexMath "}{ }" +++ eolL
744       | nd_copy h'                  => rawLatexMath indent +++
745                                        rawLatexMath "\inferrule*[Left=ndCopy]{"+++judgments2latex h+++
746                                                          rawLatexMath "}{"+++judgments2latex c+++rawLatexMath "}" +++ eolL
747       | nd_prod h1 h2 c1 c2 pf1 pf2 => rawLatexMath indent +++
748                                        rawLatexMath "% prod " +++ eolL +++
749                                        rawLatexMath indent +++
750                                        rawLatexMath "\begin{array}{c c}" +++ eolL +++
751                                        (nd_toLatexMath pf1 ("  "+++indent)) +++
752                                        rawLatexMath indent +++
753                                        rawLatexMath " & " +++ eolL +++
754                                        (nd_toLatexMath pf2 ("  "+++indent)) +++
755                                        rawLatexMath indent +++
756                                        rawLatexMath "\end{array}"
757       | nd_comp h  m     c  pf1 pf2 => rawLatexMath indent +++
758                                        rawLatexMath "% comp " +++ eolL +++
759                                        rawLatexMath indent +++
760                                        rawLatexMath "\begin{array}{c}" +++ eolL +++
761                                        (nd_toLatexMath pf1 ("  "+++indent)) +++
762                                        rawLatexMath indent +++
763                                        rawLatexMath " \\ " +++ eolL +++
764                                        (nd_toLatexMath pf2 ("  "+++indent)) +++
765                                        rawLatexMath indent +++
766                                        rawLatexMath "\end{array}"
767       | nd_cancell a                => rawLatexMath indent +++
768                                        rawLatexMath "% nd-cancell " +++ (judgments2latex a) +++ eolL
769       | nd_cancelr a                => rawLatexMath indent +++
770                                        rawLatexMath "% nd-cancelr " +++ (judgments2latex a) +++ eolL
771       | nd_llecnac a                => rawLatexMath indent +++
772                                        rawLatexMath "% nd-llecnac " +++ (judgments2latex a) +++ eolL
773       | nd_rlecnac a                => rawLatexMath indent +++
774                                        rawLatexMath "% nd-rlecnac " +++ (judgments2latex a) +++ eolL
775       | nd_assoc   a b c            => rawLatexMath ""
776       | nd_cossa   a b c            => rawLatexMath ""
777       | nd_rule    h c r            => toLatexMath r
778     end.
779
780 End ToLatex.
781
782 Close Scope pf_scope.
783 Close Scope nd_scope.