monad
stringclasses 7
values | context
stringclasses 7
values |
---|---|
instance Monad Maybe' where
return = pure
Nothing' >>= _ = Nothing'
Just' x >>= f = f x | data Maybe' a = Just' a | Nothing'
instance Functor Maybe' where
fmap _ Nothing' = Nothing'
fmap f (Just' x) = Just' (f x)
instance Applicative Maybe' where
pure = Just'
Nothing' <*> _ = Nothing'
_ <*> Nothing' = Nothing'
Just' f <*> Just' x = Just' (f x)
|
instance Monad List' where
return = pure
Empty' >>= _ = Empty'
Cons' x xs >>= f = append' (f x) (xs >>= f)
where
append' Empty' ys = ys
append' (Cons' x xs) ys = Cons' x (append' xs ys) | data List' a = Empty' | Cons' a (List' a)
instance Functor List' where
fmap _ Empty' = Empty'
fmap f (Cons' x xs) = Cons' (f x) (fmap f xs)
instance Applicative List' where
pure x = Cons' x Empty'
Empty' <*> _ = Empty'
_ <*> Empty' = Empty'
Cons' f fs <*> xs = append' (fmap f xs) (fs <*> xs)
where
append' Empty' ys = ys
append' (Cons' x xs) ys = Cons' x (append' xs ys)
|
instance Monad Identity' where
return = pure
(Identity' x) >>= f = f x | newtype Identity' a = Identity' { runIdentity' :: a }
instance Functor Identity' where
fmap f (Identity' x) = Identity' (f x)
instance Applicative Identity' where
pure = Identity'
(Identity' f) <*> (Identity' x) = Identity' (f x)
|
instance Monad (Reader' r) where
return = pure
(Reader' ra) >>= aRb = Reader' (\r -> runReader' (aRb (ra r)) r) | newtype Reader' r a = Reader' { runReader' :: r -> a }
instance Functor (Reader' r) where
fmap f (Reader' ra) = Reader' (\r -> f (ra r))
instance Applicative (Reader' r) where
pure a = Reader' (\_ -> a)
(Reader' rab) <*> (Reader' ra) = Reader' (\r -> (rab r) (ra r))
|
instance Monad (Cont r) where
return = pure
(Cont ca) >>= aCb = Cont (\br -> ca (\a -> runCont (aCb a) br)) | newtype Cont r a = Cont { runCont :: (a -> r) -> r }
instance Functor (Cont r) where
fmap f (Cont ca) = Cont (\br -> ca (br . f))
instance Applicative (Cont r) where
pure x = Cont ($ x)
(Cont cf) <*> (Cont ca) = Cont (\br -> cf (\f -> ca (\a -> br (f a))))
|
instance Monad (State' s) where
return = pure
(State' sa) >>= aSb = State' $ \s ->
let (a, newState) = sa s
(State' sb) = aSb a
in sb newState | newtype State' s a = State' { runState' :: s -> (a, s) }
instance Functor (State' s) where
fmap f (State' sa) = State' $ \s ->
let (a, newState) = sa s
in (f a, newState)
instance Applicative (State' s) where
pure a = State' $ \s -> (a, s)
(State' sab) <*> (State' sa) = State' $ \s ->
let (ab, newState) = sab s
(a, finalState) = sa newState
in (ab a, finalState)
|
instance (Monoid w) => Monad (Writer' w) where
return = pure
(Writer' (x, log)) >>= f = let (Writer' (y, newLog)) = f x in Writer' (y, log `mappend` newLog) | newtype Writer' w a = Writer' { runWriter' :: (a, w) }
instance (Monoid w) => Functor (Writer' w) where
fmap f (Writer' (x, log)) = Writer' (f x, log)
instance (Monoid w) => Applicative (Writer' w) where
pure x = Writer' (x, mempty)
(Writer' (f, log1)) <*> (Writer' (x, log2)) = Writer' (f x, log1 `mappend` log2)
|
README.md exists but content is empty.
Use the Edit dataset card button to edit it.
- Downloads last month
- 35