#lang plai-typed (require plai-typed/s-exp-match) (define-type Value [numV (n : number)] [closV (arg : symbol) (body : ExprC) (env : Env)] [undefinedV]) ; for letrec (define-type ExprC [numC (n : number)] [idC (s : symbol)] [plusC (l : ExprC) (r : ExprC)] [multC (l : ExprC) (r : ExprC)] [lamC (n : symbol) (arg-type : Type) (body : ExprC)] [appC (fun : ExprC) (arg : ExprC)] [letrecC (n : symbol) (n-type : Type) (rhs : ExprC) (body : ExprC)]) (define-type Type [numT] [boolT] [arrowT (arg : Type) (result : Type)]) (define-type Binding [bind (name : symbol) (val : (boxof Value))]) (define-type-alias Env (listof Binding)) (define-type TypeBinding [tbind (name : symbol) (type : Type)]) (define-type-alias TypeEnv (listof TypeBinding)) (define mt-env empty) (define extend-env cons) (module+ test (print-only-errors true)) ;; parse ---------------------------------------- (define (parse [s : s-expression]) : ExprC (cond [(s-exp-match? `NUMBER s) (numC (s-exp->number s))] [(s-exp-match? `SYMBOL s) (idC (s-exp->symbol s))] [(s-exp-match? '{+ ANY ANY} s) (plusC (parse (second (s-exp->list s))) (parse (third (s-exp->list s))))] [(s-exp-match? '{* ANY ANY} s) (multC (parse (second (s-exp->list s))) (parse (third (s-exp->list s))))] [(s-exp-match? '{let {[SYMBOL : ANY ANY]} ANY} s) (let ([bs (s-exp->list (first (s-exp->list (second (s-exp->list s)))))]) (appC (lamC (s-exp->symbol (first bs)) (parse-type (third bs)) (parse (third (s-exp->list s)))) (parse (fourth bs))))] [(s-exp-match? '{letrec {[SYMBOL : ANY ANY]} ANY} s) (let ([bs (s-exp->list (first (s-exp->list (second (s-exp->list s)))))]) (letrecC (s-exp->symbol (first bs)) (parse-type (third bs)) (parse (fourth bs)) (parse (third (s-exp->list s)))))] [(s-exp-match? '{lambda {[SYMBOL : ANY]} ANY} s) (let ([arg (s-exp->list (first (s-exp->list (second (s-exp->list s)))))]) (lamC (s-exp->symbol (first arg)) (parse-type (third arg)) (parse (third (s-exp->list s)))))] [(s-exp-match? '{ANY ANY} s) (appC (parse (first (s-exp->list s))) (parse (second (s-exp->list s))))] [else (error 'parse "invalid input")])) (define (parse-type [s : s-expression]) : Type (cond [(s-exp-match? `num s) (numT)] [(s-exp-match? `bool s) (boolT)] [(s-exp-match? `(ANY -> ANY) s) (arrowT (parse-type (first (s-exp->list s))) (parse-type (third (s-exp->list s))))] [else (error 'parse-type "invalid input")])) (module+ test (test (parse '2) (numC 2)) (test (parse `x) ; note: backquote instead of normal quote (idC 'x)) (test (parse '{+ 2 1}) (plusC (numC 2) (numC 1))) (test (parse '{* 3 4}) (multC (numC 3) (numC 4))) (test (parse '{+ {* 3 4} 8}) (plusC (multC (numC 3) (numC 4)) (numC 8))) (test (parse '{let {[x : num {+ 1 2}]} y}) (appC (lamC 'x (numT) (idC 'y)) (plusC (numC 1) (numC 2)))) (test (parse '{letrec {[x : num {+ 1 2}]} y}) (letrecC 'x (numT) (plusC (numC 1) (numC 2)) (idC 'y))) (test (parse '{lambda {[x : num]} 9}) (lamC 'x (numT) (numC 9))) (test (parse '{double 9}) (appC (idC 'double) (numC 9))) (test/exn (parse '{{+ 1 2}}) "invalid input") (test (parse-type `num) (numT)) (test (parse-type `bool) (boolT)) (test (parse-type `(num -> bool)) (arrowT (numT) (boolT))) (test/exn (parse-type '1) "invalid input")) ;; interp ---------------------------------------- (define (interp [a : ExprC] [env : Env]) : Value (type-case ExprC a [numC (n) (numV n)] [idC (s) (lookup s env)] [plusC (l r) (num+ (interp l env) (interp r env))] [multC (l r) (num* (interp l env) (interp r env))] [lamC (n t body) (closV n body env)] [appC (fun arg) (type-case Value (interp fun env) [closV (n body c-env) (interp body (extend-env (bind n (box (interp arg env))) c-env))] [else (error 'interp "not a function")])] [letrecC (n t rhs body) (let ([b (box (undefinedV))]) (let ([new-env (extend-env (bind n b) env)]) (begin (set-box! b (interp rhs new-env)) (interp body new-env))))])) (module+ test (test (interp (parse '2) mt-env) (numV 2)) (test/exn (interp (parse `x) mt-env) "free variable") (test (interp (parse `x) (extend-env (bind 'x (box (numV 9))) mt-env)) (numV 9)) (test (interp (parse '{+ 2 1}) mt-env) (numV 3)) (test (interp (parse '{* 2 1}) mt-env) (numV 2)) (test (interp (parse '{+ {* 2 3} {+ 5 8}}) mt-env) (numV 19)) (test (interp (parse '{lambda {[x : num]} {+ x x}}) mt-env) (closV 'x (plusC (idC 'x) (idC 'x)) mt-env)) (test (interp (parse '{let {[x : num 5]} {+ x x}}) mt-env) (numV 10)) (test (interp (parse '{let {[x : num 5]} {let {[x : num {+ 1 x}]} {+ x x}}}) mt-env) (numV 12)) (test (interp (parse '{let {[x : num 5]} {let {[y : num 6]} x}}) mt-env) (numV 5)) (test (interp (parse '{{lambda {[x : num]} {+ x x}} 8}) mt-env) (numV 16)) (test (interp (parse '{letrec {[f : num f]} f}) mt-env) (undefinedV)) (test (interp (parse '{letrec {[f : (num -> num) {lambda {[x : num]} {+ x x}}]} {f 8}}) mt-env) (numV 16)) (test (interp (parse '{letrec {[f : (((num -> num) -> ((num -> num) -> (num -> num))) -> (num -> num)) {lambda {[sel : ((num -> num) -> ((num -> num) -> (num -> num)))]} {{sel {lambda {[x : num]} {{f {lambda {[f : (num -> num)]} {lambda {[g : (num -> num)]} g}}} 3}}} {lambda {[x : num]} x}}}]} {{f {lambda {[f : (num -> num)]} {lambda {[g : (num -> num)]} f}}} 9}}) mt-env) (numV 3)) (test/exn (interp (parse '{1 2}) mt-env) "not a function") (test/exn (interp (parse '{+ 1 {lambda {[x : num]} x}}) mt-env) "not a number") (test/exn (interp (parse '{let {[bad : (num -> num) {lambda {[x : num]} {+ x y}}]} {let {[y : num 5]} {bad 2}}}) mt-env) "free variable")) ;; num+ and num* ---------------------------------------- (define (num-op [op : (number number -> number)] [l : Value] [r : Value]) : Value (cond [(and (numV? l) (numV? r)) (numV (op (numV-n l) (numV-n r)))] [else (error 'interp "not a number")])) (define (num+ [l : Value] [r : Value]) : Value (num-op + l r)) (define (num* [l : Value] [r : Value]) : Value (num-op * l r)) (module+ test (test (num+ (numV 1) (numV 2)) (numV 3)) (test (num* (numV 2) (numV 3)) (numV 6))) ;; lookup ---------------------------------------- (define (make-lookup [name-of : ('a -> symbol)] [val-of : ('a -> 'b)]) (lambda ([name : symbol] [vals : (listof 'a)]) : 'b (cond [(empty? vals) (error 'find "free variable")] [else (if (equal? name (name-of (first vals))) (val-of (first vals)) ((make-lookup name-of val-of) name (rest vals)))]))) (define lookup (make-lookup bind-name (lambda (b) (unbox (bind-val b))))) (module+ test (test/exn (lookup 'x mt-env) "free variable") (test (lookup 'x (extend-env (bind 'x (box (numV 8))) mt-env)) (numV 8)) (test (lookup 'x (extend-env (bind 'x (box (numV 9))) (extend-env (bind 'x (box (numV 8))) mt-env))) (numV 9)) (test (lookup 'y (extend-env (bind 'x (box (numV 9))) (extend-env (bind 'y (box (numV 8))) mt-env))) (numV 8))) ;; typecheck ---------------------------------------- (define (typecheck [a : ExprC] [tenv : TypeEnv]) (type-case ExprC a [numC (n) (numT)] [plusC (l r) (typecheck-nums l r tenv)] [multC (l r) (typecheck-nums l r tenv)] [idC (n) (type-lookup n tenv)] [lamC (n arg-type body) (arrowT arg-type (typecheck body (extend-env (tbind n arg-type) tenv)))] [appC (fun arg) (type-case Type (typecheck fun tenv) [arrowT (arg-type result-type) (if (equal? arg-type (typecheck arg tenv)) result-type (type-error arg (to-string arg-type)))] [else (type-error fun "function")])] [letrecC (n t rhs body) (let ([new-tenv (extend-env (tbind n t) tenv)]) (if (equal? t (typecheck rhs new-tenv)) (typecheck body new-tenv) (type-error rhs (to-string t))))])) (define (typecheck-nums l r tenv) (type-case Type (typecheck l tenv) [numT () (type-case Type (typecheck r tenv) [numT () (numT)] [else (type-error r "num")])] [else (type-error l "num")])) (define (type-error a msg) (error 'typecheck (string-append "no type: " (string-append (to-string a) (string-append " not " msg))))) (define type-lookup (make-lookup tbind-name tbind-type)) (module+ test (test (typecheck (parse '10) mt-env) (numT)) (test (typecheck (parse '{+ 10 17}) mt-env) (numT)) (test (typecheck (parse '{* 10 17}) mt-env) (numT)) (test (typecheck (parse '{lambda {[x : num]} 12}) mt-env) (arrowT (numT) (numT))) (test (typecheck (parse '{lambda {[x : num]} {lambda {[y : bool]} x}}) mt-env) (arrowT (numT) (arrowT (boolT) (numT)))) (test (typecheck (parse '{{lambda {[x : num]} 12} {+ 1 17}}) mt-env) (numT)) (test (typecheck (parse '{let {[x : num 4]} {let {[f : (num -> num) {lambda {[y : num]} {+ x y}}]} {f x}}}) mt-env) (numT)) (test (typecheck (parse '{letrec {[f : bool f]} f}) mt-env) (boolT)) (test (typecheck (parse '{letrec {[f : (num -> num) {lambda {[x : num]} {+ x x}}]} {f 8}}) mt-env) (numT)) (test (typecheck (parse '{letrec {[f : (((num -> num) -> ((num -> num) -> (num -> num))) -> (num -> num)) {lambda {[sel : ((num -> num) -> ((num -> num) -> (num -> num)))]} {{sel {lambda {[x : num]} {{f {lambda {[f : (num -> num)]} {lambda {[g : (num -> num)]} g}}} 3}}} {lambda {[x : num]} x}}}]} {{f {lambda {[f : (num -> num)]} {lambda {[g : (num -> num)]} f}}} 9}}) mt-env) (numT)) (test/exn (typecheck (parse '{letrec {[x : (num -> num) 5]} x}) mt-env) "no type") (test/exn (typecheck (parse '{1 2}) mt-env) "no type") (test/exn (typecheck (parse '{{lambda {[x : bool]} x} 2}) mt-env) "no type") (test/exn (typecheck (parse '{+ 1 {lambda {[x : num]} x}}) mt-env) "no type") (test/exn (typecheck (parse '{* {lambda {[x : num]} x} 1}) mt-env) "no type"))