-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
160 lines (118 loc) · 3.74 KB
/
Main.hs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
module Main where
import Prelude hiding (Maybe, Nothing, Just)
import Control.Applicative (Applicative)
import Control.Monad (ap, return)
-------------------------------------------------------------------------------
-- I - Maybe Functor / Monad
-------------------------------------------------------------------------------
data Maybe a = Nothing | Just a deriving Show
instance Functor Maybe where
-- fmap :: (a -> b) -> (Maybe a -> Maybe b)
fmap f Nothing = Nothing
fmap f (Just a) = Just (f a)
instance Applicative Maybe where
pure = return
(<*>) = ap
instance Monad Maybe where
-- return :: a -> Maybe a
return a = Just a
-- (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
(>>=) Nothing k = Nothing
(>>=) (Just a) k = k a
-- optional
(>=>) :: (b -> Maybe c) -> (a -> Maybe b) -> (a -> Maybe c)
(>=>) f g = \a -> (g a) >>= f
join :: Maybe(Maybe a) -> Maybe a
join Nothing = Nothing
join (Just(Just a)) = Just a
-------------------------------------------------------------------------------
-- I - Maybe Example
-------------------------------------------------------------------------------
-- sqrt :: Float -> Float
inv :: Float -> Float
inv x = 1/x
-- Imperative, Impure pseudo code
-- safeInvSqrt x = try sqrt inv x catch error
-- Pure, Kleisli arrow
safeInvSqrt1 x = if (x < 0 || x == 0)
then Nothing
else Just (sqrt (inv x))
-- Pure, with the Maybe Monad:
safeSqrt :: Float -> Maybe Float
safeSqrt x = if (x < 0)
then Nothing
else Just(sqrt x)
safeInv :: Float -> Maybe Float
safeInv x = if (x == 0)
then Nothing
else Just(inv x)
safeInvSqrt2 :: Float -> Maybe Float
safeInvSqrt2 x = safeSqrt x >>= safeInv
safeInvSqrt3 :: Float -> Maybe Float
safeInvSqrt3 = safeSqrt >=> safeInv
neg:: Float -> Float
neg x = -x
safeNegInvSqrt :: Float -> Maybe Float
safeNegInvSqrt x = fmap neg (safeInvSqrt3 x)
-------------------------------------------------------------------------------
-- II State Functor / Monad
-------------------------------------------------------------------------------
newtype State s a = State { runState :: s -> (a, s) }
instance Functor (State s) where
-- fmap :: (a -> b) -> (State s a -> State s b)
fmap f (State g) = State $ \s ->
let (a, s') = g s
in (f a, s')
instance Applicative (State s) where
pure = return
(<*>) = ap
instance Monad (State s) where
-- return :: a -> State s a
return a = State $ \s -> (a, s)
-- (>>=) :: State s a -> (a -> State s b) -> State s b
(>>=) (State f) k = State $ \s ->
let (a, s') = f s
in runState (k a) s'
get :: State s s
get = State $ \s -> (s, s)
put :: s -> State s ()
put s = State $ \_ -> ((), s)
modify :: (s -> s) -> State s ()
modify f = get >>= \x -> put (f x)
evalState :: State s a -> s -> a
evalState f = fst . runState f
execState :: State s a -> s -> s
execState f = snd . runState f
-------------------------------------------------------------------------------
-- II - State Example
-------------------------------------------------------------------------------
-- Stack
type Stack = [Int]
emptyStack :: Stack
emptyStack = []
pop :: State Stack Int
pop = State $ \(x:xs) -> (x, xs)
push :: Int -> State Stack ()
push a = State $ \xs -> ((), a:xs)
topOfStack :: State Stack Int
topOfStack = State $ \(x:xs) -> (x, x:xs)
stackManip :: State Stack Int
stackManip = do
push 10
push 20
a <- pop
b <- pop
push (a + b)
topOfStack
stackManip2 = (push 10)
>>= \_ -> (push 20)
>>= \_ -> pop
>>= \a -> pop
>>= \b -> push(a + b)
>>= \_ -> topOfStack
main :: IO ()
main = do
let res = evalState stackManip2 emptyStack
print res
-- let res = safeNegInvSqrt (4)
-- print res