summaryrefslogtreecommitdiff
path: root/modules/oop/pf-objects.scm
blob: 5937d37d91a929e5bbec2dd6127ab2d44be5ce0a (about) (plain)
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
(define-module (oop pf-objects)
  #:use-module (oop goops)
  #:use-module (ice-9 vlist)
  #:use-module (ice-9 match)
  #:export (set ref make-pf <p> <py> <pf> <pyf>
                call with copy fset fcall make-p put put!
                pcall pcall! get fset-x pyclass? refq
                def-pf-class  mk-pf-class  make-pf-class
                def-p-class   mk-p-class   make-p-class
                def-pyf-class mk-pyf-class make-pyf-class
                def-py-class  mk-py-class  make-py-class
                define-python-class get-type py-class
                ))
#|
Python object system is basically syntactic suger otop of a hashmap and one
this project is inspired by the python object system and what it measn when
one in stead of hasmaps use functional hashmaps. We use vhashes, but those have a drawback in that those are not thread safe. But it is a small effort to work
with assocs or tree like functional hashmaps in stead.

The hashmap works like an assoc e.g. we will define new values by 'consing' a
new binding on the list and when the assoc take up too much space it will be
reshaped and all extra bindings will be removed.

The datastructure is functional but the objects mutate. So one need to 
explicitly tell it to not update etc.
|#

