This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
flow.rkt
169 lines (151 loc) · 4.15 KB
/
flow.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
#lang racket/base
(require syntax/stx
racket/list
unstable/syntax
racket/function
racket/match)
(define (atom-union new old)
new)
(define flow-mt (hasheq))
(define (flow-union x y)
(for/fold
([x x])
([(ky vy) (in-hash y)])
(if (hash-has-key? x ky)
(hash-update x ky (curry atom-union vy))
(hash-set x ky vy))))
(define flow-funs empty)
(struct flow-fun (pred fun))
;; XXX my redo stuff doesn't really work, it should use equal? between init and next
(define flows (make-hash))
(define (flow stx)
(define (round init)
(let loop ([last init]
[l flow-funs])
(cond
[(empty? l)
last]
[(memf
(λ (x)
(equal? stx (car x)))
(rest
(continuation-mark-set->list (current-continuation-marks) 'flowing)))
=>
(λ (after)
(for-each (λ (x) (set-box! (cdr x) #t)) after)
last)]
[else
(loop (if ((flow-fun-pred (first l)) stx)
(flow-union
last
((flow-fun-fun (first l)) stx))
last)
(rest l))])))
(let loop ([i 2]
[init (hash-ref flows stx flow-mt)])
(define redo-box (box #f))
(define next
(with-continuation-mark 'flowing (cons stx redo-box)
(round init)))
;;(printf "[~a] ~a -> ~a\n" i init next)
(hash-set! flows stx next)
(if (or (<= i 0) (not (unbox redo-box)))
next
(loop (sub1 i) next))))
(define-syntax-rule (define-flow pred fun)
(set! flow-funs
(cons (flow-fun pred
fun)
flow-funs)))
(define-syntax-rule (define-flow/id id fun)
(define-flow (λ (stx)
(and (stx-list? stx)
(bound-identifier=? (stx-car stx) #'id)))
fun))
(define (flow-attr stx k dv)
(hash-ref (flow stx) k (λ () dv)))
;;;;;;;;;;;;;;;;;;;;
(define (list-lift f l1 l2)
(remove-duplicates
(for*/list ([e1 (in-list l1)]
[e2 (in-list l2)])
(f e1 e2))))
(define (all-but-last l)
(if (empty? l)
empty
(reverse (rest (reverse l)))))
(define-flow/id begin
(λ (stx)
(syntax-case stx ()
[(_ e_0 ... e_end)
(hasheq 'value (flow-attr #'e_end 'value empty)
'value-stx
#`(begin
;; XXX this should drop the last thing
#,@(flow-attr stx 'effect-stxs empty)
#,(flow-attr #'e_end 'value-stx #f)))])))
(define-flow/id begin
(λ (stx)
(syntax-case stx ()
[(_ e_0 ... e_end)
(hasheq 'effect-stxs
(append-map (λ (x) (flow-attr x 'effect-stxs empty))
(syntax->list #'(e_0 ... e_end))))])))
(define-flow/id *
(λ (stx)
(syntax-case stx ()
[(_ lhs rhs)
(hasheq 'value
(list-lift *
(flow-attr #'lhs 'value empty)
(flow-attr #'rhs 'value empty))
'effect-stxs
(append (flow-attr #'lhs 'effect-stxs empty)
(flow-attr #'rhs 'effect-stxs empty))
'value-stx
#`(* #,(flow-attr #'lhs 'value-stx #f)
#,(flow-attr #'rhs 'value-stx #f)))])))
(define-flow/id set!
(λ (stx)
(syntax-case stx ()
[(_ id rhs)
(hasheq 'value
(list (void))
'effect-stxs
(list stx)
'value-stx
#'stx)])))
(define-flow
(λ (stx)
(number? (syntax->datum stx)))
(λ (stx)
(hasheq 'value (list (syntax->datum stx))
'effect-stxs '()
'value-stx stx)))
(module+ test
(require rackunit)
(check-equal?
(hash-ref
(flow
#'(begin
(* 10 5)))
'value)
(list 50))
(check-equal?
(syntax->datum
(hash-ref
(flow
#'(begin
(* 10 5)
(* 10 5)))
'value-stx))
'(begin (* 10 5)))
(check-equal?
(syntax->datum
(hash-ref
(flow
#'(begin
(* (begin (set! x 5) 10) 5)
(* 10 5)))
'value-stx))
'(begin (set! x 5) (* 10 5))))