types - Haskell - fmap fmap doesn't work -


i'm using ghci (version 6.12.3) play bit haskell. read functors , applicative functors thought if couldn't similar <*> of applicative functors implemented using functor's primitives. after thinking came fmap fmap have (nearly) ideal type of

functor f => f (a -> b) -> f (f -> f b) or more generically

(functor f1, functor f2) => f1 (a -> b) -> f1 (f2 -> f2 b)

i tried

let q = fmap fmap 

i got following error

<interactive>:1:8:     ambiguous type variable `f1' in constraint:       `functor f1' arising use of `fmap' @ <interactive>:1:8-16     probable fix: add type signature fixes these type variable(s)  <interactive>:1:13:     ambiguous type variable `f' in constraint:       `functor f' arising use of `fmap' @ <interactive>:1:13-16     probable fix: add type signature fixes these type variable(s) 

writing above type signature suggested didn't help. craziest thing when typed :t fmap fmap got equivalent type above.

what doing wrong? why fmap fmap give type error although ghci finds type it?

looks you're running monomorphism restriction.

trying example in ghci -xnomonomorphismrestriction gives expected result.

you can subvert writing let f x = fmap fmap $ x. monomorphism restriction applies top-level definitions "look like" values, i.e. f = something, introducing explicit argument causes not apply anymore. not apply if not @ top level (for example in where clause). more details, see link.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -