leanfran::lean! {
pub mod proved;
r#"
inductive Namespace where
| value
| function
deriving DecidableEq, Repr
inductive Entry where
| binder (ns : Namespace) (name : String) (id : Nat)
| special (name : String)
deriving DecidableEq, Repr
inductive Resolution where
| lexical (id : Nat)
| dynamic (id : Nat)
| free
deriving DecidableEq, Repr
structure Site where
chain : List Entry
ns : Namespace
name : String
deriving DecidableEq, Repr
def declaredSpecial (name : String) : List Entry → Bool
| [] => false
| .special n :: rest => n == name || declaredSpecial name rest
| .binder _ _ _ :: rest => declaredSpecial name rest
theorem declaredSpecial_iff (name : String) (chain : List Entry) :
declaredSpecial name chain = chain.contains (.special name) := by
induction chain with
| nil => rfl
| cons e rest ih =>
cases e with
| binder ns n i => simp [declaredSpecial, ih]
| special n =>
simp only [declaredSpecial, ih, List.contains_cons]
by_cases h : n = name
· simp [h]
· have h' : (Entry.special name == Entry.special n) = false := by simp [Ne.symm h]
have h'' : (n == name) = false := by simp [h]
rw [h', h'']
def resolveSymbol (lexical : Bool) (globals : List String) :
List Entry → Namespace → String → Resolution
| [], _, _ => .free
| .special _ :: rest, ns, name => resolveSymbol lexical globals rest ns name
| .binder bns bname id :: rest, ns, name =>
if bns = ns ∧ bname = name then
if ns = .value ∧ (!lexical || globals.contains name || declaredSpecial name rest) then
.dynamic id
else .lexical id
else resolveSymbol lexical globals rest ns name
def renameEntry (id : Nat) (newName : String) : Entry → Entry
| .binder ns n i => if i = id then .binder ns newName i else .binder ns n i
| .special n => .special n
def renameSite (lexical : Bool) (globals : List String) (id : Nat) (newName : String)
(site : Site) : Site :=
{ chain := site.chain.map (renameEntry id newName),
ns := site.ns,
name :=
if resolveSymbol lexical globals site.chain site.ns site.name = .lexical id then newName
else site.name }
def keepsResolution (lexical : Bool) (globals : List String) (id : Nat) (newName : String)
(site : Site) : Bool :=
let before := resolveSymbol lexical globals site.chain site.ns site.name
let after := renameSite lexical globals id newName site
before != .dynamic id && resolveSymbol lexical globals after.chain after.ns after.name == before
def canLocalRename (lexical : Bool) (globals : List String) (id : Nat) (newName : String)
(sites : List Site) : Bool :=
sites.all (keepsResolution lexical globals id newName)
theorem namespaces_are_independent (lexical : Bool) (globals : List String)
(chain : List Entry) (name : String) (id : Nat) :
resolveSymbol lexical globals (.binder .function name id :: chain) .value name =
resolveSymbol lexical globals chain .value name ∧
resolveSymbol lexical globals (.binder .value name id :: chain) .function name =
resolveSymbol lexical globals chain .function name := by
simp [resolveSymbol]
theorem innermost_binder_wins (lexical : Bool) (globals : List String) (chain : List Entry)
(ns : Namespace) (name : String) (id : Nat) :
resolveSymbol lexical globals (.binder ns name id :: chain) ns name = .lexical id ∨
resolveSymbol lexical globals (.binder ns name id :: chain) ns name = .dynamic id := by
simp only [resolveSymbol, and_self, ite_true]
split <;> simp
theorem other_names_are_skipped (lexical : Bool) (globals : List String) (chain : List Entry)
(ns bns : Namespace) (name bname : String) (id : Nat) (h : bname ≠ name) :
resolveSymbol lexical globals (.binder bns bname id :: chain) ns name =
resolveSymbol lexical globals chain ns name := by
simp [resolveSymbol, h]
theorem function_bindings_are_lexical (lexical : Bool) (globals : List String)
(chain : List Entry) (name : String) (id : Nat) :
resolveSymbol lexical globals (.binder .function name id :: chain) .function name =
.lexical id := by
simp [resolveSymbol]
theorem declared_special_binding_is_dynamic (lexical : Bool) (globals : List String)
(chain : List Entry) (name : String) (id : Nat) (h : declaredSpecial name chain = true) :
resolveSymbol lexical globals (.binder .value name id :: chain) .value name = .dynamic id := by
simp [resolveSymbol, h]
theorem later_declaration_does_not_apply (lexical : Bool) (globals : List String)
(chain : List Entry) (name : String) (id : Nat) :
resolveSymbol lexical globals (.special name :: .binder .value name id :: chain) .value name =
resolveSymbol lexical globals (.binder .value name id :: chain) .value name := by
simp [resolveSymbol]
theorem undeclared_lexical_binding (globals : List String) (chain : List Entry)
(name : String) (id : Nat) (hg : name ∉ globals)
(hd : declaredSpecial name chain = false) :
resolveSymbol true globals (.binder .value name id :: chain) .value name = .lexical id := by
simp [resolveSymbol, hg, hd]
theorem dynamic_files_bind_values_dynamically (globals : List String) (chain : List Entry)
(name : String) (id : Nat) :
resolveSymbol false globals (.binder .value name id :: chain) .value name = .dynamic id := by
simp [resolveSymbol]
theorem can_local_rename_iff (lexical : Bool) (globals : List String) (id : Nat)
(newName : String) (sites : List Site) :
canLocalRename lexical globals id newName sites = true ↔
∀ site ∈ sites,
resolveSymbol lexical globals site.chain site.ns site.name ≠ .dynamic id ∧
resolveSymbol lexical globals (renameSite lexical globals id newName site).chain site.ns
(renameSite lexical globals id newName site).name =
resolveSymbol lexical globals site.chain site.ns site.name := by
simp only [canLocalRename, List.all_eq_true, keepsResolution, Bool.and_eq_true, bne_iff_ne,
ne_eq, beq_iff_eq]
constructor
· intro h site hs
exact ⟨(h site hs).1, by simpa [renameSite] using (h site hs).2⟩
· intro h site hs
exact ⟨(h site hs).1, by simpa [renameSite] using (h site hs).2⟩
theorem renamed_sites_follow_the_binder (lexical : Bool) (globals : List String) (id : Nat)
(newName : String) (site : Site)
(h : resolveSymbol lexical globals site.chain site.ns site.name = .lexical id) :
(renameSite lexical globals id newName site).name = newName := by
simp [renameSite, h]
theorem other_sites_keep_their_name (lexical : Bool) (globals : List String) (id : Nat)
(newName : String) (site : Site)
(h : resolveSymbol lexical globals site.chain site.ns site.name ≠ .lexical id) :
(renameSite lexical globals id newName site).name = site.name := by
simp [renameSite, h]
def letChain : List Entry := [.binder .value "f" 0]
theorem lisp2_example :
resolveSymbol true [] letChain .value "f" = .lexical 0 ∧
resolveSymbol true [] letChain .function "f" = .free := by
decide
def captureSites : List Site :=
[⟨[.binder .value "x" 0], .value, "x"⟩,
⟨[.binder .value "y" 1, .binder .value "x" 0], .value, "x"⟩]
theorem capture_is_rejected :
canLocalRename true [] 0 "y" captureSites = false ∧
canLocalRename true [] 0 "z" captureSites = true := by
decide
theorem other_namespace_does_not_capture :
canLocalRename true [] 0 "g"
[⟨[.binder .function "g" 1, .binder .value "x" 0], .value, "x"⟩,
⟨[.binder .function "g" 1, .binder .value "x" 0], .function, "g"⟩] = true := by
decide
theorem special_new_name_is_rejected :
canLocalRename true ["y"] 0 "y" [⟨[.binder .value "x" 0], .value, "x"⟩] = false ∧
canLocalRename false [] 0 "y" [⟨[.binder .value "x" 0], .value, "x"⟩] = false := by
decide
"#
}
pub use proved::{Entry, Namespace, Resolution, Site};
pub fn binder(ns: Namespace, name: &str, id: usize) -> Entry {
Entry::Binder {
ns,
name: name.to_owned(),
id: (id as u64).into(),
}
}
pub fn resolve(
lexical: bool,
globals: &[String],
chain: &[Entry],
ns: Namespace,
name: &str,
) -> Resolution {
proved::resolve_symbol(lexical, globals.to_vec(), chain.to_vec(), ns, name)
}
pub fn can_local_rename(
lexical: bool,
globals: &[String],
id: usize,
new_name: &str,
sites: Vec<Site>,
) -> bool {
proved::can_local_rename(
lexical,
globals.to_vec(),
(id as u64).into(),
new_name,
sites,
)
}
pub fn binder_id(resolution: &Resolution) -> Option<usize> {
match resolution {
Resolution::Lexical { id } | Resolution::Dynamic { id } => Some(
usize::try_from(u64::try_from(id.clone()).expect("an id fits in u64"))
.expect("an id fits in usize"),
),
Resolution::Free => None,
}
}