Type checking/inference

Glynn Clements glynn.clements at virgin.net
Sun Dec 28 08:27:19 EST 2003


Lee Dixon wrote:

> I've run into a small problem whilst doing some manual type checking, to see 
> if I could match the results given by hugs
> 
> prelude> :t foldr filter
> foldr filter :: [a] -> [a -> Bool] -> [a]
> -- This was fine and was the same as my answer, so I tested it with
> prelude> foldr filter [1,2,3,4] [even,odd]
> -- and the answer was indeed an empty list
> 
> -- However I got stuck when attempting to derive the result of
> map (foldr filter)
> -- Hugs says that:
> prelude> :t map (foldr filter)
> map (foldr filter) :: [[a]] -> [[a -> Bool] -> [a]]
> 
> Two main questions:
> 1/ How does hugs derive this answer?

filter           :: (a -> Bool) -> [a] -> [a]
foldr            :: (a -> b -> b) -> b -> [a] -> b
map              :: (a -> b) -> [a] -> [b]

Passing filter as the argument to foldr instantiates a as (a -> Bool)
and b as [a], giving:

foldr filter      :: [a] -> [a -> Bool] -> [a]

Passing (foldr filter) as the argument to map instantiates a as [a]
and b as ([a -> Bool] -> [a]), giving:

map (foldr filter) :: [[a]] -> [[a -> Bool] -> [a]]

> 2/ What input can I give so that it yields a correct result? I've tried 
> giving it a list of lists but it fails...

The return value is a list of functions, and functions aren't
instances of Show, so it can't print the result.

Maybe you didn't mean (map (foldr filter))? The argument to map is
usually a function of one argument (i.e. a function whose result
*isn't* a function).

What are you ultimately trying to achieve?

-- 
Glynn Clements <glynn.clements at virgin.net>


More information about the Haskell-Cafe mailing list