typed-racket icon indicating copy to clipboard operation
typed-racket copied to clipboard

Type inference problem

Open NoahStoryM opened this issue 3 years ago • 0 comments

What version of Racket are you using?

v8.2

What program did you run?

#lang typed/racket/base

(define-type Im-Nums (U Number (Immutable-HashTable Symbol Im-Nums)))
(define-predicate im-nums? Im-Nums)

(define-type Top-Nums (U Number HashTableTop))
(define-predicate top-nums? Top-Nums)

(define-type Nums (U Top-Nums Im-Nums))
(define-predicate nums? Nums)

(: test0 [-> Nums HashTableTop])
(define test0
  (λ (ht)
    (cond
      [(number? ht) (error "")]
      [(im-nums? ht) ht]
      [(top-nums? ht) ht])))

(: test1 [-> Nums HashTableTop])
(define test1
  (λ (ht)
    (cond
      [(number? ht) (error "")]
      [(or (top-nums? ht) (im-nums? ht)) ht])))

(: test2 [-> Nums HashTableTop])
(define test2
  (λ (ht)
    (cond
      [(number? ht) (error "")]
      [(nums? ht) ht])))

(: test3 [-> Nums HashTableTop])
(define test3
  (λ (ht)
    (cond
      [(number? ht) (error "")]
      [else ht])))

What should have happened?

I guess these test functions are equivalent, so there should be no errors. But only test0 works.

If you got an error message, please include it here.

test1 to test3 all fail:

Type Checker: type mismatch
  expected: HashTableTop
  given: Nums
  in: ht

NoahStoryM avatar Nov 06 '21 10:11 NoahStoryM