src/lazy/lazy.bend checks
raw source on the hub · import bolt@1.45.0.0/src/lazy/lazy.bend as Lazy
lazy: branches that do not run both ways. Bool.pick, Bool.and and Bool.or
are ordinary defs, so every argument is evaluated before the call: a
search written as Bool.pick(T, hit, here, go(rest)) walks the whole list
whatever hit says (measured native, 100 iterations over 100k cells: the
hit at the head costs 0.10 s, the same as the hit at the end, while the
forms here cost 0.00 s). A thunk Unit -> T is a value, so the branch
that is not taken is never applied. Keep them for work that does not
recurse: a thunk around a search's own recursive step allocates a closure
and leaves the loop on every step, where carrying the test as a Bool into
the next call keeps it a loop (the thunk rule, U015).
No law can say that a branch is not taken, since a law states what a term equals, not what evaluation skipped: each form here matches on the Bool before it applies the thunk, and laws/LazyMatchesEagerCombinators.bend proves the values agree with the strict forms.
1 import
import Base
Definitions
def stop source · line 19 · raw
@-A:Type -> @cc:Bool -> @aa:A -> @rest:(@_:Unit -> A) -> A
a when c, else what rest gives: the early exit of a search
def or_else source · line 27 · raw
@aa:Bool -> @rest:(@_:Unit -> Bool) -> Bool
True when a, else what rest gives: or that stops at the first True
def and_then source · line 35 · raw
@aa:Bool -> @rest:(@_:Unit -> Bool) -> Bool
what rest gives when a, else False: and that stops at the first False
def either source · line 44 · raw
@-A:Type -> @cc:Bool -> @yes:(@_:Unit -> A) -> @no:(@_:Unit -> A) -> A
what yes gives when c, else what no gives: a pick whose two branches each recurse, only the one taken run