Proposal for associated type synonyms in Template Haskell

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

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?...
Cheers,
Pedro
On Wed, Nov 5, 2008 at 15:57, Thomas van Noort
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 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Pedro, You are right, it is a partial implementation. We chose not to propose an implementation for associated datatypes and type families because it is unknown if there is a demand for it. But I don't think coming up with the TH AST modifications for associated type synonyms and type families is that much harder. Especially associated datatypes are very similar to associated type synonyms. The difficult part is in the GHC translation of course. Regards, Thomas José Pedro Magalhães wrote:
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?...
Cheers, Pedro
On Wed, Nov 5, 2008 at 15:57, Thomas van Noort
mailto:thomas@cs.ru.nl> wrote: 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 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org mailto:Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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.
Simon
From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@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?...
Cheers,
Pedro
On Wed, Nov 5, 2008 at 15:57, Thomas van Noort

Any movement on this?
(I am actually just looking forward to generating kind ascriptions and
having access to the kinds when processing TH.Dec, TH.Type, and such.)
2008/11/27 Simon Peyton-Jones
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.
Simon
From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@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?...
Cheers, Pedro
On Wed, Nov 5, 2008 at 15:57, Thomas van Noort
wrote: 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 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

It looks like Manuel Chakravarty is making some progress on this. http://hackage.haskell.org/trac/ghc/ticket/1673#comment:11 Sean On Thu, Jan 15, 2009 at 21:57, Nicolas Frisby wrote:
Any movement on this?
(I am actually just looking forward to generating kind ascriptions and having access to the kinds when processing TH.Dec, TH.Type, and such.)
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
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
same time. Roman needs this.
· Your proposal for kinds looks fine.
Simon
From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@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
2008/11/27 Simon Peyton-Jones
: them the this?... Cheers, Pedro
On Wed, Nov 5, 2008 at 15:57, Thomas van Noort
wrote: 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
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
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
datatypes. proposal 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
participants (5)
-
José Pedro Magalhães
-
Nicolas Frisby
-
Sean Leather
-
Simon Peyton-Jones
-
Thomas van Noort