diff --git a/CHANGELOG.md b/CHANGELOG.md index 7333309..54b4493 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,11 @@ All changes that a user can see are in this file. The format follows Keep a Chan - `rulean-store` has the term store of spec 04: `u32` handles with the kind in the handle, one node array for each kind, side records with the hash, the loose bound variable range and the flags, and hash-consing with an open-addressing table. It also interns strings, names, levels, level lists and natural number literals. - `rulean-format` has the lean4export NDJSON 3.x parser of spec 07.1. It is a hand-written scanner that writes into the term store and returns the declaration stream of spec 04.7. It checks the input rules of 07.1.2: index tables with a defined bit, no duplicate keys or indices, strict integers, UTF-8 and surrogate pairs, `natVal` form and bound, and `mdata` stripping. All 225 Arena tests parse. - `rulean-core` has the universe level functions of spec 05.6, as a transcription of `level.cpp` of the official kernel v4.35.0: `mk_max` and `mk_imax` with their simplifications, `normalize` with a cache for each level handle, `is_equivalent`, `is_geq`, `normalizes_to_zero`, `instantiate` and the name order of the kernel. +- `rulean-core` has the expression operations of engine R, as a transcription of `instantiate.cpp`, `abstract.cpp`, `expr.cpp` and `expr_eq_fn.cpp` of the official kernel v4.35.0: `instantiate`, `instantiate_rev`, `abstract_fvars`, `lift_loose_bvars`, `lower_loose_bvars`, `has_loose_bvar`, `instantiate_lparams`, the application spine functions, `head_beta`, `cheap_beta_reduce` and `expr_equal`. Each operation has a cache keyed by the node and the binder offset. + +### Changed + +- The store hash of a `lam`, `pi` or `let` node does not mix the binder name or the binder info. This is the method of Lean. Alpha-equivalent terms now have equal hashes, so `expr_equal` can use the hash to reject unequal terms. ### Fixed diff --git a/crates/rulean-core/src/expr.rs b/crates/rulean-core/src/expr.rs new file mode 100644 index 0000000..27770b0 --- /dev/null +++ b/crates/rulean-core/src/expr.rs @@ -0,0 +1,688 @@ +//! Operations on expressions: instantiation, abstraction, lifting, level instantiation, application spines, beta reduction and alpha-equality. +//! +//! This module is a transcription of `instantiate.cpp`, `abstract.cpp`, the loose bound variable part of `expr.cpp` and `expr_eq_fn.cpp` of the official kernel v4.35.0. +//! +//! The official functions use `replace` with a cache for shared nodes. Here each function has its own recursion and a `Memo` keyed by the node and the binder offset. A free variable is an atom: the functions do not go into its type. + +use alloc::vec::Vec; +use rulean_store::{Expr, ExprId, Flags, LevelId, NameId, SATURATED, Store}; + +use crate::level; +use crate::memo::Memo; + +/// The scratch maps of the expression operations. Each operation clears its own map when it starts. +#[derive(Debug, Default)] +pub struct Scratch { + inst: Memo, + lift: Memo, + abs: Memo, + occ: Memo, + lps: Memo, + eq: Memo, +} + +impl Scratch { + /// New empty maps. + pub fn new() -> Scratch { + Scratch { + inst: Memo::new(), + lift: Memo::new(), + abs: Memo::new(), + occ: Memo::new(), + lps: Memo::new(), + eq: Memo::new(), + } + } +} + +/// True if `e` has no loose bound variable with an index `k` or more. A saturated range can hide any index, so it never gives true. +pub fn no_loose_from(st: &Store, e: ExprId, k: u64) -> bool { + let r = st.loose_range(e); + r != SATURATED && k >= r as u64 +} + +/// True if `e` has a loose bound variable. +pub fn has_loose_bvars(st: &Store, e: ExprId) -> bool { + st.loose_range(e) != 0 +} + +/// True if `e` has a free variable. +pub fn has_fvar(st: &Store, e: ExprId) -> bool { + st.flags(e).has(Flags::HAS_FVAR) +} + +/// True if `e` has a level parameter. +pub fn has_level_param(st: &Store, e: ExprId) -> bool { + st.flags(e).has(Flags::HAS_LEVEL_PARAM) +} + +/// The bound variable `i`, or the largest index if `i` is too large for `u32`. +fn bvar_at(st: &mut Store, i: u64) -> ExprId { + let k = if i > u32::MAX as u64 { + u32::MAX + } else { + i as u32 + }; + st.mk_bvar(k) +} + +// Instantiation. + +/// Replaces the loose bound variable `i` with `subst[i]` for `i < n`, and lowers the other loose bound variables by `n` (`instantiate(e, n, s)`). +pub fn instantiate(st: &mut Store, x: &mut Scratch, e: ExprId, subst: &[ExprId]) -> ExprId { + instantiate_from(st, x, e, 0, subst) +} + +/// `instantiate(e, s, n, subst)` of the official kernel: replaces the loose bound variable `s + i` with `subst[i]`. +pub fn instantiate_from( + st: &mut Store, + x: &mut Scratch, + e: ExprId, + s: u32, + subst: &[ExprId], +) -> ExprId { + if subst.len() == 0 || no_loose_from(st, e, s as u64) { + return e; + } + x.inst.clear(); + inst_rec(st, x, e, 0, s, subst, false) +} + +/// `instantiate(e, v)`: replaces the loose bound variable 0 with `v`. +pub fn instantiate1(st: &mut Store, x: &mut Scratch, e: ExprId, v: ExprId) -> ExprId { + instantiate(st, x, e, &[v]) +} + +/// Replaces the loose bound variable `i` with `subst[n - i - 1]` for `i < n` (`instantiate_rev`). The last item of `subst` is the innermost binder. +pub fn instantiate_rev(st: &mut Store, x: &mut Scratch, e: ExprId, subst: &[ExprId]) -> ExprId { + if subst.len() == 0 || !has_loose_bvars(st, e) { + return e; + } + x.inst.clear(); + inst_rec(st, x, e, 0, 0, subst, true) +} + +#[allow(clippy::too_many_arguments)] +fn inst_rec( + st: &mut Store, + x: &mut Scratch, + e: ExprId, + offset: u32, + s: u32, + subst: &[ExprId], + rev: bool, +) -> ExprId { + let s1 = s as u64 + offset as u64; + if no_loose_from(st, e, s1) { + return e; + } + let n = subst.len() as u64; + if let Expr::BVar(i) = st.node(e) { + let iv = i as u64; + if iv < s1 { + return e; + } + if iv < s1 + n { + let k = (iv - s1) as usize; + let v = if rev { + subst[subst.len() - k - 1] + } else { + subst[k] + }; + return lift_loose_bvars(st, x, v, 0, offset); + } + return bvar_at(st, iv - n); + } + match x.inst.get(e.0, offset) { + Some(r) => ExprId(r), + None => { + let r = inst_node(st, x, e, offset, s, subst, rev); + x.inst.put(e.0, offset, r.0); + r + } + } +} + +#[allow(clippy::too_many_arguments)] +fn inst_node( + st: &mut Store, + x: &mut Scratch, + e: ExprId, + offset: u32, + s: u32, + subst: &[ExprId], + rev: bool, +) -> ExprId { + match st.node(e) { + Expr::App(f, a) => { + let f2 = inst_rec(st, x, f, offset, s, subst, rev); + let a2 = inst_rec(st, x, a, offset, s, subst, rev); + if f2 == f && a2 == a { + e + } else { + st.mk_app(f2, a2) + } + } + Expr::Lam(b) => { + let d = inst_rec(st, x, b.dom, offset, s, subst, rev); + let body = inst_rec(st, x, b.body, offset + 1, s, subst, rev); + st.mk_lam(b.name, b.info, d, body) + } + Expr::Pi(b) => { + let d = inst_rec(st, x, b.dom, offset, s, subst, rev); + let body = inst_rec(st, x, b.body, offset + 1, s, subst, rev); + st.mk_pi(b.name, b.info, d, body) + } + Expr::Let(l) => { + let t = inst_rec(st, x, l.ty, offset, s, subst, rev); + let v = inst_rec(st, x, l.value, offset, s, subst, rev); + let body = inst_rec(st, x, l.body, offset + 1, s, subst, rev); + st.mk_let(l.name, t, v, body) + } + Expr::Proj(p) => { + let a = inst_rec(st, x, p.expr, offset, s, subst, rev); + st.mk_proj(p.struct_name, p.index, a) + } + _ => e, + } +} + +// Lifting and lowering. + +/// Adds `d` to each loose bound variable with an index `s` or more (`lift_loose_bvars(e, s, d)`). +pub fn lift_loose_bvars(st: &mut Store, x: &mut Scratch, e: ExprId, s: u32, d: u32) -> ExprId { + if d == 0 || no_loose_from(st, e, s as u64) { + return e; + } + x.lift.clear(); + shift_rec(st, &mut x.lift, e, 0, s, d, true) +} + +/// Subtracts `d` from each loose bound variable with an index `s` or more (`lower_loose_bvars(e, s, d)`). The caller makes sure that `s >= d` and that no loose bound variable has an index in `s - d .. s`. +pub fn lower_loose_bvars(st: &mut Store, x: &mut Scratch, e: ExprId, s: u32, d: u32) -> ExprId { + if d == 0 || no_loose_from(st, e, s as u64) { + return e; + } + x.lift.clear(); + shift_rec(st, &mut x.lift, e, 0, s, d, false) +} + +fn shift_rec( + st: &mut Store, + m: &mut Memo, + e: ExprId, + offset: u32, + s: u32, + d: u32, + up: bool, +) -> ExprId { + let s1 = s as u64 + offset as u64; + if no_loose_from(st, e, s1) { + return e; + } + if let Expr::BVar(i) = st.node(e) { + let iv = i as u64; + if iv < s1 { + return e; + } + let k = if up { + iv + d as u64 + } else if iv >= d as u64 { + iv - d as u64 + } else { + 0 + }; + return bvar_at(st, k); + } + match m.get(e.0, offset) { + Some(r) => ExprId(r), + None => { + let r = shift_node(st, m, e, offset, s, d, up); + m.put(e.0, offset, r.0); + r + } + } +} + +fn shift_node( + st: &mut Store, + m: &mut Memo, + e: ExprId, + offset: u32, + s: u32, + d: u32, + up: bool, +) -> ExprId { + match st.node(e) { + Expr::App(f, a) => { + let f2 = shift_rec(st, m, f, offset, s, d, up); + let a2 = shift_rec(st, m, a, offset, s, d, up); + st.mk_app(f2, a2) + } + Expr::Lam(b) => { + let dm = shift_rec(st, m, b.dom, offset, s, d, up); + let body = shift_rec(st, m, b.body, offset + 1, s, d, up); + st.mk_lam(b.name, b.info, dm, body) + } + Expr::Pi(b) => { + let dm = shift_rec(st, m, b.dom, offset, s, d, up); + let body = shift_rec(st, m, b.body, offset + 1, s, d, up); + st.mk_pi(b.name, b.info, dm, body) + } + Expr::Let(l) => { + let t = shift_rec(st, m, l.ty, offset, s, d, up); + let v = shift_rec(st, m, l.value, offset, s, d, up); + let body = shift_rec(st, m, l.body, offset + 1, s, d, up); + st.mk_let(l.name, t, v, body) + } + Expr::Proj(p) => { + let a = shift_rec(st, m, p.expr, offset, s, d, up); + st.mk_proj(p.struct_name, p.index, a) + } + _ => e, + } +} + +// Abstraction. + +/// Replaces the free variable `fvars[i]` with the bound variable `n - i - 1`, under the binders of `e` (`abstract(e, n, subst)`). The last item of `fvars` becomes the bound variable 0. +pub fn abstract_fvars(st: &mut Store, x: &mut Scratch, e: ExprId, fvars: &[ExprId]) -> ExprId { + if fvars.len() == 0 || !has_fvar(st, e) { + return e; + } + x.abs.clear(); + abs_rec(st, x, e, 0, fvars) +} + +fn abs_rec(st: &mut Store, x: &mut Scratch, e: ExprId, offset: u32, fvars: &[ExprId]) -> ExprId { + if !has_fvar(st, e) { + return e; + } + if let Expr::FVar(_, _) = st.node(e) { + let n = fvars.len(); + let mut i = n; + let mut hit = n; + while hit == n && i > 0 { + i -= 1; + if fvars[i] == e { + hit = i; + } + } + if hit == n { + return e; + } + return bvar_at(st, offset as u64 + (n - hit - 1) as u64); + } + match x.abs.get(e.0, offset) { + Some(r) => ExprId(r), + None => { + let r = abs_node(st, x, e, offset, fvars); + x.abs.put(e.0, offset, r.0); + r + } + } +} + +fn abs_node(st: &mut Store, x: &mut Scratch, e: ExprId, offset: u32, fvars: &[ExprId]) -> ExprId { + match st.node(e) { + Expr::App(f, a) => { + let f2 = abs_rec(st, x, f, offset, fvars); + let a2 = abs_rec(st, x, a, offset, fvars); + st.mk_app(f2, a2) + } + Expr::Lam(b) => { + let d = abs_rec(st, x, b.dom, offset, fvars); + let body = abs_rec(st, x, b.body, offset + 1, fvars); + st.mk_lam(b.name, b.info, d, body) + } + Expr::Pi(b) => { + let d = abs_rec(st, x, b.dom, offset, fvars); + let body = abs_rec(st, x, b.body, offset + 1, fvars); + st.mk_pi(b.name, b.info, d, body) + } + Expr::Let(l) => { + let t = abs_rec(st, x, l.ty, offset, fvars); + let v = abs_rec(st, x, l.value, offset, fvars); + let body = abs_rec(st, x, l.body, offset + 1, fvars); + st.mk_let(l.name, t, v, body) + } + Expr::Proj(p) => { + let a = abs_rec(st, x, p.expr, offset, fvars); + st.mk_proj(p.struct_name, p.index, a) + } + _ => e, + } +} + +// Occurrence of a bound variable. + +/// True if `e` has the loose bound variable `i` (`has_loose_bvar`). +pub fn has_loose_bvar(st: &Store, x: &mut Scratch, e: ExprId, i: u32) -> bool { + if !has_loose_bvars(st, e) { + return false; + } + x.occ.clear(); + occ_rec(st, &mut x.occ, e, i as u64, 0) +} + +fn occ_rec(st: &Store, m: &mut Memo, e: ExprId, i: u64, offset: u32) -> bool { + let k = i + offset as u64; + if no_loose_from(st, e, k) { + return false; + } + if let Expr::BVar(j) = st.node(e) { + return j as u64 == k; + } + // A node seen before at this offset did not have the variable, else the walk would have stopped. + if m.contains(e.0, offset) { + return false; + } + m.put(e.0, offset, 1); + match st.node(e) { + Expr::App(f, a) => occ_rec(st, m, f, i, offset) || occ_rec(st, m, a, i, offset), + Expr::Lam(b) => occ_rec(st, m, b.dom, i, offset) || occ_rec(st, m, b.body, i, offset + 1), + Expr::Pi(b) => occ_rec(st, m, b.dom, i, offset) || occ_rec(st, m, b.body, i, offset + 1), + Expr::Let(l) => { + occ_rec(st, m, l.ty, i, offset) + || occ_rec(st, m, l.value, i, offset) + || occ_rec(st, m, l.body, i, offset + 1) + } + Expr::Proj(p) => occ_rec(st, m, p.expr, i, offset), + _ => false, + } +} + +// Level instantiation. + +/// Replaces the level parameters `ps[i]` with `ls[i]` in the sorts and constants of `e` (`instantiate_lparams`). +pub fn instantiate_lparams( + st: &mut Store, + x: &mut Scratch, + e: ExprId, + ps: &[NameId], + ls: &[LevelId], +) -> ExprId { + if ps.len() == 0 || !has_level_param(st, e) { + return e; + } + x.lps.clear(); + lps_rec(st, x, e, ps, ls) +} + +fn lps_rec(st: &mut Store, x: &mut Scratch, e: ExprId, ps: &[NameId], ls: &[LevelId]) -> ExprId { + if !has_level_param(st, e) { + return e; + } + match x.lps.get(e.0, 0) { + Some(r) => ExprId(r), + None => { + let r = lps_node(st, x, e, ps, ls); + x.lps.put(e.0, 0, r.0); + r + } + } +} + +fn lps_node(st: &mut Store, x: &mut Scratch, e: ExprId, ps: &[NameId], ls: &[LevelId]) -> ExprId { + match st.node(e) { + Expr::Sort(l) => { + let l2 = level::instantiate(st, l, ps, ls); + if l2 == l { e } else { st.mk_sort(l2) } + } + Expr::Const(n, list) => { + let items = st.level_list_vec(list); + let mut out: Vec = Vec::with_capacity(items.len()); + let mut changed = false; + let mut i = 0; + while i < items.len() { + let l2 = level::instantiate(st, items[i], ps, ls); + changed = changed || l2 != items[i]; + out.push(l2); + i += 1; + } + if changed { + let list2 = st.level_list(&out); + st.mk_const(n, list2) + } else { + e + } + } + Expr::App(f, a) => { + let f2 = lps_rec(st, x, f, ps, ls); + let a2 = lps_rec(st, x, a, ps, ls); + st.mk_app(f2, a2) + } + Expr::Lam(b) => { + let d = lps_rec(st, x, b.dom, ps, ls); + let body = lps_rec(st, x, b.body, ps, ls); + st.mk_lam(b.name, b.info, d, body) + } + Expr::Pi(b) => { + let d = lps_rec(st, x, b.dom, ps, ls); + let body = lps_rec(st, x, b.body, ps, ls); + st.mk_pi(b.name, b.info, d, body) + } + Expr::Let(l) => { + let t = lps_rec(st, x, l.ty, ps, ls); + let v = lps_rec(st, x, l.value, ps, ls); + let body = lps_rec(st, x, l.body, ps, ls); + st.mk_let(l.name, t, v, body) + } + Expr::Proj(p) => { + let a = lps_rec(st, x, p.expr, ps, ls); + st.mk_proj(p.struct_name, p.index, a) + } + _ => e, + } +} + +// Application spines. + +/// The head of an application spine (`get_app_fn`). +pub fn app_fn(st: &Store, e: ExprId) -> ExprId { + let mut cur = e; + let mut walking = true; + while walking { + match st.node(cur) { + Expr::App(f, _) => cur = f, + _ => walking = false, + } + } + cur +} + +/// The number of arguments of an application spine. +pub fn app_num_args(st: &Store, e: ExprId) -> usize { + let mut cur = e; + let mut n = 0; + let mut walking = true; + while walking { + match st.node(cur) { + Expr::App(f, _) => { + cur = f; + n += 1; + } + _ => walking = false, + } + } + n +} + +/// Writes the arguments of an application spine to `out`, from the last to the first, and gives the head (`get_app_rev_args`). `out` is cleared first. +pub fn app_rev_args(st: &Store, e: ExprId, out: &mut Vec) -> ExprId { + out.truncate(0); + let mut cur = e; + let mut walking = true; + while walking { + match st.node(cur) { + Expr::App(f, a) => { + out.push(a); + cur = f; + } + _ => walking = false, + } + } + cur +} + +/// Writes the arguments of an application spine to `out`, from the first to the last, and gives the head (`get_app_args`). `out` is cleared first. +pub fn app_args(st: &Store, e: ExprId, out: &mut Vec) -> ExprId { + let head = app_rev_args(st, e, out); + reverse(out); + head +} + +/// Reverses the items of `v` in place. The two indices are separate statements, because Charon cannot remove the overflow check of an index sum that is followed by an `IndexMut` call. +pub fn reverse(v: &mut [ExprId]) { + let n = v.len(); + let mut i = 0; + while i < n / 2 { + let j = n - 1 - i; + let a = v[i]; + let b = v[j]; + v[i] = b; + v[j] = a; + i += 1; + } +} + +/// The application of `f` to the arguments in reverse order: `f rev[n-1] ... rev[0]` (`mk_rev_app`). +pub fn mk_rev_app(st: &mut Store, f: ExprId, rev: &[ExprId]) -> ExprId { + let mut r = f; + let mut i = rev.len(); + while i > 0 { + i -= 1; + r = st.mk_app(r, rev[i]); + } + r +} + +// Beta reduction. + +/// True if `e` is an application with a lambda head (`is_head_beta`). +pub fn is_head_beta(st: &Store, e: ExprId) -> bool { + match st.node(e) { + Expr::App(_, _) => matches!(st.node(app_fn(st, e)), Expr::Lam(_)), + _ => false, + } +} + +/// Applies `f` to the arguments `rev` in reverse order and beta-reduces the lambdas at the head (`apply_beta` without zeta). +pub fn apply_beta(st: &mut Store, x: &mut Scratch, f: ExprId, rev: &[ExprId]) -> ExprId { + let num = rev.len(); + if num == 0 { + return f; + } + let mut e = f; + let mut i = 0; + let mut walking = true; + while walking && i + 1 < num { + match st.node(e) { + Expr::Lam(b) => { + e = b.body; + i += 1; + } + _ => walking = false, + } + } + if walking { + if let Expr::Lam(b) = st.node(e) { + return instantiate(st, x, b.body, rev); + } + } + let n = num - i; + let body = instantiate(st, x, e, &rev[n..]); + mk_rev_app(st, body, &rev[..n]) +} + +/// Beta-reduces at the head until the head is not a lambda (`head_beta_reduce`). +pub fn head_beta(st: &mut Store, x: &mut Scratch, e: ExprId) -> ExprId { + let mut cur = e; + let mut rev: Vec = Vec::new(); + while is_head_beta(st, cur) { + let f = app_rev_args(st, cur, &mut rev); + cur = apply_beta(st, x, f, &rev); + } + cur +} + +/// Beta-reduces at the head only when the result is cheap: the body after the lambdas is closed or is a bound variable (`cheap_beta_reduce`). +pub fn cheap_beta_reduce(st: &mut Store, e: ExprId) -> ExprId { + if !matches!(st.node(e), Expr::App(_, _)) { + return e; + } + let mut f = app_fn(st, e); + if !matches!(st.node(f), Expr::Lam(_)) { + return e; + } + let mut args: Vec = Vec::new(); + app_args(st, e, &mut args); + let mut i = 0; + let mut walking = true; + while walking && i < args.len() { + match st.node(f) { + Expr::Lam(b) => { + f = b.body; + i += 1; + } + _ => walking = false, + } + } + if !has_loose_bvars(st, f) { + return st.mk_apps(f, &args[i..]); + } + if let Expr::BVar(j) = st.node(f) { + let j = j as usize; + if j < i { + let h = args[i - j - 1]; + return st.mk_apps(h, &args[i..]); + } + } + e +} + +// Alpha-equality. + +/// True if the terms are equal up to binder names and binder info (`is_equal`, the `==` of the official kernel). The store hash does not mix binder names or binder info, so unequal hashes mean unequal terms. +pub fn expr_equal(st: &Store, x: &mut Scratch, a: ExprId, b: ExprId) -> bool { + if a == b { + return true; + } + x.eq.clear(); + eq_rec(st, &mut x.eq, a, b) +} + +fn eq_rec(st: &Store, m: &mut Memo, a: ExprId, b: ExprId) -> bool { + if a == b { + return true; + } + if st.hash(a) != st.hash(b) || a.kind() != b.kind() { + return false; + } + // A pair seen before is equal or the whole walk is already false (the cache of `expr_eq_fn`). + if m.contains(a.0, b.0) { + return true; + } + m.put(a.0, b.0, 1); + match (st.node(a), st.node(b)) { + (Expr::App(f1, a1), Expr::App(f2, a2)) => eq_rec(st, m, a1, a2) && eq_rec(st, m, f1, f2), + (Expr::Lam(b1), Expr::Lam(b2)) => { + eq_rec(st, m, b1.dom, b2.dom) && eq_rec(st, m, b1.body, b2.body) + } + (Expr::Pi(b1), Expr::Pi(b2)) => { + eq_rec(st, m, b1.dom, b2.dom) && eq_rec(st, m, b1.body, b2.body) + } + (Expr::Let(l1), Expr::Let(l2)) => { + eq_rec(st, m, l1.ty, l2.ty) + && eq_rec(st, m, l1.value, l2.value) + && eq_rec(st, m, l1.body, l2.body) + } + (Expr::Proj(p1), Expr::Proj(p2)) => { + p1.struct_name == p2.struct_name + && p1.index == p2.index + && eq_rec(st, m, p1.expr, p2.expr) + } + // The other kinds are atoms. The store interns them, so unequal handles mean unequal atoms. + _ => false, + } +} diff --git a/crates/rulean-core/src/lib.rs b/crates/rulean-core/src/lib.rs index 92cf0fa..e6e9e1d 100644 --- a/crates/rulean-core/src/lib.rs +++ b/crates/rulean-core/src/lib.rs @@ -10,7 +10,13 @@ // `Option` combinators and `?` are outside the subset, so the code matches by hand. #![allow(clippy::collapsible_if)] #![allow(clippy::comparison_chain)] +// `Vec::clear`, `slice::swap` and `checked_sub` are outside the modelled functions too. +#![allow(clippy::manual_clear)] +#![allow(clippy::manual_swap)] +#![allow(clippy::implicit_saturating_sub)] extern crate alloc; +pub mod expr; pub mod level; +pub mod memo; diff --git a/crates/rulean-core/src/memo.rs b/crates/rulean-core/src/memo.rs new file mode 100644 index 0000000..55a8890 --- /dev/null +++ b/crates/rulean-core/src/memo.rs @@ -0,0 +1,112 @@ +//! Small hash maps for the caches of the checker. +//! +//! The maps use the verified `rulean_store::table::Table` with epochs, so `clear` is O(1) (spec 04.8). The keys are kept in arrays next to the table, and a lookup compares the full key after the hash matches. So a cache never trusts a hash alone (spec 08.5). + +use alloc::vec::Vec; +use rulean_store::hash; +use rulean_store::table::{Probe, Table}; + +const PAIR_SEED: u32 = 0x4d45_4d4f; + +fn pair_hash(a: u32, b: u32) -> u32 { + hash::finish(hash::mix(hash::mix(PAIR_SEED, a), b)) +} + +/// A map from a pair of `u32` keys to a `u32` value. +#[derive(Clone, Debug)] +pub struct Memo { + ka: Vec, + kb: Vec, + vals: Vec, + table: Table, +} + +impl Default for Memo { + fn default() -> Memo { + Memo::new() + } +} + +impl Memo { + /// An empty map. + pub fn new() -> Memo { + Memo { + ka: Vec::new(), + kb: Vec::new(), + vals: Vec::new(), + table: Table::new(true), + } + } + + /// The number of entries. + pub fn len(&self) -> usize { + self.vals.len() + } + + /// True if the map has no entry. + pub fn is_empty(&self) -> bool { + self.vals.len() == 0 + } + + /// The position of the key in the entry arrays plus 1, or 0. The second value is the table position where a walk for the key stopped. + fn find(&self, a: u32, b: u32, h: u32) -> (usize, usize) { + let mut pos = self.table.start(h); + let mut found: usize = 0; + let mut walking = true; + while walking { + match self.table.get(pos) { + Probe::Free => walking = false, + Probe::Full(hh, v) => { + let i = v as usize; + if hh == h && self.ka[i] == a && self.kb[i] == b { + found = i + 1; + walking = false; + } else { + pos = self.table.next(pos); + } + } + } + } + (found, pos) + } + + /// The value of the key, if any. + pub fn get(&self, a: u32, b: u32) -> Option { + let (found, _) = self.find(a, b, pair_hash(a, b)); + if found == 0 { + None + } else { + Some(self.vals[found - 1]) + } + } + + /// True if the map has the key. + pub fn contains(&self, a: u32, b: u32) -> bool { + let (found, _) = self.find(a, b, pair_hash(a, b)); + found != 0 + } + + /// Sets the value of the key. + pub fn put(&mut self, a: u32, b: u32, v: u32) { + let h = pair_hash(a, b); + let (found, pos) = self.find(a, b, h); + if found != 0 { + let k = found - 1; + self.vals[k] = v; + } else { + let i = self.vals.len() as u32; + self.ka.push(a); + self.kb.push(b); + self.vals.push(v); + self.table.put(pos, h, i); + } + } + + /// Removes every entry. This is O(1) for the table. + pub fn clear(&mut self) { + self.ka.truncate(0); + self.kb.truncate(0); + self.vals.truncate(0); + self.table.clear(); + } +} diff --git a/crates/rulean-core/tests/expr.rs b/crates/rulean-core/tests/expr.rs new file mode 100644 index 0000000..80230ab --- /dev/null +++ b/crates/rulean-core/tests/expr.rs @@ -0,0 +1,201 @@ +//! Tests of the expression operations. The expected values follow `instantiate.cpp`, `abstract.cpp` and `expr.cpp` of the official kernel v4.35.0. + +use rulean_core::expr::{ + Scratch, abstract_fvars, app_args, cheap_beta_reduce, expr_equal, has_loose_bvar, head_beta, + instantiate, instantiate_lparams, instantiate_rev, lift_loose_bvars, lower_loose_bvars, +}; +use rulean_store::{BinderInfo, ExprId, NameId, Store}; + +struct T { + st: Store, + x: Scratch, + ty: ExprId, +} + +impl T { + fn new() -> T { + let mut st = Store::new(); + let z = st.level_zero(); + let ty = st.mk_sort(z); + T { + st, + x: Scratch::new(), + ty, + } + } + fn name(&mut self, s: &str) -> NameId { + let a = self.st.anon(); + self.st.name_str_bytes(a, s.as_bytes()) + } + fn c(&mut self, s: &str) -> ExprId { + let n = self.name(s); + let ls = self.st.level_list_empty(); + self.st.mk_const(n, ls) + } + fn b(&mut self, i: u32) -> ExprId { + self.st.mk_bvar(i) + } + fn app(&mut self, f: ExprId, a: ExprId) -> ExprId { + self.st.mk_app(f, a) + } + fn lam(&mut self, n: &str, body: ExprId) -> ExprId { + let n = self.name(n); + let ty = self.ty; + self.st.mk_lam(n, BinderInfo::Default, ty, body) + } +} + +#[test] +fn instantiate_replaces_and_lowers() { + let mut t = T::new(); + let (b0, b1, b2) = (t.b(0), t.b(1), t.b(2)); + let c = t.c("c"); + let e = t.app(b0, b1); + let want = t.app(c, b0); + assert_eq!(instantiate(&mut t.st, &mut t.x, e, &[c]), want); + // Under a binder the index of the substituted variable moves up by one. + let body = t.app(b0, b1); + let l = t.lam("x", body); + let wbody = t.app(b0, c); + let want = t.lam("x", wbody); + assert_eq!(instantiate(&mut t.st, &mut t.x, l, &[c]), want); + // A value with a loose bound variable is lifted under the binder. + let l = t.lam("x", b1); + let want = t.lam("x", b1); + assert_eq!(instantiate(&mut t.st, &mut t.x, l, &[b0]), want); + let e = t.app(b2, b0); + let want = t.app(b1, c); + assert_eq!(instantiate(&mut t.st, &mut t.x, e, &[c]), want); +} + +#[test] +fn instantiate_rev_uses_the_last_item_for_index_zero() { + let mut t = T::new(); + let (b0, b1) = (t.b(0), t.b(1)); + let (a, b) = (t.c("a"), t.c("b")); + let e = t.app(b0, b1); + let want = t.app(b, a); + assert_eq!(instantiate_rev(&mut t.st, &mut t.x, e, &[a, b]), want); +} + +#[test] +fn abstract_makes_bound_variables() { + let mut t = T::new(); + let ty = t.ty; + let f1 = t.st.mk_fvar(0, ty); + let f2 = t.st.mk_fvar(1, ty); + let e = t.app(f1, f2); + let (b0, b1, b2) = (t.b(0), t.b(1), t.b(2)); + let want = t.app(b1, b0); + assert_eq!(abstract_fvars(&mut t.st, &mut t.x, e, &[f1, f2]), want); + let l = t.lam("x", e); + let wbody = t.app(b2, b1); + let want = t.lam("x", wbody); + assert_eq!(abstract_fvars(&mut t.st, &mut t.x, l, &[f1, f2]), want); + // A free variable that is not in the list stays. + let want = t.app(f1, b0); + assert_eq!(abstract_fvars(&mut t.st, &mut t.x, e, &[f2]), want); +} + +#[test] +fn lift_and_lower() { + let mut t = T::new(); + let (b0, b2, b5) = (t.b(0), t.b(2), t.b(5)); + let e = t.app(b0, b2); + let lifted = t.app(b0, b5); + assert_eq!(lift_loose_bvars(&mut t.st, &mut t.x, e, 1, 3), lifted); + assert_eq!(lower_loose_bvars(&mut t.st, &mut t.x, lifted, 3, 3), e); +} + +#[test] +fn loose_bound_variable_occurrence() { + let mut t = T::new(); + let b1 = t.b(1); + let l = t.lam("x", b1); + assert!(has_loose_bvar(&t.st, &mut t.x, l, 0)); + assert!(!has_loose_bvar(&t.st, &mut t.x, l, 1)); + let c = t.c("c"); + assert!(!has_loose_bvar(&t.st, &mut t.x, c, 0)); +} + +#[test] +fn level_instantiation() { + let mut t = T::new(); + let nu = t.name("u"); + let u = t.st.level_param(nu); + let us = t.st.level_list(&[u]); + let n = t.name("c"); + let c = t.st.mk_const(n, us); + let s = t.st.mk_sort(u); + let e = t.app(c, s); + let z = t.st.level_zero(); + let one = t.st.level_succ(z); + let ones = t.st.level_list(&[one]); + let c1 = t.st.mk_const(n, ones); + let s1 = t.st.mk_sort(one); + let want = t.app(c1, s1); + assert_eq!( + instantiate_lparams(&mut t.st, &mut t.x, e, &[nu], &[one]), + want + ); +} + +#[test] +fn beta() { + let mut t = T::new(); + let (b0, b1) = (t.b(0), t.b(1)); + let (a, b, c) = (t.c("a"), t.c("b"), t.c("c")); + // (fun x y => x) a b is a. + let inner = t.lam("y", b1); + let k = t.lam("x", inner); + let ka = t.app(k, a); + let kab = t.app(ka, b); + assert_eq!(head_beta(&mut t.st, &mut t.x, kab), a); + assert_eq!(cheap_beta_reduce(&mut t.st, kab), a); + // (fun x => x) a b is a b. + let id = t.lam("x", b0); + let ida = t.app(id, a); + let idab = t.app(ida, b); + let ab = t.app(a, b); + assert_eq!(head_beta(&mut t.st, &mut t.x, idab), ab); + // (fun x y => c) a b is c. + let inner = t.lam("y", c); + let kc = t.lam("x", inner); + let e = t.app(kc, a); + let e = t.app(e, b); + assert_eq!(cheap_beta_reduce(&mut t.st, e), c); + // (fun x => c x) a is not cheap. + let body = t.app(c, b0); + let f = t.lam("x", body); + let fa = t.app(f, a); + assert_eq!(cheap_beta_reduce(&mut t.st, fa), fa); + let ca = t.app(c, a); + assert_eq!(head_beta(&mut t.st, &mut t.x, fa), ca); +} + +#[test] +fn spine() { + let mut t = T::new(); + let (f, a, b) = (t.c("f"), t.c("a"), t.c("b")); + let fa = t.app(f, a); + let fab = t.app(fa, b); + let mut out = Vec::new(); + assert_eq!(app_args(&t.st, fab, &mut out), f); + assert_eq!(out, vec![a, b]); +} + +#[test] +fn alpha_equality_ignores_binder_names() { + let mut t = T::new(); + let b0 = t.b(0); + let lx = t.lam("x", b0); + let ly = t.lam("y", b0); + assert_ne!(lx, ly); + assert!(expr_equal(&t.st, &mut t.x, lx, ly)); + let c = t.c("c"); + let lc = t.lam("x", c); + assert!(!expr_equal(&t.st, &mut t.x, lx, lc)); + let a = t.app(lx, c); + let b = t.app(ly, c); + assert!(expr_equal(&t.st, &mut t.x, a, b)); +} diff --git a/crates/rulean-store/src/expr.rs b/crates/rulean-store/src/expr.rs index 860a1ad..57ca479 100644 --- a/crates/rulean-store/src/expr.rs +++ b/crates/rulean-store/src/expr.rs @@ -1,6 +1,8 @@ //! Expression nodes, side records and literals (spec 04.2). //! //! Each kind has its own node array, and each node has a side record of 8 bytes in a parallel array. One open-addressing table interns the nodes of all kinds. The hash of a node is structural: it mixes the kind seed with the hashes of the fields, not with their handles. So equal terms in two stores have equal hashes. +//! +//! The hash of a `lam`, `pi` or `let` node does not mix the binder name or the binder info. This is the method of Lean. Two terms that differ only in binder names and binder info have different handles but equal hashes. So the checker can use the hash to reject terms that are not alpha-equivalent (`expr_eq_fn` of the official kernel). The interning table compares the full node, so this costs only a longer probe walk when such terms occur. use alloc::vec::Vec; use rulean_nat::Nat; @@ -381,9 +383,8 @@ impl Store { fn binder_side(&self, kind: Kind, b: Binder) -> (u32, u32) { let sd = self.side(b.dom); let sb = self.side(b.body); - let mut h = hash::mix(seed(kind), self.name_hash(b.name)); - h = hash::mix(h, b.info as u32); - h = hash::mix(hash::mix(h, sd.hash), sb.hash); + // The hash does not mix the binder name or the binder info, as in Lean. So alpha-equivalent terms have equal hashes (see the module documentation). + let h = hash::mix(hash::mix(seed(kind), sd.hash), sb.hash); let range = max32(sd.info & SATURATED, range_under(sb.info & SATURATED)); let flags = ((sd.info | sb.info) >> 24) as u8; (hash::finish(h), pack(range, flags)) @@ -418,8 +419,10 @@ impl Store { let st = self.side(ty); let sv = self.side(value); let sb = self.side(body); - let mut h = hash::mix(seed(Kind::Let), self.name_hash(name)); - h = hash::mix(hash::mix(hash::mix(h, st.hash), sv.hash), sb.hash); + let h = hash::mix( + hash::mix(hash::mix(seed(Kind::Let), st.hash), sv.hash), + sb.hash, + ); let range = max32( max32(st.info & SATURATED, sv.info & SATURATED), range_under(sb.info & SATURATED),