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

Simon Peyton-Jones <[email protected]> Thu, 27 Nov 2008 11:36:27 +0000
Newsgroups gmane.comp.lang.haskell.template,gmane.comp.lang.haskell.cafe
Message-ID <638ABD0A29C8884A91BC5FB5C349B1C33281447EE7@EA-EXMSG-C334.europe.corp.microsoft.com>
--===============1048817020==
Content-Language: en-US
Content-Type: multipart/alternative;
	boundary="_000_638ABD0A29C8884A91BC5FB5C349B1C33281447EE7EAEXMSGC334eu_"

--_000_638ABD0A29C8884A91BC5FB5C349B1C33281447EE7EAEXMSGC334eu_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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.


=B7         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"?



=B7         GHC uses

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



=B7         I agree that it'd be good to do data type/newtype families at t=
he same time.  Roman needs this.


=B7         Your proposal for kinds looks fine.


Simon

From: [email protected] [mailto:haskell-cafe-bounces@haskell=
.org] On Behalf Of Jos=E9 Pedro Magalh=E3es
Sent: 11 November 2008 11:11
To: Haskell Cafe
Subject: Re: [Haskell-cafe] Proposal for associated type synonyms in Templa=
te Haskell

Hello Thomas,

I see this is a proposal for a partial implementation of #1673 (http://hack=
age.haskell.org/trac/ghc/ticket/1673). Maybe it would be good if the remain=
ing syntax (associated datatypes and type families) would also be defined a=
nd 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 <[email protected]<mailto:thom=
[email protected]>> 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 defi=
ne type class instances to enable rewriting on his or her own datatypes. As=
 these instances follow the datatype declarations closely, we tried to gene=
rate the instances using Template Haskell. Unfortunately, associated type s=
ynonyms 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 GH=
C. 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. Fo=
r example,

class Foo a where
 type Bar a :: *

instance Foo Int where
 type Bar Int =3D String

The TH library defines a datatype Dec which contains a constructor for clas=
s declarations and instance declarations:

data Dec
=3D ...
| 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 fiel=
d is a list of type variables, and the third field is an optional kind. Sin=
ce kinds are not yet defined in TH, we have to add some kind of kind defini=
tion (pun intended):

data Kind
=3D StarK
| ArrowK Kind Kind

type KindQ =3D Q Kind
starK :: KindQ
arrowK :: KindQ -> KindQ -> KindQ

We explicitly choose not to reuse the Type type to define kinds (i.e., type=
 Kind =3D Type as in GHC) since we think a separation between the two world=
s 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
[email protected]<mailto:[email protected]>
http://www.haskell.org/mailman/listinfo/haskell-cafe


