<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 14px; font-family: Calibri, sans-serif; "><div>Looks great; I care and have no improvements to offer; +1 from me.</div><div><br></div><div>Chris</div><div><br></div><span id="OLK_SRC_BODY_SECTION"><div style="font-family:Calibri; font-size:11pt; text-align:left; color:black; BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; BORDER-TOP: #b5c4df 1pt solid; BORDER-RIGHT: medium none; PADDING-TOP: 3pt"><span style="font-weight:bold">From: </span> Simon Peyton-Jones &lt;<a href="mailto:simonpj@microsoft.com">simonpj@microsoft.com</a>&gt;<br><span style="font-weight:bold">Date: </span> Monday, 14 January 2013 18:09<br><span style="font-weight:bold">To: </span> glasgow-haskell-users &lt;<a href="mailto:glasgow-haskell-users@haskell.org">glasgow-haskell-users@haskell.org</a>&gt;<br><span style="font-weight:bold">Subject: </span> Newtype wrappers<br></div><div><br></div><div xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"><meta name="Generator" content="Microsoft Word 14 (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:Verdana;
        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: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
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
tt
        {mso-style-priority:99;
        font-family:"Courier New";}
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;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Verdana","sans-serif";
        color:windowtext;}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:"Courier New";
        mso-fareast-language:EN-GB;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:537086231;
        mso-list-template-ids:1509331144;}
@list l0:level1
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7;
        mso-level-tab-stop:36.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l0:level2
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:72.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:"Courier New";
        mso-bidi-font-family:"Times New Roman";}
@list l0:level3
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:108.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level4
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:144.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level5
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:180.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level6
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:216.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level7
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:252.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level8
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:288.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level9
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:324.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l1
        {mso-list-id:1088573738;
        mso-list-template-ids:320250126;}
@list l1:level1
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7;
        mso-level-tab-stop:36.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l1:level2
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:72.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:"Courier New";
        mso-bidi-font-family:"Times New Roman";}
@list l1:level3
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:108.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l1:level4
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:144.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l1:level5
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:180.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l1:level6
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:216.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l1:level7
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:252.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l1:level8
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:288.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l1:level9
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:324.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l2
        {mso-list-id:1593926484;
        mso-list-type:hybrid;
        mso-list-template-ids:-1024303242 134807553 134807555 134807557 134807553 134807555 134807557 134807553 134807555 134807557;}
@list l2: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 l2: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 l2: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 l2: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 l2: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 l2: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 l2: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 l2: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 l2: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;}
@list l3
        {mso-list-id:1793591400;
        mso-list-template-ids:1108776936;}
@list l3:level1
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7;
        mso-level-tab-stop:36.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l3:level2
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:72.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:"Courier New";
        mso-bidi-font-family:"Times New Roman";}
