-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype-check-Lfun.rkt
118 lines (104 loc) · 4.67 KB
/
type-check-Lfun.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
#lang racket
(require "utilities.rkt")
(require "type-check-Lvecof.rkt")
(provide type-check-Lfun type-check-Lfun-class)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Functions ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check-Lfun
;; TODO: Don't allow eq? on function types. -Jeremy
(define type-check-Lfun-class
(class type-check-Lvecof-class
(super-new)
(inherit check-type-equal?)
(field [max-parameters 32])
;; Need lenient checking for closure conversion.
;; Putting it here instead of in lambda because the C-level type
;; checkers also need it and inherit from this type checker.
(define/override (type-equal? t1 t2)
(match* (t1 t2)
[(`(,ts1 ... -> ,rt1) `(,ts2 ... -> ,rt2))
(and (for/and ([t1 ts1] [t2 ts2])
(type-equal? t1 t2))
(type-equal? rt1 rt2))]
[(other wise) (super type-equal? t1 t2)]))
(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 n)
(values (FunRef f n) (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)]
[(SetBang var rhs)
#:when (and (list? (dict-ref env var #f))
(member '-> (dict-ref env var #f)))
(error 'type-check
"assignment to top-level defined function is not allowed: ~a" e)]
[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)
(unless (< (length xs) max-parameters)
(error 'type-check "~a has too many parameters, max is ~a"
f max-parameters))
(for/fold ([params (set)]) ([x xs])
(when (set-member? params x)
(error 'type-check "duplicate argument identifier: ~a in ~a" x e))
(set-add params x))
(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)
(for ([d ds])
(when (eq? (Def-name d) 'main)
(error 'type-check
"top-level defined function can not be named 'main'")))
(define new-env
(for/fold ([new-env '()])
([d ds])
(when (dict-ref new-env (Def-name d) #f)
(error 'type-check "function has already defined: ~a" (Def-name d)))
(cons (cons (Def-name d) (fun-def-type d)) new-env)))
(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-Lfun p)
(send (new type-check-Lfun-class) type-check-program p))