-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChapter14_1.hs
More file actions
240 lines (163 loc) · 4.97 KB
/
Chapter14_1.hs
File metadata and controls
240 lines (163 loc) · 4.97 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
-----------------------------------------------------------------------
--
-- Haskell: The Craft of Functional Programming, 3e
-- Simon Thompson
-- (c) Addison-Wesley, 1996-2011.
--
-- Chapter 14, part 1
-- Also covers the properties in Section 14.7
--
-----------------------------------------------------------------------
module Chapter14_1 where
import Prelude hiding (Either(..),either,Maybe(..),maybe)
import Test.QuickCheck
import Control.Monad
-- Algebraic types
-- ^^^^^^^^^^^^^^^
-- Introducing algebraic types
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- We give a sequence of examples of increasing complexity ...
-- Enumerated types
-- ^^^^^^^^^^^^^^^^
-- Two enumerated types
data Temp = Cold | Hot
deriving (Show)
data Season = Spring | Summer | Autumn | Winter
deriving (Show,Eq,Enum)
-- A function over Season, defined using pattern matching.
weather :: Season -> Temp
weather Summer = Hot
weather _ = Cold
-- The Ordering type, as used in the class Ord.
-- data Ordering = LT | EQ | GT
-- Declaring Temp an instance of Eq.
instance Eq Temp where
Cold == Cold = True
Hot == Hot = True
_ == _ = False
-- Recursive algebraic types
-- ^^^^^^^^^^^^^^^^^^^^^^^^^
-- Expressions
-- ^^^^^^^^^^^
-- Representing an integer expression.
data Expr = Lit Integer |
Add Expr Expr |
Sub Expr Expr
deriving (Show,Eq)
-- Three examples from Expr.
expr1 = Lit 2
expr2 = Add (Lit 2) (Lit 3)
expr3 = Add (Sub (Lit 3) (Lit 1)) (Lit 3)
-- Evaluating an expression.
eval :: Expr -> Integer
eval (Lit n) = n
eval (Add e1 e2) = (eval e1) + (eval e2)
eval (Sub e1 e2) = (eval e1) - (eval e2)
-- Showing an expression.
-- instance Show Expr where
--
-- show (Lit n) = show n
-- show (Add e1 e2)
-- = "(" ++ show e1 ++ "+" ++ show e2 ++ ")"
-- show (Sub e1 e2)
-- = "(" ++ show e1 ++ "-" ++ show e2 ++ ")"
-- Trees of integers
-- ^^^^^^^^^^^^^^^^^
-- The type definition.
data NTree = NilT |
Node Integer NTree NTree
deriving (Show,Eq,Read,Ord)
-- Example trees
treeEx1 = Node 10 NilT NilT
treeEx2 = Node 17 (Node 14 NilT NilT) (Node 20 NilT NilT)
-- Definitions of many functions are primitive recursive. For instance,
sumTree,depth :: NTree -> Integer
sumTree NilT = 0
sumTree (Node n t1 t2) = n + sumTree t1 + sumTree t2
depth NilT = 0
depth (Node n t1 t2) = 1 + max (depth t1) (depth t2)
-- How many times does an integer occur in a tree?
occurs :: NTree -> Integer -> Integer
occurs NilT p = 0
occurs (Node n t1 t2) p
| n==p = 1 + occurs t1 p + occurs t2 p
| otherwise = occurs t1 p + occurs t2 p
-- Rearranging expressions
-- ^^^^^^^^^^^^^^^^^^^^^^^
-- Right-associating additions in expressions.
assoc :: Expr -> Expr
assoc (Add (Add e1 e2) e3)
= assoc (Add e1 (Add e2 e3))
assoc (Add e1 e2)
= Add (assoc e1) (assoc e2)
assoc (Sub e1 e2)
= Sub (assoc e1) (assoc e2)
assoc (Lit n)
= Lit n
-- Infix constructors
-- ^^^^^^^^^^^^^^^^^^
-- An alternative definition of Expr.
data Expr' = Lit' Integer |
Expr' :+: Expr' |
Expr' :-: Expr'
-- Mutual Recursion
-- ^^^^^^^^^^^^^^^^
-- Mutually recursive types ...
data Person = Adult Name Address Biog |
Child Name
data Biog = Parent String [Person] |
NonParent String
type Name = String
type Address = [String]
-- ... and functions.
showPerson (Adult nm ad bio)
= show nm ++ show ad ++ showBiog bio
showBiog (Parent st perList)
= st ++ concat (map showPerson perList)
-- Alternative definition of Expr (as used later in the calculator case
-- study.
-- data Expr = Lit Int |
-- Op Ops Expr Expr
-- data Ops = Add | Sub | Mul | Div
-- It is possible to extend the type Expr so that it contains
-- conditional expressions, \texttt{If b e1 e2}.
-- data Expr = Lit Int |
-- Op Ops Expr Expr |
-- If BExp Expr Expr
-- Boolean expressions.
data BExp = BoolLit Bool |
And BExp BExp |
Not BExp |
Equal Expr Expr |
Greater Expr Expr
-- QuickCheck for algebraic types
instance Arbitrary NTree where
arbitrary = sized arbNTree
arbNTree :: Int -> Gen NTree
arbNTree 0 = return NilT
arbNTree n
| n>0
= frequency[(1, return NilT),
(3, liftM3 Node arbitrary bush bush)]
where
bush = arbNTree (div n 2)
instance Arbitrary Expr where
arbitrary = sized arbExpr
arbExpr :: Int -> Gen Expr
arbExpr 0 = liftM Lit arbitrary
arbExpr n
| n>0
= frequency[(1, liftM Lit arbitrary),
(2, liftM2 Add bush bush),
(2, liftM2 Sub bush bush)]
where
bush = arbExpr (div n 2)
prop_assoc :: Expr -> Bool
prop_assoc expr =
eval expr == eval (assoc expr)
prop_depth :: NTree -> Bool
prop_depth t =
size t < 2^(depth t)
size :: NTree -> Integer
size NilT = 0
size (Node n t1 t2) = 1 + (size t1) + (depth t2)