Mix.v 27.9 KB
Newer Older
1

Pierre Letouzey's avatar
Pierre Letouzey committed
2
3
4
5
(** * Natural deduction, with a Locally Nameless encoding *)

(** The NatDed development, Pierre Letouzey, 2019.
    This file is released under the CC0 License, see the LICENSE file *)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

Require Import Defs.
Require DecimalString.
Import ListNotations.
Local Open Scope bool_scope.
Local Open Scope lazy_bool_scope.
Local Open Scope string_scope.
Local Open Scope eqb_scope.
(** We use here a Locally nameless representation of terms.
    See for instance http://www.chargueraud.org/research/2009/ln/main.pdf
*)

(** A term is given by the following recursive definition: *)

Inductive term :=
  | FVar : variable -> term (** Free variable (global name) *)
  | BVar : nat -> term (** Bounded variable (de Bruijn indices) *)
  | Fun : function_symbol -> list term -> term.

Definition Cst (f:function_symbol) := Fun f [].

Definition peano_term_example :=
  Fun "+" [Fun "S" [Cst "O"]; FVar "x"].

(** In the case of Peano, numbers are coded as iterated successors of zero *)

Fixpoint nat2term n :=
  match n with
  | O => Cst "O"
  | S n => Fun "S" [nat2term n]
  end.

Fixpoint term2nat t :=
  match t with
  | Fun f [] => if f =? "O" then Some O else None
  | Fun f [t] => if f =? "S" then option_map S (term2nat t) else None
  | _ => None
  end.

(** Term printing

    NB: + and * are printed in infix position, S(S(...O())) is printed as
    the corresponding number.
*)

Definition print_tuple {A} (pr: A -> string) (l : list A) :=
 "(" ++ String.concat "," (List.map pr l) ++ ")".

Definition is_binop s := list_mem s ["+";"*"].

Fixpoint print_term t :=
  match term2nat t with
  | Some n => DecimalString.NilZero.string_of_uint (Nat.to_uint n)
  | None =>
     match t with
     | FVar v => v
     | BVar n => "#" ++ DecimalString.NilZero.string_of_uint (Nat.to_uint n)
     | Fun f args =>
       if is_binop f then
         match args with
         | [t1;t2] =>
           "(" ++ print_term t1 ++ ")" ++ f ++ "(" ++ print_term t2 ++ ")"
         | _ => f ++ print_tuple print_term args
         end
       else f ++ print_tuple print_term args
     end
  end.

Compute print_term peano_term_example.

(** Term parsing *)

(** Actually, parsing is not so easy and not so important.
    Let's put the details elsewhere, and take for granted that
    parsing is doable :-).
*)

(* TODO: formula parsing *)

Pierre Letouzey's avatar
Pierre Letouzey committed
85
86
87
88
89

(** Some generic functions, meant to be overloaded
    with instances for terms, formulas, context, sequent, ... *)

(** Check for known function/predicate symbols + correct arity *)
90
Class Check (A : Type) := check : signature -> A -> bool.
Pierre Letouzey's avatar
Pierre Letouzey committed
91
92
93
94
95
96
97
98
99
100
101
Arguments check {_} {_} _ !_.

(** Replace a bound variable with a term *)
Class BSubst (A : Type) := bsubst : nat -> term -> A -> A.
Arguments bsubst {_} {_} _ _ !_.

(** Level : succ of max bounded variable *)
Class Level (A : Type) := level : A -> nat.
Arguments level {_} {_} !_.

(** Compute the set of free variables *)
Pierre Letouzey's avatar
Pierre Letouzey committed
102
Class FVars (A : Type) := fvars : A -> Names.t.
Pierre Letouzey's avatar
Pierre Letouzey committed
103
104
105
106
107
108
109
110
Arguments fvars {_} {_} !_.

(** General replacement of free variables *)
Class VMap (A : Type) := vmap : (variable -> term) -> A -> A.
Arguments vmap {_} {_} _ !_.

(** Some generic definitions based on the previous ones *)

111
Definition BClosed {A}`{Level A} (a:A) := level a = 0.
112

Pierre Letouzey's avatar
Pierre Letouzey committed
113
Definition FClosed {A}`{FVars A} (a:A) := Names.Empty (fvars a).
114
115

Hint Unfold BClosed FClosed.
Pierre Letouzey's avatar
Pierre Letouzey committed
116
117
118
119
120
121
122
123
124
125
126
127

(** Substitution of a free variable in a term :
    in [t], free var [v] is replaced by [u]. *)

Definition varsubst v u x := if v =? x then u else FVar x.

Definition fsubst {A}`{VMap A} (v:variable)(u:term) :=
 vmap (varsubst v u).

(** Some structural extensions of these generic functions *)

Instance check_list {A}`{Check A} : Check (list A) :=
128
 fun (sign : signature) => List.forallb (check sign).
Pierre Letouzey's avatar
Pierre Letouzey committed
129
130
131
132
133
134
135
136

Instance bsubst_list {A}`{BSubst A} : BSubst (list A) :=
 fun n t => List.map (bsubst n t).

Instance level_list {A}`{Level A} : Level (list A) :=
 fun l => list_max (List.map level l).

Instance fvars_list {A}`{FVars A} : FVars (list A) :=
Pierre Letouzey's avatar
Pierre Letouzey committed
137
 Names.unionmap fvars.
Pierre Letouzey's avatar
Pierre Letouzey committed
138
139
140
141
142

Instance vmap_list {A}`{VMap A} : VMap (list A) :=
 fun h => List.map (vmap h).

Instance check_pair {A B}`{Check A}`{Check B} : Check (A*B) :=
143
 fun (sign : signature) '(a,b) => check sign a &&& check sign b.
Pierre Letouzey's avatar
Pierre Letouzey committed
144
145
146
147
148
149
150
151

