Lazier functional programming, part 2
In Lazier functional programming, part 1, I suggested that some of the standard tools of lazy functional programming are not as lazy as they might be, including even if-then-else
.
Generalizing from boolean conditionals, I posed the puzzle of how to define a lazier either
function, which encapsulates case analysis for sum types.
The standard definition:
data Either a b = Left a | Right b
either :: (a → c) → (b → c) → Either a b → c
either f g (Left x) = f x
either f g (Right y) = g y
The comments to part 1 fairly quickly converged to something close to the laxer definition I had in mind:
eitherL f g = const (f ⊥ ⊓ g ⊥) ⊔ either f g
which is a simple generalization of Luke Palmer‘s laxer if-then-else (reconstructed from memory):
bool c a b = (a ⊓ b) ⊔ (if c then a else b)