Skip to content

Commit

Permalink
Make BoolAnd and BoolOr maximally lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
Kmeakin committed Feb 2, 2023
1 parent 63f6469 commit cbe97b7
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions fathom/src/core/prim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,44 @@ pub fn step(prim: Prim) -> Step {
Prim::BoolEq => const_step!([x: Bool, y: Bool] => Const::Bool(x == y)),
Prim::BoolNeq => const_step!([x: Bool, y: Bool] => Const::Bool(x != y)),
Prim::BoolNot => const_step!([x: Bool] => Const::Bool(bool::not(*x))),
Prim::BoolAnd => const_step!([x: Bool, y: Bool] => Const::Bool(*x && *y)),
Prim::BoolOr => const_step!([x: Bool, y: Bool] => Const::Bool(*x || *y)),
Prim::BoolXor => const_step!([x: Bool, y: Bool] => Const::Bool(*x ^ *y)),

Prim::BoolAnd => |env: &ElimEnv, spine: &[Elim]| match spine {
[Elim::FunApp(_,x), Elim::FunApp(_, y)] => {
let x = env.force_lazy(x);
match x.as_ref() {
Value::ConstLit(Const::Bool(false)) => Some(x),
Value::ConstLit(Const::Bool(true)) => {
let y = env.force_lazy(y);
match y.as_ref() {
Value::ConstLit(Const::Bool(_)) => Some(y),
_ => None,
}
}
_ => None,
}
}
_ => None,
},

Prim::BoolOr => |env: &ElimEnv, spine: &[Elim]| match spine {
[Elim::FunApp(_,x), Elim::FunApp(_, y)] => {
let x = env.force_lazy(x);
match x.as_ref() {
Value::ConstLit(Const::Bool(true)) => Some(x),
Value::ConstLit(Const::Bool(false)) => {
let y = env.force_lazy(y);
match y.as_ref() {
Value::ConstLit(Const::Bool(_)) => Some(y),
_ => None,
}
}
_ => None,
}
}
_ => None,
},

Prim::U8Eq => const_step!([x: U8, y: U8] => Const::Bool(x == y)),
Prim::U8Neq => const_step!([x: U8, y: U8] => Const::Bool(x != y)),
Prim::U8Gt => const_step!([x: U8, y: U8] => Const::Bool(x > y)),
Expand Down

0 comments on commit cbe97b7

Please sign in to comment.