Instance bsubst_pair {A B}`{BSubst A}`{BSubst B} : BSubst (A*B) :=
 fun n t '(a,b) => (bsubst n t a, bsubst n t b).

Instance level_pair {A B}`{Level A}`{Level B} : Level (A*B) :=
 fun '(a,b) => Nat.max (level a) (level b).

Instance fvars_pair {A B}`{FVars A}`{FVars B} : FVars (A*B) :=
Pierre Letouzey's avatar
Pierre Letouzey committed
152
 fun '(a,b) => Names.union (fvars a) (fvars b).
Pierre Letouzey's avatar
Pierre Letouzey committed
153
154
155
156
157

Instance vmap_pair {A B}`{VMap A}`{VMap B} : VMap (A*B) :=
 fun h '(a,b) => (vmap h a, vmap h b).


158
159
160
161
(** With respect to a particular signature, a term is valid
    iff it only refer to known function symbols and use them
    with the correct arity. *)

Pierre Letouzey's avatar
Pierre Letouzey committed
162
Instance check_term : Check term :=
163
 fun (sign : signature) =>
Pierre Letouzey's avatar
Pierre Letouzey committed
164
 fix check_term t :=
165
166
167
 match t with
  | FVar _ | BVar _ => true
  | Fun f args =>
168
     match sign.(funsymbs) f with
169
170
     | None => false
     | Some ar =>
Pierre Letouzey's avatar
Pierre Letouzey committed
171
       (List.length args =? ar) &&& (List.forallb check_term args)
172
173
174
     end
 end.

175
Compute check (Finite.to_infinite peano_sign) peano_term_example.
Pierre Letouzey's avatar
Pierre Letouzey committed
176
177
178

Instance term_fvars : FVars term :=
 fix term_fvars t :=
179
 match t with
Pierre Letouzey's avatar
Pierre Letouzey committed
180
181
182
 | BVar _ => Names.empty
 | FVar v => Names.singleton v
 | Fun _ args => Names.unionmap term_fvars args
183
184
 end.

Pierre Letouzey's avatar
Pierre Letouzey committed
185
186
Instance term_level : Level term :=
 fix term_level t :=
187
188
189
190
191
192
 match t with
 | BVar n => S n
 | FVar v => 0
 | Fun _ args => list_max (map term_level args)
 end.

Pierre Letouzey's avatar
Pierre Letouzey committed
193
194
195
Instance term_bsubst : BSubst term :=
 fun n u =>
 fix bsubst t :=
196
197
198
  match t with
  | FVar v => t
  | BVar k => if k =? n then u else t
Pierre Letouzey's avatar
Pierre Letouzey committed
199
  | Fun f args => Fun f (List.map bsubst args)
200
201
  end.

Pierre Letouzey's avatar
Pierre Letouzey committed
202
203
204
205
206
207
208
209
Instance term_vmap : VMap term :=
 fun (h:variable->term) =>
 fix vmap t :=
  match t with
  | BVar _ => t
  | FVar x => h x
  | Fun f args => Fun f (List.map vmap args)
  end.
Pierre Letouzey's avatar
Pierre Letouzey committed
210
211
212
213
214
215
216
217
218
219
220

Instance term_eqb : Eqb term :=
 fix term_eqb t1 t2 :=
  match t1, t2 with
  | BVar n1, BVar n2 => n1 =? n2
  | FVar v1, FVar v2 => v1 =? v2
  | Fun f1 args1, Fun f2 args2 =>
    (f1 =? f2) &&& (list_forallb2 term_eqb args1 args2)
  | _, _ => false
  end.

221
222
223
224
225
226
227
Fixpoint lift t :=
 match t with
 | BVar n => BVar (S n)
 | FVar v => FVar v
 | Fun f args => Fun f (List.map lift args)
 end.

228
229
230
231
232
233
234
235
(* +1 sur les dB >= k *)
Fixpoint lift_above k t :=
 match t with
 | BVar n => if (k <=? n)%nat then BVar (S n) else t
 | FVar v => FVar v
 | Fun f args => Fun f (List.map (lift_above k) args)
 end.

236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
(** Formulas *)

Inductive formula :=
  | True
  | False
  | Pred : predicate_symbol -> list term -> formula
  | Not : formula -> formula
  | Op : op -> formula -> formula -> formula
  | Quant : quant -> formula -> formula.

(** Note the lack of variable name after [Quant], we're using
    de Bruijn indices there. *)

(** One extra pseudo-constructor :
    [a<->b] is a shortcut for [a->b /\ b->a] *)

Definition Iff a b := Op And (Op Impl a b) (Op Impl b a).

(** Formula printing *)

(** Notes:
    - We use {  } for putting formulas into parenthesis, instead of ( ).
*)

Definition is_infix_pred s := list_mem s ["=";"∈"].

(* TODO affichage court du <-> *)

Fixpoint print_formula f :=
  match f with
  | True => "True"
  | False => "False"
  | Pred p args =>
    if is_infix_pred p then
      match args with
      | [t1;t2] =>
        "(" ++ print_term t1 ++ ")" ++ p ++ "(" ++ print_term t2 ++ ")"
      |  _ => p ++ print_tuple print_term args
      end
    else p ++ print_tuple print_term args
  | Not f => "~{" ++ print_formula f ++ "}"
  | Op o f f' =>
    "{" ++ print_formula f ++ "}" ++ pr_op o ++ "{" ++ print_formula f' ++ "}"
  | Quant q f => pr_quant q ++ "{" ++ print_formula f ++ "}"
  end.

Compute print_formula (Quant Ex True).

Compute print_formula (Iff True False).

(* TODO: Formula parsing *)

(* Instead : Coq notations *)

Delimit Scope formula_scope with form.
Bind Scope formula_scope with formula.

