-
Notifications
You must be signed in to change notification settings - Fork 0
/
fol_mm0.lean
2514 lines (2259 loc) · 59.3 KB
/
fol_mm0.lean
1
2
3
4
5
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import data.hash_map
import logic.function.basic
import tactic
set_option pp.parens true
lemma aux_1
{α β : Type}
[decidable_eq α]
(g : α → β)
(f f' : α → α)
(x : α)
(a : β)
(h1 : f' ∘ f = id) :
(function.update g (f x) a) ∘ f = function.update (g ∘ f) x a :=
begin
have s1 : function.left_inverse f' f,
exact congr_fun h1,
apply function.update_comp_eq_of_injective,
exact function.left_inverse.injective s1,
end
lemma aux_2
{α β : Type}
[decidable_eq α]
(g : α → β)
(f f' : α → α)
(x : α)
(a : β)
(h1 : f' ∘ f = id)
(h2 : f ∘ f' = id) :
(function.update g x a) ∘ f = function.update (g ∘ f) (f' x) a :=
begin
rewrite <- aux_1 g f f' (f' x) a h1,
congr,
rewrite <- function.comp_app f f' x,
rewrite h2,
exact id.def x,
end
lemma aux_3
{α β : Type}
[decidable_eq α]
(f g : α → β)
(x : α)
(h1 : ∀ (y : α), ¬ y = x → f y = g y) :
function.update f x (g x) = g :=
begin
apply funext, intros y,
by_cases c1 : y = x,
{
rewrite c1,
simp only [function.update_same],
},
{
simp only [function.update_noteq c1],
exact h1 y c1,
},
end
lemma list.nth_le_mem_zip
{α β : Type}
[decidable_eq α]
(l1 : list α)
(l2 : list β)
(n : ℕ)
(h1 : n < l1.length)
(h2 : n < l2.length) :
((l1.nth_le n h1, l2.nth_le n h2) ∈ l1.zip l2) :=
begin
have s1 : n < (l1.zip l2).length,
simp only [list.length_zip, lt_min_iff],
split,
{
exact h1,
},
{
exact h2,
},
have s2 : (list.zip l1 l2).nth_le n s1 = (l1.nth_le n h1, l2.nth_le n h2),
exact list.nth_le_zip,
rewrite <- s2,
exact (list.zip l1 l2).nth_le_mem n s1,
end
lemma list.map_fst_zip_is_prefix
{α β : Type}
(l1 : list α)
(l2 : list β) :
list.map prod.fst (l1.zip l2) <+: l1 :=
begin
induction l1 generalizing l2,
case list.nil : l2
{
simp only [list.zip_nil_left, list.map_nil],
},
case list.cons : l1_hd l1_tl l1_ih l2
{
induction l2,
case list.nil
{
unfold list.is_prefix,
apply exists.intro (l1_hd :: l1_tl),
simp only [list.zip_nil_right, list.map_nil, list.nil_append, eq_self_iff_true, and_self],
},
case list.cons : l2_hd l2_tl l2_ih
{
simp only [list.map, list.zip_cons_cons],
rewrite list.prefix_cons_inj,
exact l1_ih l2_tl,
},
},
end
lemma list.map_fst_zip_nodup
{α β : Type}
(l1 : list α)
(l2 : list β)
(h1 : l1.nodup) :
(list.map prod.fst (l1.zip l2)).nodup :=
begin
have s1 : list.map prod.fst (l1.zip l2) <+ l1,
apply list.is_prefix.sublist,
exact l1.map_fst_zip_is_prefix l2,
exact list.nodup.sublist s1 h1,
end
def function.update_list
{α β : Type}
[decidable_eq α]
(f : α → β) :
list (α × β) → α → β
| [] := f
| (hd :: tl) := function.update (function.update_list tl) hd.fst hd.snd
#eval function.update_list (fun (n : ℕ), n) [(0,1), (3,2), (0,2)] 0
lemma function.update_list_mem
{α β : Type}
[decidable_eq α]
(f : α → β)
(l : list (α × β))
(x : α × β)
(h1 : list.nodup (list.map prod.fst l))
(h2 : x ∈ l) :
function.update_list f l x.fst = x.snd :=
begin
induction l,
case list.nil
{
simp only [list.not_mem_nil] at h2,
contradiction,
},
case list.cons : hd tl ih
{
simp only [list.map, list.nodup_cons, list.mem_map, prod.exists,
exists_and_distrib_right, exists_eq_right, not_exists] at h1,
cases h1,
simp only [list.mem_cons_iff] at h2,
unfold function.update_list,
cases h2,
{
rewrite h2,
simp only [function.update_same],
},
{
have s1 : ¬ x.fst = hd.fst,
intro contra,
apply h1_left x.snd,
rewrite <- contra,
simp only [prod.mk.eta],
exact h2,
simp only [function.update_noteq s1],
exact ih h1_right h2,
}
},
end
lemma function.update_list_not_mem
{α β : Type}
[decidable_eq α]
(f : α → β)
(l : list (α × β))
(x : α)
(h1 : x ∉ list.map prod.fst l) :
function.update_list f l x = f x :=
begin
induction l,
case list.nil
{
unfold function.update_list,
},
case list.cons : hd tl ih
{
simp only [list.map, list.mem_cons_iff, list.mem_map, prod.exists,
exists_and_distrib_right, exists_eq_right] at h1,
push_neg at h1,
cases h1,
unfold function.update_list,
simp only [function.update_noteq h1_left],
apply ih,
simp only [list.mem_map, prod.exists, exists_and_distrib_right, exists_eq_right, not_exists],
exact h1_right,
},
end
lemma function.update_list_mem_ext
{α β : Type}
[decidable_eq α]
(f g : α → β)
(l : list (α × β))
(x : α)
(h1 : x ∈ list.map prod.fst l) :
function.update_list f l x = function.update_list g l x :=
begin
induction l,
case list.nil
{
simp only [list.map_nil, list.not_mem_nil] at h1,
contradiction,
},
case list.cons : hd tl ih
{
simp only [list.map, list.mem_cons_iff] at h1,
unfold function.update_list,
by_cases c1 : x = hd.fst,
{
rewrite c1,
simp only [function.update_same],
},
{
simp only [function.update_noteq c1],
cases h1,
{
contradiction,
},
{
exact ih h1,
}
},
},
end
lemma function.update_list_zip_mem_ext
{α β : Type}
[decidable_eq α]
(f g : α → β)
(l1 : list α)
(l2 : list β)
(x : α)
(h1 : l1.length ≤ l2.length)
(h2 : x ∈ l1) :
function.update_list f (l1.zip l2) x =
function.update_list g (l1.zip l2) x :=
begin
have s1 : x ∈ list.map prod.fst (l1.zip l2),
rewrite list.map_fst_zip l1 l2 h1,
exact h2,
exact function.update_list_mem_ext f g (list.zip l1 l2) x s1,
end
lemma function.update_list_zip_map_mem_ext
{α β : Type}
[decidable_eq α]
(l1 l2 : list α)
(f g h : α → β)
(x : α)
(h1 : l1.length ≤ l2.length)
(h2 : x ∈ l1) :
function.update_list f (l1.zip (list.map h l2)) x =
function.update_list g (l1.zip (list.map h l2)) x :=
begin
have s1 : l1.length ≤ (list.map h l2).length,
simp only [list.length_map],
exact h1,
exact function.update_list_zip_mem_ext f g l1 (list.map h l2) x s1 h2,
end
lemma function.update_list_zip_map_mem_ext'
{α β : Type}
[decidable_eq α]
(l1 l2 : list α)
(f g h h' : α → β)
(x : α)
(h1 : ∀ (y : α), y ∈ l2 → h y = h' y)
(h2 : l1.length ≤ l2.length)
(h3 : x ∈ l1) :
function.update_list f (l1.zip (list.map h l2)) x =
function.update_list g (l1.zip (list.map h' l2)) x :=
begin
have s1 : list.map h l2 = list.map h' l2,
rewrite list.map_eq_map_iff,
exact h1,
rewrite s1,
exact function.update_list_zip_map_mem_ext l1 l2 f g h' x h2 h3,
end
lemma function.update_list_zip_map_mem
{α β : Type}
[decidable_eq α]
(f g : α → β)
(l : list α)
(x : α)
(h1 : x ∈ l) :
function.update_list f (l.zip (list.map g l)) x = g x :=
begin
induction l,
case list.nil
{
simp only [list.not_mem_nil] at h1,
contradiction,
},
case list.cons : hd tl ih
{
simp only [list.mem_cons_iff] at h1,
simp only [list.map, list.zip_cons_cons],
unfold function.update_list,
by_cases c1 : x = hd,
{
rewrite c1,
simp only [function.update_same],
},
{
cases h1,
{
contradiction,
},
{
simp only [function.update_noteq c1],
exact ih h1,
}
}
},
end
lemma function.update_list_update
{α β : Type}
[decidable_eq α]
(f g : α → β)
(l1 l2 : list α)
(v : α)
(a : β)
(x : α)
(h1 : ∀ (y : α), y ∈ l2 → ¬ y = v)
(h2 : l1.length ≤ l2.length)
(h3 : x ∈ l1) :
function.update_list g (l1.zip (list.map (function.update f v a) l2)) x =
function.update_list f (l1.zip (list.map f l2)) x:=
begin
have s1 : ∀ (y : α), y ∈ l2 → function.update f v a y = f y,
intros y a1,
exact function.update_noteq (h1 y a1) a f,
exact function.update_list_zip_map_mem_ext' l1 l2 g f (function.update f v a) f x s1 h2 h3,
end
lemma function.update_list_nth_le_zip
{α β : Type}
[decidable_eq α]
(f : α → β)
(l1 : list α)
(l2 : list β)
(n : ℕ)
(h1 : n < l1.length)
(h2 : n < l2.length)
(h3 : l1.nodup) :
(function.update_list f (l1.zip l2)) (l1.nth_le n h1) = l2.nth_le n h2 :=
begin
have s1 : (list.map prod.fst (l1.zip l2)).nodup,
exact list.map_fst_zip_nodup l1 l2 h3,
have s2 : (l1.nth_le n h1, l2.nth_le n h2) ∈ l1.zip l2,
exact list.nth_le_mem_zip l1 l2 n h1 h2,
exact function.update_list_mem f (l1.zip l2) (l1.nth_le n h1, l2.nth_le n h2) s1 s2,
end
lemma function.update_list_comp
{α β γ : Type}
[decidable_eq α]
(f : α → β)
(g : β → γ)
(l : list (α × β)) :
g ∘ function.update_list f l =
function.update_list (g ∘ f) (list.map (fun (i : α × β), (i.fst, g i.snd)) l) :=
begin
induction l,
case list.nil
{
unfold list.map,
unfold function.update_list,
},
case list.cons : hd tl ih
{
unfold list.map,
unfold function.update_list,
rewrite function.comp_update,
rewrite ih,
},
end
-- Syntax
abbreviation var_name := string
abbreviation meta_var_name := string
abbreviation def_name := string
@[derive decidable_eq]
inductive formula : Type
| meta_var_ : meta_var_name → formula
| not_ : formula → formula
| imp_ : formula → formula → formula
| eq_ : var_name → var_name → formula
| forall_ : var_name → formula → formula
| def_ : def_name → list var_name → formula
open formula
/-
(v, X) ∈ Γ if and only if v is not free in meta_var_ X.
not_free Γ v φ = v is not free in φ in the context Γ
-/
def not_free (Γ : list (var_name × meta_var_name)) (v : var_name) : formula → Prop
| (meta_var_ X) := (v, X) ∈ Γ
| (not_ φ) := not_free φ
| (imp_ φ ψ) := not_free φ ∧ not_free ψ
| (eq_ x y) := x ≠ v ∧ y ≠ v
| (forall_ x φ) := x = v ∨ not_free φ
| (def_ name args) := ∀ (x : var_name), x ∈ args → ¬ x = v
def formula.meta_var_set : formula → finset meta_var_name
| (meta_var_ X) := {X}
| (not_ φ) := φ.meta_var_set
| (imp_ φ ψ) := φ.meta_var_set ∪ ψ.meta_var_set
| (eq_ x y) := ∅
| (forall_ x φ) := φ.meta_var_set
| (def_ name args) := ∅
/-
True if and only if the formula has no meta variables and all the variables
that occur free in the formula are in the list.
-/
def formula.no_meta_var_and_all_free_in_list : formula → list var_name → Prop
| (meta_var_ X) S := false
| (not_ φ) S := φ.no_meta_var_and_all_free_in_list S
| (imp_ φ ψ) S := φ.no_meta_var_and_all_free_in_list S ∧ ψ.no_meta_var_and_all_free_in_list S
| (eq_ x y) S := x ∈ S ∧ y ∈ S
| (forall_ x φ) S := φ.no_meta_var_and_all_free_in_list (x :: S)
| (def_ name args) S := args ⊆ S
lemma no_meta_var_imp_meta_var_set_is_empty
(φ : formula)
(l : list var_name)
(h1 : φ.no_meta_var_and_all_free_in_list l) :
φ.meta_var_set = ∅ :=
begin
induction φ generalizing l,
case formula.meta_var_ : X l h1
{
unfold formula.no_meta_var_and_all_free_in_list at h1,
contradiction,
},
case formula.not_ : φ φ_ih l h1
{
unfold formula.no_meta_var_and_all_free_in_list at h1,
unfold formula.meta_var_set,
exact φ_ih l h1,
},
case formula.imp_ : φ ψ φ_ih ψ_ih l h1
{
unfold formula.no_meta_var_and_all_free_in_list at h1,
cases h1,
unfold formula.meta_var_set,
rewrite φ_ih l h1_left,
rewrite ψ_ih l h1_right,
exact finset.empty_union ∅,
},
case formula.eq_ : x y l h1
{
unfold formula.no_meta_var_and_all_free_in_list at h1,
unfold formula.meta_var_set,
},
case formula.forall_ : x args φ_ih l h1
{
unfold formula.no_meta_var_and_all_free_in_list at h1,
unfold formula.meta_var_set,
exact φ_ih (x :: l) h1,
},
case formula.def_ : name args l h1
{
unfold formula.no_meta_var_and_all_free_in_list at h1,
unfold formula.meta_var_set,
},
end
/-
A substitution mapping.
A mapping of each variable name to another name.
-/
def instantiation :=
{σ : var_name → var_name // ∃ (σ' : var_name → var_name), σ ∘ σ' = id ∧ σ' ∘ σ = id}
/-
A meta substitution mapping.
A mapping of each meta variable name to a formula.
-/
def meta_instantiation : Type := meta_var_name → formula
def formula.subst (σ : instantiation) (τ : meta_instantiation) : formula → formula
| (meta_var_ X) := τ X
| (not_ φ) := not_ φ.subst
| (imp_ φ ψ) := imp_ φ.subst ψ.subst
| (eq_ x y) := eq_ (σ.1 x) (σ.1 y)
| (forall_ x φ) := forall_ (σ.1 x) φ.subst
| (def_ name args) := def_ name (list.map σ.1 args)
@[derive decidable_eq]
structure definition_ : Type :=
(name : string)
(args : list var_name)
(q : formula)
(nodup : args.nodup)
(nf : q.no_meta_var_and_all_free_in_list args)
@[derive [has_append, has_mem definition_]]
def env : Type := list definition_
def env.nodup_ : env → Prop :=
list.pairwise (fun (d1 d2 : definition_), d1.name = d2.name → d1.args.length = d2.args.length → false)
/-
True if and only if the formula is a meta variable or every definition in the
formula is defined in the environment.
-/
def formula.is_meta_var_or_all_def_in_env (E : env) : formula → Prop
| (meta_var_ _) := true
| (not_ φ) := φ.is_meta_var_or_all_def_in_env
| (imp_ φ ψ) := φ.is_meta_var_or_all_def_in_env ∧ ψ.is_meta_var_or_all_def_in_env
| (eq_ _ _) := true
| (forall_ _ φ) := φ.is_meta_var_or_all_def_in_env
| (def_ name args) :=
∃ (d : definition_), d ∈ E ∧ name = d.name ∧ args.length = d.args.length
def env.well_formed : env → Prop
| list.nil := true
| (d :: E) :=
(∀ (d' : definition_), d' ∈ E → d.name = d'.name → d.args.length = d'.args.length → false)
∧ d.q.is_meta_var_or_all_def_in_env E
∧ env.well_formed E
lemma env_well_formed_imp_nodup
(E : env)
(h1 : E.well_formed) :
E.nodup_ :=
begin
induction E,
case list.nil
{
unfold env.nodup_,
simp only [list.pairwise.nil],
},
case list.cons : hd tl ih
{
unfold env.well_formed at h1,
cases h1,
cases h1_right,
unfold env.nodup_ at ih,
unfold env.nodup_,
simp only [list.pairwise_cons],
split,
{
exact h1_left,
},
{
exact ih h1_right_right,
},
},
end
lemma is_meta_var_or_all_def_in_env_ext
(E E' : env)
(φ : formula)
(h1 : ∃ (E1 : env), E' = E1 ++ E)
(h2 : φ.is_meta_var_or_all_def_in_env E) :
φ.is_meta_var_or_all_def_in_env E' :=
begin
induction E generalizing φ,
case list.nil : φ h2
{
induction φ,
case formula.meta_var_ : X
{
unfold formula.is_meta_var_or_all_def_in_env,
},
case formula.not_ : φ φ_ih
{
unfold formula.is_meta_var_or_all_def_in_env at *,
exact φ_ih h2,
},
case formula.imp_ : φ ψ φ_ih ψ_ih
{
unfold formula.is_meta_var_or_all_def_in_env at *,
cases h2,
split,
{
exact φ_ih h2_left,
},
{
exact ψ_ih h2_right,
}
},
case formula.eq_ : x y
{
unfold formula.is_meta_var_or_all_def_in_env,
},
case formula.forall_ : x φ φ_ih
{
unfold formula.is_meta_var_or_all_def_in_env at *,
exact φ_ih h2,
},
case formula.def_ : name args
{
unfold formula.is_meta_var_or_all_def_in_env at h2,
simp only [list.not_mem_nil, false_and, exists_false] at h2,
contradiction,
},
},
case list.cons : E_hd E_tl E_ih φ h2
{
induction φ,
case formula.meta_var_ : X
{
unfold formula.is_meta_var_or_all_def_in_env,
},
case formula.not_ : φ φ_ih
{
unfold formula.is_meta_var_or_all_def_in_env at *,
exact φ_ih h2,
},
case formula.imp_ : φ ψ φ_ih ψ_ih
{
unfold formula.is_meta_var_or_all_def_in_env at *,
cases h2,
split,
{
exact φ_ih h2_left,
},
{
exact ψ_ih h2_right,
}
},
case formula.eq_ : x y
{
unfold formula.is_meta_var_or_all_def_in_env,
},
case formula.forall_ : x φ φ_ih
{
unfold formula.is_meta_var_or_all_def_in_env at *,
exact φ_ih h2,
},
case formula.def_ : name args
{
apply exists.elim h1,
intros E1 h1_1,
unfold formula.is_meta_var_or_all_def_in_env at h2,
apply exists.elim h2,
intros d h2_1,
cases h2_1,
cases h2_1_left,
{
unfold formula.is_meta_var_or_all_def_in_env,
apply exists.intro E_hd,
rewrite h1_1,
split,
{
simp only [list.mem_append, list.mem_cons_iff, eq_self_iff_true, true_or, or_true],
},
{
rewrite <- h2_1_left,
exact h2_1_right,
},
},
{
have s1 : (∃ (E1 : env), (E' = (E1 ++ E_tl))),
apply exists.intro (E1 ++ [E_hd]),
simp only [list.append_assoc, list.singleton_append],
exact h1_1,
specialize E_ih s1,
apply E_ih,
unfold formula.is_meta_var_or_all_def_in_env,
apply exists.intro d,
split,
{
exact h2_1_left,
},
{
exact h2_1_right,
},
}
},
},
end
lemma def_in_env_imp_is_meta_var_or_all_def_in_env
(E : env)
(d : definition_)
(h1 : E.well_formed)
(h2 : d ∈ E) :
d.q.is_meta_var_or_all_def_in_env E :=
begin
induction E,
case list.nil
{
simp only [list.not_mem_nil] at h2,
contradiction,
},
case list.cons : hd tl ih
{
unfold env.well_formed at h1,
cases h1,
cases h1_right,
apply is_meta_var_or_all_def_in_env_ext tl (hd :: tl),
{
apply exists.intro [hd],
simp only [list.singleton_append],
},
{
cases h2,
{
rewrite h2,
exact h1_right_left,
},
{
exact ih h1_right_right h2,
},
},
},
end
inductive is_conv (E : env) : formula → formula → Prop
| conv_refl (φ : formula) : is_conv φ φ
| conv_symm (φ φ' : formula) :
is_conv φ φ' → is_conv φ' φ
| conv_trans (φ φ' φ'' : formula) :
is_conv φ φ' → is_conv φ' φ'' → is_conv φ φ''
| conv_not (φ φ' : formula) :
is_conv φ φ' → is_conv (not_ φ) (not_ φ')
| conv_imp (φ φ' ψ ψ' : formula) :
is_conv φ φ' → is_conv ψ ψ' → is_conv (imp_ φ ψ) (imp_ φ' ψ')
| conv_forall (x : var_name) (φ φ' : formula) :
is_conv φ φ' → is_conv (forall_ x φ) (forall_ x φ')
| conv_unfold (d : definition_) (σ : instantiation) :
d ∈ E →
is_conv (def_ d.name (d.args.map σ.1)) (d.q.subst σ meta_var_)
def exists_ (x : var_name) (φ : formula) : formula := not_ (forall_ x (not_ φ))
-- (v, X) ∈ Γ if and only if v is not free in meta_var_ X.
-- Δ is a list of hypotheses.
inductive is_proof
(E : env) :
list (var_name × meta_var_name) → list formula → formula → Prop
| hyp (Γ : list (var_name × meta_var_name)) (Δ : list formula)
(φ : formula) :
φ.is_meta_var_or_all_def_in_env E →
φ ∈ Δ → is_proof Γ Δ φ
| mp (Γ : list (var_name × meta_var_name)) (Δ : list formula)
(φ ψ : formula) :
is_proof Γ Δ φ → is_proof Γ Δ (φ.imp_ ψ) → is_proof Γ Δ ψ
| prop_1 (Γ : list (var_name × meta_var_name)) (Δ : list formula)
(φ ψ : formula) :
φ.is_meta_var_or_all_def_in_env E →
ψ.is_meta_var_or_all_def_in_env E →
is_proof Γ Δ (φ.imp_ (ψ.imp_ φ))
| prop_2 (Γ : list (var_name × meta_var_name)) (Δ : list formula)
(φ ψ χ : formula) :
φ.is_meta_var_or_all_def_in_env E →
ψ.is_meta_var_or_all_def_in_env E →
χ.is_meta_var_or_all_def_in_env E →
is_proof Γ Δ ((φ.imp_ (ψ.imp_ χ)).imp_ ((φ.imp_ ψ).imp_ (φ.imp_ χ)))
| prop_3 (Γ : list (var_name × meta_var_name)) (Δ : list formula)
(φ ψ : formula) :
φ.is_meta_var_or_all_def_in_env E →
ψ.is_meta_var_or_all_def_in_env E →
is_proof Γ Δ (((not_ φ).imp_ (not_ ψ)).imp_ (ψ.imp_ φ))
| gen (Γ : list (var_name × meta_var_name)) (Δ : list formula)
(φ : formula) (x : var_name) :
is_proof Γ Δ φ → is_proof Γ Δ (forall_ x φ)
| pred_1 (Γ : list (var_name × meta_var_name)) (Δ : list formula)
(φ ψ : formula) (x : var_name) :
φ.is_meta_var_or_all_def_in_env E →
ψ.is_meta_var_or_all_def_in_env E →
is_proof Γ Δ ((forall_ x (φ.imp_ ψ)).imp_ ((forall_ x φ).imp_ (forall_ x ψ)))
| pred_2 (Γ : list (var_name × meta_var_name)) (Δ : list formula)
(φ : formula) (x : var_name) :
φ.is_meta_var_or_all_def_in_env E →
not_free Γ x φ → is_proof Γ Δ (φ.imp_ (forall_ x φ))
| eq_1 (Γ : list (var_name × meta_var_name)) (Δ : list formula)
(x y : var_name) :
y ≠ x → is_proof Γ Δ (exists_ x (eq_ x y))
| eq_2 (Γ : list (var_name × meta_var_name)) (Δ : list formula)
(x y z : var_name) :
is_proof Γ Δ ((eq_ x y).imp_ ((eq_ x z).imp_ (eq_ y z)))
| thm (Γ Γ' : list (var_name × meta_var_name)) (Δ Δ' : list formula)
(φ : formula) (σ : instantiation) (τ : meta_instantiation) :
(∀ (X : meta_var_name), X ∈ φ.meta_var_set → (τ X).is_meta_var_or_all_def_in_env E) →
(∀ (x : var_name) (X : meta_var_name), (x, X) ∈ Γ → not_free Γ' (σ.1 x) (τ X)) →
(∀ (ψ : formula), ψ ∈ Δ → is_proof Γ' Δ' (ψ.subst σ τ)) →
is_proof Γ Δ φ →
is_proof Γ' Δ' (φ.subst σ τ)
| conv (Γ : list (var_name × meta_var_name)) (Δ : list formula)
(φ φ' : formula) :
φ'.is_meta_var_or_all_def_in_env E →
is_proof Γ Δ φ → is_conv E φ φ' → is_proof Γ Δ φ'
def hash_map.values {α β : Type} [decidable_eq α] (m : hash_map α (fun _, β)) : list β :=
m.entries.map sigma.snd
structure theorem_ : Type :=
(Γ : list (var_name × meta_var_name))
(Δ : list formula)
(φ : formula)
structure proof_env : Type :=
(definition_map : hash_map string (fun _, definition_))
(theorem_map : hash_map string (fun _, theorem_))
def represents (E : env) (S : proof_env) : Prop :=
E.to_finset = S.definition_map.values.to_finset ∧
∀ (name : string) (T : theorem_),
(sigma.mk name T) ∈ S.theorem_map.entries → is_proof E T.Γ T.Δ T.φ
structure context : Type :=
(S : proof_env)
(h1 : ∃ (E : env), E.well_formed ∧ represents E S)
inductive step : Type
| mp : string → string → string → step
def dguard (p : Prop) [decidable p] : option (plift p) :=
if h : p then pure (plift.up h) else failure
open step
def check_step (C : context) : step → option context
| (mp name minor_name major_name) := do
(theorem_.mk Γ_1 Δ_1 φ_1) <- C.S.theorem_map.find minor_name,
(theorem_.mk Γ_2 Δ_2 (imp_ φ_2 ψ_2)) <- C.S.theorem_map.find major_name | none,
(plift.up h_Γ) <- dguard (Γ_1 = Γ_2),
(plift.up h_Δ) <- dguard (Δ_1 = Δ_2),
(plift.up h_φ) <- dguard (φ_1 = φ_2),
let T' := theorem_.mk Γ_2 Δ_2 ψ_2,
let S' : proof_env :=
{
theorem_map := C.S.theorem_map.insert name T',
definition_map := C.S.definition_map
},
let t1 : ∃ (E' : env), E'.well_formed ∧ represents E' S' :=
begin
apply exists.elim C.h1,
intros E h1_1,
cases h1_1,
have s1 : (sigma.mk major_name (theorem_.mk Γ_1 Δ_1 φ_1)) ∈ C.S.theorem_map.entries,
rewrite <- hash_map.find_iff C.S.theorem_map,
sorry,
apply exists.intro E,
split,
{
exact h1_1_left,
},
{
unfold represents at h1_1_right,
cases h1_1_right,
unfold represents,
split,
{
rewrite h1_1_right_left,
},
{
intros T a1,
sorry,
}
}
end,
return {S := S', h1 := t1}
-- Semantics
def valuation (D : Type) : Type := var_name → D
def meta_valuation (D : Type) : Type := meta_var_name → valuation D → Prop
/-
def holds (D : Type) : meta_valuation D → env → formula → valuation D → Prop
| M E (meta_var_ X) V := M X V
| M E (not_ φ) V := ¬ holds M E φ V
| M E (imp_ φ ψ) V := holds M E φ V → holds M E ψ V
| M E (eq_ x y) V := V x = V y
| M E (forall_ x φ) V := ∀ (a : D), holds M E φ (function.update V x a)
| M [] (def_ _ _) V := false
| M (d :: E) (def_ name args) V :=
if name = d.name ∧ args.length = d.args.length
then holds M E d.q (function.update_list V (list.zip d.args (list.map V args)))
else holds M E (def_ name args) V
-/
/-
Lean is unable to determine that the above definition of holds is decreasing,
so it needs to be broken into this pair of mutually recursive definitions.
-/
def holds'
(D : Type)
(M : meta_valuation D)