Skip to content

Commit 52447ae

Browse files
author
Will Bradley
committed
Progress in Formula.lean
1 parent f8f35a1 commit 52447ae

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import LogicFormalization.Chapter2.Section4.Term
2+
3+
inductive Formula.Atomic (L: Language) (V := ULift ℕ)
4+
| true | false
5+
| rel (R: L.ρ) (ts: Fin (arity R) → Term L V)
6+
| eq: Term L V → Term L V → Formula.Atomic L V
7+
8+
inductive Formula (L: Language) (V := ULift ℕ)
9+
| atomic: Formula.Atomic L V → Formula L V
10+
| not: Formula L V → Formula L V
11+
| or: Formula L V → Formula L V → Formula L V
12+
| and: Formula L V → Formula L V → Formula L V
13+
| exists: V → Formula L V → Formula L V
14+
| forall: V → Formula L V → Formula L V
15+
16+
namespace Formula
17+
18+
universe u
19+
variable {V: Type u} {L: Language}
20+
21+
instance : Coe (Formula.Atomic L V) (Formula L V) where
22+
coe := atomic
23+
24+
/-! We take exercise 1 as the *definition* of subformulas. -/
25+
26+
def subformulas : Formula L V → Set (Formula L V)
27+
| φ@(.atomic _) => { φ }
28+
| .not φ => { .not φ } ∪ subformulas φ
29+
| .or φ ψ => { .or φ ψ } ∪ subformulas φ ∪ subformulas ψ
30+
| .and φ ψ => { .and φ ψ } ∪ subformulas φ ∪ subformulas ψ
31+
| .exists x φ => { .exists x φ } ∪ subformulas φ
32+
| .forall x φ => { .forall x φ } ∪ subformulas φ
33+
34+
def occursFreeIn (v: V) : Formula L V → Prop :=
35+
go ∅
36+
where go (context: Set V) : Formula L V → Prop
37+
| .atomic .true | .atomic .false => False
38+
| .atomic (.eq t₁ t₂) =>
39+
(Term.occursIn v t₁ ∨ Term.occursIn v t₂) ∧ v ∉ context
40+
| .atomic (.rel _ ts) =>
41+
∃ i, Term.occursIn v (ts i) ∧ v ∉ context
42+
| .and φ ψ | .or φ ψ => go context φ ∨ go context ψ
43+
| .not φ => go context φ
44+
| .forall x φ | .exists x φ => go (insert x context) φ
45+
46+
abbrev isSentence (φ: Formula L V) :=
47+
∀ v, ¬Formula.occursFreeIn v φ
48+
49+
end Formula
50+
51+
@[reducible]
52+
def Sentence (L: Language) (V := ULift ℕ) :=
53+
{ φ : Formula L V // φ.isSentence }
54+
55+
namespace Formula
56+
57+
universe u
58+
variable {V: Type u} {L: Language}
59+
60+
def AreVarsFor (x : Set V) (φ : Formula L V) :=
61+
∀ v, occursFreeIn v φ → v ∈ x
62+
63+
def replace [DecidableEq V] (φ: Formula L V)
64+
(x: Set V) [DecidablePred (· ∈ x)] (t: x → Term L V): Formula L V :=
65+
match φ with
66+
| .atomic .true | .atomic .false => φ
67+
| .atomic (.rel R ts) => .atomic (.rel R fun i => (ts i).replace x t)
68+
| .atomic (.eq t₁ t₂) => .atomic (.eq (t₁.replace x t) (t₂.replace x t))
69+
| .and φ ψ => .and (replace φ x t) (replace ψ x t)
70+
| .or φ ψ => .or (replace φ x t) (replace ψ x t)
71+
| .not φ => .not (replace φ x t)
72+
| .forall v φ => .forall v (replace φ (x \ {v}) (fun ⟨y, hy⟩ => t ⟨y, hy.left⟩))
73+
| .exists v φ => .exists v (replace φ (x \ {v}) (fun ⟨y, hy⟩ => t ⟨y, hy.left⟩))
74+
75+
/-- Lemma 2.5.1 -/
76+
lemma isSentence_of_replace [DecidableEq V] {φ: Formula L V}
77+
{x: Set V} [DecidablePred (· ∈ x)] {t: x → Term L V}
78+
(hφ: AreVarsFor x φ) (ht: ∀ x, (t x).varFree):
79+
isSentence (replace φ x t) := by
80+
intro v hn
81+
induction φ with
82+
| atomic a =>
83+
match a with
84+
| .true | .false => exact hn
85+
| .rel R ts =>
86+
unfold occursFreeIn occursFreeIn.go at hn
87+
simp [replace] at hn
88+
simp [AreVarsFor, occursFreeIn, occursFreeIn.go] at hφ
89+
sorry
90+
| .eq t₁ t₂ => sorry
91+
| not φ ih => sorry
92+
| or φ ψ ih => sorry
93+
| and φ ψ ih => sorry
94+
| «forall» v' φ ih => sorry
95+
| «exists» v' φ ih => sorry
96+
97+
end Formula
98+
99+
namespace Language
100+
101+
inductive OrName (ϝ C: Type*)
102+
| base: ϝ → OrName ϝ C
103+
| name: C → OrName ϝ C
104+
105+
instance {ϝ} {C} [Arity ϝ] : Arity (OrName ϝ C) where
106+
arity
107+
| .base f => arity f
108+
| .name _ => 0
109+
110+
@[reducible]
111+
def withNames (L: Language) (C: Type*) : Language :=
112+
{ ρ := L.ρ, ϝ := OrName L.ϝ C }
113+
114+
variable {L: Language} {C: Type*}
115+
116+
@[simp]
117+
lemma ρ_withNames: (withNames L C).ρ = L.ρ :=
118+
rfl
119+
120+
@[simp]
121+
lemma ϝ_withNames: (withNames L C).ϝ = OrName L.ϝ C :=
122+
rfl
123+
124+
@[simp]
125+
lemma arity_OrName {ϝ: Type*} [Arity ϝ] {c: C}: arity (F := OrName ϝ C) (.name c) = 0 :=
126+
rfl
127+
128+
end Language
129+
130+
namespace Structure
131+
132+
def withNames {L: Language} {A: Type*} [Nonempty A] (𝒜: Structure L A) (C: Set A := .univ):
133+
Structure (L.withNames C) A where
134+
interpRel := 𝒜.interpRel
135+
interpFun
136+
| .base f => 𝒜.interpFun f
137+
| .name c => fun (_: Fin 0 → A) => c
138+
139+
140+
end Structure

0 commit comments

Comments
 (0)