2011-10-30 10 views
6

PolyTypeable è un analogo di Typeable per i tipi polimorfici. Ma funziona piuttosto imprevedibile: il codice sorgentepolyTypeOf è misterioso

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 

La biblioteca è abbastanza difficile da capire, potrebbe spiegare il motivo per cui si fa polyTypeOf accetta certo insieme di argomenti e non riesce ad accettare altri, anche molto simile?

risposta

7

La ragione è la stessa che per

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 

cioè, ghci di estese moroso regole consentono di risolvere l'ambiguità di un vincolo Show, ma non per un Enum vincolo. Se si tenta di compilare un file sorgente con foo = polyTypeOf show, si ottiene anche un errore variabile di tipo ambiguo (a meno che non si utilizzi {-# LANGUAGE ExtendedDefaultRules #-}).

+0

Bel esempio. . – luqui

+0

Grazie, non ci pensare, dal momento che 'polyTypeOf show' non è qualcosa di predefinito come'() -> String'. – modular

+0

E c'è un modo per rendere 'polyTypeOf fromEnum' e lavori simili? forse con l'impostazione di alcuni 'default (...)' s? – Wizek

Problemi correlati