Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

polyTypeOf is mysterious

PolyTypeable is an analog of Typeable for polymorphic types. But it works rather unpredictably:

ghci> :t show
show :: Show a => a -> String
ghci> polyTypeOf show
a1 -> [Char]
ghci> :t fromEnum 
fromEnum :: Enum a => a -> Int
ghci> polyTypeOf fromEnum 

<interactive>:1:12:
    Ambiguous type variable `a0' in the constraint:
      (Enum a0) arising from a use of `fromEnum'
    Probable fix: add a type signature that fixes these type variable(s)
    In the first argument of `polyTypeOf', namely `fromEnum'
    In the expression: polyTypeOf fromEnum
    In an equation for `it': it = polyTypeOf fromEnum

The library source code is quite hard to understand, could you explain why does polyTypeOf accept certain set of arguments and fails to accept other, even very similar?

like image 597
modular Avatar asked Oct 30 '11 21:10

modular


1 Answers

The reason is the same as for

Prelude> show undefined
"*** Exception: Prelude.undefined
Prelude> fromEnum undefined

<interactive>:0:1:
    Ambiguous type variable `a0' in the constraint:
      (Enum a0) arising from a use of `fromEnum'
    Probable fix: add a type signature that fixes these type variable(s)
    In the expression: fromEnum undefined
    In an equation for `it': it = fromEnum undefined

namely, ghci's extended defaulting rules allow it to resolve the ambiguity for a Show constraint, but not for an Enum constraint. If you try to compile a source file with foo = polyTypeOf show, you also get an ambiguous type variable error (unless you use {-# LANGUAGE ExtendedDefaultRules #-}).

like image 99
Daniel Fischer Avatar answered Nov 13 '22 14:11

Daniel Fischer