-
Notifications
You must be signed in to change notification settings - Fork 1
/
Prelude.hs
136 lines (92 loc) · 2.69 KB
/
Prelude.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
{-
This is an example and a test. `cargo run` type checks this file and prints
inferred types. Type signatures below are deliberately omitted to check inferred
types. Eventually this will be a prelude for all h10 programs.
-}
--------------------------------------------------------------------------------
-- Fuctions
id x = x
const x _ = x
f . g = \x -> f (g x)
flip f x y = f y x
--------------------------------------------------------------------------------
-- Bool
data Bool
= False
| True
not False = True
not True = False
otherwise = True
True && x = x
False && _ = False
True || _ = True
False || x = x
--------------------------------------------------------------------------------
-- Maybe
data Maybe a
= Nothing
| Just a
maybe n f Nothing = n
maybe n f (Just x) = f x
--------------------------------------------------------------------------------
-- Either
data Either a b
= Left a
| Right b
either f g (Left x) = f x
either f g (Right y) = g y
--------------------------------------------------------------------------------
-- Functor
class Functor f where
fmap :: (a -> b) -> f a -> f b
instance Functor Maybe where
fmap _ Nothing = Nothing
fmap f (Just a) = Just (f a)
instance Functor (Either a) where
fmap _ (Left a) = Left a
fmap f (Right b) = Right (f b)
instance Functor [] where
fmap _ [] = []
fmap f (x : xs) = f x : fmap f xs
instance Functor ((->) r) where
fmap = (.)
--------------------------------------------------------------------------------
-- Applicative
class Functor f => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
instance Applicative Maybe where
pure = Just
Just f <*> m = fmap f m
Nothing <*> _ = Nothing
instance Applicative [] where
pure a = [a]
fs <*> xs = concat (fmap (\f -> fmap f xs) fs)
instance Applicative ((->) r) where
pure = const
(<*>) f g x = f x (g x)
--------------------------------------------------------------------------------
-- Monad
class Applicative m => Monad m where
(>>=) :: m a -> (a -> m b) -> m b
instance Monad Maybe where
Just a >>= f = f a
Nothing >>= _ = Nothing
instance Monad [] where
xs >>= f = concat (fmap (\x -> f x) xs)
instance Monad ((->) r) where
f >>= k = \r -> k (f r) r
--------------------------------------------------------------------------------
-- Some utilities to demonstrate inferring predicates
(<$) = fmap . const
($>) = flip (<$)
void x = () <$ x
m >> k = m >>= \_ -> k
join x = x >> id
--------------------------------------------------------------------------------
map :: (a -> b) -> [a] -> [b]
map = fmap
[] ++ l = l
(x : xs) ++ l = x : (xs ++ l)
concat [] = []
concat (l : ls) = l ++ concat ls