[plt-scheme] TS-Guide example 3.2 type checking error

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Nov 27 12:56:59 EST 2009

Mark, you are 100% correct. I -- a co-author of the paper -- have  
complained about this very piece of documentation and yet nothing has  
happened. So here is the trick:

#lang typed-scheme

;; In Typed Scheme, we can define map as follows:

(: my-map
    (All (C A B ...)
         ((A B ... B -> C) (Listof A) (Listof B) ... B
                           ->
                           (Listof C))))
(define (my-map f as . bss)
   (if (or (null? as)
           (ormap null? bss))
       null
       (cons (apply f (car as) (map car bss))
             (apply my-map f (cdr as) (map cdr bss)))))


The truth is we can't quite define map that way in Typed Scheme  
because map has a special type inside of the definition of my-map. But  
if you rename the function to my-map -- as I have done -- things work  
out just fine.

;; ---

>> To this first time user the error message is not immediately more  
>> useful than some of those haskell can dispense.

This is harsh, but I guess we earned it. Then again, Haskell can't  
even do any of that so it's okay. (Are you trying to add this kind of  
stuff to Haskell?)

-- Matthias






On Nov 27, 2009, at 12:44 PM, Mark Millikan wrote:

> All of the examples in the ts-guide type check and run correctly for  
> me except the Typed Scheme version of example 3.2 (the last code  
> example on the page).
>
> This happens with DrScheme 4.2.2 and the svn snapshot of 4.2.3.2  
> captured below. To this first time user the error message is not  
> immediately more useful than some of those haskell can dispense.
>
> Any help with <1> what the error is trying to say? , <2> should the  
> example work?
>
> --------------------------------------------------------------------------------------
>
> Contents of definition window copied from http://docs.plt-scheme.org/ts-guide/index.html 
>  example 3.2
> (Same error with re-keyed example)
>
> #lang typed-scheme
>   (: map
>      (All (C A B ...)
>           ((A B ... B -> C) (Listof A) (Listof B) ... B
>            ->
>            (Listof C))))
>   (define (map f as . bss)
>     (if (or (null? as)
>             (ormap null? bss))
>         null
>         (cons (apply f (car as) (map car bss))
>               (apply map f (cdr as) (map cdr bss)))))
>
> ----------------------------------------------------------------------------------------------
> Contents of interaction window after <running> the above definition:
>
> Welcome to DrScheme, version 4.2.3.2-svn26nov2009 [3m].
> Language: Module; memory limit: 128 megabytes.
> . typecheck: Polymorphic function map could not be applied to  
> arguments:
> Domain: (A B ... B -> C) (Listof A) (Listof B) ... B
> Arguments: (All (a b) (case-lambda ((Pair a b) -> a) ((Listof a) ->  
> a))) (Listof (Listof Any))
>  in: (#%app map car bss)
> . typecheck: Bad arguments to function in apply:
> Domain: A B ... B
> Arguments: A (U) *
>  in: (#%app apply f (#%app car as) (#%app map car bss))
> . typecheck: Polymorphic function map could not be applied to  
> arguments:
> Domain: (A B ... B -> C) (Listof A) (Listof B) ... B
> Arguments: (All (a b) (case-lambda ((Pair a b) -> b) ((Listof a) ->  
> (Listof a)))) (Listof (Listof Any))
>  in: (#%app map cdr bss)
> . typecheck: Bad arguments to polymorphic function in apply:
> Domain: (A B ... B -> C) (Listof A) (Listof B) ... B
> Arguments: (A B ... B -> C) (Listof A) (U) *
>  in: (#%app apply map f (#%app cdr as) (#%app map cdr bss))
> . typecheck: Summary: 4 errors encountered in:
>   (#%app map car bss)
>   (#%app apply f (#%app car as) (#%app map car bss))
>   (#%app map cdr bss)
>   (#%app apply map f (#%app cdr as) (#%app map cdr bss))
> >
>
> Thanks,
> Mark
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.