0 | module Control.Monad.List
2 | import Control.HigherOrder
4 | import Control.Monad.Trans
8 | data ListM : (Type -> Type) -> (Type -> Type) where
10 | (::) : a -> m (ListM m a) -> ListM m a
14 | ListT : (Type -> Type) -> (Type -> Type)
15 | ListT m a = m (ListM m a)
20 | [ListT] Monad m => Semigroup (ListT m a) where
24 | (x :: xs) => pure $
x :: ((<+>) @{ListT} xs ys)
27 | [ListM] Monad m => Semigroup (ListM m a) where
29 | (x :: xs) <+> ys = x :: (<+>) @{ListT} xs (pure ys)
32 | ListTSemigroup : Monad m => Semigroup (ListT m a)
33 | ListTSemigroup = Semigroup.ListT
38 | ListT : Monad m => Monoid (ListT m a)
39 | ListT = MkMonoid @{Semigroup.ListT} (pure [])
43 | ListM : Monad m => Monoid (ListM m a)
44 | ListM = MkMonoid @{Semigroup.ListM} []
49 | [ListM] Functor m => Functor (ListM m) where
51 | map f (x :: xs) = f x :: map @{ListT} f xs
54 | [ListT] Functor m => Functor (ListT m) where
55 | map f x = map (map @{ListM} f) x
57 | namespace Applicative
59 | ListT : Monad m => Applicative (ListT m)
60 | ListT = MkApplicative @{Functor.ListT}
61 | (pure . (:: pure Nil))
66 | f :: fs => (<+>) @{Semigroup.ListT}
67 | (map @{Functor.ListT} f ys)
68 | ((<*>) @{Applicative.ListT} fs ys)
72 | bind : Monad m => ListT m a -> (a -> ListT m b) -> ListT m b
76 | x :: xs => (<+>) @{ListT} (f x) (bind xs f)
79 | ListT : Monad m => Monad (ListT m)
80 | ListT = MkMonad @{Applicative.ListT}
82 | (flip (bind {m}) id)
84 | namespace Alternative
86 | ListT : Monad m => Alternative (ListT m)
87 | ListT = MkAlternative @{Applicative.ListT} (pure []) (\x, y => (<+>) @{ListT} x y)
90 | MonadTrans ListT where
91 | lift = (>>= pure @{ListT})
94 | runListT' : Monad m => ListT m a -> m (Maybe a)
98 | x :: xs => pure (Just x)
101 | runListT : Monad m => ListT m a -> m (List a)
105 | x :: xs => map (x ::) (runListT xs)
108 | liftList : Applicative m => List a -> ListT m a
109 | liftList [] = pure []
110 | liftList (x :: xs) = pure (x :: liftList xs)