-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathtc-envops.rkt
243 lines (235 loc) · 11.3 KB
/
tc-envops.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#lang racket/base
(require "../utils/utils.rkt"
racket/match racket/list
(for-syntax racket/base syntax/parse)
(contract-req)
"../rep/type-rep.rkt"
"../rep/prop-rep.rkt"
"../rep/object-rep.rkt"
"../rep/rep-utils.rkt"
"../utils/tc-utils.rkt"
racket/set
"../types/tc-result.rkt"
"../types/resolve.rkt"
"../types/update.rkt"
"../types/prop-ops.rkt"
"../types/subtract.rkt"
"../types/path-type.rkt"
"../env/type-env-structs.rkt"
"../env/lexical-env.rkt"
"../env/mvar-env.rkt"
(only-in "../infer/infer.rkt" intersect)
(rename-in "../types/abbrev.rkt"
[-> -->]
[->* -->*]
[one-of/c -one-of/c])
"tc-metafunctions.rkt"
"tc-subst.rkt")
(provide with-lexical-env+props
with-lexical-env+props-simple
implies-in-env?
env+)
(define (implies-in-env? env p1 p2)
(not (env+ env (list p1 (negate-prop p2)))))
;; Returns #f if anything becomes (U)
(define (env+ env ps)
(cond
[(or (null? ps) (andmap TrueProp? ps)) env]
[else
(define-values (props atoms^) (combine-props ps (env-props env)))
(define atoms (and atoms^
;; fix the order of paths to the same object.
;; move objects with fewer path elements forward.
(sort atoms^
(lambda (x y)
(match* (x y)
[((TypeProp: (Path: pes1 (? identifier? var1)) _)
(TypeProp: (Path: pes2 (? identifier? var2)) _))
#:when (equal? var1 var2)
(and (< (length pes1) (length pes2)))]
[(_ _) #f])))))
(cond
[props
(let loop ([todo atoms]
[atoms '()]
[negs '()]
[new '()]
[Γ (env-replace-props env props)])
(match todo
[(cons p ps)
;; update-obj-pos-type : (listof Prop?) env? Object? Type? -> env or #f
;; sometimes we need to update the object's type directly -- this helper
;; does that and continues looping
(define (update-obj-pos-type new Γ obj pt)
(let ([t (lookup-obj-type/lexical obj Γ #:fail (λ (_) Univ))])
(define new-t (intersect t pt obj))
(cond
[(Bottom? new-t) #f]
[(equal? t new-t) (loop ps (cons p atoms) negs new Γ)]
[else
;; it's a new type! check if there are any logical propositions that can
;; be extracted from new-t
(define-values (new-t* new-props) (extract-props obj new-t))
(loop ps
(cons (-is-type obj new-t*) atoms)
negs
(append new-props new)
(env-set-obj-type Γ obj new-t*))])))
(match p
[(TypeProp: (and obj (Path: pes (? identifier? x))) pt)
(define t (lookup-id-type/lexical x Γ #:fail (λ (_) Univ)))
(define new-t (update t pt #t pes))
(cond
[(Bottom? new-t) #f]
[(equal? t new-t)
(cond
[(ormap uninterpreted-PE? pes) (update-obj-pos-type new Γ obj pt)]
[else (loop ps (cons p atoms) negs new Γ)])]
[else
;; it's a new type! check if there are any logical propositions that can
;; be extracted from new-t
(define-values (new-t* new-props) (extract-props (-id-path x) new-t))
(cond
;; if the path contains an uninterpreted path element,
;; we need to update the object's type in addition to
;; the identifier's type
[(ormap uninterpreted-PE? pes)
(update-obj-pos-type (append new-props new)
(env-set-id-type Γ x new-t*)
obj
pt)]
[(path-type pes new-t*)
=>
(lambda (pt)
(loop ps
(cons (-is-type obj pt) atoms)
negs
(append new-props new)
(env-set-id-type Γ x new-t*)))]
[else #f])])]
[(TypeProp: obj pt)
(update-obj-pos-type new Γ obj pt)]
;; process negative info _after_ positive info so we don't miss anything!
;; (overly simple example: if we started with x ∈ Any, updating it's type in Γ
;; with x ∉ String and then x ∈ String just produces a Γ with x ∈ String,
;; but updating with x ∈ String _and then_ x ∉ String derives a contradiction)
[(? NotTypeProp?)
(loop ps atoms (cons p negs) new Γ)]
[_ (loop ps atoms negs new Γ)])]
[_ (let loop ([negs negs]
[atoms atoms]
[new new]
[Γ Γ])
(match negs
[(cons p negs)
;; update-obj-neg-type : (listof Prop?) env? Object? Type? -> env or #f
;; sometimes we need to update the object's type directly -- this helper
;; does that and continues looping
(define (update-obj-neg-type new Γ obj pt)
(let ([t (lookup-obj-type/lexical obj Γ #:fail (λ (_) Univ))])
(define new-t (subtract t pt))
(cond
[(Bottom? new-t) #f]
[(equal? t new-t) (loop negs (cons p atoms) new Γ)]
[else
;; it's a new type! check if there are any logical propositions that can
;; be extracted from new-t
(define-values (new-t* new-props) (extract-props obj new-t))
(loop negs
(cons (-is-type obj new-t*) atoms)
(append new-props new)
(env-set-obj-type Γ obj new-t*))])))
(match p
[(NotTypeProp: (and obj (Path: pes (? identifier? x))) pt)
(define t (lookup-id-type/lexical x Γ #:fail (λ (_) Univ)))
(define new-t (update t pt #f pes))
(cond
[(Bottom? new-t) #f]
[(equal? t new-t)
(cond
[(ormap uninterpreted-PE? pes) (update-obj-neg-type new Γ obj pt)]
[else (loop negs (cons p atoms) new Γ)])]
[else
;; it's a new type! check if there are any logical propositions that can
;; be extracted from new-t
(define-values (new-t* new-props) (extract-props (-id-path x) new-t))
(cond
;; if the path contains an uninterpreted path element,
;; we need to update the object's type in addition to
;; the identifier's type
[(ormap uninterpreted-PE? pes)
(update-obj-neg-type (append new-props new)
(env-set-id-type Γ x new-t*)
obj
pt)]
[else
(loop negs
(cons p atoms)
(append new-props new)
(env-set-id-type Γ x new-t*))])])]
[(NotTypeProp: obj pt)
(update-obj-neg-type new Γ obj pt)])]
[_
(cond
;; there was a contradiction, return #f
[(not Γ) #f]
;; we're done updating types and nothing additional appeared, return the new Γ
[(null? new) (env-replace-props Γ (append atoms (env-props Γ)))]
;; Q: What if 'new' is not null?
;; A: as we're updating types, it's possible a new type had some refinements in it
;; that we have now extracted. For example, say we updated x's type from
;; (U String (Refine [n : Int] (<= n 42)))
;; to
;; (Refine [n : Int] (<= n 42))
;; now we _know_ not only that x has a more specific type, we know the logical
;; proposition (<= x 42).
;; So in cases like this where we have a new type and 'extract-props' found
;; something, we go back and logically add those extracted props to the environment
;; in case it further refines our logical view of the program.
[else (env+ (env-replace-props Γ (append atoms (env-props Γ))) new)])]))]))]
[else #f])]))
;; run code in an extended env and with replaced props.
;; Requires the body to return a tc-results.
;; WARNING: this may bail out when code is unreachable
(define-syntax (with-lexical-env+props stx)
(define-splicing-syntax-class unreachable?
(pattern (~seq #:unreachable form:expr))
(pattern (~seq) #:with form #'(begin)))
(syntax-parse stx
[(_ ps:expr
#:expected expected
u:unreachable? . b)
(syntax/loc stx
(let ([old-props (env-props (lexical-env))]
[new-env (env+ (lexical-env) ps)])
(cond
[new-env
(with-lexical-env
new-env
(let ([result (let () . b)])
(match expected
;; if there was not any expected results, then
;; return any new info that was learned while
;; extending the environment
[(or #f (tc-any-results: #f))
(define new-props
(make-AndProp (set-subtract (env-props new-env) old-props)))
(add-unconditional-prop result new-props)]
;; otherwise, just return the expected results
[_ (fix-results expected)])))]
[else
;; unreachable, bail out
u.form
(ret -Bottom)])))]))
(define-syntax (with-lexical-env+props-simple stx)
(syntax-parse stx
[(_ ps:expr #:absurd absurd:expr . b)
(syntax/loc stx
(let ([old-props (env-props (lexical-env))]
[new-env (env+ (lexical-env) ps)])
(cond
[new-env
(with-lexical-env
new-env
(let () . b))]
[else absurd])))]))