can you explain the type of `id`

40 views
Skip to first unread message

Nikita Korotkih

unread,
Dec 3, 2017, 12:04:52 PM12/3/17
to purescript
Hello!

I'm a bit confused about the type of id function:

> import Prelude
> :t id
forall t a. Category a => a t t

How to interpret it? Why isn't it just `id::a -> a`

P.S. 
can you please invite me to your slack channel :)

Brian Marick

unread,
Dec 3, 2017, 7:01:16 PM12/3/17
to pures...@googlegroups.com
I'm a bit confused about the type of id function:

> import Prelude
> :t id
forall t a. Category a => a t t

How to interpret it? Why isn't it just `id::a -> a`

I might be able to answer this, as I’m just now trying to explain `Category` to beginners while being at best an advanced beginner myself.

The typical type annotation for a function looks like this:

id : a -> a 

But we can think of that as just being shorthand, the same way operators are shorthand for functions. That is, we could think of that annotation like this:

id : (->) a a 

In Purescript, there’s a name that corresponds to `(->)` in the same way that `(<<<)` corresponds to `compose`. It’s `Function`. So the above could be written like this:

id : Function a a 

Just like a bunch of types (strings, lists, etc.) can be lumped under `Monoid` because they implement the ideas of “an empty element” and “combining two elements in an associative way”, `Function` is not unique. If you generalize a function to anything that “morphs”/converts one value to another (possibly of a different type), you get something called a “morphism”. (In the book I’m writing, I’m using lenses as an example of a non-function morphism. Correctly, I hope.)

So: if you type `id` like this:


forall t a. Category a => a t t

… you can make it available to morphisms other than `Function`. That makes it more useful (to experts) than:

forall t. Function t t

… or the equivalent 


forall t. t -> t


Shameless plug for the (in progress) book: https://leanpub.com/outsidefp

Send me email and I’ll gift you a free copy. 

Louis Pilfold

unread,
Dec 3, 2017, 7:44:52 PM12/3/17
to pures...@googlegroups.com
Thanks for the detailed explanation Brian, very helpful!

--
You received this message because you are subscribed to the Google Groups "purescript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to purescript+...@googlegroups.com.
Visit this group at https://groups.google.com/group/purescript.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages