2011-10-30 7 views
6

PolyTypeable jest analogiem Typeable dla typów polimorficznych. Ale to działa dość nieprzewidywalnie: kod źródłowypolyTypeOf jest tajemniczy

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 

Biblioteka jest dość trudne do zrozumienia, można wyjaśnić dlaczego polyTypeOf zaakceptować pewien zbiór argumentów i nie akceptować innych, nawet bardzo podobne?

Odpowiedz

7

Powód jest taki sam jak dla

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 

mianowicie ghci przedłużony zalegających przepisy pozwalają rozwiązać niejednoznaczność dla Show przymusu, ale nie dla Enum ograniczeń. Jeśli spróbujesz skompilować plik źródłowy za pomocą foo = polyTypeOf show, otrzymasz także niejasny błąd zmiennej typu (chyba że używasz {-# LANGUAGE ExtendedDefaultRules #-}).

+0

Dobry przykład. . – luqui

+0

Dzięki, nie myślałem o tym, ponieważ 'polyTypeOf show' nie jest czymś domyślnym jak'() -> String'. – modular

+0

I czy istnieje sposób na zrobienie 'polyTypeOf fromEnum' i podobnej pracy? może z ustawieniem 'default (...)' s? – Wizek

Powiązane problemy