[racket] Question about match in typed/racket

From: Timothy Nelson (tbnelson at gmail.com)
Date: Fri Apr 13 21:43:20 EDT 2012

One more question. Thanks for your patience. :-)

I had this before:
(list args ...)

which became:
(list #{args : (Listof Sexp)} ...)

Now suppose I want to match only a list of *symbols* instead of a list of
Sexps, finishing with a symbol bound separately. In normal Racket, this
would be:

> (match '(a b c)
    [(list (? symbol? args) ... (? symbol? lastarg))
     (list args lastarg)])
'((a b) c)

If I leave this as-is in typed/racket I naturally get the same error as
before, but if I annotate args in the same way as in my last question:
(list (? symbol? #{args : (Listof Symbol)}) ... (? symbol? lastarg))

I get a bizarre type error: "Type Checker: Expected (Listof Symbol), but
got (Listof Any) in:", and then my entire (match ...).

If I annotate the (? symbol args) instead of just args -- I wasn't sure
what the right syntax was -- I get the original expected (Listof Symbol)
got (Listof Any) error.

Am I mis-using annotations?

- Tim

On Thu, Apr 12, 2012 at 12:48 PM, Timothy Nelson <tbnelson at gmail.com> wrote:

> That fixes it for me. Thanks, both of you!
> On Thu, Apr 12, 2012 at 12:27 AM, Sam Tobin-Hochstadt <samth at ccs.neu.edu>wrote:
>> On Wed, Apr 11, 2012 at 3:36 PM, Timothy Nelson <tbnelson at gmail.com>
>> wrote:
>> >
>> > I'd like to write a function that consumes an s-expression and produces
>> a
>> > struct -- something similar to building a tree struct out of a tree
>> sexp. In
>> > the past, I've always used match for this kind of sexp manipulation.
>> > However, if I have a match clause within a function like this:
>> >
>> > (: my-func (Sexp -> mystruct))
>> > (define (my-func s)
>> >   (match s
>> >     [(list args ...) (make-mystruct (map my-func args))]))
>> The problem here is that Typed Racket can't guess what type you mean
>> for `args' to have, so you have to tell it when you bind `args'.  That
>> looks like this:
>> (: my-func (Sexp -> mystruct))
>> (define (my-func s)
>>  (match s
>>     [(list #{args : (Listof Sexp)} ...)
>>      (make-mystruct (map my-func args))]))
>> which typechecks correctly for me.
>> --
>> sam th
>> samth at ccs.neu.edu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120413/fd7af21f/attachment-0001.html>

Posted on the users mailing list.