PolyTypeOf is mysterious

PolyTypeable is an analog of Typeable for polymorphic types. But this works quite 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 source code of the library is quite difficult to understand, could you explain why polyTypeOf accepts a certain set of arguments and cannot accept others, even very similar ones?

+6
source share
1 answer

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, the extended default rules of ghci allow ambiguity to be restricted for the Show restriction, but not for the Enum restriction. If you try to compile the source file using foo = polyTypeOf show , you will also get an ambiguous variable type error (unless you use {-# LANGUAGE ExtendedDefaultRules #-} ).

+7
source

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


All Articles