import Beth.Semantics
import Beth.BethTrees
import Logic.Syntax
import Beth.Coverage
/-!
## Trivial Beth Frame from a Kripke Frame
Any Kripke frame gives rise to a trivial Beth frame with coverage given
by all maximal sieves i.e. the discrete topology
-/
namespace Beth
namespace Examples
open Syntax
universe u
instance bethFrameOfKripke (W : Type u) [F : Kripke.Frame W] : BethFrame W where
coverage := maximalSieveCoverage W
val := F.val
le_val := F.le_val
sheaf_condition := byW : Type u⊢ ∀ {i : ℕ} {w : W} {S : Sieve w}, covers w S → (∀ q ∈ S, Kripke.Frame.val q i) → Kripke.Frame.val w i
intro i w S hS hvalW : Type uw : W
apply hval wW : Type uw : W
show w ∈ S.carrierW : Type uw : W
rw [hS]W : Type uw : W; exact le_refl w
nonempty_covers := byW : Type u
intro w S hS
exact ⟨⟨w, by rw [hS]; exact le_refl w⟩⟩
namespace BinaryBitsTree
section BinaryBitsTree
open BethTrees
/-!
### Binary Bits Tree as a Beth Frame
Binary tree over `Bool` is the `BethTree` where every node is valid.
We have the following valuation:
- `val xs p = (εₛ :> ff ≤ xs)` — "p forced along left branch"
- `val xs q = (εₛ :> tt ≤ xs)` — "q forced along right branch"
- `val l _ = False` — no other variables forced
We can then use a bar at depth 1 to show that p ∨ q is true at the root.
Note that this is not true in the underlying Kripke frame
-/
def fullBinaryTree : BethTree Bool where
tree_node _ := True
root := trivial
prefix_closed _ _ _ := trivial
pruned _l _ := ⟨false, trivial⟩
private lemma nil_prefix {α : Type u} (l : SnocList α) : εₛ ≤ l := by
induction l with
| nil => exact le_refl _
| snoc _ _ ih => exact Or.inr ih
private lemma not_prefix_false_of_const_true (l : SnocList Bool) (n : ℕ)
(h : ¬ (SnocList.nil :> false : SnocList Bool) ≤ l) :
¬ (SnocList.nil :> false : SnocList Bool) ≤ l ++ initial (fun _ => true) n := by
induction n with
| zero => simpa [initial]
| succ n ih =>
simp [initial]
intro hc
rcases hc with hc | hc
· rw [SnocList.snoc.injEq] at hc
apply absurd hc.2
decide
· exact ih hc
private lemma not_prefix_true_of_const_false (l : SnocList Bool) (n : ℕ)
(h : ¬ (SnocList.nil :> true : SnocList Bool) ≤ l) :
¬ (SnocList.nil :> true : SnocList Bool) ≤ l ++ initial (fun _ => false) n := by
induction n with
| zero => simpa [initial]
| succ n ih =>
simp only [initial]
intro hc
rcases hc with hc | hc
· rw [SnocList.snoc.injEq] at hc
exact absurd hc.2 (by decide)
· exact ih hc
def binaryBranchVal : BethVal fullBinaryTree where
val l i := match i with
| 0 => (SnocList.nil :> false) ≤ l
| 1 => (SnocList.nil :> true) ≤ l
| _ => False
mono_val := by
intro l m i hle _ hval
match i with
| 0 => exact SnocList.is_prefix_trans hval hle
| 1 => exact SnocList.is_prefix_trans hval hle
| _ + 2 => exact hval.elim
sheaf_val := by
intro l i _ hbar
match i with
| 0 =>
by_contra h
simp at h
simp at hbar
-- If ¬ εₛ :> ff ≤ l, then the l ++ initial (fun _ => tt) is not barred
obtain ⟨n, hn⟩ := hbar (fun _ => true) trivial (fun _ => trivial)
apply not_prefix_false_of_const_true l n h hn
| 1 =>
by_contra h
simp at h
simp at hbar
-- If ¬ εₛ :> tt ≤ l, then the l ++ initial (fun _ => ff) is not barred
obtain ⟨n, hn⟩ := hbar (fun _ => false) trivial (fun _ => trivial)
exact not_prefix_true_of_const_false l n h hn
| i + 2 =>
simp at hbar
obtain ⟨_, hm, _⟩ := IsBar.nonempty fullBinaryTree hbar trivial
exact hm.elim
instance fullBinaryBethFrame : BethFrame {l : SnocList Bool // fullBinaryTree.tree_node l} :=
BethTreeFrame fullBinaryTree binaryBranchVal
example : at_world root (Tm.or (Tm.var 0) (Tm.var 1)) := by
simp only [at_world]
let S : Sieve root := {
carrier := {w | (εₛ :> false : SnocList Bool) ≤ w.1 ∨
(εₛ :> true : SnocList Bool) ≤ w.1},
bounded := fun _ => nil_prefix _,
upward_closed := by
intro q r hmem r_le_q
simp at hmem
apply hmem.impf
· exact (fun h => SnocList.is_prefix_trans h r_le_q)
· exact (fun h => SnocList.is_prefix_trans h r_le_q)
}
refine ⟨S, ?_, ?_⟩refine_1
· -- The sieve is a bar at nil: at depth 1, f 0 is false or true.
intro f _ _
refine ⟨1, trivial, ?_⟩
-- Reduce nil ++ initial f 1 = nil :> f 0 and unfold set membership
simp only [initial, SnocList.append_snoc, SnocList.append_nil]
-- Goal: nil :> false ≤ nil :> f 0 ∨ nil :> true ≤ nil :> f 0
simp [S]
exact Bool.casesOn (f 0) (Or.inl (Or.inl rfl)) (Or.inr (Or.inl rfl))
· -- Every element of the sieve forces var 0 or var 1.
intro ⟨m, _⟩ hm
rcases hm with h | h
· exact Or.inl h
· exact Or.inr h
end BinaryBitsTree
end BinaryBitsTree
end Examples
end Beth