(if (eqv? (ast-type x) 'var)
(list (cons x body))
(let* ([constructor (car x)]
- [destructor (lambda (i) `(destruct ,i ,constructor))])
+ [destructor (lambda (i) (dtor-name constructor i))])
(flat-map (lambda (y i)
(pattern-match y (list (destructor i) body)))
(cdr x)
,@(filter (lambda (x) (eqv? (statement-type x) 'expr))
program)))
+ ; gets both constructors and destructors
+ ; (data A (foo Int Bool)
+ ; (bar Bool))
+ ; |
+ ; v
+ ; (foo . (abs Int (abs Bool A)))
+ ; (foo~0 . (abs A Int)
+ ; (foo~1 . (abs A Bool)
+ ; (bar . (abs Bool A)
+ ; (bar~0 . (abs A Bool)
+
+(define (data-tors data-def)
+ (define (constructor-type t products)
+ (fold-right (lambda (x acc) `(abs ,x ,acc)) t products))
+
+ (define (destructor ctor-name prod-type part-type index)
+ (let ([name (dtor-name ctor-name index)])
+ (cons name `(abs ,prod-type ,part-type))))
+
+ (let ([type-name (cadr data-def)]
+ [ctors (cddr data-def)])
+ (fold-right
+ (lambda (ctor acc)
+ (let* ([ctor-name (car ctor)]
+ [products (cdr ctor)]
+
+ [maker (cons ctor-name (constructor-type type-name products))]
+
+ [dtors (map (lambda (t i) (destructor ctor-name type-name t i))
+ products
+ (range 0 (length products)))])
+
+ (cons maker (append dtors acc))))
+ '()
+ ctrs)))
+
+(define (dtor-name ctor-name index)
+ (string->symbol
+ (string-append (symbol->string ctor-name)
+ "~"
+ (number->string index))))
+
; for use in normalized form
(define lambda-arg caadr)
; for use elsewhere
(let ((str (read-file "/tmp/test-output.txt")))
(test str output)))
+(test (data-tors '(data A
+ (foo Int Bool)
+ (bar Bool)))
+ '((foo . (abs Int (abs Bool A)))
+ (foo~0 . (abs A Int))
+ (foo~1 . (abs A Bool))
+ (bar . (abs Bool A))
+ (bar~0 . (abs A Bool))))
+
(test-types (typecheck '((lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))))
'(abs (abs Int (abs Int Int)) Int))
; ('a, ('b, 'a))
(define (env-lookup env n)
- (if (null? env) (error #f "empty env") ; it's a type equality
+ (if (null? env) (error #f "empty env" env n) ; it's a type equality
(if (eq? (caar env) n)
(cdar env)
(env-lookup (cdr env) n))))
(check new-env (last (let-body x)))))
(define (check env x)
- (display "check: ")
- (display x)
- (display "\n\t")
- (display env)
- (newline)
+ ;; (display "check: ")
+ ;; (display x)
+ ;; (display "\n\t")
+ ;; (display env)
+ ;; (newline)
(let
((res
(case (ast-type x)
(let ((return-type (substitute cs (caddr resolved-func-type))))
(list cs return-type))
(error #f "not a function")))))))
- (display "result of ")
- (display x)
- (display ":\n\t")
- (display (pretty-type (cadr res)))
- (display "\n\t[")
- (display (pretty-constraints (car res)))
- (display "]\n")
+ ;; (display "result of ")
+ ;; (display x)
+ ;; (display ":\n\t")
+ ;; (display (pretty-type (cadr res)))
+ ;; (display "\n\t[")
+ ;; (display (pretty-constraints (car res)))
+ ;; (display "]\n")
res))
; we typecheck the lambda calculus only (only single arg lambdas)
(define (typecheck prog)
- (define (constructor-type t ctr)
- (fold-left (lambda (acc x) `(abs ,x ,acc)) t (cdr ctr)))
- (define (constructors data-def)
- (let ([type-name (cadr data-def)]
- [ctrs (cddr data-def)])
- (fold-left (lambda (acc ctr)
- (cons (cons (car ctr) (constructor-type type-name ctr))
- acc))
- '()
- ctrs)))
- (let ([init-env (flat-map constructors (program-datas prog))])
+
+ (let ([init-env (flat-map data-tors (program-datas prog))])
(display init-env)
+ (newline)
(cadr (check init-env (normalize (program-body prog))))))
+
; returns a list of constraints
(define (~ a b)
(let ([res (unify? a b)])