-(define (~ a b)
- (list (list a b)))
-
-(define (consolidate x y)
- (define (merge a b)
- (cond ((null? a) b)
- ((null? b) a)
- (else (if (member (car b) a)
- (merge a (cdr b))
- (cons (car b) (merge a (cdr b)))))))
- (define (overlap? a b)
- (if (or (null? a) (null? b))
- #f
- (if (fold-left (lambda (acc v)
- (or acc (eq? v (car a))))
- #f b)
- #t
- (overlap? (cdr a) b))))
-
- (cond ((null? y) x)
- ((null? x) y)
- (else (let* ((a (car y))
- (merged (fold-left
- (lambda (acc b)
- (if acc
- acc
- (if (overlap? a b)
- (cons (merge a b) b)
- #f)))
- #f x))
- (removed (if merged
- (filter (lambda (b) (not (eq? b (cdr merged)))) x)
- x)))
- (if merged
- (consolidate removed (cons (car merged) (cdr y)))
- (consolidate (cons a x) (cdr y)))))))
+ (define (union p q)
+ (append (filter (lambda (x) (not (assoc (car x) p)))
+ q)
+ p))
+ (union a (map f b)))
+
+
+;; ; a1 -> a2 ~ a3 -> a4;
+;; ; a1 -> a2 !~ bool -> bool
+;; ; basically can the tvars be renamed
+(define (types-equal? x y)
+ (let ([cs (unify? x y)])
+ (if (not cs) #f
+ (let*
+ ([test (lambda (acc c)
+ (and acc
+ (tvar? (car c)) ; the only substitutions allowed are tvar -> tvar
+ (tvar? (cdr c))))])
+ (fold-left test #t cs)))))
+
+ ; input: a list of binds ((x . y) (y . 3))
+ ; returns: pair of verts, edges ((x y) . (x . y))
+(define (graph bs)
+ (define (go bs orig-bs)
+ (define (find-refs prog)
+ (ast-collect
+ (lambda (x)
+ (case (ast-type x)
+ ; only count a reference if its a binding
+ ['var (if (assoc x orig-bs) (list x) '())]
+ [else '()]))
+ prog))
+ (if (null? bs)
+ '(() . ())
+ (let* [(bind (car bs))
+
+ (vert (car bind))
+ (refs (find-refs (cdr bind)))
+ (edges (map (lambda (x) (cons vert x))
+ refs))
+
+ (rest (if (null? (cdr bs))
+ (cons '() '())
+ (go (cdr bs) orig-bs)))
+ (total-verts (cons vert (car rest)))
+ (total-edges (append edges (cdr rest)))]
+ (cons total-verts total-edges))))
+ (go bs bs))
+
+(define (successors graph v)
+ (define (go v E)
+ (if (null? E)
+ '()
+ (if (eqv? v (caar E))
+ (cons (cdar E) (go v (cdr E)))
+ (go v (cdr E)))))
+ (go v (cdr graph)))
+
+ ; takes in a graph (pair of vertices, edges)
+ ; returns a list of strongly connected components
+
+ ; ((x y w) . ((x . y) (x . w) (w . x))
+
+ ; =>
+ ; .->x->y
+ ; | |
+ ; | v
+ ; .--w
+
+ ; ((x w) (y))
+
+ ; this uses tarjan's algorithm, to get reverse
+ ; topological sorting for free
+(define (sccs graph)
+
+ (let* ([indices (make-hash-table)]
+ [lowlinks (make-hash-table)]
+ [on-stack (make-hash-table)]
+ [current 0]
+ [stack '()]
+ [result '()])
+
+ (define (index v)
+ (get-hash-table indices v #f))
+ (define (lowlink v)
+ (get-hash-table lowlinks v #f))
+
+ (letrec
+ ([strong-connect
+ (lambda (v)
+ (begin
+ (put-hash-table! indices v current)
+ (put-hash-table! lowlinks v current)
+ (set! current (+ current 1))
+ (push! stack v)
+ (put-hash-table! on-stack v #t)
+
+ (for-each
+ (lambda (w)
+ (if (not (hashtable-contains? indices w))
+ ; successor w has not been visited, recurse
+ (begin
+ (strong-connect w)
+ (put-hash-table! lowlinks
+ v
+ (min (lowlink v) (lowlink w))))
+ ; successor w has been visited
+ (when (get-hash-table on-stack w #f)
+ (put-hash-table! lowlinks v (min (lowlink v) (index w))))))
+ (successors graph v))
+
+ (when (= (index v) (lowlink v))
+ (let ([scc
+ (let new-scc ()
+ (let ([w (pop! stack)])
+ (put-hash-table! on-stack w #f)
+ (if (eqv? w v)
+ (list w)
+ (cons w (new-scc)))))])
+ (set! result (cons scc result))))))])
+ (for-each
+ (lambda (v)
+ (when (not (hashtable-contains? indices v)) ; v.index == -1
+ (strong-connect v)))
+ (car graph)))
+ result))
+