File: Monad.hs

package info (click to toggle)
bali-phy 4.0~beta16%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,192 kB
  • sloc: cpp: 119,288; xml: 13,482; haskell: 9,722; python: 2,930; yacc: 1,329; perl: 1,169; lex: 904; sh: 343; makefile: 26
file content (118 lines) | stat: -rw-r--r-- 2,328 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
{-# LANGUAGE NoImplicitPrelude #-}
module Control.Monad where

import Compiler.Base
import Compiler.Error  -- for error
import Data.Function   -- for id
import Data.Maybe
import Data.OldList
import Data.Functor
import Data.Ord
import Compiler.Num

import Control.Applicative

infixl 1 >>, >>=

class Applicative m => Monad m where
    return :: a -> m a
    (>>=)  :: m a -> (a -> m b) -> m b
    (>>)   :: m a -> m b -> m b
    fail   :: String -> m a
    unsafeInterleaveIO :: m a -> m a


    return = pure
    f >> g = f >>= (\x -> g)
    fail s = error s
    unsafeInterleaveIO = error "no unsafeInterleaveIO for this class"

instance Monad [] where
    xs >>= f = concatMap f xs

instance Monad Maybe where
    (Just x) >>= f = f x
    Nothing >>=  f = Nothing

mapM f = sequence . map f

mapM_ f = sequence_ . map f

forM = flip mapM

forM_ = flip mapM_

sequence []     = return []
sequence (a:as) = do x <- a
                     xs <- sequence as
                     return (x:xs)

sequence_ [] = return ()
sequence_ (a:as) = do a
                      sequence_ as
                      return ()

(=<<) = flip (>>=)

infixr 1 <=<, >=>

f >=> g = \x -> do y <- f x
                   g y

f <=< g = flip (>=>)

forever as = do as
                forever as

--

join x = x >>= id

replicateM n a | n <= 0    = return []
               | otherwise = do x <- a
                                xs <- replicateM (n-1) a
                                return (x:xs)

replicateM_ n a | n <= 0     = return ()
                | otherwise  = do a
                                  replicateM_ (n-1) a

--

--guard

--when

--unless

---


liftM f a = do x <- a
               return $ f x

liftM2 f a b = do x <- a
                  y <- b
                  return $ f x y

liftM3 f a b c = do x <- a
                    y <- b
                    z <- c
                    return $ f x y z

liftM4 f a b c d = do x <- a
                      y <- b
                      z <- c
                      w <- d
                      return $ f x y z w

liftM5 f a b c d e = do x <- a
                        y <- b
                        z <- c
                        w <- d
                        u <- e
                        return $ f x y z w u

ap mf mx = do f <- mf
              x <- mx
              return $ f x