-
Notifications
You must be signed in to change notification settings - Fork 1
/
type-check-Rfun.rkt
84 lines (73 loc) · 3.21 KB
/
type-check-Rfun.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
#lang racket
(require "utilities.rkt")
(require "type-check-Rvec.rkt")
(provide type-check-Rfun type-check-Rfun-class)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Functions ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check-Rfun
;; TODO: Don't allow eq? on function types. -Jeremy
(define type-check-Rfun-class
(class type-check-Rvec-class
(super-new)
(inherit check-type-equal?)
(define/public (type-check-apply env e es)
(define-values (e^ ty) ((type-check-exp env) e))
(define-values (e* ty*) (for/lists (e* ty*) ([e (in-list es)])
((type-check-exp env) e)))
(match ty
[`(,ty^* ... -> ,rt)
(for ([arg-ty ty*] [param-ty ty^*])
(check-type-equal? arg-ty param-ty (Apply e es)))
(values e^ e* rt)]
[else (error 'type-check "expected a function, not ~a" ty)]))
(define/override (type-check-exp env)
(lambda (e)
(match e
[(FunRef f)
(values (FunRef f) (dict-ref env f))]
[(Apply e es)
(define-values (e^ es^ rt) (type-check-apply env e es))
(values (Apply e^ es^) rt)]
[(Call e es)
(define-values (e^ es^ rt) (type-check-apply env e es))
(values (Call e^ es^) rt)]
[else ((super type-check-exp env) e)]
)))
(define/public (type-check-def env)
(lambda (e)
(match e
[(Def f (and p:t* (list `[,xs : ,ps] ...)) rt info body)
(define new-env (append (map cons xs ps) env))
(define-values (body^ ty^) ((type-check-exp new-env) body))
(check-type-equal? ty^ rt body)
(Def f p:t* rt info body^)]
[else (error 'type-check "ill-formed function definition ~a" e)]
)))
(define/public (fun-def-type d)
(match d [(Def f (list `[,xs : ,ps] ...) rt info body) `(,@ps -> ,rt)]
[else (error 'type-check "ill-formed function definition in ~a" d)]))
(define/override (type-check-program e)
(match e
[(ProgramDefsExp info ds body)
(define new-env (for/list ([d ds])
(cons (Def-name d) (fun-def-type d))))
(define ds^ (for/list ([d ds]) ((type-check-def new-env) d)))
(define-values (body^ ty) ((type-check-exp new-env) body))
(check-type-equal? ty 'Integer body)
(ProgramDefsExp info ds^ body^)]
[(ProgramDefs info ds)
(define new-env (for/list ([d ds])
(cons (Def-name d) (fun-def-type d))))
(define ds^ (for/list ([d ds]) ((type-check-def new-env) d)))
;; TODO: check that main has Integer return type.
(ProgramDefs info ds^)]
[(Program info body)
(define-values (body^ ty) ((type-check-exp '()) body))
(check-type-equal? ty 'Integer body)
(ProgramDefsExp info '() body^)]
[else (error 'type-check "unrecognized ~a" e)]))
))
(define (type-check-Rfun p)
(send (new type-check-Rfun-class) type-check-program p))