[Haskell-beginners] Question regarding infering the type of a
function
Joe Fredette
jfredett at gmail.com
Mon Sep 21 16:18:45 EDT 2009
The type of `(.)` is
(.) :: (b->c) -> (a->b) -> a -> c
it can be defined as:
(.) f g x = f (g x)
So, let f = (.), then we can plug that in and make it infix in the
defn to get:
(.) (.) g x q y = g x . q $ y
I had to add a few variables there to make it pointed, as opposed to
the more succint semipointfree version:
(.) (.) g x = (.) g x
So we can see that this function, `(.) (.)` takes four arguments, a
function on 2 inputs (g, because (.) is going to feed it another one,
and it's taking one already, in the form of x). One of the variables
of appropriate some arbitrary type `t` (since g is the only thing that
will ever see it, you can think of it as a kind of index to an indexed
family of functions of type `b->c`.) giving `(.) (.)` just `x` will
reduce the type to something you should recognize, that is if:
foo x = something in (\ g q y -> (.) (.) g x' q y)
then foo has type:
foo :: (b -> c) -> (a -> b) -> a -> c
eg, foo = (.)
So, (.) (.) effectively says, "Given a indexed family of functions
from `b -> c`, indexed by some type `t` compose the `t`th function
with some function `q` from `a -> b`, and return a function of type `a
-> c`.
You might use it for something like mapping over a list at a given
starting point, or something.
HTH.
/Joe
On Sep 21, 2009, at 3:47 PM, MoC wrote:
> Hi everybody,
>
> today somebody on IRC (#haskell on Freenode) jokingly submitted the
> following to lambdabot:
> > (.) (.)
> It turned out that (.) (.) :: (a1 -> b -> c) -> a1 -> (a -> b) -> a -
> > c. I got interested in why that is so, but unfortunately I have
> not been able to figure this out by myself yet. I know that partial
> application is used, but don't know to what it would expand to and
> why.
> Could someone please explain?
> (I asked on the channel, but nobody did answer so I thought I'd try
> my luck here.)
>
> Regards,
> MoC
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list