[racket] Typed Racket: can this recursive typing become polymorphic?

From: Matthew Butterick (mb at mbtype.com)
Date: Tue Feb 3 19:29:47 EST 2015

I'm trying to create a polymorphic type for `flatten` that reflects its usual behavior. 

This monomorphic version works:

#lang typed/racket/base
(require/typed rackunit [check-equal? (Any Any . -> . Any)])
(require/typed racket/list [flatten (All (A) (Rec as (U Integer (Listof as))) -> (Listof Integer))])

(define flatten-integers (inst flatten Integer))
(check-equal? (flatten-integers 1) '(1))
(check-equal? (flatten-integers '(1)) '(1))
(check-equal? (flatten-integers '(1 (2 3) 4)) '(1 2 3 4))
(check-equal? (flatten-integers '(1 (2 3 (4 5)) 6)) '(1 2 3 4 5 6))


But when I try to substitute the polymorphic type `A` for `Integer`, it stops working:

#lang typed/racket/base
(require/typed rackunit [check-equal? (Any Any . -> . Any)])
(require/typed racket/list [flatten (All (A) (Rec as (U A (Listof as))) -> (Listof A))])

(define flatten-integers (inst flatten Integer))
(check-equal? (flatten-integers 1) '(1))
(check-equal? (flatten-integers '(1)) '(1))
(check-equal? (flatten-integers '(1 (2 3) 4)) '(1 2 3 4))
(check-equal? (flatten-integers '(1 (2 3 (4 5)) 6)) '(1 2 3 4 5 6))

> git/racket/racket/collects/racket/contract/private/blame.rkt:143:0: flatten: contract violation; none of the branches of the or/c matched



Is it possible? If so, what am I missing?

Posted on the users mailing list.