opportunistic move

should be way faster now
This commit is contained in:
2023-09-16 12:57:50 +01:00
parent 0bcf10659b
commit 1078835e8b
36 changed files with 535 additions and 521 deletions

View File

@@ -13,17 +13,14 @@ use crate::utils::Side;
fn map_at<E>(
path: &[Side],
source: ExprInst,
mapper: &mut impl FnMut(&Clause) -> Result<Clause, E>,
mapper: &mut impl FnMut(Clause) -> Result<Clause, E>,
) -> Result<ExprInst, E> {
source
.try_update(|value, _loc| {
// Pass right through lambdas
if let Clause::Lambda { args, body } = value {
return Ok((
Clause::Lambda {
args: args.clone(),
body: map_at(path, body.clone(), mapper)?,
},
Clause::Lambda { args, body: map_at(path, body, mapper)? },
(),
));
}
@@ -37,14 +34,8 @@ fn map_at<E>(
if let Clause::Apply { f, x } = value {
return Ok((
match head {
Side::Left => Clause::Apply {
f: map_at(tail, f.clone(), mapper)?,
x: x.clone(),
},
Side::Right => Clause::Apply {
f: f.clone(),
x: map_at(tail, x.clone(), mapper)?,
},
Side::Left => Clause::Apply { f: map_at(tail, f, mapper)?, x },
Side::Right => Clause::Apply { f, x: map_at(tail, x, mapper)? },
},
(),
));
@@ -63,8 +54,8 @@ fn substitute(paths: &PathSet, value: Clause, body: ExprInst) -> ExprInst {
match (checkpoint, next) {
(Clause::Lambda { .. }, _) => unreachable!("Handled by map_at"),
(Clause::Apply { f, x }, Some((left, right))) => Ok(Clause::Apply {
f: substitute(left, value.clone(), f.clone()),
x: substitute(right, value.clone(), x.clone()),
f: substitute(left, value.clone(), f),
x: substitute(right, value.clone(), x),
}),
(Clause::LambdaArg, None) => Ok(value.clone()),
(_, None) => {
@@ -91,20 +82,19 @@ pub fn apply(
Ok((clause, (ctx.gas.map(|g| g - 1), false)))
},
Clause::Lambda { args, body } => Ok(if let Some(args) = args {
let x_cls = x.expr().clause.clone();
let new_xpr_inst = substitute(args, x_cls, body.clone());
let new_xpr = new_xpr_inst.expr();
let x_cls = x.expr_val().clause;
let result = substitute(&args, x_cls, body);
// cost of substitution
// XXX: should this be the number of occurrences instead?
(new_xpr.clause.clone(), (ctx.gas.map(|x| x - 1), false))
(result.expr_val().clause, (ctx.gas.map(|x| x - 1), false))
} else {
(body.expr().clause.clone(), (ctx.gas, false))
(body.expr_val().clause, (ctx.gas, false))
}),
Clause::Constant(name) =>
if let Some(sym) = ctx.symbols.get(name) {
if let Some(sym) = ctx.symbols.get(&name) {
Ok((Clause::Apply { f: sym.clone(), x }, (ctx.gas, false)))
} else {
Err(RuntimeError::MissingSymbol(name.clone(), loc.clone()))
Err(RuntimeError::MissingSymbol(name.clone(), loc))
},
Clause::P(Primitive::Atom(atom)) => {
// take a step in expanding atom
@@ -113,11 +103,11 @@ pub fn apply(
},
Clause::Apply { f: fun, x: arg } => {
// take a step in resolving pre-function
let ret = apply(fun.clone(), arg.clone(), ctx.clone())?;
let ret = apply(fun, arg, ctx.clone())?;
let Return { state, inert, gas } = ret;
Ok((Clause::Apply { f: state, x }, (gas, inert)))
},
_ => Err(RuntimeError::NonFunctionApplication(f.clone())),
_ => Err(RuntimeError::NonFunctionApplication(loc)),
})?;
Ok(Return { state, gas, inert })
}

View File

@@ -2,7 +2,6 @@ use std::fmt::Display;
use std::rc::Rc;
use crate::foreign::ExternError;
use crate::representations::interpreted::ExprInst;
use crate::{Location, Sym};
/// Problems in the process of execution
@@ -11,7 +10,7 @@ pub enum RuntimeError {
/// A Rust function encountered an error
Extern(Rc<dyn ExternError>),
/// Primitive applied as function
NonFunctionApplication(ExprInst),
NonFunctionApplication(Location),
/// Symbol not in context
MissingSymbol(Sym, Location),
}
@@ -24,8 +23,8 @@ impl Display for RuntimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Extern(e) => write!(f, "Error in external function: {e}"),
Self::NonFunctionApplication(expr) => {
write!(f, "Primitive applied as function at {}", expr.expr().location)
Self::NonFunctionApplication(location) => {
write!(f, "Primitive applied as function at {}", location)
},
Self::MissingSymbol(sym, loc) => {
write!(

View File

@@ -5,12 +5,13 @@ use hashbrown::HashMap;
use trait_set::trait_set;
use super::{run, Context, Return, RuntimeError};
use crate::foreign::ExternError;
use crate::interpreted::{Clause, ExprInst};
use crate::foreign::{Atom, Atomic, ExternError};
use crate::interpreted::{Clause, Expr, ExprInst};
use crate::utils::take_with_output;
use crate::Primitive;
trait_set! {
trait Handler = for<'b> FnMut(&'b dyn Any) -> HandlerRes;
trait Handler = FnMut(Box<dyn Any>) -> HandlerRes;
}
/// A table of command handlers
@@ -26,16 +27,22 @@ impl<'a> HandlerTable<'a> {
/// next. This function can be impure.
pub fn register<T: 'static>(
&mut self,
mut f: impl for<'b> FnMut(&'b T) -> HandlerRes + 'a,
mut f: impl FnMut(Box<T>) -> HandlerRes + 'a,
) {
let cb = move |a: &dyn Any| f(a.downcast_ref().expect("found by TypeId"));
let cb = move |a: Box<dyn Any>| f(a.downcast().expect("found by TypeId"));
let prev = self.handlers.insert(TypeId::of::<T>(), Box::new(cb));
assert!(prev.is_none(), "A handler for this type is already registered");
}
/// Find and execute the corresponding handler for this type
pub fn dispatch(&mut self, arg: &dyn Any) -> Option<HandlerRes> {
self.handlers.get_mut(&arg.type_id()).map(|f| f(arg))
pub fn dispatch(
&mut self,
arg: Box<dyn Atomic>,
) -> Result<HandlerRes, Box<dyn Atomic>> {
match self.handlers.get_mut(&arg.as_any_ref().type_id()) {
Some(f) => Ok(f(arg.as_any())),
None => Err(arg),
}
}
/// Combine two non-overlapping handler sets
@@ -60,16 +67,23 @@ pub fn run_handler(
mut ctx: Context,
) -> Result<Return, RuntimeError> {
loop {
let ret = run(expr.clone(), ctx.clone())?;
if let Clause::P(Primitive::Atom(a)) = &ret.state.expr().clause {
if let Some(e) = handlers.dispatch(a.0.as_any()) {
expr = e?;
ctx.gas = ret.gas;
if ret.gas.map_or(true, |g| g > 0) {
continue;
let mut ret = run(expr, ctx.clone())?;
let quit = take_with_output(&mut ret.state, |exi| match exi.expr_val() {
Expr { clause: Clause::P(Primitive::Atom(a)), .. } => {
match handlers.dispatch(a.0) {
Err(b) => (Clause::P(Primitive::Atom(Atom(b))).wrap(), Ok(true)),
Ok(e) => match e {
Ok(expr) => (expr, Ok(false)),
Err(e) => (Clause::Bottom.wrap(), Err(e)),
},
}
}
},
expr => (ExprInst::new(expr), Ok(true)),
})?;
if quit | ret.gas.map_or(false, |g| g == 0) {
return Ok(ret);
}
return Ok(ret);
ctx.gas = ret.gas;
expr = ret.state;
}
}

View File

@@ -7,41 +7,40 @@ use crate::representations::Primitive;
/// Normalize an expression using beta reduction with memoization
pub fn run(expr: ExprInst, mut ctx: Context) -> Result<Return, RuntimeError> {
let (state, (gas, inert)) =
expr.try_normalize(|cls, loc| -> Result<(Clause, _), RuntimeError> {
let mut i = cls.clone();
let (state, (gas, inert)) = expr.try_normalize(
|mut cls, loc| -> Result<(Clause, _), RuntimeError> {
while ctx.gas.map(|g| g > 0).unwrap_or(true) {
match &i {
match cls {
Clause::Apply { f, x } => {
let res = apply(f.clone(), x.clone(), ctx.clone())?;
let res = apply(f, x, ctx.clone())?;
if res.inert {
return Ok((i, (res.gas, true)));
return Ok((res.state.expr_val().clause, (res.gas, true)));
}
ctx.gas = res.gas;
i = res.state.expr().clause.clone();
cls = res.state.expr().clause.clone();
},
Clause::P(Primitive::Atom(data)) => {
let ret = data.run(ctx.clone())?;
let AtomicReturn { clause, gas, inert } = ret;
let AtomicReturn { clause, gas, inert } = data.run(ctx.clone())?;
if inert {
return Ok((i, (gas, true)));
return Ok((clause, (gas, true)));
}
ctx.gas = gas;
i = clause.clone();
cls = clause;
},
Clause::Constant(c) => {
let symval = (ctx.symbols.get(c)).ok_or_else(|| {
let symval = (ctx.symbols.get(&c)).ok_or_else(|| {
RuntimeError::MissingSymbol(c.clone(), loc.clone())
})?;
ctx.gas = ctx.gas.map(|g| g - 1); // cost of lookup
i = symval.expr().clause.clone();
cls = symval.expr().clause.clone();
},
// non-reducible
_ => return Ok((i, (ctx.gas, true))),
_ => return Ok((cls, (ctx.gas, true))),
}
}
// out of gas
Ok((i, (ctx.gas, false)))
})?;
Ok((cls, (ctx.gas, false)))
},
)?;
Ok(Return { state, gas, inert })
}