Added sample implicit module snippet
Getty Ritter
10 years ago
| 1 | sig Monad where | |
| 2 | type t : * -> * | |
| 3 | return : a -> t a | |
| 4 | bind : t a -> (a -> t b) -> t b | |
| 5 | ||
| 6 | return : { M : Monad } -> a -> M.t a | |
| 7 | return x = M.return x | |
| 8 | ||
| 9 | (>>=) : { M : Monad } -> M.t a -> (a -> M.t b) -> M.t b | |
| 10 | (>>=) x f = M.bind x f | |
| 11 | ||
| 12 | map : { M : Monad } -> M.t a -> (a -> b) -> Mt. b | |
| 13 | map m f = m >>= (\ x -> return (f x)) | |
| 14 | ||
| 15 | join : { M : Monad } -> M.t (M.t a) -> M.t a | |
| 16 | join m = m >>= id | |
| 17 | ||
| 18 | unless : { M : Monad } -> Bool -> M.t () -> M.t () | |
| 19 | unless True _ = return () | |
| 20 | unless False m = m | |
| 21 | ||
| 22 | implicit struct MonadOpen where | |
| 23 | type t a = Option a | |
| 24 | return = Some | |
| 25 | bind (Some x) f = f x | |
| 26 | bind None _ = None | |
| 27 | ||
| 28 | implicit struct MonadList where | |
| 29 | type t a = List a | |
| 30 | return x = [x] | |
| 31 | bind x f = concat (map f x) |