Type question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Type question

Paul Johnston
Hi, I was playing around after getting the new O'Reilly book with lists
and their operators!
Not sure what the below actually means?

Prelude> [] : []
[[]]
it :: [[a]]

(Got :set +t on )

I thought the first argument of ':' must be an element, so is the empty
list an element of the same type of the contents of the empty list?

Yours confused Paul :-)



Reply | Threaded
Open this post in threaded view
|

Type question

Yitzchak Gale
Hi Paul,

You wrote:
> Prelude> [] : []
> [[]]
> it :: [[a]]
> I thought the first argument of ':' must be an element, so is the empty list
> an element of the same type of the contents of the empty list?

There is not just one "empty list". The symbol [] is actually
polymorphic - it can refer to the empty list in [a], for any type a.

In particular, "a" can itself be a list type. So [] : [] is an element
of [[a]], the type of list of lists of a, for any type a.

Hope this helps,
Yitz
Reply | Threaded
Open this post in threaded view
|

Type question

Brandon S Allbery KF8NH
In reply to this post by Paul Johnston
On 2009 Jan 5, at 15:40, Paul Johnston wrote:

> Hi, I was playing around after getting the new O'Reilly book with  
> lists and their operators!
> Not sure what the below actually means?
>
> Prelude> [] : []
> [[]]
> it :: [[a]]
>
> (Got :set +t on )
>
> I thought the first argument of ':' must be an element, so is the  
> empty list an element of the same type of the contents of the empty  
> list?


(:) :: a -> [a] -> [a]

In this case the first argument is an empty list (type forall a. [a])  
and the second must therefore be an empty list of lists (type forall  
a. [[a]]).  Hence the result is also of type forall a. [[a]] (with all  
the `a's unified).

--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [hidden email]
system administrator [openafs,heimdal,too many hats] [hidden email]
electrical and computer engineering, carnegie mellon university    KF8NH