2012-11-11 13 views

risposta

10

Sì, sono supportate le classi di tipi a più parametri.

Un po 'confusamente, l'argomento Type si riferisce alla intera istanza testa, e, anche se non è davvero un tipo, sembra abbastanza come uno sintatticamente che il tipo Type è stato riutilizzato per questo scopo.

Pertanto, se si sta generando un multi-parametro instance Foo Int Bool, è necessario utilizzare il "tipo" Foo Int Bool, costruito per esempio come questo:

(ConT (mkName "Foo") `AppT` ConT (mkName "Int")) `AppT` ConT (mkName "Bool") 

Ecco un esempio completo:

{-# LANGUAGE MultiParamTypeClasses, TemplateHaskell #-} 

import Language.Haskell.TH 

class Foo a b where 
    foo :: (a, b) 

$(return [InstanceD [] (((ConT (mkName "Foo")) `AppT` ConT (mkName "Int")) `AppT` ConT (mkName "Bool")) 
    [ValD (VarP (mkName "foo")) 
     (NormalB (TupE [LitE (IntegerL 42), ConE (mkName "False")])) []]]) 

main = print (foo :: (Int, Bool)) 
6

Un modo semplice per rispondere a questo e simile è utilizzare runQ con definizioni spliced. Ad esempio, nel ghci:

$ ghci 
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help 
Prelude> :set -XTemplateHaskell 
Prelude> :set -XMultiParamTypeClasses 
Prelude> import Language.Haskell.TH 
Prelude Language.Haskell.TH> class Class a b where 
Prelude Language.Haskell.TH> runQ [d| instance Class Int Bool where |] 
[InstanceD [] (AppT (AppT (ConT :Interactive.Class) (ConT GHC.Types.Int)) (ConT GHC.Types.Bool)) []] 

Questo dimostra la forma esatta necessario, sostituire Class con qualsiasi classe che si sta utilizzando.

Problemi correlati