<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
</head><body bgcolor="#FFFFFF" text="#000000">Dear Simon, et al,<br>
<br>
These are very good points to make for people writing such traversals
and queries. I would be more than happy to write a page on the pitfalls
etc. on the wiki, but in my experience so far, exploring the innards of
GHC is tremendously helped by trying small things out and showing (bits
of) the intermediate structures. For me, personally, this has always
been hindered by the absence of good instances of Data and/or Show (not
having to bring DynFlags and not just visualising with the pretty
printer are very helpful).<br>
<br>
So... does anyone object to me changing these "broken" instances with
the ones given by DeriveDataTypeable?<br>
<br>
Also, many of these internal data structures could be provided with
useful lenses to improve such traversals further. Anyone ever go at
that? Would be people be interested?<br>
<br>
Regards,<br>
Philip<br>
<br>
<blockquote style="border: 0px none;"
cite="mid:618BE556AADD624C9C918AA5D5911BEF10438545@DB3PRD3001MB020.064d.mgd.msft.net"
type="cite">
<div style="margin:30px 25px 10px 25px;" class="__pbConvHr"><div
style="display:table;width:100%;border-top:1px solid
#EDEEF0;padding-top:5px"> <div
style="display:table-cell;vertical-align:middle;padding-right:6px;"><img
photoaddress="simonpj@microsoft.com" photoname="Simon Peyton Jones"
src="cid:part1.04010703.00020207@utwente.nl" name="postbox-contact.jpg"
height="25px" width="25px"></div> <div
style="display:table-cell;white-space:nowrap;vertical-align:middle;width:100%">
<a moz-do-not-send="true" href="mailto:simonpj@microsoft.com"
style="color:#737F92
!important;padding-right:6px;font-weight:bold;text-decoration:none
!important;">Simon Peyton Jones</a></div> <div
style="display:table-cell;white-space:nowrap;vertical-align:middle;">
<font color="#9FA2A5"><span style="padding-left:6px">24 Jul 2014 18:22</span></font></div></div></div>
<div style="color:#888888;margin-left:24px;margin-right:24px;"
__pbrmquotes="true" class="__pbConvBody">
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
<meta content="Microsoft Word 15 (filtered medium)" name="Generator">
<style><!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-fareast-language:EN-US;}
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:11.0pt;
font-family:"Calibri","sans-serif";
mso-fareast-language:EN-US;}
p.Code, li.Code, div.Code
{mso-style-name:Code;
margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:36.0pt;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Courier New";
mso-fareast-language:EN-US;}
span.EmailStyle19
{mso-style-type:personal;
font-family:"Calibri","sans-serif";
color:windowtext;}
span.EmailStyle20
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
@list l0
{mso-list-id:682827397;
mso-list-type:hybrid;
mso-list-template-ids:302831504 134807553 134807555 134807557 134807553 134807555 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";}
@list l0:level3
{mso-level-number-format:bullet;
mso-level-text:\F0A7;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Wingdings;}
@list l0:level4
{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:level5
{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";}
@list l0:level6
{mso-level-number-format:bullet;
mso-level-text:\F0A7;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Wingdings;}
@list l0:level7
{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:level8
{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";}
@list l0:level9
{mso-level-number-format:bullet;
mso-level-text:\F0A7;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Wingdings;}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:12.0pt;color:#1F497D">GHC’s
data structures are often mutually recursive. e.g.<o:p></o:p></span></p>
<p style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"
class="MsoListParagraph"><!--[if !supportLists]--><span
style="font-size:12.0pt;font-family:Symbol;color:#1F497D"><span
style="mso-list:Ignore">·<span style="font:7.0pt "Times New
Roman"">
</span></span></span><!--[endif]--><span
style="font-size:12.0pt;color:#1F497D">The TyCon for Maybe contains the
DataCon for Just<o:p></o:p></span></p>
<p style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"
class="MsoListParagraph"><!--[if !supportLists]--><span
style="font-size:12.0pt;font-family:Symbol;color:#1F497D"><span
style="mso-list:Ignore">·<span style="font:7.0pt "Times New
Roman"">
</span></span></span><!--[endif]--><span
style="font-size:12.0pt;color:#1F497D">The DataCon For just contains
Just’s type<o:p></o:p></span></p>
<p style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"
class="MsoListParagraph"><!--[if !supportLists]--><span
style="font-size:12.0pt;font-family:Symbol;color:#1F497D"><span
style="mso-list:Ignore">·<span style="font:7.0pt "Times New
Roman"">
</span></span></span><!--[endif]--><span
style="font-size:12.0pt;color:#1F497D">Just’s type contains the TyCon
for Maybe<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;color:#1F497D">So any
attempt to recursively walk over all these structures, as you would a
tree, will fail.
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;color:#1F497D">Also
there’s a lot of sharing. For example, every occurrence of ‘map’ is a
Var, and inside that Var is map’s type, its strictness, its rewrite
RULE, etc etc. In walking over a term you may
not want to walk over all that stuff at every occurrence of map.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;color:#1F497D">Maybe
that’s it; I’m not certain since I did not write the Data instances for
any of GHC’s types<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;color:#1F497D">Simon<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;color:#1F497D"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm
4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm
0cm 0cm">
<p class="MsoNormal"><b><span style="mso-fareast-language:EN-GB"
lang="EN-US">From:</span></b><span style="mso-fareast-language:EN-GB"
lang="EN-US"> ghc-devs [<a class="moz-txt-link-freetext" href="mailto:ghc-devs-bounces@haskell.org">mailto:ghc-devs-bounces@haskell.org</a>]
<b>On Behalf Of </b><a class="moz-txt-link-abbreviated" href="mailto:p.k.f.holzenspies@utwente.nl">p.k.f.holzenspies@utwente.nl</a><br>
<b>Sent:</b> 24 July 2014 16:42<br>
<b>To:</b> <a class="moz-txt-link-abbreviated" href="mailto:ghc-devs@haskell.org">ghc-devs@haskell.org</a><br>
<b>Subject:</b> Broken Data.Data instances<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span lang="EN-US">Dear GHC-ers,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Is there a reason for explicitly
broken Data.Data instances? Case in point:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">> instance Data Var where<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">> -- don't traverse?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">> toConstr _ =
abstractConstr "Var"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">> gunfold _ _ = error
"gunfold"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">> dataTypeOf _ =
mkNoRepType "Var"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I understand (vaguely) arguments
about abstract data types, but this also excludes convenient queries
that can, e.g. extract all types from a CoreExpr. I had hoped to do
stuff like this:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">> collect :: (Typeable b,
Data a, MonadPlus m) => a -> m b<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">> collect = everything mplus $
mkQ mzero return<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">> allTypes :: CoreExpr ->
[Type]<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">> allTypes = collect<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Especially when still exploring
(parts of) the GHC API, being able to extract things in this fashion is
very helpful. SYB’s “everything” being broken by these instances, not so
much.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Would a patch “fixing” these
instances be acceptable?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Regards,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Philip<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
</div>
</div>
</blockquote>
</body></html>