My Hackage-search-fu might be weak, but does anyone know about a library that lifts various Bool operations to Applicative? I’m thinking things like:
(&&^) = liftA2 (&&)
notA = fmap not
etc
Normally I would use the functions in extra for this, but these all (unnecessarily) require Monad. I’m happy to patch extra, but before I do that I wanted to know if what I wanted already existed in a different library. I haven’t had any luck finding such a library yet!
The functions in extra do actually need more than Applicative, as they’re “lazy” in their effects:
ifM c t f = do
b <- c
if b then t else f
a ||^ b = ifM a (pure True) b
a &&^ b = ifM a b (pure False)
selective does offer variants which don’t require Monad, if a Selective constraint isn’t too much.
As for actual Applicative variants, I haven’t seen them exposed from anywhere … honestly, they don’t seem to cross the Fairbairn threshold in the first place.
Well, matter of what you’re working on, I think. I’m working with reactive-banana where everything is in Behavior. The point of this code is to be as obvious as possible - having to liftA2 (&&) imo slightly reduces that. Of course, nothing stopping me just defining this locally though.
I reimplement these all the time, especially when writing parsers, so I’ve opened a pull request to add them to Control.Applicative.Combinators and Control.Monad.Combinators in the parser-combinators package: Add <&&> and <||> combinators.
I am aware the OP did not ask for this, but is anyone using the Applicative or Monad structure itself as truth values? E.g. False ~ mzero and True ~ return ()? One could say this is conceptually the opposite to what functions like guard offer. The fun thing is, one can vary the logic by varying the monad.
Sure, yes = pure () and no = empty are in my vocab for logic programming with logict and similar monads. Haven’t tried this but pretty sure RebindableSyntax is enough to also give you roughly the right semantics for if, namely, if a then b else c = a *> b if a ≠ empty or c if a = empty.