简体   繁体   中英

Most general types in Haskell and defining them

Hello I have a problem with some of my uni Haskell exercises concerning types. I can define types for functions like (*), map etc. but the problem appears when I must define the type of:

f 7 (g 'a')

How I start with this? I know we should use some most general unifier but I didn't find anything about it on the Internet?

Further more there is another task to find most general type taking into account possible classes of types.

  • foldl (++)
  • square (fx)
  • g for function:
    • g [ ] = Nothing
    • g [x] = Just x
    • g (x:y:l) = if x < y then Just x else Just y

What is most general type here? What does it mean in Haskell and what should be done here? I would be grateful for any explanations. This is not any homework just preparation for exam, any help would be more than welcome.

Start with explicit parentheses:

f 7 (g 'a') == (f 7) (g 'a')

Knowing that 7 :: Num a => a and 'a' :: Char , it's immediately clear that

f :: Num a => a -> b
g :: Char -> c

for some unconstrained types b and c . However, we also know that b is a function type, because the return value of f is applied to the return value of g . Further, we know the return value of g has type c . So we can refine our guess for what b must be:

f :: Num a => a -> (c -> d)  -- parentheses are redundant
g :: Char -> c

There's not much else you can do at this point, other than make the following observations:

f 7 :: c -> d
g 'a' :: c
f 7 (g 'a') :: d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM