[racket] Typed racket and sort

From: Vincent St-Amour (stamourv at ccs.neu.edu)
Date: Wed Aug 3 15:17:25 EDT 2011

At Wed, 3 Aug 2011 20:00:57 +0100,
Norman Gray wrote:
> #lang typed/racket
> (define ss '("one" "two" "three")) ; (Listof String)
> (sort ss string<?)
> I get an error message:
> Type Checker: Cannot apply expression of type (All (a b)
> (case-lambda ((Listof a) (a a -> Boolean) [#:cache-keys? Boolean] ->
> (Listof a)) ((Listof a) (b b -> Boolean) [#:cache-keys? Boolean]
> #:key (a -> b) -> (Listof a)))), since it is not a function type in:
> (sort ss string<?)
> I'm a little perplexed -- could anyone unpack this for me?

I agree that the error message is really bad. I'll look into
it. There's not really anything to explain, since the error message is
just flat out wrong.

> I can see that this is the type declaration for SORT, but 
>   (a) The type checker can't do keywords, so I'm slightly surprised
>   to see what appear to be keywords in what appears to be a type
>   declaration, and

It's not currently possible to define, in Typed Racket code, functions
that take keyword arguments, but it's possible to use Racket function
that take them. The problem is that TR cannot currently handle the
expansion of functions that take keyword arguments.

>   (b) There doesn't seem to be a case for the keywordless case (and
>   the error doesn't magically go away if I supply #:cache-keys?).

The keyword arguments is optional, so that's not the problem.

> I suspect I'm not grokking something important about polymorphic
> functions, but I don't know what.

Inference does not currently work well with function that take keyword
arguments. The solution in cases like this, is to instantiate the type
variables manually:
  ((inst sort String String) ss string<?)
Here, you're making explicit that you're using sort to sort a list of
Strings, and that Strings will be used as keys.

I hope we can find a solution to get rid of this limitation. In the
meantime, I'll see what I can do about the error message. I agree that
the solution is far from obvious given the error message.


Posted on the users mailing list.