Notation "~ f" := (Not f) : formula_scope.
Infix "/\" := (Op And) : formula_scope.
Infix "\/" := (Op Or) : formula_scope.
Infix "->" := (Op Impl) : formula_scope.
Infix "<->" := Iff : formula_scope.

299
Notation "# n" := (BVar n) (at level 20, format "# n") : formula_scope.
300
301
302
303
304
305
306
307
308
309

Notation " A" := (Quant All A)
 (at level 200, right associativity) : formula_scope.
Notation " A" := (Quant Ex A)
 (at level 200, right associativity) : formula_scope.

Definition test_form := (∃ (True <-> Pred "p" [#0;#0]))%form.

(** Utilities about formula *)

Pierre Letouzey's avatar
Pierre Letouzey committed
310
Instance check_formula : Check formula :=
311
 fun (sign : signature) =>
Pierre Letouzey's avatar
Pierre Letouzey committed
312
 fix check_formula f :=
313
314
  match f with
  | True | False => true
Pierre Letouzey's avatar
Pierre Letouzey committed
315
316
317
  | Not f => check_formula f
  | Op _ f f' => check_formula f &&& check_formula f'
  | Quant _ f => check_formula f
318
  | Pred p args =>
319
     match sign.(predsymbs) p with
320
321
     | None => false
     | Some ar =>
Pierre Letouzey's avatar
Pierre Letouzey committed
322
       (List.length args =? ar) &&& (List.forallb (check sign) args)
323
324
325
     end
  end.

Pierre Letouzey's avatar
Pierre Letouzey committed
326
327
Instance form_level : Level formula :=
  fix form_level f :=
328
329
  match f with
  | True | False => 0
Pierre Letouzey's avatar
Pierre Letouzey committed
330
  | Not f => form_level f
331
332
  | Op _ f f' => Nat.max (form_level f) (form_level f')
  | Quant _ f => Nat.pred (form_level f)
Pierre Letouzey's avatar
Pierre Letouzey committed
333
  | Pred _ args => list_max (map level args)
334
335
  end.

336
337
338
339
(** Substitution of a bounded variable by a term [t] in a formula [f].
    Note : we try to do something sensible when [t] has itself some
    bounded variables (we lift them when entering [f]'s quantifiers).
    But this situtation is nonetheless to be used with caution.
340
341
    Actually, we'll mostly use [bsubst] when [t] is [BClosed].
    Notable exception : induction schema in Peano.v *)
342

Pierre Letouzey's avatar
Pierre Letouzey committed
343
344
Instance form_bsubst : BSubst formula :=
 fix form_bsubst n t f :=
345
346
 match f with
  | True | False => f
Pierre Letouzey's avatar
Pierre Letouzey committed
347
  | Pred p args => Pred p (List.map (bsubst n t) args)
348
349
  | Not f => Not (form_bsubst n t f)
  | Op o f f' => Op o (form_bsubst n t f) (form_bsubst n t f')
350
  | Quant q f' => Quant q (form_bsubst (S n) (lift t) f')
351
352
 end.

Pierre Letouzey's avatar
Pierre Letouzey committed
353
354
355
Instance form_fvars : FVars formula :=
 fix form_fvars f :=
  match f with
Pierre Letouzey's avatar
Pierre Letouzey committed
356
  | True | False => Names.empty
Pierre Letouzey's avatar
Pierre Letouzey committed
357
  | Not f => form_fvars f
Pierre Letouzey's avatar
Pierre Letouzey committed
358
  | Op _ f f' => Names.union (form_fvars f) (form_fvars f')
Pierre Letouzey's avatar
Pierre Letouzey committed
359
  | Quant _ f => form_fvars f
Pierre Letouzey's avatar
Pierre Letouzey committed
360
  | Pred _ args => Names.unionmap fvars args
Pierre Letouzey's avatar
Pierre Letouzey committed
361
  end.
Pierre Letouzey's avatar
Pierre Letouzey committed
362

Pierre Letouzey's avatar
Pierre Letouzey committed
363
364
365
366
367
368
369
370
371
372
Instance form_vmap : VMap formula :=
 fun (h:variable->term) =>
 fix form_vmap f :=
   match f with
   | True | False => f
   | Pred p args => Pred p (List.map (vmap h) args)
   | Not f => Not (form_vmap f)
   | Op o f f' => Op o (form_vmap f) (form_vmap f')
   | Quant q f' => Quant q (form_vmap f')
   end.
373

Pierre Letouzey's avatar
Pierre Letouzey committed
374
375
Instance form_eqb : Eqb formula :=
 fix form_eqb f1 f2 :=
376
377
378
  match f1, f2 with
  | True, True | False, False => true
  | Pred p1 args1, Pred p2 args2 =>
Pierre Letouzey's avatar
Pierre Letouzey committed
379
    (p1 =? p2) &&& (args1 =? args2)
380
381
382
383
384
385
386
387
388
389
  | Not f1, Not f2 => form_eqb f1 f2
  | Op o1 f1 f1', Op o2 f2 f2' =>
    (o1 =? o2) &&&
    form_eqb f1 f2 &&&
    form_eqb f1' f2'
  | Quant q1 f1', Quant q2 f2' =>
    (q1 =? q2) &&& form_eqb f1' f2'
  | _,_ => false
  end.

