-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLFPS.lean
More file actions
2270 lines (1782 loc) · 74.7 KB
/
LFPS.lean
File metadata and controls
2270 lines (1782 loc) · 74.7 KB
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 Mathlib.Data.List.Basic
import Mathlib.Tactic
import Mathlib.Data.Finset.Basic
import Mathlib.Tactic.Use
--- Inductive Definition for Terms
inductive Term
| var : String → Term
| placeholder : Term -- Placeholder for generalizing Ext
--- Inductive Definition for Formulas
inductive Formula
| bot : Formula
| var : String → Formula
| and : Formula → Formula → Formula
| or : Formula → Formula → Formula
| imp : Formula → Formula → Formula
| iff : Formula → Formula → Formula
| neg : Formula → Formula
| pred : String → List Term → Formula
| forall_ : String → Formula → Formula
| exists_ : String → Formula → Formula
| diamond : List (String × Nat) → Formula → Formula
| box : List (String × Nat) → Formula → Formula
| eq : Term → Term → Formula
---Warning: this definition alone doesn't block quantifying in, although quantifying in is (importantly) not allowed in the language of logical possibility proposed in LFPS
def strip_arity (ℒ : List (String × Nat)) : List String :=
ℒ.map Prod.fst
--- Decidable Equality for String
instance : DecidableEq String := inferInstance
--- Notation for Modal Operators
notation "◇_{" rs "} " φ => Formula.diamond rs φ
notation "□_{" rs "} " φ => Formula.box rs φ
notation:70 φ " ⋀ " ψ => Formula.and φ ψ
notation:65 φ " ⋁ " ψ => Formula.or φ ψ
notation:60 φ " ⇒ " ψ => Formula.imp φ ψ
notation:55 φ " ⇔ " ψ => Formula.iff φ ψ
notation "!" φ => Formula.neg φ
notation "A" x ", " φ => Formula.forall_ x φ
notation "E" x ", " φ => Formula.exists_ x φ
-- First-order logic domain
universe u
variable {U : Type u} -- U represents the domain of quantification
variable (P Q P1 P2 P3 P4 : U → Prop) -- Unary predicate
variable (R S R1 R2 R3 R4 : U → U → Prop) -- Binary predicate
variable (𝓛 : List (U → Prop)) --list of unary predicates
variable (𝓛₂ : List (U → U → Prop)) --list of binary predicates
-- Defining modal operators for LEAN native propsoition version of my sentences: possibility and necessity content restricted
inductive possibly (ℒ : List String) (P : Prop) : Prop
| intro : P → possibly ℒ P
inductive necessarily (ℒ : List String) (P : Prop) : Prop
| intro : P → necessarily ℒ P
--- BEq Implementation for Term (No change needed)
instance : BEq Term where
beq t1 t2 :=
match t1, t2 with
| Term.var v1, Term.var v2 => v1 == v2
| Term.placeholder, Term.placeholder => true
| _, _ => false
-- Helper Function for Recursively Comparing Formulas
def beq_formula : Formula → Formula → Bool
| Formula.bot, Formula.bot => true
| Formula.var v1, Formula.var v2 => v1 == v2
| Formula.and φ₁ ψ₁, Formula.and φ₂ ψ₂ =>
beq_formula φ₁ φ₂ && beq_formula ψ₁ ψ₂
| Formula.or φ₁ ψ₁, Formula.or φ₂ ψ₂ =>
beq_formula φ₁ φ₂ && beq_formula ψ₁ ψ₂
| Formula.imp φ₁ ψ₁, Formula.imp φ₂ ψ₂ =>
beq_formula φ₁ φ₂ && beq_formula ψ₁ ψ₂
| Formula.neg φ₁, Formula.neg φ₂ =>
beq_formula φ₁ φ₂
| Formula.pred r1 args1, Formula.pred r2 args2 =>
(r1 == r2) && (List.all₂ BEq.beq args1 args2)
| Formula.forall_ x₁ φ₁, Formula.forall_ x₂ φ₂ =>
(x₁ == x₂) && beq_formula φ₁ φ₂
| Formula.exists_ x₁ φ₁, Formula.exists_ x₂ φ₂ =>
(x₁ == x₂) && beq_formula φ₁ φ₂
| Formula.box rels1 φ₁, Formula.box rels2 φ₂ =>
(rels1 == rels2) && beq_formula φ₁ φ₂
| Formula.diamond rels1 φ₁, Formula.diamond rels2 φ₂ =>
(rels1 == rels2) && beq_formula φ₁ φ₂
| _, _ => false
-- BEq Implementation for Formula (Using Helper Function)
instance : BEq Formula where
beq := beq_formula
-- Pretty Printing for Term
instance : ToString Term where
toString t :=
match t with
| Term.var v => v
| Term.placeholder => "_"
--- Pretty Printing for Formula
private def termToString : Term → String
| Term.var v => v
| Term.placeholder => "_"
instance : ToString Formula where
toString :=
let rec aux : Formula → String :=
fun
| Formula.bot => "⊥"
| Formula.var v => v
| Formula.and φ ψ => "(" ++ aux φ ++ " ⋀ " ++ aux ψ ++ ")"
| Formula.or φ ψ => "(" ++ aux φ ++ " ⋁ " ++ aux ψ ++ ")"
| Formula.imp φ ψ => "(" ++ aux φ ++ " ⇒ " ++ aux ψ ++ ")"
| Formula.iff φ ψ => "(" ++ aux φ ++ " ⇔ " ++ aux ψ ++ ")"
| Formula.neg φ => "!" ++ aux φ
| Formula.pred r args =>
r ++ "(" ++ String.intercalate ", " (args.map termToString) ++ ")"
| Formula.forall_ x φ => "A " ++ x ++ ", " ++ aux φ
| Formula.exists_ x φ => "E " ++ x ++ ", " ++ aux φ
| Formula.diamond rs φ =>
"◇_{" ++ String.intercalate ", " (rs.map Prod.fst) ++ "} " ++ aux φ
| Formula.box rs φ =>
"□_{" ++ String.intercalate ", " (rs.map Prod.fst) ++ "} " ++ aux φ
| Formula.eq t₁ t₂ => termToString t₁ ++ " = " ++ termToString t₂
aux
-- Assume U is the type of objects in your domain
-- Uninterpreted Predicate Symbols with Explicit Type Parameter
variable {U : Type}[Inhabited U]
-- Assignment Function for Variables
def Assignment (U : Type U)[Inhabited U] := String → U
-- Now provide an Inhabited instance for Assignment U
instance [Inhabited U] : Inhabited (Assignment U) where
default := λ _ => default -- Maps every variable to the default value of U
-- First, collect all predicate symbols from a formula
def collect_predicates : Formula → List String
| Formula.bot => []
| Formula.var _ => []
| Formula.and φ ψ => collect_predicates φ ++ collect_predicates ψ
| Formula.or φ ψ => collect_predicates φ ++ collect_predicates ψ
| Formula.imp φ ψ => collect_predicates φ ++ collect_predicates ψ
| Formula.iff φ ψ => collect_predicates φ ++ collect_predicates ψ
| Formula.neg φ => collect_predicates φ
| Formula.pred r _ => [r]
| Formula.forall_ _ φ => collect_predicates φ
| Formula.exists_ _ φ => collect_predicates φ
| Formula.diamond _ φ => collect_predicates φ
| Formula.box _ φ => collect_predicates φ
| Formula.eq _ _ => []
-- Collect all predicates from a list of formulas
def collect_predicates_from_list : List Formula → List String
| [] => []
| (φ :: rest) => collect_predicates φ ++ collect_predicates_from_list rest
-- Remove duplicates from a list
def remove_duplicates (xs : List String) : List String :=
xs.foldl (λ acc x => if x ∈ acc then acc else acc ++ [x]) []
-- Transform formula to Lean proposition using Lean predicate variables
def to_lean_prop
{σ : Type} [Inhabited σ]
(P₀ : Formula → Prop)
(P₁ : String → σ → Prop)
(P₂ : String → σ → σ → Prop)
(σ₁ : Assignment σ) : Formula → Prop
| Formula.bot => False
| Formula.var s => P₀ (Formula.var s) -- propositional variables
| Formula.and φ ψ => to_lean_prop P₀ P₁ P₂ σ₁ φ ∧ to_lean_prop P₀ P₁ P₂ σ₁ ψ
| Formula.or φ ψ => to_lean_prop P₀ P₁ P₂ σ₁ φ ∨ to_lean_prop P₀ P₁ P₂ σ₁ ψ
| Formula.imp φ ψ => to_lean_prop P₀ P₁ P₂ σ₁ φ → to_lean_prop P₀ P₁ P₂ σ₁ ψ
| Formula.iff φ ψ => to_lean_prop P₀ P₁ P₂ σ₁ φ ↔ to_lean_prop P₀ P₁ P₂ σ₁ ψ
| Formula.neg φ => ¬ to_lean_prop P₀ P₁ P₂ σ₁ φ
| Formula.pred r [Term.var x] => P₁ r (σ₁ x)
| Formula.pred r [Term.var x, Term.var y] => P₂ r (σ₁ x) (σ₁ y)
| Formula.pred _ _ => False -- fallback for malformed arities
| Formula.forall_ x φ => ∀ (u : σ), to_lean_prop P₀ P₁ P₂ (Function.update σ₁ x u) φ
| Formula.exists_ x φ => ∃ (u : σ), to_lean_prop P₀ P₁ P₂ (Function.update σ₁ x u) φ
| Formula.box ℒ φ => P₀ (Formula.box ℒ φ) -- treat modal formulas as atoms
| Formula.diamond ℒ φ => P₀ (Formula.diamond ℒ φ)
| Formula.eq (Term.var x) (Term.var y) => σ₁ x = σ₁ y
| Formula.eq _ _ => False -- unsupported identity terms
def FOL_equiv_all : Formula → Formula → Prop
| φ, ψ =>
∀ {σ : Type} [Inhabited σ],
∀ (P₀ : Formula → Prop)
(P₁ : String → σ → Prop)
(P₂ : String → σ → σ → Prop)
(σ₁ : Assignment σ),
to_lean_prop P₀ P₁ P₂ σ₁ φ ↔ to_lean_prop P₀ P₁ P₂ σ₁ ψ
--- orList and andList Utility Function
@[simp] def orList : List Formula → Formula
| [] => Formula.bot
| [f] => f
| f :: fs => Formula.or f (orList fs)
@[simp] def andList : List Formula → Formula
| [] => Formula.bot
| [f] => f
| f :: fs => Formula.and f (andList fs)
---forall_many and exists_many utility functions
def Formula.forall_many : List String → Formula → Formula
| [], φ => φ
| x :: xs, φ => Formula.forall_ x (Formula.forall_many xs φ)
@[simp] def Formula.exists_many : List String → Formula → Formula
| [], φ => φ
| x :: xs, φ => Formula.exists_ x (Formula.exists_many xs φ)
-- Manually insert an element at a given index
@[simp] def insertAt {α : Type} (lst : List α) (idx : Nat) (elem : α) : List α :=
let (before, after) := lst.splitAt idx
before ++ [elem] ++ after
-- Utility: Generate a list of variable names ["x0", "x1", ..., "x(n-1)"]
@[simp] def genVars (n : Nat) : List String :=
List.range n |>.map (λ i => "x" ++ toString i)
-- Generalized Ext Function
@[simp] def Ext (ℒ : List (String × Nat)) (y : String) : Formula :=
let fs := ℒ.map (λ (rel, arity) =>
-- Handle relations of arbitrary arity
if arity > 0 then
-- Generate the required variables for quantification
let vars := genVars (arity - 1)
-- Generate disjuncts for each position of y
let disjuncts := List.range arity |>.map (λ j =>
-- Insert y at position j
let args_with_y := insertAt (vars.map Term.var) j (Term.var y)
-- Construct the predicate
let pred := Formula.pred rel args_with_y
pred
)
-- Combine the disjuncts with orList
let combined_disjuncts := orList disjuncts
-- Quantify over all variables except y, starting from the last
vars.reverse.foldl (λ acc x => Formula.exists_ x acc) combined_disjuncts
else
Formula.bot -- Ignore relations of arity 0
)
-- Combine all quantified formulas with orList (now we have a List Formula)
orList fs
-- is_sentence to check if a formula is a sentence (i.e., no free variables)
@[simp] def is_sentence : Formula → List String → Bool
| Formula.bot, _ => true
| Formula.var _, _ => false -- Propositional variables are not sentences
| Formula.and φ ψ, ctx => is_sentence φ ctx && is_sentence ψ ctx
| Formula.or φ ψ, ctx => is_sentence φ ctx && is_sentence ψ ctx
| Formula.imp φ ψ, ctx => is_sentence φ ctx && is_sentence ψ ctx
| Formula.iff φ ψ, ctx => is_sentence φ ctx && is_sentence ψ ctx
| Formula.neg φ, ctx => is_sentence φ ctx
| Formula.pred _ args, ctx =>
args.all (λ t => match t with
| Term.var v => ctx.elem v
| _ => true
)
| Formula.forall_ x φ, ctx =>
is_sentence φ (x :: ctx) -- Correctly adding the bound variable
| Formula.exists_ x φ, ctx =>
is_sentence φ (x :: ctx) -- Same for existential quantifier
| Formula.diamond _ φ, _ => is_sentence φ [] -- Empty context for Diamond
| Formula.box _ φ, _ => is_sentence φ [] -- Empty context for Box
| Formula.eq t₁ t₂, ctx =>
let check_term := λ t => match t with
| Term.var v => ctx.elem v
| _ => true -- placeholder or constants assumed safe
check_term t₁ && check_term t₂
@[simp] def FOL_entails_all (Γ : List Formula) (φ : Formula) : Prop :=
∀ {σ : Type} [Inhabited σ]
(P₀ : Formula → Prop)
(P₁ : String → σ → Prop)
(P₂ : String → σ → σ → Prop)
(σ₁ : Assignment σ),
(∀ γ ∈ Γ, to_lean_prop P₀ P₁ P₂ σ₁ γ) → to_lean_prop P₀ P₁ P₂ σ₁ φ
def appears_free_in (x : String) : Formula → Bool
| Formula.bot => false
| Formula.var v => v = x
| Formula.and φ ψ => appears_free_in x φ || appears_free_in x ψ
| Formula.or φ ψ => appears_free_in x φ || appears_free_in x ψ
| Formula.imp φ ψ => appears_free_in x φ || appears_free_in x ψ
| Formula.iff φ ψ => appears_free_in x φ || appears_free_in x ψ
| Formula.neg φ => appears_free_in x φ
| Formula.pred _ args => args.any (λ t => match t with
| Term.var v => v = x
| _ => false)
| Formula.forall_ y φ => if y = x then false else appears_free_in x φ
| Formula.exists_ y φ => if y = x then false else appears_free_in x φ
| Formula.diamond _ φ => appears_free_in x φ
| Formula.box _ φ => appears_free_in x φ
| Formula.eq t₁ t₂ =>
match t₁, t₂ with
| Term.var v₁, Term.var v₂ => x = v₁ || x = v₂
| Term.var v, _ => x = v
| _, Term.var v => x = v
| _, _ => false
/--
`restrict_formula_explicitly_core ℒ inModal φ` transforms the formula `φ` so that:
- If `inModal = false`, all ∀ and ∃ quantifiers are “content-restricted”
by inserting `Ext ℒ x` into them.
- If `inModal = true`, we do NOT insert content restrictions (since we are
inside a box/diamond).
- For diamonds and boxes, we switch `inModal` to `true` for subformulas.
--/
@[simp] def restrict_formula_explicitly_core
(ℒ : List (String × Nat))
(inModal : Bool)
: Formula → Formula
| Formula.bot => Formula.bot
| Formula.var v => Formula.var v
| Formula.and φ ψ => Formula.and
(restrict_formula_explicitly_core ℒ inModal φ)
(restrict_formula_explicitly_core ℒ inModal ψ)
| Formula.or φ ψ => Formula.or
(restrict_formula_explicitly_core ℒ inModal φ)
(restrict_formula_explicitly_core ℒ inModal ψ)
| Formula.imp φ ψ => Formula.imp
(restrict_formula_explicitly_core ℒ inModal φ)
(restrict_formula_explicitly_core ℒ inModal ψ)
| Formula.iff φ ψ => Formula.iff
(restrict_formula_explicitly_core ℒ inModal φ)
(restrict_formula_explicitly_core ℒ inModal ψ)
| Formula.neg φ =>
Formula.neg (restrict_formula_explicitly_core ℒ inModal φ)
| Formula.pred r args =>
Formula.pred r args -- No change for atomic predicates
| Formula.forall_ x body =>
if inModal then
-- Do not insert content restrictions inside modal subformulas
Formula.forall_ x (restrict_formula_explicitly_core ℒ inModal body)
else
-- Outside modal context: ∀ x φ becomes ∀ x (Ext(ℒ, x) → φ)
Formula.forall_ x (Formula.imp (Ext ℒ x) (restrict_formula_explicitly_core ℒ inModal body))
| Formula.exists_ x body =>
if inModal then
-- Same as above
Formula.exists_ x (restrict_formula_explicitly_core ℒ inModal body)
else
-- Outside modal context: ∃ x φ becomes ∃ x (Ext(ℒ, x) ∧ φ)
Formula.exists_ x (Formula.and (Ext ℒ x) (restrict_formula_explicitly_core ℒ inModal body))
| Formula.diamond rels φ =>
-- Once we enter a diamond, subformulas are in modal context
Formula.diamond rels (restrict_formula_explicitly_core ℒ true φ)
| Formula.box rels φ =>
-- Likewise for box
Formula.box rels (restrict_formula_explicitly_core ℒ true φ)
| Formula.eq t₁ t₂ => Formula.eq t₁ t₂
/--
Main user function:
Applies content restriction to *outer* quantifiers of formula `φ`
by inserting `Ext(ℒ, x)` at each ∀x or ∃x, stopping once we reach
a diamond or box subformula.
--/
@[simp] def restrict_formula_explicitly
(ℒ : List (String × Nat))
(φ : Formula)
: Formula :=
restrict_formula_explicitly_core ℒ false φ
-- Utility Function to Check Subset for Lists
@[simp]def list_subset (ℒ' ℒ : List String) : Bool :=
(ℒ'.toFinset ⊆ ℒ.toFinset)
-- is_content_restricted checks if a formula is content restricted to exactly a list of relations ℒ
@[simp] def is_content_restricted : Formula → List (String × Nat) → List String → Bool
| Formula.bot, _, _ => true -- Clause 1
| Formula.var _, _, _ => false -- Propositional variables are not content-restricted
| Formula.pred r args, ℒ, ctx =>
(r ∈ ℒ.map Prod.fst) && args.all (λ t => match t with
| Term.var v => v ∈ ctx
| _ => true
) -- Clause 3
| Formula.and φ ψ, ℒ, ctx =>
is_content_restricted φ ℒ ctx && is_content_restricted ψ ℒ ctx -- Clause 4
| Formula.or φ ψ, ℒ, ctx =>
is_content_restricted φ ℒ ctx && is_content_restricted ψ ℒ ctx -- Clause 4
| Formula.imp φ ψ, ℒ, ctx =>
is_content_restricted φ ℒ ctx && is_content_restricted ψ ℒ ctx -- Clause 4
| Formula.neg φ, ℒ, ctx =>
is_content_restricted φ ℒ ctx -- Clause 4
| Formula.exists_ x (Formula.and ψ φ), ℒ, ctx =>
-- Call Ext and check if the generated formula matches the first conjunct
let expected_ext := Ext ℒ x
(ψ == expected_ext) && is_content_restricted φ ℒ (x :: ctx)
| Formula.forall_ x (Formula.imp ψ φ), ℒ, ctx =>
-- Call Ext and check if the generated formula matches the antecedent
let expected_ext := Ext ℒ x
(ψ == expected_ext) && is_content_restricted φ ℒ (x :: ctx)
| Formula.diamond ℒ' φ, ℒ, _ =>
ℒ'.all (λ r => r ∈ ℒ) &&
is_sentence φ []
| Formula.box ℒ' φ, ℒ, _ =>
ℒ'.all (λ r => r ∈ ℒ) &&
is_sentence φ []
| _, _, _ => false -- Fallback for non-matching cases "
@[simp] def is_content_restricted_to (ℒ : List (String × Nat)) (φ : Formula) : Prop :=
is_content_restricted φ ℒ []
@[simp] def is_implicitly_content_restricted_to (ℒ : List (String × Nat)) (φ : Formula) : Prop :=
FOL_equiv_all φ (restrict_formula_explicitly ℒ φ) ∧
is_content_restricted_to ℒ (restrict_formula_explicitly ℒ φ)
@[simp] def genIndexedVars (pre : String) (n : Nat) : List String :=
List.range n |>.map (fun i => pre ++ toString i)
--Building Blocks for Simple Comprehension Rule
@[simp] def mentions_relation (r : String) : Formula → Bool
| Formula.bot => false
| Formula.var _ => false
| Formula.and φ ψ => mentions_relation r φ || mentions_relation r ψ
| Formula.or φ ψ => mentions_relation r φ || mentions_relation r ψ
| Formula.imp φ ψ => mentions_relation r φ || mentions_relation r ψ
| Formula.iff φ ψ => mentions_relation r φ || mentions_relation r ψ
| Formula.neg φ => mentions_relation r φ
| Formula.pred r' _ => r = r'
| Formula.forall_ _ φ => mentions_relation r φ
| Formula.exists_ _ φ => mentions_relation r φ
| Formula.diamond _ φ => mentions_relation r φ
| Formula.box _ φ => mentions_relation r φ
| Formula.eq _ _ => false
def instantiate_vars (φ : Formula) (vars : List String) : Formula :=
φ -- eventually this could plug in terms for placeholders
-- Helper: construct the expected comprehension body
def comprehension_body (Ψ φ : Formula) (R : String) (vars : List String) : Formula :=
Formula.and Ψ (
Formula.forall_many vars (
Formula.iff
(Formula.pred R (vars.map Term.var))
φ
)
)
--- Building Blocks for Cutback
-- (∃x) P(x)
@[simp] def cutback_exists (P : String) : Formula :=
Formula.exists_ "x" (Formula.pred P [Term.var "x"])
-- (∀x) Ext(ℒ, x) → P(x)
@[simp] def cutback_ext (ℒ : List (String × Nat)) (P : String) : Formula :=
Formula.forall_ "x" (
Formula.imp (Ext ℒ "x") (Formula.pred P [Term.var "x"])
)
-- (∀x) P(x)
@[simp] def cutback_forall (P : String) : Formula :=
Formula.forall_ "x" (Formula.pred P [Term.var "x"])
--- Building Blocks for Relabeling
@[simp] def replace_relation (r : String) (relabel : List (String × String)) : String :=
match relabel.find? (λ p => p.1 = r) with
| some (_, new_r) => new_r
| none => r
@[simp] def Formula.rename_relations (relabel : List (String × String)) : Formula → Formula
| Formula.bot => Formula.bot
| Formula.var v => Formula.var v
| Formula.and φ ψ => Formula.and (Formula.rename_relations relabel φ) (Formula.rename_relations relabel ψ)
| Formula.or φ ψ => Formula.or (Formula.rename_relations relabel φ) (Formula.rename_relations relabel ψ)
| Formula.imp φ ψ => Formula.imp (Formula.rename_relations relabel φ) (Formula.rename_relations relabel ψ)
| Formula.iff φ ψ => Formula.iff (Formula.rename_relations relabel φ) (Formula.rename_relations relabel ψ)
| Formula.neg φ => Formula.neg (Formula.rename_relations relabel φ)
| Formula.pred r args => Formula.pred (replace_relation r relabel) args
| Formula.forall_ x φ => Formula.forall_ x (Formula.rename_relations relabel φ)
| Formula.exists_ x φ => Formula.exists_ x (Formula.rename_relations relabel φ)
| Formula.diamond ℒ φ => Formula.diamond ℒ (Formula.rename_relations relabel φ)
| Formula.box ℒ φ => Formula.box ℒ (Formula.rename_relations relabel φ)
| Formula.eq t₁ t₂ => Formula.eq t₁ t₂
@[simp] def extract_relabeling_pairs (old new : List String) : Option (List (String × String)) :=
if old.length ≠ new.length then none
else some (old.zip new)
-- Building blocks for Modal Comprehension
/--
Abbreviated “unique existence” in our object language:
∃! x. φ(x) := ∃ x. φ(x) ∧ ∀ x. (φ(x) → x = x)
--/
@[simp] def Formula.exists_unique (x : String) (φ : Formula) : Formula :=
Formula.exists_ x (
Formula.and φ (
Formula.forall_ x (
Formula.imp φ (Formula.eq (Term.var x) (Term.var x))
)
)
)
/--
Abbreviated “unique existence” for n‑tuples:
∃! vars. R(vars)
--/
-- ∃!tuple vars. Q(vars)
@[simp]
def Formula.exists_unique_many (R : String) (n : Nat) : Formula :=
let vars := genVars n
let pred := Formula.pred R (vars.map Term.var)
let exists_clause := Formula.exists_many vars pred -- ∃v0 v1... R(vs)
let vars₁ := vars.map (λ x => x ++ "_1") -- ["v0_1", "v1_1"]
let vars₂ := vars.map (λ x => x ++ "_2") -- ["v0_2", "v1_2"]
let pred₁ := Formula.pred R (vars₁.map Term.var)
let pred₂ := Formula.pred R (vars₂.map Term.var)
let eqs := List.zipWith (λ x y => Formula.eq (Term.var x) (Term.var y)) vars₁ vars₂
let all_eqs := andList eqs
let uniqueness :=
Formula.forall_many (vars₁ ++ vars₂) (
Formula.imp (Formula.and pred₁ pred₂) all_eqs
)
Formula.and exists_clause uniqueness
def unique_witness_formula : Formula :=
Formula.exists_unique_many "witness" 2
#eval unique_witness_formula
def all_in_Ext (ℒ : List (String × Nat)) (vars : List String) : Formula :=
andList (vars.map (λ v => Ext ℒ v))
def restrict_R_to_structure (ℒ : List (String × Nat)) (R : String) (vars : List String) (φ : Formula) : Formula :=
Formula.iff
(Formula.pred R (vars.map Term.var))
(Formula.and (all_in_Ext ℒ vars) φ)
def modal_comprehension_formula
(ℒ : List (String × Nat))
(Ψ φ : Formula)
(R Q : String)
(n : Nat) : Formula :=
let vars := genVars n
let uniqueQ := Formula.exists_unique_many Q n
let inner := Formula.exists_many vars (
Formula.and
(Formula.pred Q (vars.map Term.var))
(restrict_R_to_structure ℒ R vars φ)
)
let boxed := Formula.box (ℒ ++ [(R, n)]) (Formula.imp uniqueQ inner)
Formula.diamond ℒ (Formula.and Ψ boxed)
--- Building Blocks for Infinity
-- Clause 1: ∀x ∀y ∀y'. S(x,y) ∧ S(x,y') → y = y'
def infinity_clause_1 : Formula :=
Formula.forall_many ["x", "y", "y'"] (
Formula.imp
(Formula.and
(Formula.pred "S" [Term.var "x", Term.var "y"])
(Formula.pred "S" [Term.var "x", Term.var "y'"]))
(Formula.eq (Term.var "y") (Term.var "y'"))
)
-- Clause 2: ∀x ∀y ∀x'. S(x,y) ∧ S(x',y) → x = x'
def infinity_clause_2 : Formula :=
Formula.forall_many ["x", "y", "x'"] (
Formula.imp
(Formula.and
(Formula.pred "S" [Term.var "x", Term.var "y"])
(Formula.pred "S" [Term.var "x'", Term.var "y"]))
(Formula.eq (Term.var "x") (Term.var "x'")
))
-- Clause 3: ∃!x ∃y. S(x,y) ∧ ∀y. ¬S(y,x)
def infinity_clause_3 : Formula :=
let inner :=
Formula.exists_ "y" (
Formula.and
(Formula.pred "S" [Term.var "x", Term.var "y"])
(Formula.forall_ "y" (Formula.neg (Formula.pred "S" [Term.var "y", Term.var "x"])))
)
Formula.exists_ "x" inner -- ∃! could be mimicked if needed, but ∃ works here
-- Clause 4: ∀x. (∃y. S(y,x)) → (∃z. S(x,z))
def infinity_clause_4 : Formula :=
Formula.forall_ "x" (
Formula.imp
(Formula.exists_ "y" (Formula.pred "S" [Term.var "y", Term.var "x"]))
(Formula.exists_ "z" (Formula.pred "S" [Term.var "x", Term.var "z"]))
)
-- Clause 5: ∀x ∀y. S(x,y) → ¬S(y,x)
def infinity_clause_5 : Formula :=
Formula.forall_many ["x", "y"] (
Formula.imp
(Formula.pred "S" [Term.var "x", Term.var "y"])
(Formula.neg (Formula.pred "S" [Term.var "y", Term.var "x"]))
)
def infinity_axiom_body : Formula :=
andList [
infinity_clause_1,
infinity_clause_2,
infinity_clause_3,
infinity_clause_4,
infinity_clause_5
]
def infinity_axiom_modal : Formula :=
Formula.diamond [("S", 2)] infinity_axiom_body
--- Building Blocks for Possible Powerset
-- Clause 1: ∀x. ¬(C(x) ∧ F(x))
def powerset_clause_1 (F C : String) : Formula :=
Formula.forall_ "x" (
Formula.neg (
Formula.and
(Formula.pred C [Term.var "x"])
(Formula.pred F [Term.var "x"])
)
)
-- Clause 2: ∀x ∀y. (x ∈_C y) → (F(x) ∧ C(y))
def powerset_clause_2 (F C mem : String) : Formula :=
Formula.forall_many ["x", "y"] (
Formula.imp
(Formula.pred mem [Term.var "x", Term.var "y"])
(Formula.and
(Formula.pred F [Term.var "x"])
(Formula.pred C [Term.var "y"]))
)
-- Clause 3: □_{F,C,mem}(∃x. C(x) ∧ ∀y. ((F(y) ∧ K(y)) ↔ y ∈_C x))
def powerset_clause_3 (F C mem : String) : Formula :=
let inner :=
Formula.exists_ "x" (
Formula.and
(Formula.pred C [Term.var "x"])
(Formula.forall_ "y" (
Formula.iff
(Formula.and
(Formula.pred F [Term.var "y"])
(Formula.pred "K" [Term.var "y"]))
(Formula.pred mem [Term.var "y", Term.var "x"])
))
)
Formula.box [(F,1), (C,1), (mem,2)] inner
-- Clause 4: ∀y ∀y'. (C(y) ∧ C(y') ∧ ¬(y = y')) → ∃x. ¬(x ∈_C y ↔ x ∈_C y')
def powerset_clause_4 (C mem : String) : Formula :=
Formula.forall_many ["y", "y'"] (
Formula.imp
(Formula.and
(Formula.and
(Formula.pred C [Term.var "y"])
(Formula.pred C [Term.var "y'"]))
(Formula.neg (Formula.eq (Term.var "y") (Term.var "y'"))))
(Formula.exists_ "x" (
Formula.neg (
Formula.iff
(Formula.pred mem [Term.var "x", Term.var "y"])
(Formula.pred mem [Term.var "x", Term.var "y'"])
))
))
def possible_powerset_body (F C mem : String) : Formula :=
andList [
powerset_clause_1 F C,
powerset_clause_2 F C mem,
powerset_clause_3 F C mem,
powerset_clause_4 C mem
]
-- Building blocks for Choice
-- Unique existence: ∃! y. φ(y) := ∃ y. φ(y) ∧ ∀ y' (φ(y') → y' = y)
def exists_unique (vars : List String) (φ : Formula) : Formula :=
match vars with
| [] => φ
| y :: ys =>
Formula.exists_ y (
Formula.and φ (
Formula.forall_ y (
Formula.imp φ (Formula.eq (Term.var y) (Term.var y))
)
)
) -- (simplified version — for now we assume m = 1)
-- Core body of the choice axiom
def choice_axiom_body
(n m : Nat)
(I R R_hat : String)
(φ : Formula) : Formula :=
let x_vars := List.range n |>.map (fun i => "x" ++ toString i)
let y_vars := List.range m |>.map (fun j => "y" ++ toString j)
let ext_tuple := x_vars ++ y_vars
let totality_clause :=
Formula.forall_many x_vars (
Formula.imp
(Formula.pred I (x_vars.map Term.var))
(Formula.exists_many y_vars (
Formula.pred R (ext_tuple.map Term.var)
))
)
let inclusion_clause :=
Formula.forall_many ext_tuple (
Formula.imp
(Formula.pred R_hat (ext_tuple.map Term.var))
(Formula.pred R (ext_tuple.map Term.var))
)
let uniqueness_clause :=
Formula.forall_many x_vars (
Formula.imp
(Formula.pred I (x_vars.map Term.var))
(Formula.exists_unique_many (R_hat) (n + m)) -- applies to full arity
)
Formula.and φ (Formula.and totality_clause (Formula.and inclusion_clause uniqueness_clause))
def choice_axiom_modal
(L : List (String × Nat))
(n m : Nat)
(I R R_hat : String)
(φ : Formula) : Formula :=
Formula.diamond (L ++ [(I, n), (R, n + m)]) (choice_axiom_body n m I R R_hat φ)
namespace Formula
/-- Helper recursive function to track bound variables -/
def free_vars_core : Formula → List String → List String
| Formula.bot, _ => []
| Formula.var v, ctx => if v ∈ ctx then [] else [v]
| Formula.and φ ψ, ctx => free_vars_core φ ctx ++ free_vars_core ψ ctx
| Formula.or φ ψ, ctx => free_vars_core φ ctx ++ free_vars_core ψ ctx
| Formula.imp φ ψ, ctx => free_vars_core φ ctx ++ free_vars_core ψ ctx
| Formula.iff φ ψ, ctx => free_vars_core φ ctx ++ free_vars_core ψ ctx
| Formula.neg φ, ctx => free_vars_core φ ctx
| Formula.pred _ args, ctx =>
args.foldl (fun acc t =>
match t with
| Term.var v => if v ∈ ctx then acc else v :: acc
| _ => acc
) []
| Formula.forall_ x φ, ctx => free_vars_core φ (x :: ctx)
| Formula.exists_ x φ, ctx => free_vars_core φ (x :: ctx)
| Formula.diamond _ φ, _ => free_vars_core φ [] -- modal formulas are full sentences
| Formula.box _ φ, _ => free_vars_core φ [] -- likewise
| Formula.eq t₁ t₂, ctx =>
let collect := λ t => match t with
| Term.var v => if v ∈ ctx then [] else [v]
| _ => []
collect t₁ ++ collect t₂
/-- Public function: computes the list of free variables in a formula -/
def free_vars (φ : Formula) : List String :=
(free_vars_core φ []).eraseDups
end Formula
--Building blocks for comprehension
namespace Formula
/--
Helper for "big disjunction" in disjoint slices
--/
def big_or : List Formula → Formula
| [] => Formula.bot
| [f] => f
| (f::fs) => Formula.or f (big_or fs)
/--
Given a list of old relations Rs and a list of corresponding new slice relations Rhats,
rewrite a formula by replacing each occurrence of Rᵢ(t₁, …, tₙ) with R̂ᵢ(t₁, …, tₙ, x).
That is, each Rᵢ is mapped to R̂ᵢ, and the indexing term (usually x) is appended at the end.
Assumes Rs and Rhats are aligned lists of same length.
We always add Term.var "x" as the index.
map_relations : Apply a function to every atomic relation occurrence inside a formula.
Traverse a Formula, replacing each atomic relation r(args)
with (f r args).
f : takes a relation name `r` and its argument list, returns a new Formula.
--/
def map_relations (f : String → List Term → Formula) : Formula → Formula
| Formula.bot => Formula.bot
| Formula.var v => Formula.var v
| Formula.pred r ts => f r ts
| Formula.eq t1 t2 => Formula.eq t1 t2
| Formula.neg φ => Formula.neg (map_relations f φ)
| Formula.and φ ψ => Formula.and (map_relations f φ) (map_relations f ψ)
| Formula.or φ ψ => Formula.or (map_relations f φ) (map_relations f ψ)
| Formula.imp φ ψ => Formula.imp (map_relations f φ) (map_relations f ψ)
| Formula.iff φ ψ => Formula.iff (map_relations f φ) (map_relations f ψ)
| Formula.forall_ x φ => Formula.forall_ x (map_relations f φ)
| Formula.exists_ x φ => Formula.exists_ x (map_relations f φ)
| Formula.box ℒ φ => Formula.box ℒ (map_relations f φ)
| Formula.diamond ℒ φ => Formula.diamond ℒ (map_relations f φ)
/--
Substitute free occurrences of variable `old` with `new` in a Term.
--/
def Term.substitute (old new : String) : Term → Term
| Term.var v => if v = old then Term.var new else Term.var v
| t => t
/--
Substitute free occurrences of variable `old` with `new` in a Formula.
(Avoids capture: does not recurse under a binder for the same variable.)
--/
def substitute_var (φ : Formula) (old new : String) : Formula :=
match φ with
| Formula.bot => Formula.bot
| Formula.var v => Formula.var v
| Formula.pred r args => Formula.pred r (args.map (Term.substitute old new))
| Formula.eq t1 t2 => Formula.eq (Term.substitute old new t1) (Term.substitute old new t2)
| Formula.neg φ₁ => Formula.neg (Formula.substitute_var φ₁ old new)
| Formula.and φ₁ φ₂ => Formula.and (Formula.substitute_var φ₁ old new) (Formula.substitute_var φ₂ old new)
| Formula.or φ₁ φ₂ => Formula.or (Formula.substitute_var φ₁ old new) (Formula.substitute_var φ₂ old new)
| Formula.imp φ₁ φ₂ => Formula.imp (Formula.substitute_var φ₁ old new) (Formula.substitute_var φ₂ old new)
| Formula.iff φ₁ φ₂ => Formula.iff (Formula.substitute_var φ₁ old new) (Formula.substitute_var φ₂ old new)
| Formula.forall_ x φ₁ => if x = old then Formula.forall_ x φ₁ else Formula.forall_ x (Formula.substitute_var φ₁ old new)
| Formula.exists_ x φ₁ => if x = old then Formula.exists_ x φ₁ else Formula.exists_ x (Formula.substitute_var φ₁ old new)
| Formula.box ℒ φ₁ => Formula.box ℒ (Formula.substitute_var φ₁ old new)
| Formula.diamond ℒ φ₁ => Formula.diamond ℒ (Formula.substitute_var φ₁ old new)
def rename_relations_with_index (φ : Formula) (Rs Rhats : List (String × Nat)) : Formula :=
φ.map_relations (λ r args =>
match List.findIdx? (λ (p : String × Nat) => p.fst = r) Rs with
| some idx =>
let new_rhat := Rhats.get! idx
Formula.pred new_rhat.fst ( [Term.var "x"] ++ args ) -- append index x
| none => Formula.pred r args -- if relation not among Rs, leave untouched
)
/--
Builds the general amalgamation possibility sentence:
◇_L ( (∀x (I(x) → φ'(x))) ∧ (∀x y z (I(x) ∧ I(y) ∧ x ≠ y ∧ (⋁₁ⁿ (R'ᵢ(z,x) ∧ R'ᵢ(z,y))) → Ext L z)) )
where:
- L is the background structure,
- I is the indexing predicate (must appear in L),
- Rs are the original relations appearing in φ,
- R̂s are the new slice-relations,
- φ is the original one-place formula.
φ' is obtained from φ with each R_i replaced by Rhat_i(...,x), adding the slice index x appropriately.
--/
def amalgamation_axiom
(L : List (String × Nat))
(I : String)
(Rs : List (String × Nat))
(Rhats : List (String × Nat))
(φ : Formula)
: Formula :=
let φ' := φ.rename_relations_with_index Rs Rhats;
let per_x : Formula :=
Formula.forall_ "x" (
Formula.imp
(Formula.pred I [Term.var "x"])
φ'
);
let disjoint_slices : Formula :=
Formula.forall_many ["x","y","z"] (
Formula.imp
( Formula.and
(Formula.pred I [Term.var "x"])
( Formula.and
(Formula.pred I [Term.var "y"])
( Formula.and
(Formula.neg (Formula.eq (Term.var "x") (Term.var "y")))
( big_or (
List.map (λ r =>
Formula.and
(Formula.pred r.fst [Term.var "z", Term.var "x"])
(Formula.pred r.fst [Term.var "z", Term.var "y"]))
Rhats)
)
)
)
)
(Ext L "z")
);
Formula.diamond L (Formula.and per_x disjoint_slices)
end Formula
---- PROOF RULES START HERE
inductive NDProof : List Formula → Formula → Type where
-- Assumption
| assumption {Γ : List Formula} {φ : Formula}
(h : φ ∈ Γ) : NDProof Γ φ
-- Conjunction Introduction
| andI {Γ φ ψ} (h1 : NDProof Γ φ) (h2 : NDProof Γ ψ) : NDProof Γ (Formula.and φ ψ)