concrete functor and monad transformers
#69Alternative instance for ContT
instance (Monoid r, Applicative m) => Alternative (ContT r m) where
empty = ContT $ const (pure mempty)
(ContT f) <|> (ContT g) = ContT $ \c -> liftA2 (<>) (f c) (g c)
Some ways this can be useful:
You're using
ContT
to implement a backtracking search.r
isMaybe (First Result))
for someResult :: *
.a <|> b
then means "trya
then tryb
".You have a function
inferType :: Expr a -> (a -> Type) -> Type
whereExpr a
represents an AST containing free variables ina
, and(a -> Type)
is an environment which maps free variables to their type.Type
has a monoid instance wheremempty
is the type of bottom and(<>)
is unification; you form aCont Type a
by way ofcont (inferType e)
. Then(<|>)
will lift(<>)
from working in the empty environment to working in arbitrary ones.