Skip to content

Commit 7e45bc7

Browse files
committed
Allow inference when the result has free variables.
1 parent e930195 commit 7e45bc7

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,10 @@
723723
#f]))))
724724

725725
;; C : cset? - set of constraints found by the inference engine
726+
;; X : (listof symbol?) - type variables that must have entries
726727
;; Y : (listof symbol?) - index variables that must have entries
727728
;; R : Type/c - result type into which we will be substituting
728-
(define/cond-contract (subst-gen C Y R)
729+
(define/cond-contract (subst-gen C X Y R)
729730
(cset? (listof symbol?) (or/c Values/c AnyValues? ValuesDots?) . -> . (or/c #f substitution/c))
730731
(define var-hash (free-vars-hash (free-vars* R)))
731732
(define idx-hash (free-vars-hash (free-idxs* R)))
@@ -816,7 +817,7 @@
816817
(for/hash ([(k v) (in-hash cmap)])
817818
(values k (t-subst (constraint->type v var-hash)))))])
818819
;; verify that we got all the important variables
819-
(and (for/and ([v (in-list (fv R))])
820+
(and (for/and ([v (in-list X)])
820821
(let ([entry (hash-ref subst v #f)])
821822
;; Make sure we got a subst entry for a type var
822823
;; (i.e. just a type to substitute)
@@ -867,8 +868,8 @@
867868
[cs (and expected-cset
868869
(cgen/list null X Y S T #:expected-cset expected-cset))]
869870
[cs* (% cset-meet cs expected-cset)])
870-
(and cs* (if R (subst-gen cs* Y R) #t))))
871-
infer)) ;to export a variable binding and not syntax
871+
(and cs* (if R (subst-gen cs* X Y R) #t))))
872+
infer)) ;to export a variable binding and not syntax
872873

873874
;; like infer, but T-var is the vararg type:
874875
(define (infer/vararg X Y S T T-var R [expected #f])
@@ -903,6 +904,6 @@
903904
#:return-unless cs #f
904905
(define m (cset-meet cs expected-cset))
905906
#:return-unless m #f
906-
(subst-gen m (list dotted-var) R)))
907+
(subst-gen m X (list dotted-var) R)))
907908

908909

pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/unit-tests/infer-tests.rkt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,36 @@
2020
(list (quote elems) ...))))
2121

2222
(begin-for-syntax
23+
(define-splicing-syntax-class result
24+
(pattern (~seq) #:with v #'#f)
25+
(pattern (~seq #:result v:expr)))
2326
(define-splicing-syntax-class vars
2427
(pattern (~seq) #:with vars #'empty)
25-
(pattern (~seq #:vars vars:expr) ))
28+
(pattern (~seq #:vars vars:expr)))
2629
(define-splicing-syntax-class indices
2730
(pattern (~seq) #:with indices #'empty)
28-
(pattern (~seq #:indices indices:expr) ))
31+
(pattern (~seq #:indices indices:expr)))
2932
(define-splicing-syntax-class pass
3033
(pattern (~seq) #:with pass #'#t)
3134
(pattern #:pass #:with pass #'#t)
3235
(pattern #:fail #:with pass #'#f)))
3336

3437
(define-syntax (infer-t stx)
3538
(syntax-parse stx
36-
([_ S:expr T:expr :vars :indices :pass]
39+
([_ S:expr T:expr R:result :vars :indices :pass]
3740
(syntax/loc stx
3841
(test-case (format "~a ~a~a" S T (if pass "" " should fail"))
39-
(define result (infer vars indices (list S) (list T) #f))
40-
(unless (equal? result pass)
42+
(define result (infer vars indices (list S) (list T) R.v))
43+
(unless (if pass result (not result))
4144
(fail-check "Could not infer a substitution")))))))
4245

4346
(define-syntax (infer-l stx)
4447
(syntax-parse stx
45-
([_ S:expr T:expr :vars :indices :pass]
48+
([_ S:expr T:expr R:result :vars :indices :pass]
4649
(syntax/loc stx
4750
(test-case (format "~a ~a~a" S T (if pass "" " should fail"))
48-
(define result (infer vars indices S T #f))
49-
(unless (equal? result pass)
51+
(define result (infer vars indices S T R.v))
52+
(unless (if pass result (not result))
5053
(fail-check "Could not infer a substitution")))))))
5154

5255

@@ -87,6 +90,7 @@
8790
(test-suite "Tests for infer"
8891
(infer-t Univ Univ)
8992
(infer-t (-v a) Univ)
93+
(infer-t (-v a) (-v a) #:result (-v a))
9094
(infer-t Univ (-v a) #:fail)
9195
(infer-t Univ (-v a) #:vars '(a))
9296
(infer-t (-v a) Univ #:vars '(a))
@@ -101,6 +105,9 @@
101105
(infer-t (make-ListDots -Symbol 'b) (make-ListDots Univ 'b) #:indices '(b))
102106
(infer-t (make-ListDots (-v b) 'b) (make-ListDots (-v b) 'b) #:indices '(b))
103107
(infer-t (make-ListDots (-v b) 'b) (make-ListDots Univ 'b) #:indices '(b))
108+
(infer-t (-pair (-v a) (make-ListDots (-v b) 'b))
109+
(-pair (-v a) (make-ListDots (-v b) 'b))
110+
#:result (-v a))
104111

105112
[infer-t (->... null ((-v a) a) (-v b)) (-> -Symbol -String) #:vars '(b) #:indices '(a)]
106113
[infer-t (->... null ((-v a) a) (make-ListDots (-v a) 'a)) (-> -String -Symbol (-lst* -String -Symbol)) #:indices '(a)]

0 commit comments

Comments
 (0)