haskell - Why does `let foo = (fmap . const)` fail with "No instance for (Functor f0) arising from a use of `fmap'"? -
in ghci can this:
ghci> (fmap . const) 5 [1,2,3,4,5] [5,5,5,5,5]
but if try extract sub-expression (fmap . const)
variable error:
ghci> let foo = (fmap . const) <interactive>:3:12: no instance (functor f0) arising use of `fmap' type variable `f0' ambiguous possible fix: add type signature fixes these type variable(s) note: there several potential instances: instance functor ((,) a) -- defined in `ghc.base' instance functor ((->) r) -- defined in `ghc.base' instance functor io -- defined in `ghc.base' ...plus 2 others in first argument of `(.)', namely `fmap' in expression: (fmap . const) in equation `foo': foo = (fmap . const)
i thought might mean ghc's type inference failing somehow, when ask ghci type of subexpresiion in isolation has no problem:
ghci> :t (fmap . const) (fmap . const) :: functor f => b -> f -> f b
so what's going on here? there way extract subexpression variable? why doesn't straightforward let
work?
update:
"what monomorphism restriction" might thing link in answer (ie: "see also..."), isn't duplicate of question unless stackoverflow has become sort of weird version of game show jeopardy. knew of monomorphism restriction when asked question, not @ obvious me mr cause of error receiving.
the answer question doesn't in regard. says "what means that, in circumstances, if type ambiguous ... compiler choose instantiate type not ambiguous", opposite of happening here (mr creating ambiguity, not removing it.)
it's dreaded monomorphism restriction. if run :set -xnomonomorphismrestriction
work. can define explicit type signature this
foo :: functor f => b -> f -> f b foo = fmap . const
Comments
Post a Comment