-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoo1d.lisp
385 lines (301 loc) · 10.4 KB
/
oo1d.lisp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
; vim: set filetype=lisp tabstop=2 shiftwidth=2 expandtab :
#|
In this assignment you will use the LISP macro system
to learn that "objects" can be viewed as macros that
generate "lambdas".
To find what you need to do, look for TODOx"
When its all done, you should see output like this:
> clisp oo1d.lisp
(ENCAPSULATON 105.0 110.25 110.25)
(ENCAPSULATION 90.25)
(ENCAPSULATION 70.25)
(ENCAPSULATION 50.25)
(ENCAPSULATION 30.25)
(ENCAPSULATION 10.25)
(ENCAPSULATION -9.75)
(ENCAPSULATION -29.75)
(ENCAPSULATION -49.75)
(ENCAPSULATION -69.75)
(ENCAPSULATION -89.75)
(POLYMORPHISM 115.7079632679489662L0)
(INHERITANCE 100 105.0 110.25 110.25 1)
(INHERITANCE 90.25)
(INHERITANCE 70.25)
(INHERITANCE 50.25)
(INHERITANCE 30.25)
(INHERITANCE 10.25)
(INHERITANCE INSUFFICIENT-FUNDS)
(INHERITANCE INSUFFICIENT-FUNDS)
(INHERITANCE INSUFFICIENT-FUNDS)
(INHERITANCE INSUFFICIENT-FUNDS)
(INHERITANCE INSUFFICIENT-FUNDS)
(META ((ID . 2) (INTEREST-RATE . 0.05) (BALANCE . 0) (NAME)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; BACKGROUND:
In the following you need to debug some macro expansions. One tool
you can use for that is "xpand" For example, LISP's "case" statement
is a macro that expands into a "cond". e.g.
(xpand '(case x ((1 2 3) (print 'num))
((a b c) (print 'sym))
("string" (print 'string))
(otherwise (print 'dull))))
(let ((#:case-key-3001 x))
(cond
((or (eql #:case-key-3001 '1)
(eql #:case-key-3001 '2)
(eql #:case-key-3001 '3)) (print 'num))
((or (eql #:case-key-3001 'a)
(eql #:case-key-3001 'b)
(eql #:case-key-3001 'c)) (print 'sym))
((eql #:case-key-3001 '"string") (print 'string))
(t (print 'dull))))
|#
(defun xpand(x)
(terpri)
(write
(macroexpand-1 x) :pretty t :right-margin 40 :case :downcase)
(terpri))
#|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ENCAPSULATION
The following code calls the defthing macro to return a function
that can create an "account".
(defthing
account
:has ((name) (balance 0) (interest-rate .05))
:does ((withdraw (amt)
(decf balance amt))
(deposit (amt)
(incf balance amt))
(interest ()
(incf balance
(* interest-rate balance)))))
"defthing" creates a function that returns a lambda body.
This lambda body holds a case statement which, when called
with a message, returns another lambda body that can do something (run
some code or return a value). For example, from the above
defthing, the following function is generated:
(defun account (&key (name) (balance 0) (interest-rate .05))
(lambda (#:message2822)
(case #:message2822
(withdraw
(lambda (amt) (decf balance amt)))
(deposit
(lambda (amt) (incf balance amt)))
(interest
(lambda nil
(incf balance
(* interest-rate balance))))
(name (lambda nil name))
(balance (lambda nil balance))
(interest-rate
(lambda nil interest-rate)))))
Note the jump from the input form to the output form.
The forms "(name)" and "(balance .05)" expands into two
accessor lines (within the case statement.
(name (lambda nil name))
(balance (lambda nil balance))
which has the general form:
(slotname (lambda nil soltname))
The forms "(name)" and "(balance .05)" also expands into
init form in the header of the function.
(defun account (&key (name) (balance 0) (interest-rate .05))
...
)
which has the general form:
(defun account (&key INITS)
...
)
The code for this is as follows (but "methods-as-case"
and "datas-as-case" is missing... till you write it.
|#
(defun send (obj mess &rest args)
(apply (funcall obj mess) args))
(defmacro defthing (klass &key has does)
(let* ((message (gensym "MESSAGE")))
`(defun ,klass (&key ,@has)
(lambda (,message)
(case ,message
,@(methods-as-case does)
,@(datas-as-case (mapcar #'car has)))))))
#|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1. Make defthing work
TODO 1a. Why does mapcar call #'car over the "has"?
TODO 1b. Why is message set to a gensym?
TODO 1c. Implement "data-as-case":
(datas-as-case '(name balance interest-rate))
==>
((NAME (LAMBDA NIL NAME))
(BALANCE (LAMBDA NIL BALANCE))
(INTEREST-RATE (LAMBDA NIL INTEREST-RATE)))
1d. Implement "methods-as-case"
(methods-as-case '((more (x) (+ x 1)) (less (x) (- x 1))))
==>
((MORE (LAMBDA (X) (+ X 1)))
(LESS (LAMBDA (X) (- X 1))))
Now that that is working, the following should
expand nicely:
|#
; but first, uncomment this code
'(defthing
account
:has ((name) (balance 0) (interest-rate .05))
:does ((withdraw (amt)
(decf balance amt))
(deposit (amt)
(incf balance amt))
(interest ()
(incf balance
(* interest-rate balance)))))
#|
TODO 1e. Show the result of expanding you account.
|#
; uncomment this to see what an account looks like
'(xpand (account))
#|
TODO 1f.. Show the output from the following function
|#
(defun encapsulation ()
(let ((acc (account :balance 100)))
(print `(encapsulaton
,(send acc 'interest)
,(send acc 'interest)
,(send acc 'balance)))
(dotimes (i 10)
(print `(encapsulation
,(send acc 'withdraw 20))))
))
; to run encapuatlion, uncomment the following
'(encapsulation)
#|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; POLYMORPHISM
TODO 2a. Define an object "cirle" with variables x,y
(for the center of the circle) and radius
(to hold the size of the circle). Add a method
"area" that returns 2 *pi*radius^2
; run this to peek inside circle
'(xpand (circle))
TODO 2b. Define an object "rectangle" with variables x1,x2,y1,y2
that all default value of 0. Add
a method "area" that returns the area of that rectangle
TODO 2c. Show the output from the following test
|#
(defun polymorphism()
(let ((sum 0)
(all (list (circle :radius 1)
(rectangle :x2 10 :y2 10)
(circle :radius 2))))
(dolist (one all)
(incf sum (send one 'area)))
(print `(polymorphism ,sum))))
; to run, uncomment the following
'(polymorphism)
#|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Inheritance
3. To make inheritance work, we accumulate the "defthing" specs.
So when a subclass is created, we include into its definition all
its details from the superclass.
To store that accumulation, use a hashtable that stores "about"
structs.
|#
(defvar *meta* (make-hash-table))
(defstruct about has does )
#|
Also, yu'll need to write a new definition of the "defthing" macro
which we call "defklass". As a side-effect of creating the lambda,
it also sends that object a pointer to itself (see "_self!") as
well as telling the object what kind of thing it is (see "_ako!").
This new "defklass" macro returns:
`(defun ,klass (&key ,@has)
(let ((,self (lambda (,message)
(case ,message
,@(methods-as-case does)
,@(datas-as-case (mapcar #'car has))))))
(send ,self '_self! ,self)
(send ,self '_isa! ',klass)
,self))))
The key thing about defklass is that, before it returns anything,
it combines the subckass "has" and "uses" unformation with that of
the parent class.
To make that work, defklass has a ":isa" creation key. Using that
"isa" thing, we look up the "has" and "uses" slots of the parent
and then concat them into the new definitions. Then we store the
concatenated parts so we can access them later.
To store these concatenated definitions, use
(setf (gethash klass *meta*)
(make-about :has has :does does))
To access these definitions, use
(let* ((b4 (and isa (gethash isa *meta*)))
(has-before (and b4 (about-has b4)))
(does-before (and b4 (about-does b4))))
etc
Here's the new defklass macros using it, we define the following
hierarchy:
object
account
trimmed-account
|#
; implement defklass here
(let ((_counter 0))
(defun counter () (incf _counter)))
(defun meta? (x)
(and (symbolp x)
(eql (char (symbol-name x) 0) #\_)))
; uncomment the following when defklass is implemented
'(defklass
object
:has ((_self) (_isa) (id (counter)))
:does (
(_isa! (x) (setf _isa x))
(_self! (x) (setf _self x))
(show () (let ((slot-values)
(slots (mapcar #'car
(about-has (gethash _isa *meta*)))))
(dolist (one slots slot-values)
(if (not (meta? one))
(push `(,one . ,(send _self one))
slot-values)))))))
; uncomment the following when defklass is implemented
'(defklass
account
:isa object
:has ((name) (balance 0) (interest-rate .05))
:does ((withdraw (amt)
(decf balance amt))
(deposit (amt)
(incf balance amt))
(interest ()
(incf balance
(* interest-rate balance)))))
; uncomment this to see what is going on
'(xpand (account))
; uncomment the following when defklass is implemented
'(defklass
trimmed-account
:isa account
:does ((withdraw (amt)
(if (<= amt balance)
(decf balance amt)
'insufficient-funds))))
(defun inheritance ()
(let ((acc (trimmed-account)))
(print `(inheritance ,(send acc 'deposit 100)
,(send acc 'interest)
,(send acc 'interest)
,(send acc 'balance)
,(send acc 'id)))
(dotimes (i 10)
(print `(inheritance ,(send acc 'withdraw 20))))
))
; TODO: 3a show that the following works correctly
'(inheritance)
'(xpand (trimmed-account))
; TODO: 3b. show that the following prints out the slots of an object.
(defun meta ()
(let ((acc (trimmed-account)))
(print `(meta ,(send acc 'show))
)))
'(meta)