foldr:: (a -> b -> b) -> b -> [a] -> b
map :: (a -> b) -> [a] -> [b]
mys :: a -> a
(.) :: (a -> b) -> (c -> a) -> c -> b
what is inferred type of:
a.map mys ::
b.mys map ::
c.foldr map ::
d.foldr map.mys ::
I’ve tried to create mys myself using mys n = n + 2 but the type of that is
mys :: Num a => a -> a
What’s the difference between ‘Num a => a -> a’ and just ‘a -> a’? Or what does ‘Num a =>’ mean? Is it that mys would only take Num type?
So anyways,
a) I got [a] -> [a] I think because it would just take a list and return it +2’d according to my definition of mys
b) (a -> b) -> [a] -> [b] I think because map still needs take both arguments like (*3) and a list then returns [a] which goes to mys and returns [b]
c) I’m not sure how to do this 1.
d) I’m not sure how to do this 1 either but map.mys means do mys first then map right?
Are my answers and thoughts correct? If not, why not?
THANKS!
(Update: Apparently my original answer wasn’t very helpful for the OP… See below the second HR for an appendix.)
In situations like this, what you really want to do is to launch
ghciand use:tto find out the type of various expressions. E.g.If you need to define a name first, use
let(which would not be needed in a Haskell source file):Note the use of
undefinedwith an explicit type signature. It’s perfectly ok for finding out the type of expressions of various forms and might even be useful in actual code as a placeholder at early planning stages.The
Num a =>is a type class constraint ona; see e.g. Type Classes and Overloading from “A Gentle Introduction to Haskell, Version 98” or Chapter 6. Using Typeclasses from “Real World Haskell” for more information. (Basically, it does what you think it does. :-))The above should be useful for verifying your answers (and the resources on type classes are good). As for how to solve problems of this kind yourself:
Type inference is an application of what is called “the unification algorithm”. Google for “unification” for a number of resources; if you add a name of a programming language to your query, you’ll likely find example implementations.
As for how to approach the examples at hand…
a.
map mys:maptakes a function of typea -> band returns a function of type[a] -> [b]. In general,acan be different tob, butmysis of typea -> a, so the returned function will be of type[a] -> [a].Here’s some hand-unification:
b.
mys map:mysis a function which takes an object of some type and returns an object of the same type. In particular, if you pass it an argument of type(a -> b) -> [a] -> [b], that will be the type of the return value.Incidentally, there is only one “interesting” (not
undefined) function whose type signature isa -> a(without type class constraints), namelyid. See Philip Wadler’s paper “Theorems for free!” (which you can download from this page) for an extended discussion.c.
foldr map: Firstly, note that theas andbs infoldr‘s signature having nothing to do with those inmap‘s signature. It might be convenient to renamemap‘satocandbtodand use the signaturemap :: (c -> d) -> [c] -> [d]to make this more readily apparent below. Also note that(a -> b -> b)is just a simpler way of writing(a -> (b -> b)).More hand-unification (explanation follows below):
What happens here is that
foldrtakes a function argument of type(a -> (b -> b)). If you pass inmapas that argument, theagets unified withc -> d, thebwith[c]and then again with[d], which meanscequalsd.The return value of
foldrhas typeb -> [a] -> b; substituting the more specific types obtained in the previous paragraph we getThe
ccomes from our changed signature formap; with the originalb, this becomesd.
foldr map . mys: This is actually(foldr map) . mys, notfoldr (map . mys)— function application (“the invisible operator”) binds the strongest! Combining a. and c. from above to solve this one is left as an exercise to the reader. 😉