Sync commit

This commit is contained in:
2022-12-08 17:52:57 +00:00
parent 3557107248
commit a500f8b87a
11 changed files with 338 additions and 275 deletions

View File

@@ -10,48 +10,38 @@ use super::super::representations::typed::{Clause, Expr};
/// Call the function with the first Expression that isn't an Auto,
/// wrap all elements in the returned iterator back in the original sequence of Autos.
pub fn skip_autos<'a,
F: 'a + FnOnce(Mrc<Expr>, usize) -> I,
F: 'a + FnOnce(Mrc<Expr>) -> I,
I: Iterator<Item = Mrc<Expr>> + 'static
>(
depth: usize, expr: Mrc<Expr>, function: F
expr: Mrc<Expr>, function: F
) -> BoxedIter<'static, Mrc<Expr>> {
match expr.as_ref() {
Expr(Clause::Auto(arg, body), typ) => {
return Box::new(skip_autos(depth + 1, Mrc::clone(body), function).map({
let arg = arg.as_ref().map(Mrc::clone);
let typ = Mrc::clone(typ);
move |body| {
Mrc::new(Expr(Clause::Auto(
arg.as_ref().map(Mrc::clone),
body
), Mrc::clone(&typ)))
}
})) as BoxedIter<'static, Mrc<Expr>>
}
Expr(Clause::Explicit(expr, targ), typ) => {
return Box::new(skip_autos(depth, Mrc::clone(expr), function).map({
let (targ, typ) = (Mrc::clone(targ), Mrc::clone(typ));
move |expr| {
Mrc::new(Expr(Clause::Explicit(expr, Mrc::clone(&targ)), Mrc::clone(&typ)))
}
})) as BoxedIter<'static, Mrc<Expr>>
}
_ => ()
if let Expr(Clause::Auto(id, arg, body), typ) = expr.as_ref() {
return Box::new(skip_autos(Mrc::clone(body), function).map({
let arg = arg.as_ref().map(Mrc::clone);
let typ = Mrc::clone(typ);
move |body| {
Mrc::new(Expr(Clause::Auto(
*id,
arg.as_ref().map(Mrc::clone),
body
), Mrc::clone(&typ)))
}
})) as BoxedIter<'static, Mrc<Expr>>
}
Box::new(function(expr, depth))
Box::new(function(expr))
}
/// Produces an iterator of every expression that can be produced from this one through B-reduction.
fn direct_reductions(ex: Mrc<Expr>) -> impl Iterator<Item = Mrc<Expr>> {
skip_autos(0, ex, |mexpr, _| {
skip_autos(ex, |mexpr| {
let Expr(clause, typ_ref) = mexpr.as_ref();
match clause {
Clause::Apply(f, x) => box_chain!(
skip_autos(0, Mrc::clone(f), |mexpr, _| {
skip_autos(Mrc::clone(f), |mexpr| {
let Expr(f, _) = mexpr.as_ref();
match f {
Clause::Lambda(_, body) => box_once(
apply_lambda(Mrc::clone(body), Mrc::clone(x))
Clause::Lambda(id, _, body) => box_once(
apply_lambda(*id, Mrc::clone(x), Mrc::clone(body))
),
Clause::ExternFn(xfn) => {
let Expr(xval, xtyp) = x.as_ref();
@@ -63,8 +53,7 @@ fn direct_reductions(ex: Mrc<Expr>) -> impl Iterator<Item = Mrc<Expr>> {
Clause::Atom(..) | Clause::Argument(..) | Clause::Apply(..) => box_empty(),
Clause::Literal(lit) =>
panic!("Literal expression {lit:?} can't be applied as function"),
Clause::Auto(..) | Clause::Explicit(..) =>
unreachable!("skip_autos should have filtered these"),
Clause::Auto(..) => unreachable!("skip_autos should have filtered this"),
}
}),
direct_reductions(Mrc::clone(f)).map({
@@ -84,18 +73,24 @@ fn direct_reductions(ex: Mrc<Expr>) -> impl Iterator<Item = Mrc<Expr>> {
), Mrc::clone(&typ)))
})
),
Clause::Lambda(argt, body) => Box::new(direct_reductions(Mrc::clone(body)).map({
Clause::Lambda(id, argt, body) => {
let id = *id;
let typ = Mrc::clone(typ_ref);
let argt = argt.as_ref().map(Mrc::clone);
move |body| Mrc::new(Expr(Clause::Lambda(
argt.as_ref().map(Mrc::clone),
body
), Mrc::clone(&typ)))
})),
let body = Mrc::clone(body);
let body_reductions = direct_reductions(body)
.map(move |body| {
let argt = argt.as_ref().map(Mrc::clone);
Mrc::new(Expr(
Clause::Lambda(id, argt, body),
Mrc::clone(&typ)
))
});
Box::new(body_reductions)
},
Clause::Literal(..) | Clause::ExternFn(..) | Clause::Atom(..) | Clause::Argument(..) =>
box_empty(),
Clause::Auto(..) | Clause::Explicit(..) =>
unreachable!("skip_autos should have filtered these"),
Clause::Auto(..) => unreachable!("skip_autos should have filtered this"),
}
})
}