[Haskell-beginners] map type explanation

coot at coot.me coot at coot.me
Sun Dec 20 19:43:24 UTC 2020


Hi,

The `(a -> b)` in `map :: (a -> b) -> [a] -> [b]` is a type of function from a type `a` to a type `b`.   Haskell, by default, is using implicit quantification, however using `ScopedTypeVariables` extension you could write `map :: forall a b. (a -> b) -> [a] -> [b]` to make it explicit.  This way you see that both `a` and `b` are introduced by `forall`.  In particular, you can substitute any types, e.g. if you substitute `a` with `Char` and `b` with `Int`, you'll get `map :: (Char -> Int) -> [Char] -> [Int]`.  If you enable `TypeApplication` extension you can do that in `ghci`:  `:t map @Char @Int`  You can read the type of `map` as follows: given a function `f` from type `a` to some type `b`, `map f :: [a] -> [b]`, i.e. `map f` is a function from list of `a` to list of `b`'s.

Being able to write functions like `map`, is called parametric polymorphism. Using such  polymorphism you guarantee that the implentation of `map` cannot do any thing with `a`'s and `b`'s, as there's no knowledge about what they are, beside the recepe how to transform `a`'s into `b` given by `f`.  This limits what `map f` can do to a list of `a`'s.

Best regards,

Marcin Szamotulski

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Friday, December 18th, 2020 at 19:30, Lawrence Bottorff <borgauf at gmail.com> wrote:

> I'm looking at this
> 

> ghci> :type map
> 

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

> and wondering what the (a -> b) part is about. map takes a function and applies it to an incoming list. Good. Understood. I'm guessing that the whole Haskell type declaration idea is based on currying, and I do understand how the (a -> b) part "takes" an incoming list, [a] and produces the [b] output. Also, I don't understand a and b very well either. Typically, a is just a generic variable, then b is another generic variable not necessarily the same as a. But how are they being used in this type declaration?
> 

> LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20201220/887c6b4a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 509 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20201220/887c6b4a/attachment.sig>


More information about the Beginners mailing list