[Haskell-cafe] Proposal for associated type synonyms in Template Haskell

Simon Peyton-Jones simonpj at microsoft.com
Thu Nov 27 06:36:27 EST 2008

I've been away.  I hope others will reply to this thread too; whatever you decide will end up in TH indefinitely.  I know that Roman is interested in this.

·         You focus just on type families in class declarations (which is indeed where associated types started).  But I suggest you also allow them at top level, as GHC does using the syntax

type family T a :: *

Indeed, since you propose to add to Dec, that'll happen automatically.  But perhaps "AssocTySynKindD" is not a good name. Perhaps "TySynFamilyD"?

·         GHC uses

type instance T [a] = Tree a
as the way to add an equation to the definition of T.  So perhaps "TySynInstance" rather than "AssocTySynD"?

·         I agree that it'd be good to do data type/newtype families at the same time.  Roman needs this.

·         Your proposal for kinds looks fine.


From: haskell-cafe-bounces at haskell.org [mailto:haskell-cafe-bounces at haskell.org] On Behalf Of José Pedro Magalhães
Sent: 11 November 2008 11:11
To: Haskell Cafe
Subject: Re: [Haskell-cafe] Proposal for associated type synonyms in Template Haskell

Hello Thomas,

I see this is a proposal for a partial implementation of #1673 (http://hackage.haskell.org/trac/ghc/ticket/1673). Maybe it would be good if the remaining syntax (associated datatypes and type families) would also be defined and implemented in TH. Or maybe there isn't much demand for this?...

On Wed, Nov 5, 2008 at 15:57, Thomas van Noort <thomas at cs.ru.nl<mailto:thomas at cs.ru.nl>> wrote:

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.

Haskell-Cafe mailing list
Haskell-Cafe at haskell.org<mailto:Haskell-Cafe at haskell.org>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081127/efafce24/attachment.htm

More information about the Haskell-Cafe mailing list