-
Notifications
You must be signed in to change notification settings - Fork 73
/
type-check-Lfun.rkt
123 lines (106 loc) · 4.38 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
118
119
120
121
122
#lang racket
(require "utilities.rkt")
(require "type-check-Lvec.rkt")
(require "type-check-Lvecof.rkt")
(provide type-check-Lfun type-check-Lfun-has-type
type-check-Lfun-class type-check-fun-mixin)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Functions ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check-fun-mixin (for use in Lfun and Cfun)
(define (type-check-fun-mixin super-class)
(class super-class
(super-new)
(inherit check-type-equal?)
(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/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-exp env)
(lambda (e)
(match e
[(FunRef f n)
(values (FunRef f n) (dict-ref env f))]
[(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)]
)))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check-Lfun
;; TODO: Don't allow eq? on function types. -Jeremy
(define type-check-Lfun-class
(class (type-check-fun-mixin type-check-Lvecof-class)
(super-new)
(inherit check-type-equal? type-check-apply fun-def-type)
(field [max-parameters 32])
(define/override (type-check-exp env)
(lambda (e)
(match e
[(Apply e es)
(define-values (e^ es^ rt) (type-check-apply env e es))
(values (Apply 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)
(unless (< (length xs) max-parameters)
(error 'type-check "~a has too many parameters, max is ~a"
f max-parameters))
(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/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-Lfun p)
(send (new type-check-Lfun-class) type-check-program p))
(define (type-check-Lfun-has-type p)
(begin
(typed-vec #t)
(typed-vecof #t)
(define t (type-check-Lfun p))
(typed-vec #f)
(typed-vecof #f)
t))