Typeclasses 101: is GHC too impatient for instance output?

Given the following code:

class C a where
  foo :: a -> a

f :: (C a) => a -> a
f = id

p :: (C a) => (a -> a) -> a -> a
p g = foo . g

Now, if I try to call pf, the GHC complains:

> p f
No instance for (C a0) arising from a use of `p'
In the expression: p f
In an equation for `it': it = p f

I find this somewhat unexpected, since f accepts only "a", which should be an instance of the type class C. What is the reason?

Edit: I know that I did not define any instance for C, but should not answer “correct”:

p f :: (C a) => a -> a 
+4
source share
2 answers

When you put a simple expression in ghci, it basically tries printit, so

> p f

about the same as in the file

main :: IO ()
main = print $ p f

, p f :: (C a) => a -> a. print $ p f GHC p f. GHC , . C a a, . Show a -> a.

No instance for (Show (a -> a)) arising from a use of `print'
No instance for (C a) arising from a use of `p'
+8

. GHC , .

a.) , {-# LANGUAGE NoMonomorphismRestriction #-} .

b.) :

foo :: C a => a -> a
foo = p f

c.) ( ):

foo x = p f x 
+4

Source: https://habr.com/ru/post/1599334/


All Articles