--_000_638ABD0A29C8884A91BC5FB5C349B1C33281447EE7EAEXMSGC334eu_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=3DContent-Type content=3D"text/html; charset=3Diso-8859-1"=
>
<meta name=3DGenerator content=3D"Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:Wingdings;
	panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
	{font-family:Wingdings;
	panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:Tahoma;
	panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
	{mso-style-priority:34;
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:36.0pt;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman","serif";}
span.EmailStyle17
	{mso-style-type:personal-reply;
	font-family:"Calibri","sans-serif";
	color:#1F497D;}
.MsoChpDefault
	{mso-style-type:export-only;}
@page Section1
	{size:612.0pt 792.0pt;
	margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.Section1
	{page:Section1;}
 /* List Definitions */
 @list l0
	{mso-list-id:1144201459;
	mso-list-type:hybrid;
	mso-list-template-ids:1217565398 134807553 134807555 134807557 134807553 1=
34807555 134807557 134807553 134807555 134807557;}
@list l0:level1
	{mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-18.0pt;
	font-family:Symbol;}
@list l0:level2
	{mso-level-number-format:bullet;
	mso-level-text:o;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-18.0pt;
	font-family:"Courier New";}
ol
	{margin-bottom:0cm;}
ul
	{margin-bottom:0cm;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext=3D"edit">
  <o:idmap v:ext=3D"edit" data=3D"1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=3DEN-GB link=3Dblue vlink=3Dpurple>

<div class=3DSection1>

<p class=3DMsoNormal><span style=3D'font-size:11.0pt;font-family:"Calibri",=
"sans-serif";
color:#1F497D'>I&#8217;ve been away.=A0 I hope others will reply to this th=
read
too; whatever you decide will end up in TH indefinitely.=A0 I know that Rom=
an is
interested in this.<o:p></o:p></span></p>

<p class=3DMsoNormal><span style=3D'font-size:11.0pt;font-family:"Calibri",=
"sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=3DMsoListParagraph style=3D'text-indent:-18.0pt;mso-list:l0 level1=
 lfo1'><![if !supportLists]><span
style=3D'font-size:11.0pt;font-family:Symbol;color:#1F497D'><span
style=3D'mso-list:Ignore'>=B7<span style=3D'font:7.0pt "Times New Roman"'>&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><![endif]><span style=3D'font-size:11.0pt;font-family:=
"Calibri","sans-serif";
color:#1F497D'>You focus just on type families in class declarations (which=
 is
indeed where associated types started).=A0 But I suggest you also allow the=
m <b>at
top level</b>, as GHC does using the syntax<o:p></o:p></span></p>

<p class=3DMsoListParagraph style=3D'margin-left:72.0pt'><span style=3D'fon=
t-size:
11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>type family T a ::=
 *<o:p></o:p></span></p>

<p class=3DMsoListParagraph><span style=3D'font-size:11.0pt;font-family:"Ca=
libri","sans-serif";
color:#1F497D'>Indeed, since you propose to add to Dec, that&#8217;ll happe=
n
automatically.=A0 But perhaps &#8220;</span>AssocTySynKindD&#8221; is not a=
 good
name. Perhaps &#8220;TySynFamilyD&#8221;?<o:p></o:p></p>

<p class=3DMsoListParagraph><o:p>&nbsp;</o:p></p>

<p class=3DMsoListParagraph style=3D'text-indent:-18.0pt;mso-list:l0 level1=
 lfo1'><![if !supportLists]><span
style=3D'font-family:Symbol'><span style=3D'mso-list:Ignore'>=B7<span
style=3D'font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;
</span></span></span><![endif]>GHC uses<o:p></o:p></p>

<p class=3DMsoListParagraph style=3D'margin-left:72.0pt'>type instance T [a=
] =3D Tree
a<o:p></o:p></p>

<p class=3DMsoNormal style=3D'margin-left:36.0pt'>as the way to add an equa=
tion to
the definition of T.=A0 So perhaps &#8220;TySynInstance&#8221; rather than =
&#8220;AssocTySynD&#8221;?<o:p></o:p></p>

<p class=3DMsoListParagraph><o:p>&nbsp;</o:p></p>

<p class=3DMsoListParagraph style=3D'text-indent:-18.0pt;mso-list:l0 level1=
 lfo1'><![if !supportLists]><span
style=3D'font-family:Symbol'><span style=3D'mso-list:Ignore'>=B7<span
style=3D'font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;
</span></span></span><![endif]>I agree that it&#8217;d be good to do data t=
ype/newtype
families at the same time.=A0 Roman needs this.<o:p></o:p></p>

<p class=3DMsoNormal><o:p>&nbsp;</o:p></p>

<p class=3DMsoListParagraph style=3D'text-indent:-18.0pt;mso-list:l0 level1=
 lfo1'><![if !supportLists]><span
style=3D'font-size:11.0pt;font-family:Symbol;color:#1F497D'><span
style=3D'mso-list:Ignore'>=B7<span style=3D'font:7.0pt "Times New Roman"'>&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><![endif]><span style=3D'font-size:11.0pt;font-family:=
"Calibri","sans-serif";
color:#1F497D'>Your proposal for kinds looks fine.<o:p></o:p></span></p>

<p class=3DMsoListParagraph><span style=3D'font-size:11.0pt;font-family:"Ca=
libri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=3DMsoNormal><span style=3D'font-size:11.0pt;font-family:"Calibri",=
"sans-serif";
color:#1F497D'>Simon<o:p></o:p></span></p>

<p class=3DMsoNormal><span style=3D'font-size:11.0pt;font-family:"Calibri",=
"sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<div style=3D'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm =
4.0pt'>

<div>

<div style=3D'border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm =
0cm 0cm'>

<p class=3DMsoNormal><b><span lang=3DEN-US style=3D'font-size:10.0pt;font-f=
amily:
"Tahoma","sans-serif"'>From:</span></b><span lang=3DEN-US style=3D'font-siz=
e:10.0pt;
font-family:"Tahoma","sans-serif"'> [email protected]
[mailto:[email protected]] <b>On Behalf Of </b>Jos=E9 Pedro
Magalh=E3es<br>
<b>Sent:</b> 11 November 2008 11:11<br>
<b>To:</b> Haskell Cafe<br>
<b>Subject:</b> Re: [Haskell-cafe] Proposal for associated type synonyms in
Template Haskell<o:p></o:p></span></p>

</div>

</div>

<p class=3DMsoNormal><o:p>&nbsp;</o:p></p>

<p class=3DMsoNormal style=3D'margin-bottom:12.0pt'>Hello Thomas,<br>
<br>
I see this is a proposal for a partial implementation of #1673 (<a
href=3D"http://hackage.haskell.org/trac/ghc/ticket/1673" target=3D"_blank">=
http://hackage.haskell.org/trac/ghc/ticket/1673</a>).
Maybe it would be good if the remaining syntax (associated datatypes and ty=
pe
families) would also be defined and implemented in TH. Or maybe there isn't
much demand for this?...<br>
<br>
<br>
Cheers,<br>
Pedro<o:p></o:p></p>

<div>

<p class=3DMsoNormal>On Wed, Nov 5, 2008 at 15:57, Thomas van Noort &lt;<a
href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>&gt; w=
rote:<o:p></o:p></p>

<p class=3DMsoNormal>Hello,<br>
<br>
Recently, we released a library on Hackage for generic rewriting (package
&quot;rewriting&quot; if you are curious). The user of the library is expec=
ted
to define type class instances to enable rewriting on his or her own dataty=
pes.
As these instances follow the datatype declarations closely, we tried to ge=
nerate
the instances using Template Haskell. Unfortunately, associated type synony=
ms
are not yet supported by TH.<br>
<br>
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.<br>
<br>
The TH AST must allow 1) kind declarations of associated type synonyms<br>
in class declarations and 2) their definitions in instance declarations. Fo=
r
example,<br>
<br>
class Foo a where<br>
&nbsp;type Bar a :: *<br>
<br>
instance Foo Int where<br>
&nbsp;type Bar Int =3D String<br>
<br>
The TH library defines a datatype Dec which contains a constructor for clas=
s
declarations and instance declarations:<br>
<br>
data Dec<br>
=3D ...<br>
| ClassD Cxt Name [Name] [FunDep] [Dec]<br>
| InstanceD Cxt Type [Dec]<br>
&nbsp;...<br>
<br>
1) Associated type synonym kind declarations<br>
<br>
We suggest to add a constructor to the Dec type:<br>
<br>
&nbsp;...<br>
| AssocTySynKindD Name [Name] (Maybe Kind)<br>
&nbsp;...<br>
<br>
assocTySynKindD :: Name -&gt; [Name] -&gt; Maybe KindQ -&gt; DecQ<br>
<br>
The first field is the name of the associated type synonym, the second fiel=
d is
a list of type variables, and the third field is an optional kind. Since ki=
nds
are not yet defined in TH, we have to add some kind of kind definition (pun
intended):<br>
<br>
data Kind<br>
=3D StarK<br>
| ArrowK Kind Kind<br>
<br>
type KindQ =3D Q Kind<br>
starK :: KindQ<br>
arrowK :: KindQ -&gt; KindQ -&gt; KindQ<br>
<br>
We explicitly choose not to reuse the Type type to define kinds (i.e., type
Kind =3D Type as in GHC) since we think a separation between the two worlds=
 is
much clearer to the users of TH.<br>
<br>
2) Associated type synonym definitions<br>
<br>
We suggest to add another constructor to the Dec type:<br>
<br>
&nbsp;...<br>
| AssocTySynD Name [Type] Type<br>
&nbsp;...<br>
<br>
assocTySynD :: Name -&gt; [TypeQ] -&gt; TypeQ -&gt; DecQ<br>
<br>
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.<br>
<br>
We would like to hear your comments to this proposal.<br>
<br>
Regards,<br>
Thomas<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Haskell-Cafe@=
haskell.org</a><br>
<a href=3D"http://www.haskell.org/mailman/listinfo/haskell-cafe" target=3D"=
_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><o:p></o:p>=
</p>

</div>

<p class=3DMsoNormal><o:p>&nbsp;</o:p></p>

</div>

</div>

</body>

</html>

--_000_638ABD0A29C8884A91BC5FB5C349B1C33281447EE7EAEXMSGC334eu_--

--===============1048817020==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
template-haskell mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/template-haskell

--===============1048817020==--