Skip to content

Commit 668a0d5

Browse files
committed
simplify code x1
1 parent 755a62c commit 668a0d5

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

typed-racket-lib/typed-racket/infer/infer-unit.rkt

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"signatures.rkt" "fail.rkt"
2323
"promote-demote.rkt"
2424
racket/match
25-
(only-in racket/function curry curryr)
25+
(only-in racket/function curry curryr thunk)
2626
;racket/trace
2727
(contract-req)
2828
(for-syntax
@@ -66,6 +66,7 @@
6666
[(context V X Y)
6767
(context (append bounds V) (append vars X) (append indices Y))]))
6868

69+
6970
(define (inferable-index? ctx bound)
7071
(match ctx
7172
[(context _ _ Y)
@@ -493,6 +494,21 @@
493494
;; this constrains just x (which is a single var)
494495
(define (singleton S x T)
495496
(insert empty x S T))
497+
498+
(define (constrain tvar-a tvar-b #:above above)
499+
(match-define (F: var maybe-type-bound) tvar-a)
500+
(define-values (default sub sing) (if above
501+
(values Univ
502+
(thunk (subtype tvar-b maybe-type-bound obj))
503+
(curry singleton (var-promote tvar-b (context-bounds context)) var))
504+
(values -Bottom
505+
(thunk (subtype maybe-type-bound tvar-b obj))
506+
(curryr singleton var (var-demote tvar-b (context-bounds context))))))
507+
(cond
508+
[(not maybe-type-bound) (sing default)]
509+
[(sub) (sing maybe-type-bound)]
510+
[else #f]))
511+
496512
;; FIXME -- figure out how to use parameters less here
497513
;; subtyping doesn't need to use it quite as much
498514
(define cs (current-seen))
@@ -569,34 +585,24 @@
569585

570586
;; variables that are in X and should be constrained
571587
;; all other variables are compatible only with themselves
572-
[((F: (? (inferable-var? context) v) maybe-type-bound) T)
588+
[((and (F: (? (inferable-var? context))) S) T)
573589
#:return-when
574590
(match T
575591
;; fail when v* is an index variable
576592
[(F: v*) (and (bound-index? v*) (not (bound-tvar? v*)))]
577593
[_ #f])
578594
#f
579-
;; constrain v to be below T (but don't mention bounds)
580-
(let ([sing (curryr singleton v (var-demote T (context-bounds context)))])
581-
(cond
582-
[(and maybe-type-bound (subtype maybe-type-bound T obj))
583-
(sing maybe-type-bound)]
584-
[(not maybe-type-bound) (sing -Bottom)]
585-
[else #f]))]
586-
587-
[(S (F: (? (inferable-var? context) v) maybe-type-bound))
595+
;; constrain S to be below T (but don't mention bounds)
596+
(constrain S T #:above #f)]
597+
598+
[(S (and (F: (? (inferable-var? context))) T))
588599
#:return-when
589600
(match S
590601
[(F: v*) (and (bound-index? v*) (not (bound-tvar? v*)))]
591602
[_ #f])
592603
#f
593-
;; constrain v to be above S (but don't mention bounds)
594-
(let ([sing (curry singleton (var-promote S (context-bounds context)) v)])
595-
(cond
596-
[(and maybe-type-bound (subtype S maybe-type-bound obj))
597-
(sing maybe-type-bound)]
598-
[(not maybe-type-bound) (sing Univ)]
599-
[else #f]))]
604+
;; constrain T to be above S (but don't mention bounds)
605+
(constrain T S #:above #t)]
600606

601607
;; recursive names should get resolved as they're seen
602608
[(s (? Name? t))

0 commit comments

Comments
 (0)