Pierre Letouzey's avatar
Pierre Letouzey committed
390
Compute eqb
391
        (∀ (Pred "A" [ #0 ] -> Pred "A" [ #0 ]))%form
Pierre Letouzey's avatar
Pierre Letouzey committed
392
        (∀ (Pred "A" [FVar "z"] -> Pred "A" [FVar "z"]))%form.
393

394
395
396
397
398
399
400
401
402
(* +1 sur les dB >= k *)
Fixpoint lift_form_above k f :=
 match f with
 | True | False => f
 | Pred p l => Pred p (map (lift_above k) l)
 | Not f => Not (lift_form_above k f)
 | Op o f f' => Op o (lift_form_above k f) (lift_form_above k f')
 | Quant q f => Quant q (lift_form_above (S k) f)
 end.
403
404
405
406
407
408
409
410

(** Contexts *)

Definition context := list formula.

Definition print_ctx Γ :=
  String.concat "," (List.map print_formula Γ).

Pierre Letouzey's avatar
Pierre Letouzey committed
411
412
(** check, bsubst, level, fvars, vmap, eqb : given by instances
    on lists. *)
413
414
415
416
417
418
419
420
421
422
423

(** Sequent *)

Inductive sequent :=
| Seq : context -> formula -> sequent.

Infix "" := Seq (at level 100).

Definition print_seq '(Γ ⊢ A) :=
  print_ctx Γ ++ "  " ++ print_formula A.

Pierre Letouzey's avatar
Pierre Letouzey committed
424
425
Instance check_seq : Check sequent :=
 fun sign '(Γ ⊢ A) => check sign Γ &&& check sign A.
426

Pierre Letouzey's avatar
Pierre Letouzey committed
427
428
Instance bsubst_seq : BSubst sequent :=
 fun n u '(Γ ⊢ A) => (bsubst n u Γ ⊢ bsubst n u A).
429

Pierre Letouzey's avatar
Pierre Letouzey committed
430
431
432
433
Instance level_seq : Level sequent :=
 fun '(Γ ⊢ A) => Nat.max (level Γ) (level A).

Instance seq_fvars : FVars sequent :=
Pierre Letouzey's avatar
Pierre Letouzey committed
434
 fun '(Γ ⊢ A) => Names.union (fvars Γ) (fvars A).
435

Pierre Letouzey's avatar
Pierre Letouzey committed
436
437
Instance seq_vmap : VMap sequent :=
 fun h '(Γ ⊢ A) => (vmap h Γ ⊢ vmap h A).
438

Pierre Letouzey's avatar
Pierre Letouzey committed
439
440
Instance seq_eqb : Eqb sequent :=
 fun '(Γ1 ⊢ A1) '(Γ2 ⊢ A2) => (Γ1 =? Γ2) &&& (A1 =? A2).
441
442
443

(** Derivation *)

444
Inductive rule_kind :=
445
446
447
448
449
450
451
452
453
454
455
456
  | Ax
  | Tr_i
  | Fa_e
  | Not_i | Not_e
  | And_i | And_e1 | And_e2
  | Or_i1 | Or_i2 | Or_e
  | Imp_i | Imp_e
  | All_i (v:variable)| All_e (wit:term)
  | Ex_i (wit:term) | Ex_e (v:variable)
  | Absu.

Inductive derivation :=
457
  | Rule : rule_kind -> sequent -> list derivation -> derivation.
458

459
460
461
462
463
(** The final sequent of a derivation *)

Definition claim '(Rule _ s _) := s.

(** Utility functions on derivations:
464
    - bounded-vars level (used by the [BClosed] predicate),
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
    - check w.r.t. signature *)

Instance level_rule_kind : Level rule_kind :=
 fun r =>
 match r with
 | All_e wit | Ex_i wit => level wit
 | _ => 0
 end.

Instance level_derivation : Level derivation :=
 fix level_derivation d :=
  let '(Rule r s ds) := d in
  list_max (level r :: level s :: List.map level_derivation ds).

Instance check_rule_kind : Check rule_kind :=
 fun sign r =>
 match r with
 | All_e wit | Ex_i wit => check sign wit
 | _ => true
 end.

Instance check_derivation : Check derivation :=
 fun sign =>
 fix check_derivation d :=
  let '(Rule r s ds) := d in
  check sign r &&&
  check sign s &&&
  List.forallb check_derivation ds.

494
495
496
Instance fvars_rule : FVars rule_kind :=
 fun r =>
 match r with
Pierre Letouzey's avatar
Pierre Letouzey committed
497
 | All_i x | Ex_e x => Names.singleton x
498
 | All_e wit | Ex_i wit => fvars wit
Pierre Letouzey's avatar
Pierre Letouzey committed
499
 | _ => Names.empty
500
501
502
503
504
 end.

Instance fvars_derivation : FVars derivation :=
 fix fvars_derivation d :=
  let '(Rule r s ds) := d in
Pierre Letouzey's avatar
Pierre Letouzey committed
505
  Names.unions [fvars r; fvars s; Names.unionmap fvars_derivation ds].
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529

Instance bsubst_rule : BSubst rule_kind :=
 fun n u r =>
 match r with
 | All_e wit => All_e (bsubst n u wit)
 | Ex_i wit => Ex_i (bsubst n u wit)
 | _ => r
 end.

Instance bsubst_deriv : BSubst derivation :=
 fix bsubst_deriv n u d :=
 let '(Rule r s ds) := d in
 Rule (bsubst n u r) (bsubst n u s ) (map (bsubst_deriv n u) ds).

Instance vmap_rule : VMap rule_kind :=
 fun h r =>
 match r with
 | All_e wit => All_e (vmap h wit)
 | Ex_i wit => Ex_i (vmap h wit)
 | r => r
 end.

(** See Meta for [vmap_deriv], which is slightly more complex
    due to some variable renaming. *)
530
531
532

(** Validity of a derivation : is it using correct rules
    of classical logic (resp. intuitionistic logic) ? *)
533

534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
(** Note : this validity notion does *not* ensure that
    the terms and formulas in this derivation are well-formed
    (i.e. have no unbound [BVar] variables and properly use
    the symbols of a signature). We will see later how to
    "force" a derivation to be well-formed (see [Meta.forcelevel]
    and [Meta.restrict]).

    In particular, forcing here all formulas to be [BClosed] would
    be tedious. See for instance [Fa_e] below, any formula can be
    deduced from [False], even non-well-formed ones. In a former
    version of this work, [BClosed] conditions were imposed on
    [All_e] and [Ex_i] witnesses [t], but this isn't mandatory
    anymore now that [subst] incorporates a [lift] operation.

    Example of earlier issue : consider [∀ ∃ ~(Pred "=" [#0;#1])] i.e.
549
    [∀x∃y,x≠y], a possible way of saying that the world isn't
550
551
552
553
    a singleton. By [∀e] we can deduce
    [bsubst 0 (#0) (∃ ~(Pred "=" [#0;#1]))], and without [lift] this
    was reducing to [∃ ~(Pred "=" [#0;#0])], a formula negating
    the reflexivity of equality !
554
555
*)

556
Definition valid_deriv_step logic '(Rule r s ld) :=
557
  match r, s, List.map claim ld with
Pierre Letouzey's avatar
Pierre Letouzey committed
558
  | Ax,     (Γ ⊢ A), [] => list_mem A Γ
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
  | Tr_i,   (_ ⊢ True), [] => true
  | Fa_e,   (Γ ⊢ _), [s] => s =? (Γ ⊢ False)
  | Not_i,  (Γ ⊢ Not A), [s] => s =? (A::Γ ⊢ False)
  | Not_e,  (Γ ⊢ False), [Γ1 ⊢ A; Γ2 ⊢ Not A'] =>
    (A =? A') &&& (Γ =? Γ1) &&& (Γ =? Γ2)
  | And_i,  (Γ ⊢ A/\B), [s1; s2] =>
    (s1 =? (Γ ⊢ A)) &&& (s2 =? (Γ ⊢ B))
  | And_e1, s, [Γ ⊢ A/\_] => s =? (Γ ⊢ A)
  | And_e2, s, [Γ ⊢ _/\B] => s =? (Γ ⊢ B)
  | Or_i1,  (Γ ⊢ A\/_), [s] => s =? (Γ ⊢ A)
  | Or_i2,  (Γ ⊢ _\/B), [s] => s =? (Γ ⊢ B)
  | Or_e,   (Γ ⊢ C), [Γ' ⊢ A\/B; s2; s3] =>
     (Γ=?Γ') &&& (s2 =? (A::Γ ⊢ C)) &&& (s3 =? (B::Γ ⊢ C))
  | Imp_i,  (Γ ⊢ A->B), [s] => s =? (A::Γ ⊢ B)
  | Imp_e,  s, [Γ ⊢ A->B;s2] =>
     (s =? (Γ ⊢ B)) &&& (s2 =? (Γ ⊢ A))
  | All_i x,  (Γ⊢∀A), [Γ' ⊢ A'] =>
Pierre Letouzey's avatar
Pierre Letouzey committed
576
     (Γ =? Γ') &&& (A' =? bsubst 0 (FVar x) A)
Pierre Letouzey's avatar
Pierre Letouzey committed
577
     &&& negb (Names.mem x (fvars (Γ⊢A)))
578
  | All_e t, (Γ ⊢ B), [Γ'⊢ ∀A] =>
579
    (Γ =? Γ') &&& (B =? bsubst 0 t A)
580
  | Ex_i t,  (Γ ⊢ ∃A), [Γ'⊢B] =>
581
    (Γ =? Γ') &&& (B =? bsubst 0 t A)
582
583
  | Ex_e x,  s, [Γ⊢∃A; A'::Γ'⊢B] =>
     (s =? (Γ ⊢ B)) &&& (Γ' =? Γ)
Pierre Letouzey's avatar
Pierre Letouzey committed
584
     &&& (A' =? bsubst 0 (FVar x) A)
Pierre Letouzey's avatar
Pierre Letouzey committed
585
     &&& negb (Names.mem x (fvars (A::Γ⊢B)))
586
  | Absu, s, [Not A::Γ ⊢ False] =>
587
    (logic =? Classic) &&& (s =? (Γ ⊢ A))
588
589
590
591
592
593
594
595
  | _,_,_ => false
  end.

Fixpoint valid_deriv logic d :=
  valid_deriv_step logic d &&&
   (let '(Rule _ _ ld) := d in
    List.forallb (valid_deriv logic) ld).

596
(** Some examples of derivations *)
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661

Definition deriv_example :=
  let A := Pred "A" [] in
  Rule Imp_i ([]⊢A->A) [Rule Ax ([A]⊢A) []].

Compute valid_deriv Intuiti deriv_example.

Definition example_gen (A:formula) :=
  Rule Imp_i ([]⊢A->A) [Rule Ax ([A]⊢A) []].

Compute valid_deriv Intuiti (example_gen (Pred "A" [])).

Definition example2 (A B : term->formula):=
  (Rule Imp_i ([]⊢(∀A(#0)/\B(#0))->(∀A(#0))/\(∀B(#0)))
    (let C := (∀(A(#0)/\B(#0))) in
     [Rule And_i ([C] ⊢ (∀A(#0))/\(∀B(#0)))
       [Rule (All_i "x") ([C]⊢∀A(#0))
         [Rule And_e1 ([C]⊢A(FVar "x"))
           [Rule (All_e (FVar "x")) ([C]⊢ A(FVar "x")/\B(FVar "x"))
             [Rule Ax ([C]⊢C) []]]]
        ;
        Rule (All_i "x") ([C]⊢∀B(#0))
         [Rule And_e2 ([C]⊢B(FVar "x"))
           [Rule (All_e (FVar "x")) ([C]⊢A(FVar "x")/\B(FVar "x"))
             [Rule Ax ([C]⊢C) []]]]]]))%form.

Compute valid_deriv Intuiti
         (example2 (fun x => Pred "A" [x])
                   (fun x => Pred "B" [x])).

Definition em (A:formula) :=
  Rule Absu ([]⊢A\/~A)
    [Rule Not_e ([~(A\/~A)]⊢False)
       [Rule Or_i2 ([~(A\/~A)]⊢A\/~A)
         [Rule Not_i ([~(A\/~A)]⊢~A)
           [Rule Not_e ([A;~(A\/~A)]⊢False)
             [Rule Or_i1 ([A;~(A\/~A)]⊢A\/~A)
               [Rule Ax ([A;~(A\/~A)]⊢A) []]
              ;
              Rule Ax ([A;~(A\/~A)]⊢~(A\/~A)) []]]]
        ;
        Rule Ax ([~(A\/~A)]⊢~(A\/~A)) []]]%form.

Compute valid_deriv Classic (em (Pred "A" [])).
Compute valid_deriv Intuiti (em (Pred "A" [])).

(** Example of free alpha-renaming during a proof,
    (not provable without alpha-renaming) *)

Definition captcha :=
  let A := fun x => Pred "A" [x] in
  Rule (All_i "z") ([A(FVar "x")]⊢∀(A(#0)->A(#0)))%form
   [Rule Imp_i ([A(FVar "x")]⊢A(FVar "z")->A(FVar "z"))
     [Rule Ax ([A(FVar "z");A(FVar "x")]⊢A(FVar "z")) []]].

Compute valid_deriv Intuiti captcha.

Definition captcha_bug :=
  let A := fun x => Pred "A" [x] in
  Rule (All_i "x") ([A(FVar "x")]⊢∀(A(#0)->A(#0)))%form
   [Rule Imp_i ([A(FVar "x")]⊢A(FVar "x")->A(FVar "x"))
    [Rule Ax ([A(FVar "x");A(FVar "x")]⊢A(FVar "x")) []]].

Compute valid_deriv Intuiti captcha_bug.

662
663
(** Correctness of earlier boolean equality tests *)

Pierre Letouzey's avatar
Pierre Letouzey committed
664
Instance : EqbSpec term.
665
Proof.
Pierre Letouzey's avatar
Pierre Letouzey committed
666
667
668
669
670
671
672
673
 red.
 fix IH 1. destruct x as [v|n|f l], y as [v'|n'|f' l']; cbn; try cons.
 - case eqbspec; cons.
 - case eqbspec; cons.
 - case eqbspec; [ intros <- | cons ].
   change (list_forallb2 eqb l l') with (l =? l').
   change (EqbSpec term) in IH.
   case eqbspec; cons.
674
675
Qed.

Pierre Letouzey's avatar
Pierre Letouzey committed
676
Instance : EqbSpec formula.
677
Proof.
Pierre Letouzey's avatar
Pierre Letouzey committed
678
679
680
681
682
683
684
685
686
687
 red.
 induction x; destruct y; cbn; try cons.
 - case eqbspec; [ intros <- | cons ].
   case eqbspec; cons.
 - case IHx; cons.
 - case eqbspec; [ intros <- | cons ].
   case IHx1; [ intros <- | cons].
   case IHx2; cons.
 - case eqbspec; [ intros <- | cons ].
   case IHx; cons.
688
689
Qed.

Pierre Letouzey's avatar
Pierre Letouzey committed
690
Instance : EqbSpec context.
691
Proof.
Pierre Letouzey's avatar
Pierre Letouzey committed
692
 apply eqbspec_list.
693
694
Qed.

Pierre Letouzey's avatar
Pierre Letouzey committed
695
Instance : EqbSpec sequent.
696
Proof.
Pierre Letouzey's avatar
Pierre Letouzey committed
697
 intros [] []. cbn. repeat (case eqbspec; try cons).
698
699
Qed.

Pierre Letouzey's avatar
Pierre Letouzey committed
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
(** Better induction principle on terms *)

Lemma term_ind' (P: term -> Prop) :
  (forall v, P (FVar v)) ->
  (forall n, P (BVar n)) ->
  (forall f args, (forall a, In a args -> P a) -> P (Fun f args)) ->
  forall t, P t.
Proof.
 intros Hv Hn Hl.
 fix IH 1. destruct t.
 - apply Hv.
 - apply Hn.
 - apply Hl.
   revert l.
   fix IH' 1. destruct l.
   + intros a [ ].
   + intros a [<-|Ha]. apply IH. apply (IH' l a Ha).
Qed.

719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
(** Induction principle on derivations with correct
    handling of sub-derivation lists. *)

Lemma derivation_ind' (P: derivation -> Prop) :
  (forall r s ds, Forall P ds -> P (Rule r s ds)) ->
  forall d, P d.
Proof.
 intros Step.
 fix IH 1. destruct d as (r,s,ds).
 apply Step.
 revert ds.
 fix IH' 1. destruct ds; simpl; constructor.
 apply IH.
 apply IH'.
Qed.

(** A derivation "claims" a sequent ... if it ends with this sequent.
    This is just nicer than putting [claim ... = ...] everywhere. *)

Definition Claim d s := claim d = s.
Arguments Claim _ _ /.
Hint Extern 1 (Claim _ _) => cbn.

(** An inductive counterpart to valid_deriv, easier to use in proofs *)

Inductive Valid (l:logic) : derivation -> Prop :=
 | V_Ax Γ A : In A Γ -> Valid l (Rule Ax (Γ ⊢ A) [])
 | V_Tr_i Γ : Valid l (Rule Tr_i (Γ ⊢ True) [])
 | V_Fa_e d Γ A :
     Valid l d -> Claim d (Γ ⊢ False) ->
     Valid l (Rule Fa_e (Γ ⊢ A) [d])
 | V_Not_i d Γ A :
     Valid l d -> Claim d (A::Γ ⊢ False) ->
     Valid l (Rule Not_i (Γ ⊢ ~A) [d])
 | V_Not_e d1 d2 Γ A :
     Valid l d1 -> Valid l d2 ->
     Claim d1 (Γ ⊢ A) -> Claim d2 (Γ ⊢ ~A) ->
     Valid l (Rule Not_e (Γ ⊢ False) [d1;d2])
 | V_And_i d1 d2 Γ A B :
     Valid l d1 -> Valid l d2 ->
     Claim d1 (Γ ⊢ A) -> Claim d2 (Γ ⊢ B) ->
     Valid l (Rule And_i (Γ ⊢ A/\B) [d1;d2])
 | V_And_e1 d Γ A B :
     Valid l d -> Claim d (Γ ⊢ A/\B) ->
     Valid l (Rule And_e1 (Γ ⊢ A) [d])
 | V_And_e2 d Γ A B :
     Valid l d -> Claim d (Γ ⊢ A/\B) ->
     Valid l (Rule And_e2 (Γ ⊢ B) [d])
 | V_Or_i1 d Γ A B :
     Valid l d -> Claim d (Γ ⊢ A) ->
     Valid l (Rule Or_i1 (Γ ⊢ A\/B) [d])
 | V_Or_i2 d Γ A B :
     Valid l d -> Claim d (Γ ⊢ B) ->
     Valid l (Rule Or_i2 (Γ ⊢ A\/B) [d])
 | V_Or_e d1 d2 d3 Γ A B C :
     Valid l d1 -> Valid l d2 -> Valid l d3 ->
     Claim d1 (Γ ⊢ A\/B) -> Claim d2 (A::Γ ⊢ C) -> Claim d3 (B::Γ ⊢ C) ->
     Valid l (Rule Or_e (Γ ⊢ C) [d1;d2;d3])
 | V_Imp_i d Γ A B :
     Valid l d -> Claim d (A::Γ ⊢ B) ->
     Valid l (Rule Imp_i (Γ ⊢ A->B) [d])
 | V_Imp_e d1 d2 Γ A B :
     Valid l d1 -> Valid l d2 ->
     Claim d1 (Γ ⊢ A->B) -> Claim d2 (Γ ⊢ A) ->
     Valid l (Rule Imp_e (Γ ⊢ B) [d1;d2])
 | V_All_i x d Γ A :
Pierre Letouzey's avatar
Pierre Letouzey committed
785
     ~Names.In x (fvars (Γ ⊢ A)) ->
786
787
788
     Valid l d -> Claim d (Γ ⊢ bsubst 0 (FVar x) A) ->
     Valid l (Rule (All_i x) (Γ ⊢ ∀A) [d])
 | V_All_e t d Γ A :
789
     Valid l d -> Claim d (Γ ⊢ ∀A) ->
790
791
     Valid l (Rule (All_e t) (Γ ⊢ bsubst 0 t A) [d])
 | V_Ex_i t d Γ A :
792
     Valid l d -> Claim d (Γ ⊢ bsubst 0 t A) ->
793
794
     Valid l (Rule (Ex_i t) (Γ ⊢ ∃A) [d])
 | V_Ex_e x d1 d2 Γ A B :
Pierre Letouzey's avatar
Pierre Letouzey committed
795
     ~Names.In x (fvars (A::Γ⊢B)) ->
796
797
798
799
800
801
802
803
804
805
806
     Valid l d1 -> Valid l d2 ->
     Claim d1 (Γ ⊢ ∃A) -> Claim d2 ((bsubst 0 (FVar x) A)::Γ ⊢ B) ->
     Valid l (Rule (Ex_e x) (Γ ⊢ B) [d1;d2])
 | V_Absu d Γ A :
     l=Classic ->
     Valid l d -> Claim d (Not A :: Γ ⊢ False) ->
     Valid l (Rule Absu (Γ ⊢ A) [d]).

Hint Constructors Valid.

(** Let's prove now that [valid_deriv] is equivalent to [Valid] *)
807
808
809
810
811

Ltac break :=
 match goal with
 | H : match _ with true => _ | false => _ end = true |- _ =>
   rewrite !lazy_andb_iff in H
812
 | H : match claim ?x with _ => _ end = true |- _ =>
813
814
815
816
817
818
819
820
821
   destruct x; simpl in H; try easy; break
 | H : match map _ ?x with _ => _ end = true |- _ =>
   destruct x; simpl in H; try easy; break
 | H : match ?x with _ => _ end = true |- _ =>
   destruct x; simpl in H; try easy; break
 | _ => idtac
 end.

Ltac mytac :=
822
823
824
825
826
 cbn in *;
 break;
 rewrite ?andb_true_r, ?andb_true_iff, ?lazy_andb_iff in *;
 repeat match goal with H : _ /\ _ |- _ => destruct H end;
 repeat match goal with IH : Forall _ _  |- _ => inversion_clear IH end;
827
 rewrite ?@eqb_eq in * by auto with typeclass_instances.
828

829
Ltac rewr :=
830
 unfold Claim, BClosed in *;
831
832
833
834
835
836
 match goal with
 | H: _ = _ |- _ => rewrite H in *; clear H; rewr
 | _ => rewrite ?eqb_refl
 end.

Lemma Valid_equiv l d : valid_deriv l d = true <-> Valid l d.
837
Proof.
838
839
840
841
842
843
844
845
846
 split.
 - induction d as [r s ds IH] using derivation_ind'.
   cbn - [valid_deriv_step]. rewrite lazy_andb_iff. intros (H,H').
   assert (IH' : Forall (fun d => Valid l d) ds).
   { rewrite Forall_forall, forallb_forall in *. auto. }
   clear IH H'.
   mytac; subst; eauto.
   + now apply V_Ax, list_mem_in.
   + apply V_All_i; auto.
Pierre Letouzey's avatar
Pierre Letouzey committed
847
     rewrite <- Names.mem_spec. cbn. intros EQ. now rewrite EQ in *.
848
   + apply V_Ex_e with f; auto.
Pierre Letouzey's avatar
Pierre Letouzey committed
849
     rewrite <- Names.mem_spec. cbn. intros EQ. now rewrite EQ in *.
850
 - induction 1; simpl; rewr; auto.
851
   + apply list_mem_in in H. now rewrite H.
Pierre Letouzey's avatar
Pierre Letouzey committed
852
853
   + rewrite <- Names.mem_spec in H. destruct Names.mem; auto.
   + rewrite <- Names.mem_spec in H. destruct Names.mem; auto.
854
855
856
857
858
859
860
861
862
863
864
865
Qed.

(** A notion of provability, directly on a sequent *)

Definition Provable logic (s : sequent) :=
  exists d, Valid logic d /\ Claim d s.

Lemma thm_example :
  let A := Pred "A" [] in
  Provable Intuiti ([]⊢A->A).
Proof.
 exists deriv_example. now rewrite <- Valid_equiv.
866
Qed.
867

868
869
870

(** A provability notion directly on sequents, without derivations.
    Pros: lighter
871
872
    Cons: no easy way to express meta-theoretical facts about the proof
          itself (e.g. free or bounded variables used during the proof). *)
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899

Inductive Pr (l:logic) : sequent -> Prop :=
 | R_Ax Γ A : In A Γ -> Pr l (Γ ⊢ A)
 | R_Tr_i Γ : Pr l (Γ ⊢ True)
 | R_Fa_e Γ A : Pr l (Γ ⊢ False) ->
                  Pr l (Γ ⊢ A)
 | R_Not_i Γ A : Pr l (A::Γ ⊢ False) ->
                   Pr l (Γ ⊢ ~A)
 | R_Not_e Γ A : Pr l (Γ ⊢ A) -> Pr l (Γ ⊢ ~A) ->
                   Pr l (Γ ⊢ False)
 | R_And_i Γ A B : Pr l (Γ ⊢ A) -> Pr l (Γ ⊢ B) ->
                   Pr l (Γ ⊢ A/\B)
 | R_And_e1 Γ A B : Pr l (Γ ⊢ A/\B) ->
                    Pr l (Γ ⊢ A)
 | R_And_e2 Γ A B : Pr l (Γ ⊢ A/\B) ->
                    Pr l (Γ ⊢ B)
 | R_Or_i1 Γ A B : Pr l (Γ ⊢ A) ->
                   Pr l (Γ ⊢ A\/B)
 | R_Or_i2 Γ A B : Pr l (Γ ⊢ B) ->
                   Pr l (Γ ⊢ A\/B)
 | R_Or_e Γ A B C :
     Pr l (Γ ⊢ A\/B) -> Pr l (A::Γ ⊢ C) -> Pr l (B::Γ ⊢ C) ->
     Pr l (Γ ⊢ C)
 | R_Imp_i Γ A B : Pr l (A::Γ ⊢ B) ->
                     Pr l (Γ ⊢ A->B)
 | R_Imp_e Γ A B : Pr l (Γ ⊢ A->B) -> Pr l (Γ ⊢ A) ->
                   Pr l (Γ ⊢ B)
Pierre Letouzey's avatar
Pierre Letouzey committed
900
 | R_All_i x Γ A : ~Names.In x (fvars (Γ ⊢ A)) ->
901
902
                   Pr l (Γ ⊢ bsubst 0 (FVar x) A) ->
                   Pr l (Γ ⊢ ∀A)
903
904
 | R_All_e t Γ A : Pr l (Γ ⊢ ∀A) -> Pr l (Γ ⊢ bsubst 0 t A)
 | R_Ex_i t Γ A : Pr l (Γ ⊢ bsubst 0 t A) -> Pr l (Γ ⊢ ∃A)
Pierre Letouzey's avatar
Pierre Letouzey committed
905
 | R_Ex_e x Γ A B : ~Names.In x (fvars (A::Γ⊢B)) ->
906
907
908
909
910
911
912
913
      Pr l (Γ ⊢ ∃A) -> Pr l ((bsubst 0 (FVar x) A)::Γ ⊢ B) ->
      Pr l (Γ ⊢ B)
 | R_Absu Γ A : l=Classic -> Pr l (Not A :: Γ ⊢ False) ->
                  Pr l (Γ ⊢ A).
Hint Constructors Pr.

Lemma Valid_Pr lg d :
  Valid lg d -> Pr lg (claim d).
914
Proof.
915
 induction 1; cbn in *; rewr; eauto 2.
916
917
Qed.

918
919
Lemma Provable_alt lg s :
  Pr lg s <-> Provable lg s.
920
Proof.
921
922
923
924
925
926
 split.
 - induction 1;
   repeat match goal with H:Provable _ _ |- _ =>
          let d := fresh "d" in destruct H as (d & ? & ?) end;
   eexists (Rule _ _ _); split; try reflexivity; eauto 2.
 - intros (d & Hd & <-). now apply Valid_Pr.
927
928
Qed.

929
(* Some useful statements. *)
930

931
932
Lemma Pr_intuit_classic s : Pr Intuiti s -> Pr Classic s.
Proof.
933
 induction 1; eauto 2.
934
935
936
937
938
939
940
941
942
943
944
945
Qed.

Lemma Pr_intuit_any lg s : Pr Intuiti s -> Pr lg s.
Proof.
 destruct lg. apply Pr_intuit_classic. trivial.
Qed.

Lemma Pr_any_classic lg s : Pr lg s -> Pr Classic s.
Proof.
 destruct lg. trivial. apply Pr_intuit_classic.
Qed.

946
Lemma intuit_classic d : Valid Intuiti d -> Valid Classic d.
947
Proof.
948
 induction 1; eauto.
949
950
Qed.

951
Lemma any_classic d lg : Valid lg d -> Valid Classic d.
952
953
Proof.
 destruct lg. trivial. apply intuit_classic.
954
Qed.