@list l3:level3
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:108.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l3:level4
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:144.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l3:level5
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:180.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l3:level6
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:216.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l3:level7
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:252.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l3:level8
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:288.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l3:level9
        {mso-level-number-format:bullet;
        mso-level-text:\F0A7;
        mso-level-tab-stop:324.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.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 lang="EN-GB" link="blue" vlink="purple"><div class="WordSection1"><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; ">Friends<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; ">I&#8217;d like to propose a way to &#8220;promote&#8221; newtypes over their enclosing type.&nbsp; Here&#8217;s the writeup<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://hackage.haskell.org/trac/ghc/wiki/NewtypeWrappers">
http://hackage.haskell.org/trac/ghc/wiki/NewtypeWrappers</a><o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; ">Any comments?&nbsp; Below is the problem statement, taken from the above page.<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; ">I&#8217;d appreciate<o:p></o:p></span></p><p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l2 level1 lfo4"><!--[if !supportLists]--><span style=""><span style="mso-list:Ignore"><span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><!--[endif]--><span style="font-family: Verdana, sans-serif; ">A sense of whether you care. Does this matter?<o:p></o:p></span></p><p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l2 level1 lfo4"><!--[if !supportLists]--><span style=""><span style="mso-list:Ignore"><span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><!--[endif]--><span style="font-family: Verdana, sans-serif; ">Improvements to the design I propose<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; ">Simon<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal"><b><span style="font-family: Verdana, sans-serif; ">The problem<o:p></o:p></span></b></p><p>Suppose we have <o:p></o:p></p><pre>newtype Age = MkAge Int<o:p></o:p></pre><p>Then if <tt><span style="font-size:10.0pt">n :: Int</span></tt>, we can convert
<tt><span style="font-size:10.0pt">n</span></tt> to an <tt><span style="font-size:10.0pt">Age</span></tt> thus:
<tt><span style="font-size:10.0pt">MkAge n :: Age</span></tt>. Moreover, this conversion is a type conversion only, and involves no runtime instructions whatsoever. This cost model -- that newtypes are free -- is important to Haskell programmers, and encourages
 them to use newtypes freely to express type distinctions without introducing runtime overhead.
<o:p></o:p></p><p>Alas, the newtype cost model breaks down when we involve other data structures. Suppose we have these declarations
<o:p></o:p></p><pre>data T a&nbsp;&nbsp; = TLeaf a&nbsp;&nbsp;&nbsp;&nbsp; | TNode (Tree a) (Tree a)<o:p></o:p></pre><pre>data S m a = SLeaf (m a) | SNode (S m a) (S m a)<o:p></o:p></pre><p>and we have these variables in scope <o:p></o:p></p><pre>x1 :: [Int]<o:p></o:p></pre><pre>x2 :: Char -&gt; Int<o:p></o:p></pre><pre>x3 :: T Int<o:p></o:p></pre><pre>x4 :: S IO Int<o:p></o:p></pre><p>Can we convert these into the corresponding forms where the <tt><span style="font-size:10.0pt">Int</span></tt> is replaced by
<tt><span style="font-size:10.0pt">Age</span></tt>? Alas, not easily, and certainly not without overhead.
<o:p></o:p></p><ul type="disc"><li class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l1 level1 lfo1">
For <tt><span style="font-size:10.0pt">x1</span></tt> we can write <tt><span style="font-size:10.0pt">map MkAge x1 :: [Age]</span></tt>. But this does not follow the newtype cost model: there will be runtime overhead from executing the
<tt><span style="font-size:10.0pt">map</span></tt> at runtime, and sharing will be lost too. Could GHC optimise the
<tt><span style="font-size:10.0pt">map</span></tt> somehow? This is hard; apart from anything else, how would GHC know that
<tt><span style="font-size:10.0pt">map</span></tt> was special? And it it gets worse.
<o:p></o:p></li></ul><ul type="disc"><li class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l0 level1 lfo2">
For <tt><span style="font-size:10.0pt">x2</span></tt> we'd have to eta-expand: <tt><span style="font-size:10.0pt">(\y -&gt; MkAge (x2 y)) :: Char -&gt; Age</span></tt>. But this isn't good either, because eta exapansion isn't semantically valid (if
<tt><span style="font-size:10.0pt">x2</span></tt> was bottom, <tt><span style="font-size:10.0pt">seq</span></tt> could distinguish the two). See
<a href="http://hackage.haskell.org/trac/ghc/ticket/7542" title="bug: GHC doesn't optimize (strict) composition with id (new)">
#7542</a> for a real life example. <o:p></o:p></li></ul><ul type="disc"><li class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l3 level1 lfo3">
For <tt><span style="font-size:10.0pt">x3</span></tt>, we'd have to map over <tt><span style="font-size:10.0pt">T</span></tt>, thus <tt><span style="font-size:10.0pt">mapT MkAge x3</span></tt>. But what if
<tt><span style="font-size:10.0pt">mapT</span></tt> didn't exist? We'd have to make it. And not all data types have maps.
<tt><span style="font-size:10.0pt">S</span></tt> is a harder one: you could only map over S-values if
<tt><span style="font-size:10.0pt">m</span></tt> was a functor. There's a lot of discussion abou this on
<a href="http://hackage.haskell.org/trac/ghc/ticket/2110" title="feature request: Rules to eliminate casted id's (new)">
#2110</a>. <o:p></o:p></li></ul><p class="MsoNormal"><span style="font-family: Verdana, sans-serif; "><o:p>&nbsp;</o:p></span></p></div></div></div>
_______________________________________________
Glasgow-haskell-users mailing list
<a href="mailto:Glasgow-haskell-users@haskell.org">Glasgow-haskell-users@haskell.org</a>
<a href="http://www.haskell.org/mailman/listinfo/glasgow-haskell-users">http://www.haskell.org/mailman/listinfo/glasgow-haskell-users</a>
</span></body></html>