use crate::ast::*;
use crate::eval::*;
use crate::intern::*;
use crate::modules::{for_children, level_of, tail_fn, BFn, Builtin, Def};
use crate::stepcache::{self as sc_, DOp, Loc, Table, Write};
use crate::value::*;
use std::cell::{Cell, OnceCell, RefCell};
use std::collections::HashSet;
use std::rc::Rc;
pub struct Cx<'a> {
pub ev: &'a Evaluator,
pub f: &'a StateData,
pub cur: &'a StateData,
pub nxt: &'a StateData,
pub nv: i64,
pub fallbacks: &'a Cell<u64>,
pub cache: Option<&'a RefCell<Table>>,
pub rec_on: &'a Cell<bool>,
pub rec: &'a RefCell<Rec>,
pub cache_always: bool,
pub meas: &'a Meas,
pub cur_ids: &'a [crate::hashcons::Id],
pub out: Option<&'a RefCell<sc_::Rows>>,
pub vals: Option<&'a dyn Fn()>,
pub fpx: Option<&'a FpCtx<'a>>,
}
pub struct FpCtx<'a> {
pub seen: &'a crate::checker::FpSet,
pub ids: crate::hashcons::Local,
pub words: &'a [u64],
pub terms: &'a [u64],
pub sum: u64,
pub verify: bool,
}
enum Skel {
Or(Vec<Skel>),
Exists {
slot: usize,
dom: Dom,
body: Box<Skel>,
run: Rc<AE>,
},
Pre { ops: Vec<PreOp>, body: Box<Skel> },
Unit(Rc<UnitNode>),
}
enum Dom {
Const { set: Rc<CE>, ids: OnceCell<Option<Box<[crate::hashcons::Id]>>> },
Domain(usize),
}
struct UnitNode {
id: u32,
bound: Vec<usize>,
clear: Vec<usize>,
inner: AE,
stat: Rc<UnitStat>,
n: N,
}
#[derive(Clone, Copy)]
enum Step<'a> {
Bind(usize, crate::hashcons::Id),
Pre(&'a [PreOp]),
}
const PATH_MAX: usize = 24;
struct Path<'a> {
n: usize,
a: [std::mem::MaybeUninit<Step<'a>>; PATH_MAX],
}
impl<'a> Path<'a> {
#[inline]
fn new() -> Path<'a> {
Path { n: 0, a: [std::mem::MaybeUninit::uninit(); PATH_MAX] }
}
#[inline]
fn push(&mut self, s: Step<'a>) {
self.a[self.n].write(s);
self.n += 1;
}
#[inline]
fn pop(&mut self) {
self.n -= 1;
}
#[inline]
fn steps(&self) -> impl Iterator<Item = &Step<'a>> {
self.a[..self.n].iter().map(|s| unsafe { s.assume_init_ref() })
}
}
impl Skel {
fn depth(&self) -> usize {
match self {
Skel::Or(items) => items.iter().map(Skel::depth).max().unwrap_or(0),
Skel::Exists { body, .. } | Skel::Pre { body, .. } => 1 + body.depth(),
Skel::Unit(_) => 0,
}
}
}
fn skel_run<'s>(sk: &'s Skel, cx: &Cx, path: &mut Path<'s>, bids: &mut SmallIds, k: &mut dyn FnMut() -> R<()>) -> R<()> {
match sk {
Skel::Or(items) => {
for it in items {
skel_run(it, cx, path, bids, k)?;
}
Ok(())
}
Skel::Pre { ops, body } => {
path.push(Step::Pre(ops));
let r = skel_run(body, cx, path, bids, k);
path.pop();
r
}
Skel::Exists { slot, dom, body, run } => {
use crate::hashcons as hc;
let n0 = bids.n;
let each = |x: crate::hashcons::Id, path: &mut Path<'s>, bids: &mut SmallIds, k: &mut dyn FnMut() -> R<()>| -> R<()> {
bids.push(x);
path.push(Step::Bind(*slot, x));
let r = skel_run(body, cx, path, bids, k);
path.pop();
bids.n = n0;
r
};
match dom {
Dom::Domain(var) => {
let d = cx.cache.and_then(|t| t.borrow().domain_ids(cx.cur_ids[*var]));
if let Some(d) = d {
for &x in d.iter() {
each(x, path, bids, k)?;
}
return Ok(());
}
}
Dom::Const { set, ids } => {
let got = ids.get_or_init(|| {
on_values(cx, path);
let s = as_set(set(cx).ok()?).ok()?;
if let SetK::Interval(lo, hi) = &s.k {
return (*lo..=*hi).map(|i| hc::id(&Value::Int(i))).collect();
}
s.elems().ok()?.iter().map(hc::id).collect()
});
if let Some(xs) = got {
for &x in xs.iter() {
each(x, path, bids, k)?;
}
return Ok(());
}
}
}
on_values(cx, path);
run(cx, k)
}
Skel::Unit(u) => {
let table = cx.cache.expect("a skeleton runs with the step cache on");
if !use_cache(cx, &u.stat) {
on_values(cx, path);
return (u.inner)(cx, k);
}
unit_cached(cx, k, table, u, bids.as_slice(), &|| on_values(cx, path))
}
}
}
fn on_values(cx: &Cx, path: &Path) {
if let Some(v) = cx.vals {
v();
}
for st in path.steps() {
match st {
Step::Bind(slot, x) => cx.f.set(*slot, crate::hashcons::value(*x)),
Step::Pre(ops) => run_pre(ops, cx),
}
}
}
#[derive(Default)]
pub struct Rec {
reads: Vec<(Loc, Value)>,
bad: bool,
delta: Vec<Option<Vec<DOp>>>,
succs: Vec<Vec<Write>>,
}
type DProg = dyn Fn(&Cx, &mut Vec<DOp>) -> R<bool>;
#[derive(Clone)]
struct DShape {
var: usize,
direct: bool,
prog: Rc<DProg>,
}
fn apply_ops(cx: &Cx, parent: &Value, ops: &[DOp], n: N) -> Option<Value> {
let mut v = parent.clone();
for op in ops {
v = match op {
DOp::Bag { e, plus } => crate::stdlib::bag_one(&v, e, *plus)?,
DOp::Set { e, add } => match &v {
Value::Set(s) => set_one(s, e, *add)?,
_ => return None,
},
DOp::Exc { path, v: nv } => {
let mut hit = false;
let r = cx
.ev
.except_with(
v,
path,
0,
&mut |_| {
hit = true;
Ok(nv.clone())
},
n,
)
.ok()?;
if !hit {
return None;
}
r
}
};
}
Some(v)
}
fn uses_at(n: N) -> bool {
if matches!(n.e, E::At) {
return true;
}
let mut r = false;
for_children(n, &mut |c| {
if !r && uses_at(c) {
r = true;
}
});
r
}
type CE = Box<dyn Fn(&Cx) -> R<Value>>;
const BAG_DELTA_MAX: i64 = 64;
type AE = Box<dyn Fn(&Cx, &mut dyn FnMut() -> R<()>) -> R<()>>;
#[cold]
fn bail<T>() -> R<T> {
eval_err(String::new())
}
struct Sc {
k: ScK,
next: Option<Rc<Sc>>,
}
enum ScK {
Root(C),
Slot { name: Name, slot: usize },
At { slot: usize },
Arg { name: Name, node: N, sc: Rc<Sc>, bd: ArgBd },
Let { d: &'static DefNode, defs: &'static [LetDef], below: Rc<Sc>, head: bool, cache: usize, ce: RefCell<Option<(Rc<CE>, bool)>>, root: bool },
RootVal { name: Name, v: Value },
}
enum ArgBd {
Const(Value),
ByName { ce: Rc<CE>, cache: Option<usize>, stable: bool },
}
fn build(sc: &Rc<Sc>, cx: &Cx, share: bool) -> R<C> {
let below = |cx: &Cx| -> R<C> {
match &sc.next {
Some(n) => build(n, cx, share),
None => Ok(None),
}
};
Ok(match &sc.k {
ScK::Root(c) => c.clone(),
ScK::Slot { name, slot } => cons(*name, cx.f.get(*slot), &below(cx)?),
ScK::At { slot } => cons(cx.ev.at, cx.f.get(*slot), &below(cx)?),
ScK::Arg { name, node, sc: asc, bd } => {
let known = match bd {
ArgBd::Const(v) => Some(v.clone()),
ArgBd::ByName { cache: Some(slot), .. } if share => Some(cx.f.get(*slot)).filter(|v| !matches!(v, Value::Undef)),
_ => None,
};
let v = match known {
Some(v) => v,
None => cx.ev.mk_arg(node, &build(asc, cx, share)?)?,
};
cons(*name, v, &below(cx)?)
}
ScK::Let { root: true, .. } | ScK::RootVal { .. } => below(cx)?,
ScK::Let { defs, below: b, head, .. } => {
debug_assert!(*head);
let ctx = cx.ev.bind_let(defs, &build(b, cx, share)?);
let mut s = if share { Some(sc) } else { None };
while let Some(node) = s {
match &node.k {
ScK::Let { d, cache, root: false, below: b2, .. } if Rc::ptr_eq(b2, b) => {
let v = cx.f.get(*cache);
if !matches!(v, Value::Undef) {
let mut c = ctx.as_ref();
while let Some(cn) = c {
if cn.name == d.name {
if let Bind::LetLazy { v: cell, st, .. } = &cn.val {
*cell.borrow_mut() = v;
st.set(cx.cur.id());
}
break;
}
c = cn.next.as_ref();
}
}
}
_ => break,
}
s = node.next.as_ref();
}
ctx
}
})
}
fn debug_fb(seen: &Cell<bool>, what: &str, node: N) {
if !seen.replace(true) && std::env::var_os("EVERYSTATE_EVAL_DEBUG").is_some() {
eprintln!("fallback {} at line {} col {} to line {} col {}", what, node.loc[0], node.loc[1], node.loc[2], node.loc[3]);
}
}
fn primeless(n: N, seen: &mut HashSet<usize>) -> bool {
match &n.e {
E::Op(K::Prime | K::Enabled | K::Unchanged | K::Cdot | K::BoxAct | K::AngleAct | K::Always | K::Eventually | K::LeadsTo | K::Wf | K::Sf | K::Guarantee, _) => return false,
E::TQuant { .. } | E::StepRef | E::AssumeProve => return false,
E::App { r, .. } => {
let d = match r.get() {
Res::Def(d) => Some(d),
Res::Decl(d) => d.subst.get(),
Res::Local => None,
Res::None => return false,
};
if let Some(d) = d {
let d = d.over.get().unwrap_or(d);
if seen.insert(d as *const Def as usize) {
if let Some(inst) = &d.inst {
let _ = inst;
return false;
}
if d.builtin.get().is_none() {
match d.body.get() {
Some(b) => {
if !primeless(b, seen) {
return false;
}
}
None => return false,
}
}
}
}
}
E::Let { defs, .. } => {
if defs.iter().any(|d| matches!(d, LetDef::Inst(_))) {
return false;
}
}
_ => {}
}
let mut ok = true;
for_children(n, &mut |c| {
if ok && !primeless(c, seen) {
ok = false;
}
});
ok
}
fn fb(node: N, sc: &Rc<Sc>) -> CE {
let sc = sc.clone();
let seen = Cell::new(false);
let share = primeless(node, &mut HashSet::new());
Box::new(move |cx| {
debug_fb(&seen, "expression", node);
cx.fallbacks.set(cx.fallbacks.get() + 1);
let ctx = build(&sc, cx, share)?;
cx.ev.eval(node, &ctx)
})
}
fn fb_act(node: N, sc: &Rc<Sc>) -> AE {
let sc = sc.clone();
let seen = Cell::new(false);
Box::new(move |cx, k| {
debug_fb(&seen, "action", node);
cx.fallbacks.set(cx.fallbacks.get() + 1);
let ctx = build(&sc, cx, false)?;
cx.ev.enumerate(node, &ctx, k)
})
}
#[inline]
fn truth(v: Value) -> R<bool> {
match &v {
Value::Bool(b) => {
let b = *b;
std::mem::forget(v); Ok(b)
}
_ => bail(),
}
}
#[inline]
fn as_set(v: Value) -> R<Rc<SetV>> {
match v {
Value::Set(s) => Ok(s),
_ => bail(),
}
}
fn plain(v: &Value) -> bool {
match v {
Value::Int(_) | Value::Bool(_) | Value::Str(_) | Value::Model(_) | Value::Tuple(_) | Value::Record(_) | Value::Fcn(_) => true,
Value::Set(s) => s.reducible(),
_ => false,
}
}
fn pure_module(m: Name) -> bool {
thread_local! {
static PURE: Vec<Name> = ["Naturals", "Integers", "Reals", "Sequences", "FiniteSets", "Bags"].iter().map(|s| name(s)).collect();
}
PURE.with(|p| p.contains(&m))
}
fn pure_builtin(d: &'static Def) -> bool {
if pure_module(d.module) {
return true;
}
let n = name_str(d.name);
name_str(d.module).as_ref() == "TLC" && matches!(n.as_ref(), ":>" | "@@")
}
fn impure(n: N, seen: &mut HashSet<usize>) -> bool {
let r = impure0(n, seen);
if r && std::env::var_os("EVERYSTATE_EVAL_DEBUG").is_some() {
if let E::App { name, r, .. } = &n.e {
eprintln!(" (may have an effect, or is not understood: {}{} at line {})", name, match r.get() { Res::Def(d) => format!(" of module {}{}", d.module, if d.inst.is_some() { " (instance)" } else { "" }), Res::Decl(_) => " (substituted declaration)".into(), _ => String::new() }, n.loc[0]);
}
}
r
}
fn impure_def(d: &'static Def, seen: &mut HashSet<usize>) -> bool {
let d = d.over.get().unwrap_or(d);
if !seen.insert(d as *const Def as usize) {
return false;
}
if let Some(bi) = d.builtin.get() {
return !(pure_builtin(d) || matches!(bi.f, BFn::Const(_) | BFn::Table(..)));
}
if let Some(inst) = &d.inst {
return impure_def(inst.orig, seen) || inst.substs.iter().any(|s| impure(s.e, seen));
}
match d.body.get() {
Some(b) => impure(b, seen),
None => true,
}
}
fn impure0(n: N, seen: &mut HashSet<usize>) -> bool {
if let E::App { r, .. } = &n.e {
match r.get() {
Res::Def(d) => {
if impure_def(d, seen) {
return true;
}
}
Res::Decl(d) => {
if let Some(sd) = d.subst.get() {
if impure_def(sd, seen) {
return true;
}
}
}
Res::Local => {}
Res::None => return true,
}
}
if matches!(n.e, E::Let { .. }) {
if let E::Let { defs, .. } = &n.e {
if defs.iter().any(|d| matches!(d, LetDef::Inst(_))) {
return true;
}
}
}
let mut bad = false;
for_children(n, &mut |c| {
if !bad && impure(c, seen) {
bad = true;
}
});
bad
}
struct Comp {
nslots: usize,
stack: Vec<usize>,
budget: i64,
unstable: bool,
shape: Option<One>,
shape_ok: bool,
dshape: Option<DShape>,
dshape_ok: bool,
cache: bool,
top: bool,
descend: bool,
cache_slots: Vec<usize>,
top_slots: Vec<usize>,
units: u32,
unit_stats: Vec<Rc<UnitStat>>,
plan_id: u32,
skel: Option<Skel>,
skel_out: Option<Skel>,
}
pub struct UnitStat {
loc: Loc4,
lookups: Cell<u64>,
hits: Cell<u64>,
off: Cell<bool>,
skip: Cell<u64>,
penalty: Cell<u64>,
phase: Cell<u8>,
count: Cell<u64>,
acc: [Acc; 2],
seen: Cell<u64>,
arm: Cell<bool>,
last: Cell<(f64, f64)>,
trials: Cell<u64>,
again: Cell<u64>,
held: Cell<bool>,
mark: Cell<u64>,
mark_l: Cell<u64>,
stored: Cell<u64>,
mark_s: Cell<u64>,
}
type Loc4 = [u32; 4];
#[derive(Clone, Copy, Debug)]
pub struct Verdict {
loc: Loc4,
kind: VerdictKind,
}
#[derive(Clone, Copy, Debug)]
enum VerdictKind {
Unknown,
Pays,
DoesNotPay { penalty: u64, skip: u64 },
}
#[derive(Clone)]
struct One {
bag: bool,
elem: Rc<CE>,
clear: Vec<PreOp>,
}
#[derive(Clone, Copy)]
enum PreOp {
Clear(usize),
Intern(N),
}
#[inline]
fn run_pre(pre: &[PreOp], cx: &Cx) {
for op in pre {
match op {
PreOp::Clear(s) => cx.f.clear(*s),
PreOp::Intern(n) => {
if let E::Str(s, id) = &n.e {
cx.ev.str_node(s, id);
}
}
}
}
}
impl One {
#[inline]
fn eval(&self, cx: &Cx) -> R<Value> {
run_pre(&self.clear, cx);
(self.elem)(cx)
}
}
fn locate(es: &[Value], e: &Value) -> Option<(usize, bool)> {
let (mut lo, mut hi) = (0, es.len());
while lo < hi {
let mid = (lo + hi) / 2;
let c = match scalar_cmp(&es[mid], e) {
Some(c) => c,
None => compare(&es[mid], e).ok()?,
};
match c {
std::cmp::Ordering::Less => lo = mid + 1,
std::cmp::Ordering::Greater => hi = mid,
std::cmp::Ordering::Equal => return Some((mid, true)),
}
}
Some((lo, false))
}
fn set_one(s: &Rc<SetV>, e: &Value, add: bool) -> Option<Value> {
let es = match &s.k {
SetK::Enum(es) => es,
_ => return None,
};
let (i, found) = locate(es, e)?;
if found == add {
return Some(Value::Set(s.clone()));
}
let mut out = Vec::with_capacity(if add { es.len() + 1 } else { es.len() - 1 });
out.extend_from_slice(&es[..i]);
if add {
out.push(e.clone());
out.extend_from_slice(&es[i..]);
} else {
out.extend_from_slice(&es[i + 1..]);
}
Some(SetV::of(out))
}
enum Found {
Slot(usize),
Const(Value),
ByName(Rc<CE>, Option<usize>),
Let(Rc<Sc>, Rc<Sc>),
Arg(Rc<Sc>),
No,
}
fn lookup(sc: &Rc<Sc>, nm: Name) -> Found {
let mut cur = Some(sc);
let mut head: Option<&Rc<Sc>> = None;
while let Some(s) = cur {
match &s.k {
ScK::Slot { name, slot } if *name == nm => return Found::Slot(*slot),
ScK::Arg { name, .. } if *name == nm => return Found::Arg(s.clone()),
ScK::Let { d, head: h, .. } => {
if *h {
head = Some(s);
}
if d.name == nm {
return match head {
Some(h) => Found::Let(s.clone(), h.clone()),
None => Found::No,
};
}
}
ScK::RootVal { name, v } if *name == nm => return Found::Const(v.clone()),
ScK::Root(_) => return Found::No,
_ => {}
}
cur = s.next.as_ref();
}
Found::No
}
fn lookup_at(sc: &Rc<Sc>) -> Option<usize> {
let mut cur = Some(sc);
while let Some(s) = cur {
if let ScK::At { slot } = &s.k {
return Some(*slot);
}
cur = s.next.as_ref();
}
None
}
fn push(sc: &Rc<Sc>, k: ScK) -> Rc<Sc> {
Rc::new(Sc { k, next: Some(sc.clone()) })
}
struct Bnds {
sets: Vec<CE>,
flat: Vec<FlatB>,
}
struct FlatB {
set: usize,
slots: Vec<usize>,
tuple: bool,
}
impl Bnds {
fn each(&self, cx: &Cx, f: &mut dyn FnMut() -> R<bool>) -> R<bool> {
let mut sets: Vec<Rc<SetV>> = Vec::with_capacity(self.sets.len());
for s in &self.sets {
let s = as_set(s(cx)?)?;
s.elems()?;
sets.push(s);
}
fn rec(b: &Bnds, sets: &[Rc<SetV>], i: isize, cx: &Cx, f: &mut dyn FnMut() -> R<bool>) -> R<bool> {
if i < 0 {
return f();
}
let fb = &b.flat[i as usize];
for v in sets[fb.set].elems()? {
if fb.tuple {
match v {
Value::Tuple(t) if t.len() == fb.slots.len() => {
for (j, &s) in fb.slots.iter().enumerate() {
cx.f.set(s, t[j].clone());
}
}
_ => return bail(),
}
} else {
cx.f.set(fb.slots[0], v.clone());
}
if rec(b, sets, i - 1, cx, f)? {
return Ok(true);
}
}
Ok(false)
}
rec(self, &sets, self.flat.len() as isize - 1, cx, f)
}
}
fn simple_bound(bounds: &'static [Bound]) -> Option<(Name, N)> {
if bounds.len() != 1 {
return None;
}
one_bound(&bounds[0])
}
fn one_bound(b: &'static Bound) -> Option<(Name, N)> {
match (&b.names, &b.tuple, &b.set) {
(Some(ns), None, Some(set)) if ns.len() == 1 => Some((ns[0], set)),
_ => None,
}
}
fn closed_const(n: N) -> bool {
fn walk(n: N) -> bool {
match &n.e {
E::Num(_) | E::Str(..) | E::Bool(_) => true,
E::Op(_, a) => a.iter().all(walk),
E::App { args, sel: None, r, .. } => {
args.iter().all(walk)
&& match r.get() {
Res::Decl(d) => !d.is_var,
Res::Def(d) => d.over.get().unwrap_or(d).inst.is_none(),
_ => false,
}
}
E::Rec(fields, _) => fields.iter().all(|(_, x)| walk(x)),
_ => false,
}
}
walk(n) && crate::modules::level_of(n) == 0
}
fn var_of(n: N) -> Option<usize> {
if let E::App { args, r, .. } = &n.e {
if let Res::Decl(d) = r.get() {
if d.is_var && d.idx.get() >= 0 && args.is_empty() {
return Some(d.idx.get() as usize);
}
}
}
None
}
impl Comp {
fn new(cache: bool, plan_id: u32) -> Comp {
Comp { nslots: 0, stack: vec![], budget: 20_000, unstable: false, shape: None, shape_ok: false, dshape: None, dshape_ok: false, cache, top: false, descend: false, cache_slots: vec![], top_slots: vec![], units: 0, unit_stats: vec![], plan_id, skel: None, skel_out: None }
}
fn bounds(&mut self, bounds: &'static [Bound], sc: &Rc<Sc>) -> Option<(Bnds, Rc<Sc>)> {
if bounds.iter().any(|b| b.set.is_none() || (b.names.is_none() && b.tuple.is_none())) {
return None;
}
let mut sets = vec![];
let mut flat: Vec<(FlatB, Vec<Name>)> = vec![];
for (i, b) in bounds.iter().enumerate() {
sets.push(self.expr(b.set.as_ref().unwrap(), sc));
if let Some(t) = &b.tuple {
let slots = t.iter().map(|_| self.slot()).collect();
flat.push((FlatB { set: i, slots, tuple: true }, t.clone()));
} else {
for nm in b.names.as_ref().unwrap() {
let s = self.slot();
flat.push((FlatB { set: i, slots: vec![s], tuple: false }, vec![*nm]));
}
}
}
let mut inner = sc.clone();
for (fb, names) in flat.iter().rev() {
for (j, nm) in names.iter().enumerate() {
inner = push(&inner, ScK::Slot { name: *nm, slot: fb.slots[j] });
}
}
Some((Bnds { sets, flat: flat.into_iter().map(|x| x.0).collect() }, inner))
}
fn slot(&mut self) -> usize {
self.nslots += 1;
self.nslots - 1
}
fn fallback(&mut self, n: N, sc: &Rc<Sc>) -> CE {
if level_of(n) >= 2 {
self.unstable = true;
}
fb(n, sc)
}
fn bind_args(&mut self, params: &'static [Param], args: &'static [Node], base: Rc<Sc>, sc: &Rc<Sc>) -> Option<(Rc<Sc>, Vec<PreOp>)> {
if params.len() != args.len() || params.iter().any(|p| p.arity != 0) {
return None;
}
let mut inner = base;
let mut clear = vec![];
for (p, a) in params.iter().zip(args.iter()) {
if matches!(a.e, E::Lambda { .. }) {
return None;
}
let bd = match &a.e {
E::Num(v) => ArgBd::Const(Value::Int(*v)),
E::Bool(b) => ArgBd::Const(Value::Bool(*b)),
_ => {
let was = std::mem::replace(&mut self.unstable, false);
let ce = self.expr(a, sc);
let stable = !self.unstable;
self.unstable |= was;
let trivial = matches!(a.e, E::Str(..))
|| matches!(&a.e, E::App { name, args, r, .. } if args.is_empty() && matches!(r.get(), Res::Local) && matches!(lookup(sc, *name), Found::Slot(_) | Found::Const(_)));
if matches!(a.e, E::Str(..)) {
clear.push(PreOp::Intern(a));
}
let cache = if stable && !trivial {
let s = self.slot();
self.cache_slots.push(s);
clear.push(PreOp::Clear(s));
Some(s)
} else {
None
};
ArgBd::ByName { ce: Rc::new(ce), cache, stable }
}
};
inner = push(&inner, ScK::Arg { name: p.name, node: a, sc: sc.clone(), bd });
}
Some((inner, clear))
}
fn by_name(ce: Rc<CE>, cache: Option<usize>) -> CE {
match cache {
None => Box::new(move |cx| ce(cx)),
Some(slot) => Box::new(move |cx| {
let v = cx.f.get(slot);
if !matches!(v, Value::Undef) {
return Ok(v);
}
let v = ce(cx)?;
cx.f.set(slot, v.clone());
Ok(v)
}),
}
}
fn let_value(&mut self, def_sc: &Rc<Sc>, head: &Rc<Sc>) -> Option<CE> {
if let ScK::Let { d, cache, ce, .. } = &def_sc.k {
if !d.params.is_empty() {
return None;
}
if ce.borrow().is_none() {
let id = *d as *const DefNode as usize;
if self.stack.contains(&id) {
return None;
}
self.stack.push(id);
let was = std::mem::replace(&mut self.unstable, false);
let body = self.expr(&d.body, head);
let stable = !self.unstable;
self.unstable |= was;
self.stack.pop();
*ce.borrow_mut() = Some((Rc::new(body), stable));
}
let (c, stable) = ce.borrow().as_ref().unwrap().clone();
if !stable {
self.unstable = true;
}
return Some(Comp::by_name(c, if stable { Some(*cache) } else { None }));
}
None
}
fn let_scope(&mut self, defs: &'static [LetDef], sc: &Rc<Sc>) -> Option<(Rc<Sc>, Vec<PreOp>)> {
let mut inner = sc.clone();
let mut clear = vec![];
let n = defs.len();
for (i, d) in defs.iter().enumerate() {
let d = match d {
LetDef::Def(d) => d,
LetDef::Inst(_) => return None,
};
let cache = self.slot();
self.cache_slots.push(cache);
clear.push(PreOp::Clear(cache));
inner = push(&inner, ScK::Let { d, defs, below: sc.clone(), head: i + 1 == n, cache, ce: RefCell::new(None), root: false });
}
if n == 0 {
return None;
}
Some((inner, clear))
}
fn operand_ok(&self, f: N, sc: &Rc<Sc>) -> bool {
if tail_fn(f) {
return false;
}
match &f.e {
E::App { name, args, r, .. } if args.is_empty() => match r.get() {
Res::Local => match lookup(sc, *name) {
Found::Slot(_) | Found::Const(_) => true,
Found::Arg(a) => match &a.k {
ScK::Arg { node, sc: asc, .. } => self.operand_ok(node, asc),
_ => false,
},
Found::Let(d, _) => matches!(&d.k, ScK::Let { d, .. } if d.params.is_empty()),
_ => false,
},
Res::Decl(d) => d.subst.get().is_none(),
Res::Def(_) => false,
Res::None => false,
},
E::Dot { .. } | E::FnApp { .. } | E::Except { .. } | E::Rec(..) => true,
E::Op(K::Tuple, _) => true,
_ => false,
}
}
fn expr(&mut self, n: N, sc: &Rc<Sc>) -> CE {
self.shape = None;
self.shape_ok = false;
self.dshape = None;
self.dshape_ok = false;
let ce = self.expr0(n, sc);
if !std::mem::replace(&mut self.shape_ok, false) {
self.shape = None;
}
if !std::mem::replace(&mut self.dshape_ok, false) {
self.dshape = None;
}
ce
}
fn keep_shape(&mut self, clear: &[PreOp]) {
if let Some(sh) = &mut self.shape {
sh.clear.extend_from_slice(clear);
self.shape_ok = true;
}
if let Some(d) = self.dshape.take() {
let pre = clear.to_vec();
let prog = d.prog.clone();
self.dshape = Some(DShape {
var: d.var,
direct: d.direct && pre.is_empty(),
prog: Rc::new(move |cx, ops| {
run_pre(&pre, cx);
prog(cx, ops)
}),
});
self.dshape_ok = true;
}
}
fn expr0(&mut self, n: N, sc: &Rc<Sc>) -> CE {
self.budget -= 1;
if self.budget < 0 {
return self.fallback(n, sc);
}
match &n.e {
E::Num(v) => {
let v = *v;
Box::new(move |_| Ok(Value::Int(v)))
}
E::Bool(b) => {
let b = *b;
Box::new(move |_| Ok(Value::Bool(b)))
}
E::Str(s, id) => Box::new(move |cx| Ok(Value::Str(cx.ev.str_node(s, id)))),
E::At => match lookup_at(sc) {
Some(slot) => Box::new(move |cx| Ok(cx.f.get(slot))),
None => self.fallback(n, sc),
},
E::App { name, args, r, .. } => {
match r.get() {
Res::Local => match lookup(sc, *name) {
Found::Slot(slot) if args.is_empty() => Box::new(move |cx| Ok(cx.f.get(slot))),
Found::Const(v) if args.is_empty() => Box::new(move |_| Ok(v.clone())),
Found::Arg(a) if args.is_empty() => match &a.k {
ScK::Arg { bd: ArgBd::Const(v), .. } => {
let v = v.clone();
Box::new(move |_| Ok(v.clone()))
}
ScK::Arg { bd: ArgBd::ByName { ce, cache, stable }, .. } => {
if !*stable {
self.unstable = true;
}
Comp::by_name(ce.clone(), *cache)
}
_ => self.fallback(n, sc),
},
Found::Let(def_sc, head) => {
let d = match &def_sc.k {
ScK::Let { d, .. } => *d,
_ => unreachable!(),
};
if d.params.is_empty() {
if !args.is_empty() {
return self.fallback(n, sc);
}
match self.let_value(&def_sc, &head) {
Some(ce) => ce,
None => self.fallback(n, sc),
}
} else {
let id = d as *const DefNode as usize;
if self.stack.contains(&id) || self.stack.len() > 40 {
return self.fallback(n, sc);
}
match self.bind_args(&d.params, args, head.clone(), sc) {
Some((inner, clear)) => {
self.stack.push(id);
let body = self.expr(&d.body, &inner);
self.stack.pop();
with_clear(clear, body)
}
None => self.fallback(n, sc),
}
}
}
_ => self.fallback(n, sc),
},
Res::Decl(d) => {
if !args.is_empty() || d.subst.get().is_some() {
return self.fallback(n, sc);
}
if d.is_var {
let idx = d.idx.get();
if idx < 0 {
return self.fallback(n, sc);
}
let idx = idx as usize;
self.dshape = Some(DShape { var: idx, direct: true, prog: Rc::new(|_, _| Ok(true)) });
self.dshape_ok = true;
return Box::new(move |cx| {
let v = cx.cur.get(idx);
if matches!(v, Value::Undef) {
return bail();
}
if cx.rec_on.get() {
cx.rec.borrow_mut().reads.push((Loc::Whole(idx as u32), v.clone()));
}
Ok(v)
});
}
let v = d.value.borrow().clone();
match v {
Some(v) if plain(&v) => Box::new(move |_| Ok(v.clone())),
_ => self.fallback(n, sc),
}
}
Res::Def(d) => self.def_app(n, d, args, sc),
Res::None => self.fallback(n, sc),
}
}
E::Op(k, a) => self.op(n, *k, a, sc),
E::Case { arms, other } => {
let arms: Vec<(CE, CE)> = arms.iter().map(|(c, e)| (self.expr(c, sc), self.expr(e, sc))).collect();
let other = other.as_ref().map(|o| self.expr(o, sc));
Box::new(move |cx| {
for (c, e) in &arms {
if truth(c(cx)?)? {
return e(cx);
}
}
match &other {
Some(o) => o(cx),
None => bail(),
}
})
}
E::Let { defs, body } => match self.let_scope(defs, sc) {
Some((inner, clear)) => {
let body = self.expr(body, &inner);
with_clear(clear, body)
}
None => self.fallback(n, sc),
},
E::Quant { forall, bounds, body } => {
let (nm, set) = match simple_bound(bounds) {
Some(x) => x,
None => {
let (b, inner) = match self.bounds(bounds, sc) {
Some(x) => x,
None => return self.fallback(n, sc),
};
let body = self.expr(body, &inner);
let forall = *forall;
return Box::new(move |cx| {
let stopped = b.each(cx, &mut || Ok(truth(body(cx)?)? != forall))?;
Ok(Value::Bool(stopped != forall))
});
}
};
let set = self.expr(set, sc);
let slot = self.slot();
let body = self.expr(body, &push(sc, ScK::Slot { name: nm, slot }));
let forall = *forall;
Box::new(move |cx| {
let s = as_set(set(cx)?)?;
if let SetK::Interval(lo, hi) = &s.k {
for i in *lo..=*hi {
cx.f.set(slot, Value::Int(i));
if truth(body(cx)?)? != forall {
return Ok(Value::Bool(!forall));
}
}
return Ok(Value::Bool(forall));
}
for x in s.elems()? {
cx.f.set(slot, x.clone());
if truth(body(cx)?)? != forall {
return Ok(Value::Bool(!forall));
}
}
Ok(Value::Bool(forall))
})
}
E::Choose { bound, body } => {
let (nm, set) = match one_bound(bound) {
Some(x) => x,
None => return self.fallback(n, sc),
};
let set = self.expr(set, sc);
let slot = self.slot();
let always = matches!(body.e, E::Bool(true));
let body = self.expr(body, &push(sc, ScK::Slot { name: nm, slot }));
Box::new(move |cx| {
let s = as_set(set(cx)?)?;
let es = s.elems()?;
if always {
if let Some(x) = es.first() {
return Ok(x.clone());
}
}
for x in es {
cx.f.set(slot, x.clone());
if truth(body(cx)?)? {
return Ok(x.clone());
}
}
bail()
})
}
E::SetFilter { bound, pred } => {
let (nm, set) = match one_bound(bound) {
Some(x) => x,
None => return self.fallback(n, sc),
};
let set = self.expr(set, sc);
let slot = self.slot();
let pred = self.expr(pred, &push(sc, ScK::Slot { name: nm, slot }));
let lazy = self.fallback(n, sc);
Box::new(move |cx| {
let s = as_set(set(cx)?)?;
if !s.reducible() {
return lazy(cx);
}
let mut out = Vec::new();
for x in s.elems()? {
cx.f.set(slot, x.clone());
if truth(pred(cx)?)? {
out.push(x.clone());
}
}
Ok(SetV::of(out))
})
}
E::SetMap { e, bounds } => {
let (nm, set) = match simple_bound(bounds) {
Some(x) => x,
None => {
let (b, inner) = match self.bounds(bounds, sc) {
Some(x) => x,
None => return self.fallback(n, sc),
};
let e = self.expr(e, &inner);
return Box::new(move |cx| {
let mut out: Vec<Value> = Vec::new();
b.each(cx, &mut || {
out.push(e(cx)?);
Ok(false)
})?;
mk_set(out)
});
}
};
let set = self.expr(set, sc);
let slot = self.slot();
let e = self.expr(e, &push(sc, ScK::Slot { name: nm, slot }));
Box::new(move |cx| {
let s = as_set(set(cx)?)?;
let mut out: Vec<Value> = Vec::new();
if let SetK::Interval(lo, hi) = &s.k {
for i in *lo..=*hi {
cx.f.set(slot, Value::Int(i));
out.push(e(cx)?);
}
} else {
for x in s.elems()? {
cx.f.set(slot, x.clone());
out.push(e(cx)?);
}
}
mk_set(out)
})
}
E::FnDef { bounds, body, is_rec, .. } => {
let (nm, set) = match simple_bound(bounds) {
Some(x) if !is_rec.get() => x,
_ => return self.fallback(n, sc),
};
let set = self.expr(set, sc);
let slot = self.slot();
let body = self.expr(body, &push(sc, ScK::Slot { name: nm, slot }));
let lazy = self.fallback(n, sc);
Box::new(move |cx| {
let s = as_set(set(cx)?)?;
if !s.enumerable() {
return lazy(cx);
}
if let SetK::Interval(1, hi) = &s.k {
let mut vals = Vec::with_capacity((*hi).max(0) as usize);
for i in 1..=*hi {
cx.f.set(slot, Value::Int(i));
vals.push(body(cx)?);
}
return Ok(if vals.is_empty() { empty_tuple() } else { Value::Tuple(TupleV::rc(vals)) });
}
let dom = s.elems()?;
let mut vals = Vec::with_capacity(dom.len());
for d in dom {
cx.f.set(slot, d.clone());
vals.push(body(cx)?);
}
Ok(s.fcn_shape()?.make(vals))
})
}
E::Rec(fields, shape) => {
let ces: Vec<CE> = fields.iter().map(|(_, e)| self.expr(e, sc)).collect();
Box::new(move |cx| {
let mut vals = Vec::with_capacity(ces.len());
for c in &ces {
vals.push(c(cx)?);
}
let sh = shape.get_or_init(|| {
let mut idx: Vec<usize> = (0..fields.len()).collect();
idx.sort_by_key(|&i| fields[i].0 .0);
let sorted = idx.iter().enumerate().all(|(i, &j)| i == j);
RecShape { names: Rc::new(idx.iter().map(|&i| fields[i].0).collect()), order: if sorted { None } else { Some(idx) } }
});
let vals = match &sh.order {
None => vals,
Some(idx) => {
let mut slots: Vec<Option<Value>> = vals.into_iter().map(Some).collect();
idx.iter().map(|&i| slots[i].take().unwrap()).collect()
}
};
Ok(Value::Record(Rc::new(RecordV::new(sh.names.clone(), vals))))
})
}
E::Except { base, updates } => {
let base = self.expr(base, sc);
let dbase = self.dshape.take();
let at = self.slot();
let inner = push(sc, ScK::At { slot: at });
if let (Some(d), true, true) = (&dbase, updates.len() == 1, self.cache) {
if d.direct {
let (path, val) = &updates[0];
let pces: Rc<Vec<CE>> = Rc::new(path.iter().map(|x| self.expr(x, sc)).collect());
let vce: Rc<CE> = Rc::new(self.expr(val, &inner));
let needs_old = path.len() > 1 || uses_at(val);
let var = d.var;
let (p2, v2) = (pces.clone(), vce.clone());
self.dshape = Some(DShape {
var,
direct: false,
prog: Rc::new(move |cx, ops| {
let mut keys = Vec::with_capacity(p2.len());
for x in p2.iter() {
keys.push(x(cx)?);
}
let cur = cx.cur.get(var);
let mut newv = None;
cx.ev.except_with(
cur.clone(),
&keys,
0,
&mut |old| {
cx.f.set(at, old);
let v = v2(cx)?;
newv = Some(v.clone());
Ok(v)
},
n,
)?;
let v = match newv {
Some(v) => v,
None => return Ok(false),
};
if needs_old {
match sc_::elem_at(&cur, &keys[0]) {
Some(old) => cx.rec.borrow_mut().reads.push((Loc::Elem(var as u32, keys[0].clone()), old)),
None => return Ok(false),
}
}
ops.push(DOp::Exc { path: keys, v });
Ok(true)
}),
});
self.dshape_ok = true;
return Box::new(move |cx| {
let f = base(cx)?;
let mut short: [Value; 2] = [Value::Undef, Value::Undef];
let long: Vec<Value>;
let keys: &[Value] = if pces.len() <= 2 {
for (i, x) in pces.iter().enumerate() {
short[i] = x(cx)?;
}
&short[..pces.len()]
} else {
let mut v = Vec::with_capacity(pces.len());
for x in pces.iter() {
v.push(x(cx)?);
}
long = v;
&long
};
cx.ev.except_with(
f,
keys,
0,
&mut |old| {
cx.f.set(at, old);
vce(cx)
},
n,
)
});
}
}
let ups: Vec<(Vec<CE>, CE)> = updates.iter().map(|(path, val)| (path.iter().map(|x| self.expr(x, sc)).collect(), self.expr(val, &inner))).collect();
Box::new(move |cx| {
let mut f = base(cx)?;
for (path, val) in &ups {
let mut short: [Value; 2] = [Value::Undef, Value::Undef];
let long: Vec<Value>;
let p: &[Value] = if path.len() <= 2 {
for (i, x) in path.iter().enumerate() {
short[i] = x(cx)?;
}
&short[..path.len()]
} else {
let mut v = Vec::with_capacity(path.len());
for x in path {
v.push(x(cx)?);
}
long = v;
&long
};
f = cx.ev.except_with(
f,
p,
0,
&mut |old| {
cx.f.set(at, old);
val(cx)
},
n,
)?;
}
Ok(f)
})
}
E::FnApp { f, args } => {
if !self.operand_ok(f, sc) {
return self.fallback(n, sc);
}
let fvar = var_of(f);
let fce = self.expr(f, sc);
let args: Vec<CE> = args.iter().map(|a| self.expr(a, sc)).collect();
if let (Some(idx), true) = (fvar, self.cache) {
return Box::new(move |cx| {
let fv = cx.cur.get(idx);
let arg = if args.len() == 1 {
args[0](cx)?
} else {
let mut t = Vec::with_capacity(args.len());
for a in &args {
t.push(a(cx)?);
}
Value::Tuple(TupleV::rc(t))
};
let v = cx.ev.apply_fcn(&fv, &arg, Some(n))?;
if cx.rec_on.get() {
cx.rec.borrow_mut().reads.push((Loc::Elem(idx as u32, arg), v.clone()));
}
if let Value::LazyFcn(lf) = &v {
return lf.force(cx.ev);
}
Ok(v)
});
}
Box::new(move |cx| {
let fv = fce(cx)?;
let arg = if args.len() == 1 {
args[0](cx)?
} else {
let mut t = Vec::with_capacity(args.len());
for a in &args {
t.push(a(cx)?);
}
Value::Tuple(TupleV::rc(t))
};
let v = cx.ev.apply_fcn(&fv, &arg, Some(n))?;
if let Value::LazyFcn(lf) = &v {
return lf.force(cx.ev);
}
Ok(v)
})
}
E::Dot { e, field } => {
let e = self.expr(e, sc);
let field = *field;
Box::new(move |cx| match e(cx)? {
Value::Record(rec) => match rec.get(field) {
Some(v) => Ok(v.clone()),
None => bail(),
},
_ => bail(),
})
}
_ => self.fallback(n, sc),
}
}
fn def_app(&mut self, n: N, d: &'static Def, args: &'static [Node], sc: &Rc<Sc>) -> CE {
let d = d.over.get().unwrap_or(d);
if let Some(bi) = d.builtin.get() {
return self.builtin(n, d, bi, args, sc);
}
let body = match d.body.get() {
Some(b) if d.inst.is_none() => b,
_ => return self.fallback(n, sc),
};
let id = d as *const Def as usize;
if self.stack.contains(&id) || self.stack.len() > 40 {
return self.fallback(n, sc);
}
if d.params.is_empty() && args.is_empty() && crate::modules::def_level(d) == 0 {
let cell: OnceCell<Value> = OnceCell::new();
return Box::new(move |cx| {
if let Some(v) = cell.get() {
return Ok(v.clone());
}
let v = cx.ev.eval(n, &None)?;
if d.cache.borrow().is_some() && plain(&v) {
let _ = cell.set(v.clone());
}
Ok(v)
});
}
let root = Rc::new(Sc { k: ScK::Root(None), next: None });
match self.bind_args(&d.params, args, root, sc) {
Some((inner, clear)) => {
self.stack.push(id);
let b = self.expr(body, &inner);
self.stack.pop();
self.keep_shape(&clear);
with_clear(clear, b)
}
None => self.fallback(n, sc),
}
}
fn builtin(&mut self, n: N, d: &'static Def, bi: &'static Builtin, args: &'static [Node], sc: &Rc<Sc>) -> CE {
if bi.lazy || bi.ops != 0 {
return self.fallback(n, sc);
}
if let BFn::Const(v) = &bi.f {
if args.is_empty() && plain(v) {
return Box::new(move |_| Ok(v.clone()));
}
}
let bags = name_str(d.module).as_ref() == "Bags";
let opname = name_str(d.name);
let mut ces: Vec<CE> = Vec::with_capacity(args.len());
let mut last_shape = None;
let mut first_d = None;
for (i, a) in args.iter().enumerate() {
ces.push(self.expr(a, sc));
last_shape = self.shape.take();
if i == 0 {
first_d = self.dshape.take();
}
}
if bags && args.len() == 2 && opname.as_ref() == "BagIn" && self.cache {
if let Some(idx) = var_of(&args[1]) {
let x = ces.into_iter().next().unwrap();
return Box::new(move |cx| {
let x = x(cx)?;
let b = cx.cur.get(idx);
let r = cx.ev.call_builtin(bi, &[x.clone(), b])?;
if cx.rec_on.get() {
cx.rec.borrow_mut().reads.push((Loc::Member(idx as u32, x), r.clone()));
}
Ok(r)
});
}
}
if bags && args.len() == 1 && opname.as_ref() == "SetToBag" {
if let Some(sh) = last_shape.take() {
if !sh.bag {
self.shape = Some(One { bag: true, ..sh });
self.shape_ok = true;
}
}
}
if bags && args.len() == 2 && matches!(opname.as_ref(), "\\oplus" | "\\ominus") {
if let Some(sh) = last_shape.take() {
if sh.bag {
let plus = opname.as_ref() == "\\oplus";
if let Some(d) = first_d {
let sh2 = sh.clone();
self.dshape = Some(DShape {
var: d.var,
direct: false,
prog: Rc::new(move |cx, ops| {
if !(d.prog)(cx, ops)? {
return Ok(false);
}
ops.push(DOp::Bag { e: sh2.eval(cx)?, plus });
Ok(true)
}),
});
self.dshape_ok = true;
}
let mut it = ces.into_iter();
let (b, one) = (it.next().unwrap(), it.next().unwrap());
return Box::new(move |cx| {
let bv = b(cx)?;
let e = sh.eval(cx)?;
if let Some(v) = crate::stdlib::bag_one(&bv, &e, plus) {
return Ok(v);
}
let y = one(cx)?;
cx.ev.call_builtin(bi, &[bv, y])
});
}
}
if let (Some(d), true) = (first_d, opname.as_ref() == "\\oplus") {
let mut it = ces.into_iter();
let (b, c) = (it.next().unwrap(), Rc::new(it.next().unwrap()));
let c2 = c.clone();
self.dshape = Some(DShape {
var: d.var,
direct: false,
prog: Rc::new(move |cx, ops| {
if !(d.prog)(cx, ops)? {
return Ok(false);
}
let cv = c2(cx)?;
if !crate::value::is_fcn(&cv) {
return Ok(false);
}
let (es, ns) = (crate::value::domain_elems(&cv)?, crate::value::fcn_values(&cv)?);
let mut total = 0;
for (e, k) in es.iter().zip(ns) {
let Value::Int(k) = k else { return Ok(false) };
total += *k;
if *k < 1 || total > BAG_DELTA_MAX {
return Ok(false);
}
for _ in 0..*k {
ops.push(DOp::Bag { e: e.clone(), plus: true });
}
}
Ok(true)
}),
});
self.dshape_ok = true;
return Box::new(move |cx| {
let bv = b(cx)?;
let cv = c(cx)?;
cx.ev.call_builtin(bi, &[bv, cv])
});
}
}
if bi.arith != 0 && ces.len() == 2 {
let mut it = ces.into_iter();
let (x, y) = (it.next().unwrap(), it.next().unwrap());
let arith = bi.arith;
return Box::new(move |cx| {
let x = x(cx)?;
let y = y(cx)?;
if let (Value::Int(a), Value::Int(b)) = (&x, &y) {
let (a, b) = (*a, *b);
std::mem::forget(x); std::mem::forget(y);
match arith {
1 | 2 => {
let r = if arith == 1 { a.checked_add(b) } else { a.checked_sub(b) };
if let Some(r) = r {
if (-2147483648..=2147483647).contains(&r) {
return Ok(Value::Int(r));
}
}
}
3 => return Ok(Value::Bool(a < b)),
4 => return Ok(Value::Bool(a > b)),
5 => return Ok(Value::Bool(a <= b)),
_ => return Ok(Value::Bool(a >= b)),
}
return cx.ev.call_builtin(bi, &[Value::Int(a), Value::Int(b)]);
}
cx.ev.call_builtin(bi, &[x, y])
});
}
match ces.len() {
1 => {
let a = ces.into_iter().next().unwrap();
Box::new(move |cx| cx.ev.call_builtin(bi, &[a(cx)?]))
}
2 => {
let mut it = ces.into_iter();
let (a, b) = (it.next().unwrap(), it.next().unwrap());
Box::new(move |cx| {
let x = a(cx)?;
let y = b(cx)?;
cx.ev.call_builtin(bi, &[x, y])
})
}
_ => Box::new(move |cx| {
let mut v = Vec::with_capacity(ces.len());
for c in &ces {
v.push(c(cx)?);
}
cx.ev.call_builtin(bi, &v)
}),
}
}
fn op(&mut self, n: N, k: K, a: &'static [Node], sc: &Rc<Sc>) -> CE {
match k {
K::And | K::Or => {
let ces: Vec<CE> = a.iter().map(|x| self.expr(x, sc)).collect();
let stop = k == K::Or;
Box::new(move |cx| {
for c in &ces {
if truth(c(cx)?)? == stop {
return Ok(Value::Bool(stop));
}
}
Ok(Value::Bool(!stop))
})
}
K::Not => {
let x = self.expr(&a[0], sc);
Box::new(move |cx| Ok(Value::Bool(!truth(x(cx)?)?)))
}
K::Imp => {
let (x, y) = (self.expr(&a[0], sc), self.expr(&a[1], sc));
Box::new(move |cx| Ok(Value::Bool(!truth(x(cx)?)? || truth(y(cx)?)?)))
}
K::Eqv => {
let (x, y) = (self.expr(&a[0], sc), self.expr(&a[1], sc));
Box::new(move |cx| Ok(Value::Bool(truth(x(cx)?)? == truth(y(cx)?)?)))
}
K::Eq | K::Ne => {
let want = k == K::Eq;
let x = self.expr(&a[0], sc);
if matches!(&a[1].e, E::Op(K::SetEnum, e) if e.is_empty()) {
return Box::new(move |cx| {
let x = x(cx)?;
let eq = match &x {
Value::Set(s) if matches!(s.k, SetK::Enum(_)) => match &s.k {
SetK::Enum(el) => el.is_empty(),
_ => unreachable!(),
},
_ => val_eq(&x, &Value::Set(empty_set()))?,
};
Ok(Value::Bool(eq == want))
});
}
let y = self.expr(&a[1], sc);
Box::new(move |cx| {
let (x, y) = (x(cx)?, y(cx)?);
let eq = match scalar_cmp(&x, &y) {
Some(c) => c == std::cmp::Ordering::Equal,
None => val_eq(&x, &y)?,
};
Ok(Value::Bool(eq == want))
})
}
K::If => {
let c: Rc<CE> = Rc::new(self.expr(&a[0], sc));
let t = self.expr(&a[1], sc);
let dt = self.dshape.take();
let e = self.expr(&a[2], sc);
let de = self.dshape.take();
if let (Some(dt), Some(de)) = (dt, de) {
if dt.var == de.var {
let c2 = c.clone();
self.dshape = Some(DShape { var: dt.var, direct: false, prog: Rc::new(move |cx, ops| if truth(c2(cx)?)? { (dt.prog)(cx, ops) } else { (de.prog)(cx, ops) }) });
self.dshape_ok = true;
}
}
Box::new(move |cx| if truth(c(cx)?)? { t(cx) } else { e(cx) })
}
K::In | K::NotIn => {
let want = k == K::In;
let (x, s) = (self.expr(&a[0], sc), self.expr(&a[1], sc));
if let (Some(idx), true) = (var_of(&a[1]), self.cache) {
return Box::new(move |cx| {
let x = x(cx)?;
let r = as_set(cx.cur.get(idx))?.contains(&x)?;
if cx.rec_on.get() {
cx.rec.borrow_mut().reads.push((Loc::Member(idx as u32, x), Value::Bool(r)));
}
Ok(Value::Bool(r == want))
});
}
Box::new(move |cx| {
let x = x(cx)?;
Ok(Value::Bool(as_set(s(cx)?)?.contains(&x)? == want))
})
}
K::Cup => {
let x = self.expr(&a[0], sc);
let dx = self.dshape.take();
let y = self.expr(&a[1], sc);
if let Some(sh) = self.shape.take().filter(|sh| !sh.bag) {
if let Some(d) = dx {
let sh2 = sh.clone();
self.dshape = Some(DShape {
var: d.var,
direct: false,
prog: Rc::new(move |cx, ops| {
if !(d.prog)(cx, ops)? {
return Ok(false);
}
ops.push(DOp::Set { e: sh2.eval(cx)?, add: true });
Ok(true)
}),
});
self.dshape_ok = true;
}
return Box::new(move |cx| {
let xs = as_set(x(cx)?)?;
let e = sh.eval(cx)?;
if let Some(v) = set_one(&xs, &e, true) {
return Ok(v);
}
let ys = as_set(y(cx)?)?;
cx.ev.set_union(xs, ys)
});
}
Box::new(move |cx| {
let x = as_set(x(cx)?)?;
let y = as_set(y(cx)?)?;
cx.ev.set_union(x, y)
})
}
K::Diff | K::Cap => {
let x = self.expr(&a[0], sc);
let dx = self.dshape.take();
let y = self.expr(&a[1], sc);
let diff = k == K::Diff;
let one = self.shape.take().filter(|sh| !sh.bag && diff);
if let (Some(d), Some(sh2)) = (dx, one.clone()) {
self.dshape = Some(DShape {
var: d.var,
direct: false,
prog: Rc::new(move |cx, ops| {
if !(d.prog)(cx, ops)? {
return Ok(false);
}
ops.push(DOp::Set { e: sh2.eval(cx)?, add: false });
Ok(true)
}),
});
self.dshape_ok = true;
}
Box::new(move |cx| {
if let Some(sh) = &one {
let xs = as_set(x(cx)?)?;
let e = sh.eval(cx)?;
if let Some(v) = set_one(&xs, &e, false) {
return Ok(v);
}
}
let x = as_set(x(cx)?)?;
let y = as_set(y(cx)?)?;
if x.reducible() && (diff || y.reducible()) {
let mut out = Vec::new();
for e in x.elems()? {
if y.contains(e)? != diff {
out.push(e.clone());
}
}
return Ok(SetV::of(out));
}
Ok(Value::Set(Rc::new(SetV::new(if diff { SetK::Diff(x, y) } else { SetK::Cap(x, y) }))))
})
}
K::SetEnum if a.len() == 1 => {
let e: Rc<CE> = Rc::new(self.expr(&a[0], sc));
self.shape = Some(One { bag: false, elem: e.clone(), clear: vec![] });
self.shape_ok = true;
Box::new(move |cx| Ok(SetV::of(vec![e(cx)?])))
}
K::SetEnum => {
let ces: Vec<CE> = a.iter().map(|x| self.expr(x, sc)).collect();
Box::new(move |cx| {
let mut out = Vec::with_capacity(ces.len());
for c in &ces {
out.push(c(cx)?);
}
mk_set(out)
})
}
K::Tuple => {
let ces: Vec<CE> = a.iter().map(|x| self.expr(x, sc)).collect();
Box::new(move |cx| {
let mut out = Vec::with_capacity(ces.len());
for c in &ces {
out.push(c(cx)?);
}
Ok(Value::Tuple(TupleV::rc(out)))
})
}
K::Domain => {
if !self.operand_ok(&a[0], sc) {
return self.fallback(n, sc);
}
let f = self.expr(&a[0], sc);
Box::new(move |cx| {
let f = f(cx)?;
if let Value::LazyFcn(lf) = &f {
return Ok(Value::Set(lf.domain(cx.ev)?));
}
match domain_of(&f) {
Some(d) => Ok(Value::Set(d)),
None => bail(),
}
})
}
_ => self.fallback(n, sc),
}
}
fn unchanged_vars(&self, e: N, out: &mut Vec<u32>, depth: u32) -> bool {
if depth > 16 {
return false;
}
if let Some(i) = var_of(e) {
out.push(i as u32);
return true;
}
match &e.e {
E::Op(K::Tuple, items) => items.iter().all(|x| self.unchanged_vars(x, out, depth + 1)),
E::App { args, r, .. } if args.is_empty() => match r.get() {
Res::Def(d) => {
let d = d.over.get().unwrap_or(d);
match d.body.get() {
Some(b) if d.builtin.get().is_none() && d.inst.is_none() && d.params.is_empty() && matches!(b.e, E::Op(K::Tuple, _)) => self.unchanged_vars(b, out, depth + 1),
_ => false,
}
}
_ => false,
},
_ => false,
}
}
fn act(&mut self, n: N, sc: &Rc<Sc>) -> AE {
let inner = self.act_inner(n, sc);
let sc = sc.clone();
Box::new(move |cx, k| {
if cx.ev.n_assigned.get() == cx.nv {
if std::env::var_os("EVERYSTATE_EVAL_DEBUG").is_some() {
eprintln!("fallback all-assigned at line {} col {}", n.loc[0], n.loc[1]);
}
cx.fallbacks.set(cx.fallbacks.get() + 1);
let ctx = build(&sc, cx, false)?;
return cx.ev.enumerate(n, &ctx, k);
}
inner(cx, k)
})
}
fn guard(&mut self, n: N, sc: &Rc<Sc>) -> AE {
let ce = self.expr(n, sc);
Box::new(move |cx, k| if truth(ce(cx)?)? { k() } else { Ok(()) })
}
fn act_inner(&mut self, n: N, sc: &Rc<Sc>) -> AE {
let top = std::mem::replace(&mut self.top, false);
if !top || !self.cache {
let r = self.act_inner0(n, sc);
self.skel = None;
return r;
}
let structural = match &n.e {
E::Op(K::Or, _) | E::Let { .. } | E::Quant { forall: false, .. } => true,
E::App { r, .. } => matches!(r.get(), Res::Def(d) if d.over.get().unwrap_or(d).builtin.get().is_none()),
_ => false,
};
if structural {
self.descend = true;
let r = self.act_inner0(n, sc);
self.descend = false;
return r;
}
self.unit(n, sc)
}
fn unit(&mut self, n: N, sc: &Rc<Sc>) -> AE {
let inner = self.act_inner0(n, sc);
let bound = self.top_slots.clone();
let clear = self.cache_slots.clone();
let id = (self.plan_id << 16) | self.units;
self.units += 1;
let stat = Rc::new(UnitStat { loc: n.loc, lookups: Cell::new(0), hits: Cell::new(0), off: Cell::new(false), skip: Cell::new(0), penalty: Cell::new(0), phase: Cell::new(PH_WARM), count: Cell::new(0), acc: Default::default(), seen: Cell::new(0), arm: Cell::new(false), last: Cell::new((0.0, 0.0)), trials: Cell::new(0), again: Cell::new(1 << 20), held: Cell::new(false), mark: Cell::new(0), mark_l: Cell::new(0), stored: Cell::new(0), mark_s: Cell::new(0) });
self.unit_stats.push(stat.clone());
let node = Rc::new(UnitNode { id, bound, clear, inner, stat, n });
self.skel = Some(Skel::Unit(node.clone()));
Box::new(move |cx, k| unit_run(cx, k, &node))
}
fn act_inner0(&mut self, n: N, sc: &Rc<Sc>) -> AE {
let desc = self.descend;
self.skel_out = None;
let r = self.act_inner1(n, sc);
let out = self.skel_out.take();
self.skel = if desc { out } else { None };
r
}
fn act_inner1(&mut self, n: N, sc: &Rc<Sc>) -> AE {
let desc = std::mem::replace(&mut self.descend, false);
self.budget -= 1;
if self.budget < 0 {
return fb_act(n, sc);
}
match &n.e {
E::Op(k, a) => match k {
K::And => {
let items: Vec<AE> = a.iter().enumerate().map(|(i, x)| if i == 0 { self.act_inner(x, sc) } else { self.act(x, sc) }).collect();
if items.is_empty() {
return Box::new(|_, k| k());
}
fn conj(items: &[AE], i: usize, cx: &Cx, k: &mut dyn FnMut() -> R<()>) -> R<()> {
if i + 1 == items.len() {
return items[i](cx, k);
}
items[i](cx, &mut || conj(items, i + 1, cx, &mut *k))
}
Box::new(move |cx, k| conj(&items, 0, cx, k))
}
K::Or => {
let mut sks = Some(vec![]);
let items: Vec<AE> = a
.iter()
.map(|x| {
self.top = desc;
let it = self.act_inner(x, sc);
match (self.skel.take(), &mut sks) {
(Some(s), Some(v)) => v.push(s),
_ => sks = None,
}
it
})
.collect();
self.skel_out = sks.map(Skel::Or);
Box::new(move |cx, k| {
for it in &items {
it(cx, k)?;
}
Ok(())
})
}
K::If => {
let c = self.expr(&a[0], sc);
let (t, e) = (self.act_inner(&a[1], sc), self.act_inner(&a[2], sc));
Box::new(move |cx, k| if truth(c(cx)?)? { t(cx, k) } else { e(cx, k) })
}
K::Eq | K::In => {
let tgt = match &a[0].e {
E::Op(K::Prime, p) => var_of(&p[0]),
_ => None,
};
match tgt {
Some(idx) => {
let rhs = self.expr(&a[1], sc);
let dprog = self.dshape.take().filter(|d| d.var == idx && self.cache && *k == K::Eq).map(|d| d.prog);
let assigned = fb_act(n, sc);
let is_eq = *k == K::Eq;
Box::new(move |cx, k| {
if !cx.nxt.is_undef(idx) {
return assigned(cx, k);
}
let ev = cx.ev;
if let (Some(prog), true) = (&dprog, cx.rec_on.get()) {
let mut ops = vec![];
if prog(cx, &mut ops)? {
if let Some(v) = apply_ops(cx, &cx.cur.get(idx), &ops, n) {
cx.nxt.set(idx, v);
cx.rec.borrow_mut().delta[idx] = Some(ops);
ev.n_assigned.set(ev.n_assigned.get() + 1);
let r = k();
cx.nxt.clear(idx);
cx.rec.borrow_mut().delta[idx] = None;
ev.n_assigned.set(ev.n_assigned.get() - 1);
return r;
}
}
}
if is_eq {
let v = ev.settle(rhs(cx)?)?;
cx.nxt.set(idx, v);
ev.n_assigned.set(ev.n_assigned.get() + 1);
let r = k();
cx.nxt.clear(idx);
ev.n_assigned.set(ev.n_assigned.get() - 1);
return r;
}
let s = as_set(rhs(cx)?)?;
let es = s.elems()?;
ev.n_assigned.set(ev.n_assigned.get() + 1);
let mut r = Ok(());
for x in es {
cx.nxt.set(idx, ev.settle(x.clone())?);
r = k();
if r.is_err() {
break;
}
}
cx.nxt.clear(idx);
ev.n_assigned.set(ev.n_assigned.get() - 1);
r
})
}
None => match &a[0].e {
E::Op(K::Prime, _) | E::Let { .. } => fb_act(n, sc),
E::App { name, args, r, .. } if args.is_empty() => match r.get() {
Res::Local if matches!(lookup(sc, *name), Found::Slot(_) | Found::Const(_)) => self.guard(n, sc),
Res::Decl(d) if d.subst.get().is_none() => self.guard(n, sc),
_ => fb_act(n, sc),
},
_ => self.guard(n, sc),
},
}
}
K::Unchanged => {
let mut idxs = vec![];
if !self.unchanged_vars(&a[0], &mut idxs, 0) || idxs.len() > 32 {
return fb_act(n, sc);
}
let mut seen: u64 = 0;
let distinct = idxs.iter().all(|&i| {
let fresh = i < 64 && seen & (1 << i) == 0;
seen |= 1 << (i & 63);
fresh
});
Box::new(move |cx, k| {
let ev = cx.ev;
if distinct && cx.nxt.can_fill(cx.cur, &idxs) {
cx.nxt.fill(cx.cur, &idxs);
ev.n_assigned.set(ev.n_assigned.get() + idxs.len() as i64);
if cx.rec_on.get() {
let mut rec = cx.rec.borrow_mut();
for &i in &idxs {
rec.delta[i as usize] = Some(vec![]);
}
}
let r = k();
if cx.rec_on.get() {
let mut rec = cx.rec.borrow_mut();
for &i in &idxs {
rec.delta[i as usize] = None;
}
}
for &i in idxs.iter().rev() {
cx.nxt.clear(i as usize);
}
ev.n_assigned.set(ev.n_assigned.get() - idxs.len() as i64);
return r;
}
if cx.rec_on.get() {
cx.rec.borrow_mut().bad = true;
}
let mut set: u32 = 0;
let mut r: R<()> = Ok(());
let mut go_on = true;
for (j, &i) in idxs.iter().enumerate() {
let i = i as usize;
if cx.nxt.is_undef(i) {
let v = cx.cur.get(i);
if matches!(v, Value::Undef) {
r = bail();
go_on = false;
break;
}
cx.nxt.set(i, v);
ev.n_assigned.set(ev.n_assigned.get() + 1);
set |= 1 << j;
} else {
match val_eq(&cx.nxt.get(i), &cx.cur.get(i)) {
Ok(true) => {}
Ok(false) => {
go_on = false;
break;
}
Err(e) => {
r = Err(e);
go_on = false;
break;
}
}
}
}
if go_on {
r = k();
}
for j in (0..idxs.len()).rev() {
if set & (1 << j) != 0 {
cx.nxt.clear(idxs[j] as usize);
}
}
ev.n_assigned.set(ev.n_assigned.get() - set.count_ones() as i64);
r
})
}
K::Imp | K::Subseteq | K::BoxAct | K::AngleAct | K::Cdot => fb_act(n, sc),
_ => self.guard(n, sc),
},
E::Quant { forall: false, bounds, body } => {
let (nm, set) = match simple_bound(bounds) {
Some(x) => x,
None => {
let (b, inner) = match self.bounds(bounds, sc) {
Some(x) => x,
None => return fb_act(n, sc),
};
self.top = desc;
let mark = self.top_slots.len();
if desc {
for fb in &b.flat {
self.top_slots.extend_from_slice(&fb.slots);
}
}
let body = self.act_inner(body, &inner);
self.top_slots.truncate(mark);
return Box::new(move |cx, k| {
b.each(cx, &mut || {
body(cx, &mut *k)?;
Ok(false)
})?;
Ok(())
});
}
};
let set_n = set;
let set: Rc<CE> = Rc::new(self.expr(set, sc));
let slot = self.slot();
self.top = desc;
if desc {
self.top_slots.push(slot);
}
let body = self.act_inner(body, &push(sc, ScK::Slot { name: nm, slot }));
let body_skel = self.skel.take();
if desc {
self.top_slots.pop();
}
let set2 = set.clone();
let run: Rc<AE> = Rc::new(Box::new(move |cx, k| {
let s = as_set(set(cx)?)?;
if let SetK::Interval(lo, hi) = &s.k {
for i in *lo..=*hi {
cx.f.set(slot, Value::Int(i));
body(cx, k)?;
}
return Ok(());
}
for x in s.elems()? {
cx.f.set(slot, x.clone());
body(cx, k)?;
}
Ok(())
}));
if let (true, Some(b)) = (desc, body_skel) {
let dom = match &set_n.e {
E::Op(K::Domain, a) if a.len() == 1 && var_of(&a[0]).is_some() => Some(Dom::Domain(var_of(&a[0]).unwrap())),
_ if closed_const(set_n) => Some(Dom::Const { set: set2, ids: OnceCell::new() }),
_ => None,
};
self.skel_out = dom.map(|dom| Skel::Exists { slot, dom, body: Box::new(b), run: run.clone() });
}
Box::new(move |cx, k| run(cx, k))
}
E::Quant { .. } => fb_act(n, sc),
E::Case { arms, other } => {
let arms: Vec<(CE, AE)> = arms.iter().map(|(c, e)| (self.expr(c, sc), self.act_inner(e, sc))).collect();
let other = other.as_ref().map(|o| self.act_inner(o, sc));
Box::new(move |cx, k| {
for (c, e) in &arms {
if truth(c(cx)?)? {
return e(cx, k);
}
}
match &other {
Some(o) => o(cx, k),
None => bail(),
}
})
}
E::Let { defs, body } => match self.let_scope(defs, sc) {
Some((inner, clear)) => {
self.top = desc;
let body = self.act_inner(body, &inner);
if let (true, Some(b)) = (desc, self.skel.take()) {
self.skel_out = Some(Skel::Pre { ops: clear.clone(), body: Box::new(b) });
}
Box::new(move |cx, k| {
run_pre(&clear, cx);
body(cx, k)
})
}
None => fb_act(n, sc),
},
E::App { name, args, r, .. } => {
match r.get() {
Res::Def(d) => {
let d = d.over.get().unwrap_or(d);
if d.builtin.get().is_some() {
return self.guard(n, sc);
}
let body = match d.body.get() {
Some(b) if d.inst.is_none() => b,
_ => return fb_act(n, sc),
};
let id = d as *const Def as usize;
if self.stack.contains(&id) || self.stack.len() > 40 {
return fb_act(n, sc);
}
let root = Rc::new(Sc { k: ScK::Root(None), next: None });
match self.bind_args(&d.params, args, root, sc) {
Some((inner, clear)) => {
self.stack.push(id);
self.top = desc;
let b = self.act_inner(body, &inner);
self.stack.pop();
if let (true, Some(s)) = (desc, self.skel.take()) {
self.skel_out = Some(Skel::Pre { ops: clear.clone(), body: Box::new(s) });
}
Box::new(move |cx, k| {
run_pre(&clear, cx);
b(cx, k)
})
}
None => fb_act(n, sc),
}
}
Res::Decl(d) => {
if d.subst.get().is_some() {
fb_act(n, sc)
} else {
self.guard(n, sc)
}
}
Res::Local => match lookup(sc, *name) {
Found::Slot(_) | Found::Const(_) if args.is_empty() => self.guard(n, sc),
_ => fb_act(n, sc),
},
Res::None => fb_act(n, sc),
}
}
E::FnApp { f, .. } if var_of(f).is_some() => self.guard(n, sc),
E::FnApp { .. } => fb_act(n, sc),
_ => self.guard(n, sc),
}
}
}
fn unit_run(cx: &Cx, k: &mut dyn FnMut() -> R<()>, u: &UnitNode) -> R<()> {
let table = match cx.cache {
Some(t) if !cx.rec_on.get() && cx.ev.n_assigned.get() == 0 => t,
_ => return (u.inner)(cx, k),
};
if !use_cache(cx, &u.stat) {
return (u.inner)(cx, k);
}
let nv = cx.nv as usize;
if cx.cur_ids.len() < nv {
return (u.inner)(cx, k);
}
let mut bids: SmallIds = SmallIds::new();
let all = cx.f.with(|fv| u.bound.iter().all(|&s| matches!(fv.get(s).and_then(crate::hashcons::id), Some(x) if bids.push(x))));
if !all {
return (u.inner)(cx, k);
}
unit_cached(cx, k, table, u, bids.as_slice(), &|| {})
}
#[inline(always)]
fn use_cache(cx: &Cx, stat: &Rc<UnitStat>) -> bool {
let c = stat.count.get() + 1;
if stat.phase.get() == PH_ON && c % 4096 != 0 && !cx.cache_always {
stat.count.set(c);
return true;
}
use_cache_slow(cx, stat)
}
#[inline(never)]
fn use_cache_slow(cx: &Cx, stat: &Rc<UnitStat>) -> bool {
if cx.cache_always {
return true;
}
let c = stat.count.get() + 1;
stat.count.set(c);
match stat.phase.get() {
PH_OFF => {
if c >= stat.skip.get() {
stat.enter(PH_WARM);
}
false
}
PH_WARM => {
if c == 1 {
stat.mark.set(stat.hits.get());
stat.mark_l.set(stat.lookups.get());
stat.mark_s.set(stat.stored.get());
}
if c == 32 && stat.hits.get() == stat.mark.get() && stat.stored.get() == stat.mark_s.get() {
stat.turn_off(true);
} else if c == 256 {
if (stat.hits.get() - stat.mark.get()) * 2 < 256 {
stat.turn_off(true);
} else {
stat.begin_trial(cx.meas);
}
}
true
}
PH_TRIAL => {
let e = cx.meas.exp.get();
if stat.seen.get() != e {
stat.seen.set(e);
stat.arm.set((sc_::mix(e, 0x7A11) >> 17) & 1 == 1);
cx.meas.touched.borrow_mut().push(stat.clone());
}
stat.arm.get()
}
_ => {
if c % 4096 == 0 {
let (looked, hit) = (stat.lookups.get() - stat.mark_l.get(), stat.hits.get() - stat.mark.get());
if c >= stat.again.get() || hit * 5 < looked * 4 {
stat.begin_trial(cx.meas);
} else {
stat.mark.set(stat.hits.get());
stat.mark_l.set(stat.lookups.get());
}
}
true
}
}
}
const PH_WARM: u8 = 0;
const PH_TRIAL: u8 = 1;
const PH_ON: u8 = 3;
const PH_OFF: u8 = 4;
#[cfg(not(target_arch = "wasm32"))]
fn now_ns() -> u64 {
use std::sync::OnceLock;
static T0: OnceLock<std::time::Instant> = OnceLock::new();
T0.get_or_init(std::time::Instant::now).elapsed().as_nanos() as u64 + 1
}
#[cfg(target_arch = "wasm32")]
fn now_ns() -> u64 {
0
}
pub struct Meas {
active: Cell<u32>,
t0: Cell<u64>,
exp: Cell<u64>,
touched: RefCell<Vec<Rc<UnitStat>>>,
}
impl Meas {
fn new() -> Meas {
Meas { active: Cell::new(0), t0: Cell::new(0), exp: Cell::new(1), touched: RefCell::new(Vec::new()) }
}
#[inline]
fn tick(&self) {
if self.active.get() == 0 && self.t0.get() == 0 {
return;
}
let now = now_ns();
let t0 = self.t0.replace(if self.active.get() == 0 { 0 } else { now });
self.exp.set(self.exp.get() + 1);
let mut t = self.touched.borrow_mut();
if t0 != 0 {
for u in t.iter() {
u.sample((now - t0) as f64, self);
}
}
t.clear();
}
}
#[derive(Default)]
struct Acc {
sum: Cell<f64>,
sq: Cell<f64>,
n: Cell<u64>,
}
impl Acc {
fn add(&self, x: f64) {
self.sum.set(self.sum.get() + x);
self.sq.set(self.sq.get() + x * x);
self.n.set(self.n.get() + 1);
}
fn reset(&self) {
self.sum.set(0.0);
self.sq.set(0.0);
self.n.set(0);
}
fn mean(&self) -> f64 {
self.sum.get() / self.n.get().max(1) as f64
}
fn var_mean(&self) -> f64 {
let n = self.n.get().max(2) as f64;
let m = self.sum.get() / n;
((self.sq.get() / n - m * m).max(0.0)) / (n - 1.0)
}
}
impl UnitStat {
fn enter(&self, phase: u8) {
self.phase.set(phase);
self.count.set(0);
}
fn begin_trial(&self, meas: &Meas) {
self.held.set(self.phase.get() == PH_ON);
self.acc[0].reset();
self.acc[1].reset();
meas.active.set(meas.active.get() + 1);
self.enter(PH_TRIAL);
}
fn sample(&self, dt: f64, meas: &Meas) {
if self.phase.get() != PH_TRIAL {
return;
}
let (a, b) = (&self.acc[0], &self.acc[1]);
let total = a.n.get() + b.n.get();
let x = if total >= 32 { dt.min(8.0 * (a.sum.get() + b.sum.get()) / total as f64) } else { dt };
self.acc[self.arm.get() as usize].add(x);
let least = a.n.get().min(b.n.get());
if least < 512 || (total + 1) % 256 != 0 {
return;
}
let (plain, cached) = (a.mean(), b.mean());
let se = (a.var_mean() + b.var_mean()).sqrt();
let d = cached - plain;
self.last.set((plain, cached));
self.trials.set(total + 1);
let done = least >= 4096;
self.again.set((self.count.get() * 16).max(1 << 20));
let held = self.held.get();
if d < -2.5 * se || (done && d < if held { se } else { -se }) {
meas.active.set(meas.active.get() - 1);
self.penalty.set(0);
self.off.set(false);
self.mark.set(self.hits.get());
self.mark_l.set(self.lookups.get());
self.enter(PH_ON);
} else if d > 2.5 * se || done || (!held && least >= 1024 && d > 0.0) {
meas.active.set(meas.active.get() - 1);
self.turn_off(d > 3.0 * se && d > 0.05 * plain);
}
}
fn turn_off(&self, far: bool) {
let p = if far { 1 << 22 } else { (self.penalty.get() * 2).clamp(1 << 18, 1 << 22) };
self.penalty.set(p);
self.skip.set(p.max(self.again.get()));
self.off.set(true);
self.enter(PH_OFF);
}
}
#[inline(always)]
fn unit_cached(cx: &Cx, k: &mut dyn FnMut() -> R<()>, table: &RefCell<Table>, u: &UnitNode, bids: &[crate::hashcons::Id], before: &dyn Fn()) -> R<()> {
let stat = &u.stat;
stat.lookups.set(stat.lookups.get() + 1);
if let Some(out) = cx.out {
let t = table.borrow();
let mut o = out.borrow_mut();
let mark = o.mark();
let o = &mut *o;
if cached_rows(&t, cx, u.id, bids, u.n, &mut o.ids, Some(&mut o.meta)).is_some() {
stat.hits.set(stat.hits.get() + 1);
return Ok(());
}
o.truncate(mark);
}
unit_cached_values(cx, k, table, u, bids, before)
}
#[inline(never)]
fn unit_cached_values(cx: &Cx, k: &mut dyn FnMut() -> R<()>, table: &RefCell<Table>, u: &UnitNode, bids: &[crate::hashcons::Id], before: &dyn Fn()) -> R<()> {
let nv = cx.nv as usize;
let stat = &u.stat;
let ev = cx.ev;
if cx.out.is_none() {
let mut rows = vec![];
let hit = cached_rows(&table.borrow(), cx, u.id, bids, u.n, &mut rows, None);
if hit.is_some() {
stat.hits.set(stat.hits.get() + 1);
return emit_values(cx, k, ev, &rows, nv);
}
}
before();
for &s in &u.clear {
cx.f.clear(s);
}
{
let mut rec = cx.rec.borrow_mut();
rec.reads.clear();
rec.bad = false;
rec.succs.clear();
rec.delta.clear();
rec.delta.resize(nv, None);
}
let f0 = cx.fallbacks.get();
cx.rec_on.set(true);
let r = (u.inner)(cx, &mut || {
cx.nxt.with(|nx| {
let mut rec = cx.rec.borrow_mut();
let mut ws = Vec::with_capacity(nv);
for i in 0..nv {
match rec.delta[i].clone() {
Some(ops) => ws.push(Write::Delta(ops)),
None => {
if i >= nx.len() || matches!(nx[i], Value::Undef) {
rec.bad = true;
} else {
ws.push(Write::Val(nx[i].clone()));
}
}
}
}
rec.succs.push(ws);
});
k()
});
cx.rec_on.set(false);
let mut t = table.borrow_mut();
let rec = cx.rec.borrow();
let stored = r.is_ok() && !rec.bad && cx.fallbacks.get() == f0 && sc_::store(&mut t, cx.cur_ids, sc_::Recorded { unit: u.id, bound: bids, reads: &rec.reads, succs: &rec.succs });
if !stored {
t.n.uncacheable += 1;
} else {
stat.stored.set(stat.stored.get() + 1);
}
r
}
struct SmallIds {
n: usize,
a: [crate::hashcons::Id; 24],
}
impl SmallIds {
#[inline]
fn new() -> SmallIds {
SmallIds { n: 0, a: [0; 24] }
}
#[inline]
fn push(&mut self, x: crate::hashcons::Id) -> bool {
if self.n == self.a.len() {
return false;
}
self.a[self.n] = x;
self.n += 1;
true
}
#[inline]
fn as_slice(&self) -> &[crate::hashcons::Id] {
&self.a[..self.n]
}
}
fn cached_rows(t: &Table, cx: &Cx, unit: u32, bids: &[crate::hashcons::Id], n: N, ids: &mut Vec<crate::hashcons::Id>, mut meta: Option<&mut sc_::RowMeta>) -> Option<usize> {
let cur = cx.cur_ids;
let tm = t.template(sc_::template_hash(unit, bids), unit, bids)?;
let mut key = SmallIds::new();
let mut h = sc_::leaf_hash_start(tm.version);
for &loc in tm.locs.iter() {
let x = t.locate(cur, loc)?;
key.push(x);
h = sc_::mix(h, x as u64);
}
let (slot, leaf) = t.leaf_slot(h, tm.version, key.as_slice())?;
let nv = cx.nv as usize;
for (si, s) in leaf.succs.iter().enumerate() {
if let (Some(m), Some(fx)) = (&mut meta, cx.fpx) {
if let Some(fp) = delta_fingerprint(t, fx, cur, s) {
if fx.verify {
verify_delta(t, cx, fx, cur, s, n, fp);
}
fx.seen.prefetch(fp);
m.recipe(fp, sc_::Recipe { slot: slot.clone(), succ: si as u32, n });
continue;
}
}
let base = ids.len();
ids.extend_from_slice(&cur[..nv]);
build_puts(t, cx, cur, s, n, &mut ids[base..base + nv])?;
let Some(m) = &mut meta else {
continue;
};
let Some(fx) = cx.fpx else {
m.added(0);
continue;
};
let mut sum = fx.sum;
for (var, _) in s.iter() {
let i = *var as usize;
let x = ids[base + i];
if x != cur[i] {
sum = sum.wrapping_sub(fx.terms[i]).wrapping_add(crate::checker::var_term(i, fx.ids.word(x)));
}
}
m.added(crate::checker::fp_finish(sum));
}
Some(leaf.succs.len())
}
#[inline]
fn build_puts(t: &Table, cx: &Cx, cur: &[crate::hashcons::Id], s: &[(u32, sc_::Put)], n: N, row: &mut [crate::hashcons::Id]) -> Option<()> {
for (var, put) in s.iter() {
let i = *var as usize;
row[i] = match put {
sc_::Put::Id { id, .. } => *id,
sc_::Put::Delta { id, ops, .. } => t.applied(cur[i], *id, |parent| apply_ops(cx, parent, ops, n))?,
};
}
Some(())
}
#[cold]
fn verify_delta(t: &Table, cx: &Cx, fx: &FpCtx, cur: &[crate::hashcons::Id], s: &[(u32, sc_::Put)], n: N, fp: u64) {
let nv = cx.nv as usize;
let mut row = cur[..nv].to_vec();
build_puts(t, cx, cur, s, n, &mut row).expect("changes whose fingerprint is known apply");
let mut sum = fx.sum;
for k in 0..nv {
if row[k] != cur[k] {
sum = sum.wrapping_sub(fx.terms[k]).wrapping_add(crate::checker::var_term(k, crate::value::value_word(&crate::hashcons::value(row[k])).unwrap()));
}
}
assert_eq!(crate::checker::fp_finish(sum), fp, "the fingerprint of a successor from its changes");
}
pub fn verify_deltas() -> bool {
static V: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*V.get_or_init(|| std::env::var_os("EVERYSTATE_VERIFY_DELTAS").is_some())
}
#[inline]
fn delta_fingerprint(t: &Table, fx: &FpCtx, cur: &[crate::hashcons::Id], s: &[(u32, sc_::Put)]) -> Option<u64> {
let mut sum = fx.sum;
for (var, put) in s.iter() {
let i = *var as usize;
let w = match put {
sc_::Put::Id { id, .. } if *id == cur[i] => continue,
sc_::Put::Id { word, .. } => *word,
sc_::Put::Delta { shift: Some(d), .. } => fx.words[i].wrapping_add(*d),
sc_::Put::Delta { iops: Some(ops), id, .. } => t.delta_word(fx.ids, cur[i], *id, ops)?,
sc_::Put::Delta { iops: None, .. } => return None,
};
sum = sum.wrapping_sub(fx.terms[i]).wrapping_add(crate::checker::var_term(i, w));
}
Some(crate::checker::fp_finish(sum))
}
fn emit_values(cx: &Cx, k: &mut dyn FnMut() -> R<()>, ev: &Evaluator, rows: &[crate::hashcons::Id], nv: usize) -> R<()> {
for row in rows.chunks_exact(nv) {
cx.cur.with(|cv| {
for i in 0..nv {
cx.nxt.set(i, if row[i] == cx.cur_ids[i] { cv[i].clone() } else { crate::hashcons::value(row[i]) });
}
});
ev.n_assigned.set(nv as i64);
let r = k();
for i in 0..nv {
cx.nxt.clear(i);
}
ev.n_assigned.set(0);
r?;
}
Ok(())
}
fn with_clear(clear: Vec<PreOp>, body: CE) -> CE {
if clear.is_empty() {
return body;
}
Box::new(move |cx| {
run_pre(&clear, cx);
body(cx)
})
}
fn root_scope(ctx: &C, c: &mut Comp, seen: &mut HashSet<usize>) -> Option<(Rc<Sc>, Vec<PreOp>)> {
let mut nodes: Vec<&Rc<Ctx>> = vec![];
let mut cur = ctx.as_ref();
while let Some(node) = cur {
if node.env || node.base.is_some() {
return None;
}
nodes.push(node);
cur = node.next.as_ref();
}
let mut sc = Rc::new(Sc { k: ScK::Root(ctx.clone()), next: None });
let mut clear = vec![];
for node in nodes.iter().rev() {
let k = match &node.val {
Bind::Val(v) if plain(v) => ScK::RootVal { name: node.name, v: v.clone() },
Bind::LetLazy { d, .. } | Bind::LetOp(d) => {
if impure(&d.body, seen) {
return None;
}
let cache = c.slot();
c.cache_slots.push(cache);
clear.push(PreOp::Clear(cache));
ScK::Let { d, defs: &[], below: sc.clone(), head: node.let_head, cache, ce: RefCell::new(None), root: true }
}
_ => return None,
};
sc = push(&sc, k);
}
Some((sc, clear))
}
pub struct ActPlan {
run: AE,
frame: StateRef,
use_: Usage,
skel: Option<Skel>,
}
pub struct PredPlan {
run: CE,
frame: StateRef,
use_: Usage,
}
#[derive(Default)]
struct Usage {
runs: Cell<u32>,
fallbacks: Cell<u64>,
off: Cell<bool>,
}
impl Usage {
#[inline]
fn note(&self, fallbacks: u64) {
let r = self.runs.get();
if r < 512 {
self.runs.set(r + 1);
self.fallbacks.set(self.fallbacks.get() + fallbacks);
if r + 1 == 512 && self.fallbacks.get() > 4 * 512 {
self.off.set(true);
}
}
}
}
#[derive(Default)]
pub struct Counters {
pub compiled: Cell<u64>,
pub uncompiled: Cell<u64>,
pub redone: Cell<u64>,
pub fallbacks: Cell<u64>,
}
pub struct Plans {
pub actions: Vec<Option<ActPlan>>,
pub invariants: Vec<Option<PredPlan>>,
pub n: Counters,
blank: StateRef,
cache: Option<RefCell<Table>>,
rec_on: Cell<bool>,
rec: RefCell<Rec>,
units: Vec<Rc<UnitStat>>,
cache_always: bool,
meas: Meas,
}
pub fn cache_enabled() -> bool {
if cfg!(target_arch = "wasm32") {
return false;
}
match std::env::var("EVERYSTATE_STEP_CACHE").as_deref() {
Ok("off") => false,
Ok("on") | Ok("always") => true,
_ => !matches!(std::env::var("EVERYSTATE_EVAL").as_deref(), Ok("tree")),
}
}
fn cache_budget() -> usize {
let mb: f64 = std::env::var("EVERYSTATE_STEP_CACHE_MB").ok().and_then(|v| v.parse().ok()).unwrap_or(32.0);
(mb.max(0.001) * 1024.0 * 1024.0) as usize
}
pub fn enabled() -> bool {
!matches!(std::env::var("EVERYSTATE_EVAL").as_deref(), Ok("tree")) || cache_enabled()
}
pub fn compile(model: &crate::model::Model) -> Plans {
let mut cache_on = cache_enabled();
let mut plan_id: u32 = 0;
let mut units = vec![];
let mut actions = vec![];
for a in &model.actions {
let mut seen = HashSet::new();
let mut c = Comp::new(cache_on, plan_id);
plan_id += 1;
let root = if impure(a.e, &mut seen) { None } else { root_scope(&a.ctx, &mut c, &mut seen) };
let (root, clear) = match root {
Some(x) => x,
None => {
if std::env::var_os("EVERYSTATE_EVAL_DEBUG").is_some() {
eprintln!("action {}{} is not compiled (an operator with an effect, or a context that is not plain values and LET definitions)", a.name, a.params);
}
actions.push(None);
continue;
}
};
c.top = true;
let body = c.act(a.e, &root);
units.append(&mut c.unit_stats);
let skel = c.skel.take().filter(|_| model.vars.len() > 0).map(|sk| if clear.is_empty() { sk } else { Skel::Pre { ops: clear.clone(), body: Box::new(sk) } }).filter(|sk| sk.depth() <= PATH_MAX);
if std::env::var_os("EVERYSTATE_EVAL_DEBUG").is_some() {
eprintln!("action {}: {}", a.name, if skel.is_some() { "runs on ids" } else { "runs on values" });
}
let run: AE = if clear.is_empty() {
body
} else {
Box::new(move |cx, k| {
run_pre(&clear, cx);
body(cx, k)
})
};
actions.push(Some(ActPlan { run, frame: StateData::blank(c.nslots.max(1)), use_: Usage::default(), skel }));
}
let with_cache = cache_on;
cache_on = false; let mut invariants = vec![];
for p in &model.invariants {
let mut seen = HashSet::new();
let mut c = Comp::new(cache_on, plan_id);
plan_id += 1;
let root = if impure(p.e, &mut seen) || level_of(p.e) > 1 { None } else { root_scope(&p.ctx, &mut c, &mut seen) };
let (root, clear) = match root {
Some(x) => x,
None => {
invariants.push(None);
continue;
}
};
let run = with_clear(clear, c.expr(p.e, &root));
invariants.push(Some(PredPlan { run, frame: StateData::blank(c.nslots.max(1)), use_: Usage::default() }));
}
Plans {
actions,
invariants,
n: Counters::default(),
blank: StateData::blank(model.vars.len()),
cache: if with_cache { Some(RefCell::new(Table::new(cache_budget()))) } else { None },
rec_on: Cell::new(false),
rec: RefCell::new(Rec::default()),
units,
cache_always: matches!(std::env::var("EVERYSTATE_STEP_CACHE").as_deref(), Ok("always")),
meas: Meas::new(),
}
}
impl Plans {
#[allow(clippy::too_many_arguments)]
pub fn run_action(&self, ev: &Evaluator, ai: usize, cur: &StateData, cur_ids: &[crate::hashcons::Id], nxt: &StateData, nv: usize, out: Option<&RefCell<sc_::Rows>>, fpx: Option<&FpCtx>, k: &mut dyn FnMut() -> R<()>) -> Option<R<()>> {
let p = match &self.actions[ai] {
Some(p) if !p.use_.off.get() => p,
_ => {
self.n.uncompiled.set(self.n.uncompiled.get() + 1);
return None;
}
};
let cx = Cx { ev, f: &p.frame, cur, nxt, nv: nv as i64, fallbacks: &self.n.fallbacks, cache: self.cache.as_ref(), rec_on: &self.rec_on, rec: &self.rec, cache_always: self.cache_always, meas: &self.meas, cur_ids, out, vals: None, fpx };
let f0 = self.n.fallbacks.get();
let r = (p.run)(&cx, k);
self.rec_on.set(false);
p.use_.note(self.n.fallbacks.get() - f0);
if r.is_err() {
self.n.redone.set(self.n.redone.get() + 1);
} else {
self.n.compiled.set(self.n.compiled.get() + 1);
}
Some(r)
}
pub fn build_recipe(&self, ev: &Evaluator, r: &sc_::Recipe, cur: &[crate::hashcons::Id], nv: usize, row: &mut Vec<crate::hashcons::Id>) -> bool {
let Some(t) = &self.cache else { return false };
let t = t.borrow();
let cx = Cx { ev, f: &self.blank, cur: &self.blank, nxt: &self.blank, nv: nv as i64, fallbacks: &self.n.fallbacks, cache: None, rec_on: &self.rec_on, rec: &self.rec, cache_always: self.cache_always, meas: &self.meas, cur_ids: cur, out: None, vals: None, fpx: None };
row.clear();
row.extend_from_slice(&cur[..nv]);
build_puts(&t, &cx, cur, r.puts(), r.n, row).is_some()
}
pub fn has_skel(&self, ai: usize) -> bool {
self.cache.is_some() && matches!(&self.actions[ai], Some(p) if p.skel.is_some() && !p.use_.off.get())
}
#[allow(clippy::too_many_arguments)]
pub fn run_skel(&self, ev: &Evaluator, ai: usize, cur: &StateData, cur_ids: &[crate::hashcons::Id], nxt: &StateData, nv: usize, out: &RefCell<sc_::Rows>, fpx: Option<&FpCtx>, vals: &dyn Fn(), k: &mut dyn FnMut() -> R<()>) -> Option<R<()>> {
let p = match &self.actions[ai] {
Some(p) if !p.use_.off.get() => p,
_ => return None,
};
let sk = p.skel.as_ref()?;
if self.cache.is_none() || cur_ids.len() < nv {
return None;
}
let cx = Cx { ev, f: &p.frame, cur, nxt, nv: nv as i64, fallbacks: &self.n.fallbacks, cache: self.cache.as_ref(), rec_on: &self.rec_on, rec: &self.rec, cache_always: self.cache_always, meas: &self.meas, cur_ids, out: Some(out), vals: Some(vals), fpx };
let f0 = self.n.fallbacks.get();
let mut path = Path::new();
let mut bids = SmallIds::new();
let r = skel_run(sk, &cx, &mut path, &mut bids, k);
self.rec_on.set(false);
p.use_.note(self.n.fallbacks.get() - f0);
if r.is_err() {
self.n.redone.set(self.n.redone.get() + 1);
} else {
self.n.compiled.set(self.n.compiled.get() + 1);
}
Some(r)
}
pub fn run_invariant(&self, ev: &Evaluator, i: usize, s: &StateData, nv: usize) -> Option<bool> {
let p = match &self.invariants[i] {
Some(p) if !p.use_.off.get() => p,
_ => {
self.n.uncompiled.set(self.n.uncompiled.get() + 1);
return None;
}
};
let cx = Cx { ev, f: &p.frame, cur: s, nxt: &self.blank, nv: nv as i64, fallbacks: &self.n.fallbacks, cache: None, rec_on: &self.rec_on, rec: &self.rec, cache_always: self.cache_always, meas: &self.meas, cur_ids: &[], out: None, vals: None, fpx: None };
let f0 = self.n.fallbacks.get();
let r = (p.run)(&cx);
p.use_.note(self.n.fallbacks.get() - f0);
match r {
Ok(Value::Bool(b)) => {
self.n.compiled.set(self.n.compiled.get() + 1);
Some(b)
}
_ => {
self.n.redone.set(self.n.redone.get() + 1);
None
}
}
}
#[inline]
pub fn caching(&self) -> bool {
self.cache.is_some()
}
pub fn caching_now(&self) -> bool {
self.cache.is_some() && self.units.iter().any(|u| !u.off.get())
}
pub fn verdicts(&self) -> Vec<Verdict> {
self.units
.iter()
.map(|u| Verdict {
loc: u.loc,
kind: match u.phase.get() {
PH_ON => VerdictKind::Pays,
PH_OFF => VerdictKind::DoesNotPay { penalty: u.penalty.get(), skip: u.skip.get() },
_ => VerdictKind::Unknown,
},
})
.collect()
}
pub fn adopt(&self, vs: &[Verdict]) {
if vs.len() != self.units.len() {
return;
}
for (u, v) in self.units.iter().zip(vs) {
if u.phase.get() != PH_WARM || v.loc != u.loc {
continue;
}
match v.kind {
VerdictKind::Pays => {
u.mark.set(u.hits.get());
u.mark_l.set(u.lookups.get());
u.off.set(false);
u.enter(PH_ON);
}
VerdictKind::DoesNotPay { penalty, skip } => {
u.penalty.set(penalty);
u.skip.set(skip);
u.off.set(true);
u.enter(PH_OFF);
}
VerdictKind::Unknown => {}
}
}
}
#[inline]
pub fn tick(&self) {
self.meas.tick();
}
pub fn report(&self) -> String {
let c = &self.n;
let mut cache = String::new();
if let Some(t) = &self.cache {
let t = t.borrow();
let off = self.units.iter().filter(|u| u.off.get()).count();
let m: [u64; 4] = std::array::from_fn(|i| t.memo[i].get());
cache = format!(
"step cache: budget {:.1} MB, in use {:.1} MB (peak {:.1} MB); {} units, {} measured as not paying; {} lookups, {} hits ({:.2}%), {} stored, {} replaced, {} not stored for size, {} evaluations not cacheable; changes applied {} times, {} computed; locations looked up {} times, {} computed\n",
t.budget as f64 / 1048576.0,
t.used as f64 / 1048576.0,
t.peak as f64 / 1048576.0,
self.units.len(),
off,
self.units.iter().map(|u| u.lookups.get()).sum::<u64>(),
self.units.iter().map(|u| u.hits.get()).sum::<u64>(),
100.0 * self.units.iter().map(|u| u.hits.get()).sum::<u64>() as f64 / self.units.iter().map(|u| u.lookups.get()).sum::<u64>().max(1) as f64,
t.n.stored,
t.n.replaced,
t.n.too_big,
t.n.uncacheable,
m[0],
m[1],
m[2],
m[3]
);
if std::env::var_os("EVERYSTATE_EVAL_DEBUG").is_some() {
for u in &self.units {
cache += &format!(
" unit at line {} col {}: {} lookups, {} hits{}; last verdict on {:.0} ns per state without against {:.0} ns with the cache ({} states)\n",
u.loc[0],
u.loc[1],
u.lookups.get(),
u.hits.get(),
if u.off.get() { " (does not pay)" } else { "" },
u.last.get().0,
u.last.get().1,
u.trials.get()
);
}
}
}
cache + &format!(
"compiled evaluator: {} of {} actions and {} of {} invariants compiled; evaluations: {} compiled, {} by the tree-walker (not compiled), {} redone by the tree-walker after a failed attempt; {} fallback expressions evaluated inside compiled code\n",
self.actions.iter().filter(|a| a.is_some()).count(),
self.actions.len(),
self.invariants.iter().filter(|a| a.is_some()).count(),
self.invariants.len(),
c.compiled.get(),
c.uncompiled.get(),
c.redone.get(),
c.fallbacks.get()
)
}
}