[Haskell-cafe] Proposal for associated type synonyms in Template
Haskell
Thomas van Noort
thomas at cs.ru.nl
Wed Nov 5 09:57:06 EST 2008
Hello,
Recently, we released a library on Hackage for generic rewriting
(package "rewriting" if you are curious). The user of the library is
expected to define type class instances to enable rewriting on his or
her own datatypes. As these instances follow the datatype declarations
closely, we tried to generate the instances using Template Haskell.
Unfortunately, associated type synonyms are not yet supported by TH.
After a presentation at the WGP'08, Simon encouraged us to write a
proposal about adding associated type synonyms to TH, so that it can be
added to GHC. So, here is our proposal.
The TH AST must allow 1) kind declarations of associated type synonyms
in class declarations and 2) their definitions in instance declarations.
For example,
class Foo a where
type Bar a :: *
instance Foo Int where
type Bar Int = String
The TH library defines a datatype Dec which contains a constructor for
class declarations and instance declarations:
data Dec
= ...
| ClassD Cxt Name [Name] [FunDep] [Dec]
| InstanceD Cxt Type [Dec]
...
1) Associated type synonym kind declarations
We suggest to add a constructor to the Dec type:
...
| AssocTySynKindD Name [Name] (Maybe Kind)
...
assocTySynKindD :: Name -> [Name] -> Maybe KindQ -> DecQ
The first field is the name of the associated type synonym, the second
field is a list of type variables, and the third field is an optional
kind. Since kinds are not yet defined in TH, we have to add some kind of
kind definition (pun intended):
data Kind
= StarK
| ArrowK Kind Kind
type KindQ = Q Kind
starK :: KindQ
arrowK :: KindQ -> KindQ -> KindQ
We explicitly choose not to reuse the Type type to define kinds (i.e.,
type Kind = Type as in GHC) since we think a separation between the two
worlds is much clearer to the users of TH.
2) Associated type synonym definitions
We suggest to add another constructor to the Dec type:
...
| AssocTySynD Name [Type] Type
...
assocTySynD :: Name -> [TypeQ] -> TypeQ -> DecQ
The first field is the name of the type synonym, the second field is a
list of type arguments, and the third field is the body of the type synonym.
We would like to hear your comments to this proposal.
Regards,
Thomas
More information about the Haskell-Cafe
mailing list