Skip to content

Commit cbe97b7

Browse files
committed
Make BoolAnd and BoolOr maximally lazy
1 parent 63f6469 commit cbe97b7

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

fathom/src/core/prim.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,10 +652,44 @@ pub fn step(prim: Prim) -> Step {
652652
Prim::BoolEq => const_step!([x: Bool, y: Bool] => Const::Bool(x == y)),
653653
Prim::BoolNeq => const_step!([x: Bool, y: Bool] => Const::Bool(x != y)),
654654
Prim::BoolNot => const_step!([x: Bool] => Const::Bool(bool::not(*x))),
655-
Prim::BoolAnd => const_step!([x: Bool, y: Bool] => Const::Bool(*x && *y)),
656-
Prim::BoolOr => const_step!([x: Bool, y: Bool] => Const::Bool(*x || *y)),
657655
Prim::BoolXor => const_step!([x: Bool, y: Bool] => Const::Bool(*x ^ *y)),
658656

657+
Prim::BoolAnd => |env: &ElimEnv, spine: &[Elim]| match spine {
658+
[Elim::FunApp(_,x), Elim::FunApp(_, y)] => {
659+
let x = env.force_lazy(x);
660+
match x.as_ref() {
661+
Value::ConstLit(Const::Bool(false)) => Some(x),
662+
Value::ConstLit(Const::Bool(true)) => {
663+
let y = env.force_lazy(y);
664+
match y.as_ref() {
665+
Value::ConstLit(Const::Bool(_)) => Some(y),
666+
_ => None,
667+
}
668+
}
669+
_ => None,
670+
}
671+
}
672+
_ => None,
673+
},
674+
675+
Prim::BoolOr => |env: &ElimEnv, spine: &[Elim]| match spine {
676+
[Elim::FunApp(_,x), Elim::FunApp(_, y)] => {
677+
let x = env.force_lazy(x);
678+
match x.as_ref() {
679+
Value::ConstLit(Const::Bool(true)) => Some(x),
680+
Value::ConstLit(Const::Bool(false)) => {
681+
let y = env.force_lazy(y);
682+
match y.as_ref() {
683+
Value::ConstLit(Const::Bool(_)) => Some(y),
684+
_ => None,
685+
}
686+
}
687+
_ => None,
688+
}
689+
}
690+
_ => None,
691+
},
692+
659693
Prim::U8Eq => const_step!([x: U8, y: U8] => Const::Bool(x == y)),
660694
Prim::U8Neq => const_step!([x: U8, y: U8] => Const::Bool(x != y)),
661695
Prim::U8Gt => const_step!([x: U8, y: U8] => Const::Bool(x > y)),

0 commit comments

Comments
 (0)