This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathEx3ResitTest.hs
208 lines (131 loc) · 6.59 KB
/
Ex3ResitTest.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
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
module Ex3ResitTest where
import Ex3
{----------------------------------------------------------------------}
{- CS316 (2019/20) EXERCISE 3 -}
{- -}
{- * * * RESIT TEST QUESTIONS * * * -}
{----------------------------------------------------------------------}
{- Submit by committing to GitLab at or before 4pm on Monday 2nd
December.
Your combined score from the submission and the test will be worth
40% of the overall marks for the class (so one mark is worth half a
percent).
This file contains the test questions. Answer the questions in this
file, and make sure that both are committed to GitLab before
arranging a call with the course lecturer (Robert Atkey
DIFFERENT TO PREVIOUS TESTS:
- most of the questions in this file require making changes to
your Ex3.hs.
- REMEMBER TO SEND BOTH FILES AT THE END OF THE TEST. -}
{----------------------------------------------------------------------}
{- GHOUL : Global Higher-order Untyped Language -}
{----------------------------------------------------------------------}
{----------------------------------------------------------------------}
{- Part 0 : ABSTRACT SYNTAX -}
{----------------------------------------------------------------------}
{- 3.0.1 List length.
Write a GHOUL program that computes the length of a list using
numbers represented by Z and S, similar to the following Haskell
program:
length [] = Z
length (x:xs) = S (length xs)
You can either write the function in GHOUL syntax (as a string), or
as a value of type 'Program'. -}
lengthProg :: Program -- replace with 'String', if you want
lengthProg = undefined
{- Also write a test case for this program and the expected output here:
-}
{- 4 MARKS -}
{----------------------------------------------------------------------}
{- Part 1 : VALUES and ENVIRONMENTS -}
{----------------------------------------------------------------------}
{- 3.1.2 Repeated Bindings
The basic version of GHOUL does not allow definitions like the
following, where we repeat a variable name in the patterns to
indicate that both arguments should be equal.
(isEqual x x) = True
(isEqual x y) = False
This restriction is enforced by the 'bindVar' function that fails
if a variable is matched more than once. Write a new version of
bindVar that allows repeated binding of a variable, as long as the
values matched are equal. -}
bindVarAllowRepeats :: String -> Value -> Env -> ErrorOr Env
bindVarAllowRepeats = undefined
{- 3 MARKS -}
{----------------------------------------------------------------------}
{- Part 2 : PATTERN MATCHING -}
{----------------------------------------------------------------------}
{- 3.2.2 Catch-all patterns
GHOUL does not allow catch-all patterns that do not bind a
variable, like Haskell's '_'. Adjust the Pat datatype and the
'matchPattern' function, and your pattern parser 'pat', to make the
following kind of definition work:
(alwaysOne _) = (S Z)
Write the changes you have made here:
REMEMBER TO UPDATE YOUR Ex3.hs FILE ON GITLAB. -}
{- 3 MARKS -}
{----------------------------------------------------------------------}
{- Part 3: EVALUATION OF EXPRESSIONS -}
{----------------------------------------------------------------------}
{- 3.3.1 A "Panic" function.
Add a case to the 'eval' function that evaluates any function
called 'panic' specially: it evaluates all the arguments, and then
aborts (using 'abortEval') with an error message containing all the
values that the arguments evaluated to pretty printed using
'ppValue'.
Example:
> runEval (eval M.empty (EA "panic" [EC "S" [EC "Z" []]])) []
Error "PANIC: (S Z)"
> runEval (eval M.empty (EA "panic" [EA "panic" [EC "AARRGH" []]])) []
Error "PANIC: AARRGH"
Write the changes you have made here:
REMEMBER TO UPDATE YOUR Ex3.hs FILE ON GITLAB.
-}
{- 5 MARKS -}
{----------------------------------------------------------------------}
{- Part 4 : PARSING -}
{----------------------------------------------------------------------}
{- 3.4.6 Desugaring lists
GHOUL allows the user to create lists by explicitly using 'Cons'
and 'Nil' constructors. For example, the program
(main) = (Cons A (Cons B (Cons C Nil)))
returns the list [A B C].
However, GHOUL does not allow the user to use a nice syntax like
'[A B C]'. (Note that we're not using commas here, just like in the
rest of GHOUL.)
For this question, modify the parser you have written to allow list
literals like '[]', '[A B C D]', '[(S Z) (S (S Z))]', '[(S Z) x]'
to appear in patterns and expressions. The parser should convert
list literals to the appropriate use of 'Cons' and 'Nil'. For
example:
> runParser pPat "[]"
OK (PC "Nil" [],"")
> runParser pPat "[A]"
OK (PC "Cons" [PC "A" [],PC "Nil" []],"")
> runParser pPat "[A B]"
OK (PC "Cons" [PC "A" [],PC "Cons" [PC "B" [],PC "Nil" []]],"")
> runParser pPat "[x]"
OK (PC "Cons" [PV "x",PC "Nil" []],"")
> runParser pPat "[x y]"
OK (PC "Cons" [PV "x",PC "Cons" [PV "y",PC "Nil" []]],"")
> runParser pPat "[(S Z) x (S (S Z))]"
OK (PC "Cons" [PC "S" [PC "Z" []],PC "Cons" [PV "x",PC "Cons" [PC "S" [PC "S" [PC "Z" []]],PC "Nil" []]]],"")
and similar for expressions.
NOTE: you do not need to alter the 'Pat' or 'Exp' datatypes, only
the 'pPat' and 'pExp' functions, plus some auxillary functions
(described below).
HINT: First define an auxillary function called 'mkPatList' of type
'[Pat] -> Pat' that takes a list of patterns and produces a single
pattern using 'PC "Cons" [_, _]' and 'PC "Nil" []' (you fill in the
'_'s). Then extend 'pPat' to recognise lists of patterns surrounded
by square brackets and separated by spaces, and use 'mkPatList' to
turn the list of patterns into a 'Pat'tern. Then do the same for
'Exp'ressions.
Write the changes you made here:
REMEMBER TO UPDATE YOUR Ex3.hs FILE ON GITLAB.
-}
{- 5 MARKS -}
{----------------------------------------------------------------------}
{- END OF EXERCISE (TEST QUESTIONS) -}
{----------------------------------------------------------------------}