-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.2-let-3.10-list2.rkt
260 lines (222 loc) · 7.6 KB
/
3.2-let-3.10-list2.rkt
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#lang eopl
; Exercise 3.10 [**] Add an operation `list` to the language. This operation should take any number of arguments,
; and return an expressed value containing the list of their values.
; because of our previous work, we already support variadic arguments, so just to add a new op will be ok
;syntax
;Program ::= Expression
; a-program (exp1)
;Expression ::= Number
; const-exp (num)
;Expression ::= Operation ( {Expression}*, )
; op-exp (op exp1 exp2)
;Expression ::= if Expression then Expression else Expression
; if-exp (exp1 exp2 exp3)
;Expression ::= Identifier
; var-exp (var)
;Expression ::= let Identifier = Expression in Expression
; let-exp (var exp1 body)
;Operation ::= +|-|*|/|equal?|greater?|less?|minus|cons|car|cdr|null?|list
; NOTE: we change `op` from symbol to string, otherwise some `op`s such as `cons` will be treated as `identifier`s
(define scanner-spec
'((white-sp (whitespace) skip)
(comment ("%" (arbno (not #\newline))) skip)
(op ((or "+" "-" "*" "/" "equal?" "greater?" "less?" "minus" "zero?"
"cons" "car" "cdr" "null?" "list")) string)
(identifier (letter (arbno (or letter digit))) symbol)
(number (digit (arbno digit)) number)))
(define grammar
'((program (expression) a-program)
(expression (number) const-exp)
(expression (op "(" (separated-list expression ",") ")") op-exp)
(expression ("if" expression "then" expression "else" expression) if-exp)
(expression (identifier) var-exp)
(expression ("let" identifier "=" expression "in" expression) let-exp)
(expression ("emptylist") null-exp)
))
(sllgen:make-define-datatypes scanner-spec grammar)
(define list-the-datatypes
(lambda ()
(sllgen:list-define-datatypes scanner-spec grammar)))
(define just-scan
(sllgen:make-string-scanner scanner-spec grammar))
(define scan&parse
(sllgen:make-string-parser scanner-spec grammar))
;; Expressed values for the LET
(define-datatype expval expval?
(num-val (num number?))
(bool-val (bool boolean?))
(null-val)
(pair-val (car expval?) (cdr expval?)))
;expval->num : ExpVal → Int
(define (expval->num val)
(cases expval val
(num-val (num) num)
(else (report-expval-extractor-error 'num val))))
;expval->bool : ExpVal → Bool
(define (expval->bool val)
(cases expval val
(bool-val (bool) bool)
(else (report-expval-extractor-error 'bool val))))
(define true (bool-val #t))
(define false (bool-val #f))
(define (report-expval-extractor-error expect val)
(eopl:error 'expval->extractor "Bad expval: expected a ~s, got ~s" expect val))
; env data structure representation, from Exercise 2.21
(define (report-no-binding-found search-var)
(eopl:error 'apply-env "No binding for ~s" search-var))
(define (report-invalid-env env)
(eopl:error 'apply-env "Bad environment: ~s" env))
(define-datatype env-type env?
(empty-env)
(extend-env (var symbol?)
(val (lambda (val) #t))
(env env?)))
(define (apply-env env search-var)
(cases env-type env
(empty-env () (report-no-binding-found search-var))
(extend-env (var val env) (if (eqv? var search-var)
val
(apply-env env search-var)))))
(define (has-binding? env search-var)
(cases env-type env
(empty-env () #f)
(extend-env (var val env)
(or (eqv? var search-var)
(has-binding? env search-var)))))
;init-env : () → Env
;usage: (init-env) = [i=1,v=5,x=10]
(define (init-env)
(extend-env
'i (num-val 1)
(extend-env
'v (num-val 5)
(extend-env
'x (num-val 10)
(empty-env)))))
; evaluation
;run : String → ExpVal
(define (run string)
(value-of-program (scan&parse string)))
;value-of-program : Program → ExpVal
(define (value-of-program pgm)
(cases program pgm
(a-program (exp1)
(value-of exp1 (init-env)))))
;value-of : Exp × Env → ExpVal
(define value-of
(lambda (exp env)
(cases expression exp
(const-exp (num) (num-val num))
(var-exp (var) (apply-env env var))
(op-exp (op args)
(let ((proc (symbol->proc op)))
(apply proc (map (lambda (arg) (value-of arg env)) args))))
(if-exp (exp1 exp2 exp3)
(let ((val1 (value-of exp1 env)))
(if (expval->bool val1)
(value-of exp2 env)
(value-of exp3 env))))
(let-exp (var exp1 body)
(let ((val1 (value-of exp1 env)))
(value-of body
(extend-env var val1 env))))
(null-exp () (null-val)))))
; primitives
; arithmetic operations
; ExpVal(Int) × ExpVal(Int) → ExpVal(Int)
(define (prim-arith+ val1 val2)
(arith-bin + val1 val2))
(define (prim-arith- val1 val2)
(arith-bin - val1 val2))
(define (prim-arith* val1 val2)
(arith-bin * val1 val2))
(define (prim-arith/ val1 val2)
(arith-bin quotient val1 val2))
(define (arith-bin proc val1 val2)
(let ((num1 (expval->num val1))
(num2 (expval->num val2)))
(num-val (proc num1 num2))))
(define (prim-minus val1)
(num-val (- (expval->num val1))))
; comparison operations
; ExpVal(Int) × ExpVal(Int) → ExpVal(Bool)
(define (prim-compare=? val1 val2)
(compare-ex = val1 val2))
(define (prim-compare>? val1 val2)
(compare-ex > val1 val2))
(define (prim-compare<? val1 val2)
(compare-ex < val1 val2))
(define (compare-ex proc val1 val2)
(let ((num1 (expval->num val1))
(num2 (expval->num val2)))
(bool-val (proc num1 num2))))
; ExpVal(Int) → ExpVal(Bool)
(define (prim-zero? val1)
(if (zero? (expval->num val1))
true
false))
; list operations
; ExpVal(Any) × ExpVal(Any) → ExpVal(Pair)
(define (prim-cons val1 val2)
(pair-val val1 val2))
; ExpVal(Pair) → ExpVal(Any)
(define (prim-car val)
(cases expval val
(pair-val (a d) a)
(else (report-expval-extractor-error 'pair val))))
(define (prim-cdr val)
(cases expval val
(pair-val (a d) d)
(else (report-expval-extractor-error 'pair val))))
; ExpVal(Any) → ExpVal(Bool)
(define (prim-null? val)
(cases expval val
(null-val () true)
(else false)))
; () → ExpVal(Null)
; { ExpVal(Any) }+ → ExpVal(Pair)
(define (prim-list . lov)
(define (iter l)
(if (null? l)
(null-val)
(pair-val (car l) (iter (cdr l)))))
(iter lov))
; symbol -> procedure map
(define (symbol->proc s)
(case s
(("+") prim-arith+)
(("-") prim-arith-)
(("*") prim-arith*)
(("/") prim-arith/)
(("minus") prim-minus)
(("equal?") prim-compare=?)
(("greater?") prim-compare>?)
(("less?") prim-compare<?)
(("zero?") prim-zero?)
(("cons") prim-cons)
(("car") prim-car)
(("cdr") prim-cdr)
(("null?") prim-null?)
(("list") prim-list)))
; utils
; fold/l accumulator on the left
(define (fold/l op init lst1 . lsts)
(if (null? lst1)
init
(apply fold/l op (apply op init (car lst1) (map car lsts)) (cdr lst1) (map cdr lsts))))
; fold/r accumulator on the right
(define (fold/r op init lst1 . lsts)
(if (null? lst1)
init
(apply op (car lst1) (fold/r (lambda (lst args)
(cons (car lst) args))
(list (apply fold/r op init (cdr lst1) (map cdr lsts)))
lsts))))
(define repl
(sllgen:make-rep-loop "--> " value-of-program
(sllgen:make-stream-parser scanner-spec grammar)))
; test
;> (run "list()")
;#(struct:null-val)
;> (run "list(1, 2)")
;#(struct:pair-val #(struct:num-val 1) #(struct:pair-val #(struct:num-val 2) #(struct:null-val)))