[racket] TR: Polymorphic types

From: Alexis King (lexi.lambda at gmail.com)
Date: Sat Jan 17 21:38:13 EST 2015

Types are a purely compile-time construct, so type polymorphism doesn’t make any sense in a dynamic context. You can create a predicate that checks if some value is any Foo:

(define-predicate foo? (Foo Any))

Or you can even create a predicate that checks if some value is a specific type of Foo:

(define-syntax-rule (Foo? A)
    (make-predicate (Foo A))

But in the latter case, the type obviously must be known at compile-time.

As Alexander mentions, the snapshot version of Typed Racket exports typed/json, but it just uses 'null for all null values, mostly to sidestep this problem, and because custom nulls are relatively pointless, anyway.

> On Jan 17, 2015, at 18:20, Jordan Johnson <jmj at fellowhuman.com> wrote:
> 
> Hi all,
> 
> In playing with TR this evening, I found
> 
>> (define-type (Foo A)
>>  (U A
>>     (List 'foo (Foo A))))
>> 
>> (define-predicate foo? Foo)
> 
> generates the error “Type Foo could not be converted to a contract: cannot generate contract for non-function polymorphic type”. I’ve read the page with caveats about inference and polymorphic types, but since the example is about using a function rather than defining a predicate, I’m not clear on how (if at all) the technique could be adapted for this.
> 
> (What I was actually doing, by the way, was seeing if I could port the JSON library to TR. I ran into the problem of the null element being of arbitrary type.)
> 
> Best,
> jmj
> 
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20150117/8044989b/attachment-0001.html>

Posted on the users mailing list.