(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
(define-class <p>  (<applicable-struct>) h)
(define-class <pf> (<p>) size n)         ; the pf object consist of a functional
                                         ; hashmap it's size and number of live
                                         ; object
(define-class <py>  (<p>))
(define-class <pyf> (<pf>))

(define (mk x)
  (letrec ((o (make (ref x '__goops__))))
    (slot-set! o 'procedure
               (lambda x
                 (apply
                  (ref o '__call__ (lambda x (error "no __call__ method")))
                  x)))
    (cond
     ((is-a? x <pf>)
      (let ((r (ref x '__const__)))
        (slot-set! o 'h    (slot-ref r 'h))
        (slot-set! o 'size (slot-ref r 'size))
        (slot-set! o 'n    (slot-ref r 'n))
        o))
     
     ((is-a? x <p>)
      (let ((r (ref x '__const__))
            (h (make-hash-table)))        
        (hash-set! h '__class__ x)
        (slot-set! o 'h    h))
      o))))
          
(define (make-pyclass x)
  (letrec ((class (make x)))
    (slot-set! class 'procedure
               (lambda x
                 (let ((obj (mk class)))
                   (aif it (ref obj '__init__)
                        (apply it x)
                        (values))
                   obj)))
    class))

;; Make an empty pf object
(define* (make-pf #:optional (class <pf>))
  (define r (make-pyclass class))
  (slot-set! r 'h vlist-null)
  (slot-set! r 'size 0)
  (slot-set! r 'n 0)
  r)

(define* (make-p #:optional (class <p>))
  (define r (make-pyclass class))
  (slot-set! r 'h (make-hash-table))
  r)

(define-syntax-rule (hif it (k h) x y)
  (let ((a (vhash-assq k h)))
    (if (pair? a)
        (let ((it (cdr a)))
          x)
        y)))

(define-syntax-rule (cif (it h) (k cl) x y)
  (let* ((h (slot-ref cl 'h))
         (a (vhash-assq k h)))
    (if (pair? a)
        (let ((it (cdr a)))
          x)
        y)))

(define fail (cons 'fail '()))
(define-syntax-rule (mrefx x key l)
  (let ()
    (define (end)
      (if (null? l)
          #f
          (car l)))

    (define (parents li)
      (let lp ((li li))
        (if (pair? li)
            (let ((p (car li)))
              (cif (it h) (key p)
                   it
                   (hif it ('__parents__ h)
                        (let ((r (parents it)))
                          (if (eq? r fail)
                              (lp (cdr li))
                              r))
                        (lp (cdr li)))))
            fail)))
  
    (cif (it h) (key x)
         it
         (hif cl ('__class__ h)
              (cif (it h) (key cl)
                   it
                   (hif p ('__parents__ h)
                        (let ((r (parents p)))
                          (if (eq? r fail)
                              (end)
                              r))
                        (end)))
              (end)))))

(define-syntax-rule (mrefx- x key l) (mrefx-- (slot-ref x 'h) key l))
(define-syntax-rule (mrefx-- hi key l)
  (let ()
    (define (end)   (if (pair? l) (car l) #f))
    (define (ret q) (if (eq? q fail) (end) q))
    
    (define (find-in-class h)
      (let lp ((class-h h))
        (let ((r (hash-ref class-h key fail)))
          (if (eq? r fail)
              (aif parents (hash-ref class-h '__parents__ #f)
                   (let lpp ((parents parents))
                     (if (pair? parents)
                         (let ((parent (car parents)))
                           (let ((r (lp (slot-ref parent 'h))))
                             (if (eq? r fail)
                                 (lpp (cdr parents))
                                 r)))
                         fail))
                   fail)
              r))))

    (let* ((h hi)
           (r (hash-ref h key fail)))
      (if (eq? r fail)
          (aif class (hash-ref h '__class__)
               (ret (find-in-class (slot-ref class 'h)))
               (end))
          r))))

(define not-implemented (cons 'not 'implemeneted))

(define-syntax-rule (mrefx-py- x key l)
  (let ((f (mrefx- x '__getattribute__ '())))
    (if (or (not f) (eq? f not-implemented))
        (mrefx- x key l)
        (apply f x key l))))

(define-syntax-rule (mrefx-py x key l)
  (let ((f (mrefx x '__getattribute__ '())))
    (if (or (not f) (eq? f not-implemented))
        (mrefx    x key l)
        (apply f x key l))))

(define-syntax-rule (unx mrefx- mref-)
  (define-syntax-rule (mref- x key l)
    (let ((xx x))
      (let ((res (mrefx- xx key l)))
        (if (and (not (struct? res)) (procedure? res))
            (lambda z
              (apply res xx z))
            res)))))

(unx mrefx-    mref-)
(unx mrefx     mref)
(unx mrefx-py  mref-py)
(unx mrefx-py- mref-py-)

(define-syntax-rule (unx mrefx- mref-)
  (define-syntax-rule (mref- x key l)
    (let ((xx x))
      (let ((res (mrefx- xx key l)))
        (if (and (not (struct? res))
                 (not (pyclass? res))
                 (procedure? res))
            (lambda z
              (apply res xx z))
            res)))))

(unx mrefx-    mref-q)
(unx mrefx     mrefq)
(unx mrefx-py  mref-pyq)
(unx mrefx-py- mref-py-q)

(define-method (ref (x <pf> )  key . l) (mref     x key l))
(define-method (ref (x <p>  )  key . l) (mref-    x key l))
(define-method (ref (x <pyf>)  key . l) (mref-py  x key l))
(define-method (ref (x <py> )  key . l) (mref-py- x key l))

(define-method (refq (x <pf> )  key . l) (mrefq     x key l))
(define-method (refq (x <p>  )  key . l) (mref-q    x key l))
(define-method (refq (x <pyf>)  key . l) (mref-pyq  x key l))
(define-method (refq (x <py> )  key . l) (mref-py-q x key l))
      
;; the reshape function that will create a fresh new pf object with less size
;; this is an expensive operation and will only be done when we now there is
;; a lot to gain essentially tho complexity is as in the number of set
(define (reshape x)
  (let ((h (slot-ref x 'h))
        (m (make-hash-table))
        (n 0))
    (define h2 (vhash-fold (lambda (k v s)
                             (if (hash-ref m k #f)
                                 s
                                 (begin
                                   (hash-set! m k #t)
                                   (set! n (+ n 1))
                                   (vhash-consq k v s))))
                           vlist-null
                           h))
    (slot-set! x 'h h2)
    (slot-set! x 'size n)
    (slot-set! x 'n    n)
    (values)))

;; on object x add a binding that key -> val
(define-syntax-rule (mset x key val)
  (let ((h (slot-ref x 'h))
        (s (slot-ref x 'size))
        (n (slot-ref x 'n)))
    (slot-set! x 'size (+ 1 s))
    (let ((r (vhash-assq key h)))
      (when (not r)
        (slot-set! x 'n (+ n 1)))
      (slot-set! x 'h (vhash-consq key val h))
      (when (> s (* 2 n))
        (reshape x))
      (values))))

(define-syntax-rule (mset-py x key val)
  (let ((f (mref-py x '__setattr__ '())))
    (if (or (eq? f not-implemented) (not f))
        (mset x key val)
        (f key val))))

(define (pkh h) (hash-for-each (lambda x (pk x)) h) h)

(define-syntax-rule (mset- x key val)
  (let ()
    (define (s h) (begin (hash-set! h key val) #f))
    (define fret #t)
    (define (r h k) (hash-ref h k))
    (define-syntax-rule (ifh h fail-code)
      (if (r h key)
          (s h)
          fail-code))
          
    (define (hm x) (slot-ref x 'h))
    (let ((h (hm x)))
      (if (ifh h
               (aif it (r h '__class__)
                    (let lp ((cl it))
                      (let ((h (hm cl)))
                        (ifh h
                             (aif it (r h '__parents__)
                                  (let lp2 ((parents it))
                                    (if (pair? parents)
                                        (if (lp (car parents))
                                            (lp2 (cdr parents))
                                            fret)
                                        fret))
                                  fret))))
                    fret))
          (s h))
      (values))))

(define-syntax-rule (mset-py- x key val)
  (let ((f (mref-py- x '__setattr__ '())))
    (if (or (eq? f not-implemented) (not f))
        (mset- x key val)
        (f key val))))

(define-method (set (x <pf>)  key val) (mset     x key val))
(define-method (set (x <p>)   key val) (mset-    x key val))
(define-method (set (x <pyf>) key val) (mset-py  x key val))
(define-method (set (x <py>)  key val) (mset-py- x key val))

;; mref will reference the value of the key in the object x, an extra default
;; parameter will tell what the fail object is else #f if fail
;; if there is no found binding in the object search the class and
;; the super classes for a binding

;; call a function as a value of key in x with the object otself as a first
;; parameter, this is pythonic object semantics
(define-syntax-rule (mk-call mcall mref)
  (define-syntax-rule (mcall x key l)
    (apply (mref x key '()) l)))

(mk-call mcall     mref)
(mk-call mcall-    mref-)
(mk-call mcall-py  mref-py)
(mk-call mcall-py- mref-py-)
  
(define-method (call (x <pf>)  key . l) (mcall     x key l))
(define-method (call (x <p>)   key . l) (mcall-    x key l))
(define-method (call (x <pyf>) key . l) (mcall-py  x key l))
(define-method (call (x <py>)  key . l) (mcall-py- x key l))


;; make a copy of a pf object
(define-syntax-rule (mcopy x)
  (let ((r (make-pyclass <pf>)))
    (slot-set! r 'h (slot-ref x 'h))
    (slot-set! r 'size (slot-ref x 'size))
    (slot-set! r 'n (slot-ref x 'n))
    r))

(define-syntax-rule (mcopy- x)
  (let* ((r (make-p))
         (h (slot-ref r 'h)))
    (hash-for-each (lambda (k v) (hash-set! h k v)) (slot-ref x 'h))
    r))

(define-method (copy (x <pf>)) (mcopy  x))
(define-method (copy (x <p> )) (mcopy- x))
  

;; with will execute thunk and restor x to it's initial state after it has
;; finished note that this is a cheap operatoin because we use a functional
;; datastructure
(define-syntax-rule (mwith x thunk)
  (let ((old (mcopy x)))
    (let ((r (thunk)))
      (slot-set! x 'h    (slot-ref old 'h))
      (slot-set! x 'size (slot-ref old 'size))    
      (slot-set! x 'n    (slot-ref old 'n))
      r)))

(define-syntax-rule (mwith- x thunk)
  (let ((old (mcopy- x)))
    (let ((r (thunk)))
      (slot-set! x 'h    (slot-ref old 'h))
      r)))



;; a functional set will return a new object with the added binding and keep
;; x untouched
(define-method (fset (x <pf>) key val)
  (let ((x (mcopy x)))
    (mset x key val)
    x))

(define-method (fset (x <p>) key val)
  (let ((x (mcopy- x)))
    (mset x key val)
    x))

(define (fset-x obj l val)
  (let lp ((obj obj) (l l) (r '()))
    (match l
      (()
       (let lp ((v val) (r r))
         (if (pair? r)
             (lp (fset (caar r) (cdar r) v) (cdr r))
             v)))
      ((k . l)
       (lp (ref obj k #f) l (cons (cons obj k) r))))))



           

;; a functional call will keep x untouched and return (values fknval newx)
;; e.g. we get both the value of the call and the new version of x with
;; perhaps new bindings added
(define-method (fcall (x <pf>) key . l)
  (let* ((y (mcopy x))
         (r (mcall y key l)))
    (if (eq? (slot-ref x 'h) (slot-ref y 'h))
        (values r x)
        (values r y))))

(define-method (fcall (x <p>) key . l)
  (let ((x (mcopy x)))
    (values (mcall- x key l)
            x)))

;; this shows how we can override addition in a pythonic way

;; lets define get put pcall etc so that we can refer to an object like
;; e.g. (put x.y.z 1) (pcall x.y 1)

(define-syntax-rule (cross x k f set)
  (call-with-values (lambda () f)
    (lambda (r y)
      (if (eq? x y)
          (values r x)
          (values r (set x k y))))))

(define-syntax-rule (cross! x k f _) f)

(define-syntax mku
  (syntax-rules ()
    ((_ cross set setx f (key) (val ...))
     (setx f key val ...))
    ((_ cross set setx f (k . l) val)
     (cross f k (mku cross set setx (ref f k) l val) set))))

(define-syntax-rule (mkk pset setx set cross)
  (define-syntax pset
    (lambda (x)   
      (syntax-case x ()
        ((_ f val (... ...))
         (let* ((to (lambda (x)
                      (datum->syntax #'f  (string->symbol x))))
                (l (string-split (symbol->string (syntax->datum #'f)) #\.)))
           (with-syntax (((a (... ...)) (map (lambda (x) #`'#,(to x))
                                             (cdr l)))
                         (h       (to (car l))))
             #'(mku cross setx set h (a (... ...)) (val (... ...))))))))))

(mkk put    fset  fset cross)
(mkk put!   set   set  cross!)
(mkk pcall! call  fset cross!)
(mkk pcall  fcall fset cross)
(mkk get    ref   fset cross!)

;; it's good to have a null object so we don't need to construct it all the
;; time because it is functional we can get away with this.
(define null (make-pf))

;; append the bindings in x in front of y + some optimizations
(define (union x y)
  (define hx (slot-ref x 'h))
  (define hy (slot-ref y 'h))
  (define n  (slot-ref x 'n))
  (define s  (slot-ref x 'size))
  (define m (make-hash-table))

  (define h
    (vhash-fold
     (lambda (k v st)
       (if (vhash-assq k hy)
           (begin
             (set! s (+ s 1))
             (vhash-consq k v st))
           (if (hash-ref m k)
               s
               (begin
                 (set! n (+ n 1))
                 (set! s (+ s 1))
                 (hash-set! m k #t)
                 (vhash-consq k v st)))))
     hy
     hx))
  
  (define out (make-pyclass <pf>))
  (slot-set! out 'h h)
  (slot-set! out 'n n)
  (slot-set! out 'size s)
  out)

(define (union- class x y)
  (define hx (slot-ref x 'h))
  (define hy (slot-ref y 'h))  
  (define out (make-p class))
  (define h  (slot-ref out 'h))
  (hash-for-each (lambda (k v) (hash-set! h k v)) hy)
  (hash-for-each (lambda (k v) (hash-set! h k v)) hx)
  out)


;; make a class. A class add some meta information to allow for multiple
;; inherritance and add effectively static data to the object the functional
;; datastructure show it's effeciency now const is data that will not change
;; and bindings that are added to all objects. Dynamic is the mutating class
;; information. supers is a list of priorities
(define-syntax-rule (mk-pf make-pf-class <pf>)
  (define-syntax make-pf-class
    (lambda (x)
      (syntax-case x ()
        ((_ name const dynamic (supers (... ...)))
         (with-syntax (((sups (... ...)) (generate-temporaries
                                          #'(supers (... ...)))))
           #'(let ((sups supers) (... ...))
               (define name (make-class (list sups (... ...) <pf>) '()))
               (define class (dynamic name))
               (define __const__
                 (union const
                        (let lp ((sup (filter-parents
                                       (list sups (... ...)))))
                          (if (pair? sup)
                              (union (ref (car sup) '__const__  null)
                                     (lp (cdr sup)))
                              null))))
               
               (reshape __const__)
               (set  class '__const__    __const__)
               (set  class '__goops__    name)
               (set  class '__name__     'name)
               (set  class '__parents__  (filter-parents
                                          (list sups (... ...))))
               
               (set class '__goops__    name)
               (set  __const__ '__name__    'name)
               (set  __const__ '__goops__   class)
               (set  __const__ '__parents__ (filter-parents
                                             (list sups (... ...))))
               (set  __const__ '__goops__   name)
               class)))))))

(mk-pf make-pf-class <pf>)
(mk-pf make-pyf-class <pyf>)

(define (filter-parents l)
  (let lp ((l l))
    (if (pair? l)
        (if (is-a? (car l) <p>)
            (cons (car l) (lp (cdr l)))
            (lp (cdr l)))
        '())))

(define-syntax-rule (mk-p make-p-class <p>)
  (define-syntax make-p-class
    (lambda (x)
      (syntax-case x ()
        ((_ name const dynamic (supers (... ...)))
         (with-syntax (((sups (... ...)) (generate-temporaries
                                          #'(supers (... ...)))))
           #'(let ((sups supers) (... ...))
               (define name (make-class (list
                                         (if (is-a? sups <p>)
                                             (aif it (ref sups '__goops__ #f)
                                                  it
                                                  sups)
                                             sups)
                                         (... ...) <p>) '()))
               
               (define class (dynamic <p>))
               (set class '__name__    'name)
               (set class '__class__   #f)
               (set class '__goops__    name)
               (set class '__parents__  (filter-parents (list sups (... ...))))
               class)))))))

(mk-p  make-p-class  <p>)
(mk-p  make-py-class <py>)

;; Let's make an object essentially just move a reference

;; the make class and defclass syntactic sugar
(define-syntax-rule (mk-p/f make-pf mk-pf-class make-pf-class)
  (define-syntax-rule (mk-pf-class name (parents (... ...))
                                   #:const
                                   ((sdef mname sval) (... ...))
                                   #:dynamic
                                   ((ddef dname dval) (... ...)))
    (let ()
      (define name 
        (letrec ((mname sval) (... ...) (dname dval) (... ...))
          (make-pf-class name
                         (let ((s (make-pf)))
                           (set s 'mname mname) (... ...)
                           s)
                         (lambda (class)
                           (let ((d (make-pf class)))
                             (set d 'dname dname) (... ...)
                             d))                 
                         (parents (... ...)))))
      name)))

(mk-p/f make-pf mk-pf-class  make-pf-class)
(mk-p/f make-p  mk-p-class   make-p-class)
(mk-p/f make-pf mk-pyf-class make-pyf-class)
(mk-p/f make-p  mk-py-class  make-py-class)

(define-syntax-rule (def-pf-class name . l)
  (define name (mk-pf-class name . l)))

(define-syntax-rule (def-p-class  name . l)
  (define name (mk-p-class name . l)))

(define-syntax-rule (def-pyf-class name . l)
  (define name (mk-pyf-class name . l)))

(define-syntax-rule (def-py-class  name . l)
  (define name (mk-py-class name . l)))

(define (get-class o)
  (cond
   ((is-a? o <p>)
    o)
   (else
    (error "not a pyclass"))))

(define (get-type o)
  (cond
   ((is-a? o <pyf>)
    'pyf)
   ((is-a? o <py>)
    'py)
   ((is-a? o <pf>)
    'pf)
   ((is-a? o <p>)
    'p)
   (else
    'none)))

(define (print o l)
  (define p1 (if (pyclass? o) "C" "O"))
  (define p2 (if (pyclass? o) "C" "O"))
  (define port (if (pair? l) (car l) #t))
  (format port "~a"
          (aif it (if (pyclass? o) #f (ref o '__repr__ #f))
               (format
                #f "~a(~a)<~a>" p1 (get-type o) (it))
               (format
                #f "~a(~a)<~a>" p2 (get-type o) (ref o '__name__ 'None)))))

(define-method (write (o <p>) . l) (print o l))
(define-method (display (o <p>) . l) (print o l))

(define-syntax-rule (define-python-class name parents code ...)
  (define name
    (mk-py-class name parents
                 #:const
                 ()
                 #:dynamic
                 (code ...))))

(define (pyclass? x)
  (and (is-a? x <p>)
       (not (ref x '__class__))))


(define-method (py-class (o <p>))
  (ref o '__class__ 'type))