[Haskell-cafe] a code that cannot compile with or without NoMonomorphismRestriction
Heinrich Apfelmus
apfelmus at quantentunnel.de
Thu Mar 29 14:25:04 CEST 2012
Ketil Malde wrote:
> Ting Lei <tinlyx at hotmail.com> writes:
>
>> (f1, f2) =
>> let commond_definitions = undefined in
>> let f1 = id.show
>> f2 x = (< x)
>> in
>> (f1, f2)
>
> I think the type signatures should be:
>
> f1 :: Show a => a -> String
>
> and
>
> f2 :: Ord b => b -> b -> Bool
>
> When I define these separately, this works:
>
> f1 :: Show a => a -> String
> f1 = id . show
>
> f2 :: Ord b => b -> b -> Bool
> f2 = flip (<)
>
>
> But when I define them as a pair
>
> f1 :: Show a => a -> String
> f2 :: Ord b => b -> b -> Bool
> (f1,f2) = (id . show, flip (<))
>
> I get an error message:
>
> Line 9: 1 error(s), 0 warning(s)
>
> Couldn't match expected type `forall a. Show a => a -> String'
> with actual type `a -> String'
> When checking that `f1'
> has the specified type `forall a1. Show a1 => a1 -> String'
>
> Defining the pair at once works:
>
> p :: (Show a, Ord b) => (a -> String, b -> b -> Bool)
> p = (id . show, flip (<))
>
> I guess that didn't help a lot, somebody with deeper GHC-fu than me will
> have to step in.
The problem is that f1 and f2 are polymorphic functions. To put
polymorphic functions in a pair, you need *impredicative polymorphism*.
Best regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
More information about the Haskell-Cafe
mailing list