Skip to content

Commit 1a042f8

Browse files
author
Andrew Kent
authored
fix parsing of refinements and type aliases
1 parent 160e926 commit 1a042f8

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

typed-racket-lib/typed-racket/env/type-alias-helper.rkt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@
111111
;; recursive type aliases should be initialized.
112112
(define-values (type-alias-dependency-map type-alias-class-map)
113113
(for/lists (_1 _2)
114-
([entry (in-list type-alias-map)])
115-
(match-define (cons name alias-info) entry)
114+
([(name alias-info) (in-assoc type-alias-map)])
116115
(define links-box (box null))
117116
(define class-box (box null))
118-
(define type
119-
(parameterize ([current-type-alias-name name]
120-
[current-referenced-aliases links-box]
121-
[current-referenced-class-parents class-box])
122-
(parse-type (car alias-info))))
117+
;; parse types for effect
118+
(parameterize ([current-type-alias-name name]
119+
[current-referenced-aliases links-box]
120+
[current-referenced-class-parents class-box]
121+
[check-type-invariants-while-parsing? #f])
122+
(parse-type (car alias-info)))
123123
(define pre-dependencies
124124
(remove-duplicates (unbox links-box) free-identifier=?))
125125
(define (filter-by-type-alias-names names)

typed-racket-lib/typed-racket/private/parse-type.rkt

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
(provide star ddd/bound
4646
current-referenced-aliases
4747
current-referenced-class-parents
48-
current-type-alias-name)
48+
current-type-alias-name
49+
check-type-invariants-while-parsing?)
4950

5051
;; current-term-names : Parameter<(Listof Id)>
5152
;; names currently "bound" by a type we are parsing
@@ -96,6 +97,14 @@
9697
(parameterize ([current-arities (cons arity (current-arities))])
9798
e ...))
9899

100+
101+
;; code in type-alias-helper.rkt calls `parse-type` for effect to build up
102+
;; info about how types depend on eachother -- during this parsing, we can't
103+
;; check certain invariant successfully (i.e. when a user writes `(car p)`
104+
;; `p` is <: (Pair Any Any), etc), so we use this flag to disable/enable
105+
;; invariant checking while parsing
106+
(define check-type-invariants-while-parsing? (make-parameter #t))
107+
99108
(define-literal-syntax-class #:for-label car)
100109
(define-literal-syntax-class #:for-label cdr)
101110
(define-literal-syntax-class #:for-label vector-length)
@@ -379,7 +388,8 @@
379388
(pattern o:symbolic-object
380389
#:do [(define obj (attribute o.val))
381390
(define obj-ty (lookup-obj-type/lexical obj))]
382-
#:fail-unless (subtype obj-ty -Int)
391+
#:fail-when (and (check-type-invariants-while-parsing?)
392+
(not (subtype obj-ty -Int)))
383393
(format "terms in linear constraints must be integers, got ~a for ~a"
384394
obj-ty obj)
385395
#:attr val (attribute o.val)))
@@ -398,7 +408,8 @@
398408
(pattern (:*^ ~! n:exact-integer o:symbolic-object-w/o-lexp)
399409
#:do [(define obj (attribute o.val))
400410
(define obj-ty (lookup-obj-type/lexical obj))]
401-
#:fail-unless (subtype obj-ty -Int)
411+
#:fail-when (and (check-type-invariants-while-parsing?)
412+
(not (subtype obj-ty -Int)))
402413
(format "terms in linear constraints must be integers, got ~a for ~a"
403414
obj-ty obj)
404415
#:attr val (-lexp (list (syntax->datum #'n) (attribute o.val))))
@@ -424,21 +435,24 @@
424435
(pattern (:car^ ~! o:symbolic-object-w/o-lexp)
425436
#:do [(define obj (attribute o.val))
426437
(define obj-ty (lookup-obj-type/lexical obj))]
427-
#:fail-unless (subtype obj-ty (-pair Univ Univ))
438+
#:fail-when (and (check-type-invariants-while-parsing?)
439+
(not (subtype obj-ty (-pair Univ Univ))))
428440
(format "car expects a pair, but got ~a for ~a"
429441
obj-ty obj)
430442
#:attr val (-car-of (attribute o.val)))
431443
(pattern (:cdr^ ~! o:symbolic-object-w/o-lexp)
432444
#:do [(define obj (attribute o.val))
433445
(define obj-ty (lookup-obj-type/lexical obj))]
434-
#:fail-unless (subtype obj-ty (-pair Univ Univ))
446+
#:fail-when (and (check-type-invariants-while-parsing?)
447+
(not (subtype obj-ty (-pair Univ Univ))))
435448
(format "cdr expects a pair, but got ~a for ~a"
436449
obj-ty obj)
437450
#:attr val (-cdr-of (attribute o.val)))
438451
(pattern (:vector-length^ ~! o:symbolic-object-w/o-lexp)
439452
#:do [(define obj (attribute o.val))
440453
(define obj-ty (lookup-obj-type/lexical obj))]
441-
#:fail-unless (subtype obj-ty -VectorTop)
454+
#:fail-when (and (check-type-invariants-while-parsing?)
455+
(not (subtype obj-ty -VectorTop)))
442456
(format "vector-length expects a vector, but got ~a for ~a"
443457
obj-ty obj)
444458
#:attr val (-vec-len-of (attribute o.val))))
@@ -461,7 +475,8 @@
461475
(pattern (:*^ ~! coeff:exact-integer o:symbolic-object-w/o-lexp)
462476
#:do [(define obj (attribute o.val))
463477
(define obj-ty (lookup-obj-type/lexical obj))]
464-
#:fail-unless (subtype obj-ty -Int)
478+
#:fail-when (and (check-type-invariants-while-parsing?)
479+
(not (subtype obj-ty -Int)))
465480
(format "terms in linear expressions must be integers, got ~a for ~a"
466481
obj-ty obj)
467482
#:attr val (-lexp (list (syntax->datum #'coeff) (attribute o.val))))
@@ -470,7 +485,8 @@
470485
(pattern o:symbolic-object-w/o-lexp
471486
#:do [(define obj (attribute o.val))
472487
(define obj-ty (lookup-obj-type/lexical obj))]
473-
#:fail-unless (subtype obj-ty -Int)
488+
#:fail-when (and (check-type-invariants-while-parsing?)
489+
(not (subtype obj-ty -Int)))
474490
(format "terms in linear expressions must be integers, got ~a for ~a"
475491
obj-ty obj)
476492
#:attr val (attribute o.val))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#lang typed/racket/base #:with-refinements
2+
3+
(define-type Pear (Pair Integer Integer))
4+
(define-type SomeVectorsInAPair (Pair (Vectorof String)
5+
(Vectorof String)))
6+
7+
(define-type Pear1
8+
(Refine [p : Pear] (= (car p) 5)))
9+
10+
(define-type Pear2
11+
(Refine [p : Pear] (= (cdr p) 5)))
12+
13+
(define-type Vec
14+
(Refine [p : SomeVectorsInAPair]
15+
(= (vector-length (car p))
16+
(vector-length (cdr p)))))

0 commit comments

Comments
 (0)