2026-04-19 04:17:45 +00:00
|
|
|
|
import BidirTT.Value
|
|
|
|
|
|
|
|
|
|
|
|
namespace BidirTT
|
|
|
|
|
|
|
2026-04-19 17:35:19 +00:00
|
|
|
|
abbrev CxtEntry := Name × Val
|
|
|
|
|
|
|
2026-04-19 04:17:45 +00:00
|
|
|
|
structure Cxt where
|
|
|
|
|
|
env : Env
|
2026-04-19 17:35:19 +00:00
|
|
|
|
types : List CxtEntry
|
2026-04-19 04:17:45 +00:00
|
|
|
|
lvl : Lvl
|
2026-04-19 17:35:19 +00:00
|
|
|
|
envWf : env.length = lvl
|
|
|
|
|
|
typesWf : types.length = lvl
|
|
|
|
|
|
|
|
|
|
|
|
def Cxt.empty : Cxt := ⟨[], [], 0, rfl, rfl⟩
|
2026-04-19 04:17:45 +00:00
|
|
|
|
|
2026-04-19 17:35:19 +00:00
|
|
|
|
instance : Inhabited Cxt := ⟨Cxt.empty⟩
|
2026-04-19 04:17:45 +00:00
|
|
|
|
|
|
|
|
|
|
def Cxt.bind (cxt : Cxt) (x : Name) (a : Val) : Cxt :=
|
2026-04-19 15:03:40 +00:00
|
|
|
|
{ env := .neu (.var cxt.lvl) :: cxt.env
|
2026-04-19 04:17:45 +00:00
|
|
|
|
, types := (x, a) :: cxt.types
|
2026-04-19 17:35:19 +00:00
|
|
|
|
, lvl := cxt.lvl + 1
|
|
|
|
|
|
, envWf := by simp [cxt.envWf]
|
|
|
|
|
|
, typesWf := by simp [cxt.typesWf] }
|
2026-04-19 04:17:45 +00:00
|
|
|
|
|
|
|
|
|
|
def Cxt.define (cxt : Cxt) (x : Name) (v a : Val) : Cxt :=
|
|
|
|
|
|
{ env := v :: cxt.env
|
|
|
|
|
|
, types := (x, a) :: cxt.types
|
2026-04-19 17:35:19 +00:00
|
|
|
|
, lvl := cxt.lvl + 1
|
|
|
|
|
|
, envWf := by simp [cxt.envWf]
|
|
|
|
|
|
, typesWf := by simp [cxt.typesWf] }
|
|
|
|
|
|
|
|
|
|
|
|
private def castFin {n m : Nat} (h : n = m) (i : Fin n) : Fin m :=
|
|
|
|
|
|
⟨i.val, h ▸ i.isLt⟩
|
|
|
|
|
|
|
|
|
|
|
|
private def lookupGo (x : Name) : (entries : List CxtEntry) → Option (Fin entries.length × Val)
|
|
|
|
|
|
| [] => none
|
|
|
|
|
|
| (y, a) :: rest =>
|
|
|
|
|
|
if x == y then
|
|
|
|
|
|
some (⟨0, by simp⟩, a)
|
|
|
|
|
|
else
|
|
|
|
|
|
match lookupGo x rest with
|
|
|
|
|
|
| some (i, a) =>
|
|
|
|
|
|
some (⟨i.val + 1, by simp [i.isLt]⟩, a)
|
|
|
|
|
|
| none =>
|
|
|
|
|
|
none
|
|
|
|
|
|
|
|
|
|
|
|
def Cxt.isWellFormed (cxt : Cxt) : Bool :=
|
|
|
|
|
|
cxt.env.length == cxt.lvl && cxt.types.length == cxt.lvl
|
|
|
|
|
|
|
|
|
|
|
|
def Cxt.typeAt (cxt : Cxt) (idx : Fin cxt.lvl) : CxtEntry :=
|
|
|
|
|
|
cxt.types.get (castFin cxt.typesWf.symm idx)
|
|
|
|
|
|
|
|
|
|
|
|
def Cxt.valueAt (cxt : Cxt) (idx : Fin cxt.lvl) : Val :=
|
|
|
|
|
|
cxt.env.get (castFin cxt.envWf.symm idx)
|
|
|
|
|
|
|
|
|
|
|
|
def Cxt.lookup (cxt : Cxt) (x : Name) : Option (Fin cxt.lvl × Val) :=
|
|
|
|
|
|
match lookupGo x cxt.types with
|
|
|
|
|
|
| some (idx, a) =>
|
|
|
|
|
|
some (castFin cxt.typesWf idx, a)
|
|
|
|
|
|
| none =>
|
|
|
|
|
|
none
|
2026-04-19 04:17:45 +00:00
|
|
|
|
|
|
|
|
|
|
end BidirTT
|