<br><br><div class="gmail_quote">On Thu, Jun 3, 2010 at 8:14 AM, Gabriel Riba <span dir="ltr">&lt;<a href="mailto:griba2001@gmail.com">griba2001@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Extending sum types with data constructors would spare runtime errors or<br>
exception control,<br>
<br>
when applying functions to inappropriate branches, as in the example ...<br>
<br>
   data List a = Nil | Cons a (List a)  -- List!Nil and List!Cons<br>
                                        -- as extended types<br>
<br>
<br>
* Actual system, with runtime errors (as in GHC Data.List head) or<br>
exception throwing<br>
<br>
   hd :: List a -&gt; a<br>
   hd (Cons x _) -&gt; x<br>
   hd Nil -&gt; error &quot;error: hd: empty list&quot; -- error or exception throwing<br>
<br>
<br>
* Proposed system extending types with constructors as Type!Constructor:<br>
<br>
User must do pattern matching before applying the constructor-specific<br>
type function.<br>
<br>
In &#39;&#39;var @ (Constructor _ _)&#39;&#39; the compiler should append the constructor<br>
to the type as a pair (Type, Constructor) as an extended type for &#39;&#39;var&#39;&#39;<br>
<br>
No need for runtime errors or exception control<br>
<br>
   hd :: List!Cons a -&gt; a<br>
<br>
   hd (Cons x _) = x<br></blockquote><div><br></div><div>How will this proposal scale with data types that have multiple alternatives that make sense?  No natural examples come to mind so how about a contrived example:</div>
<div><br></div><div>data List2 a = Nil | Cons a (List2 a) | Cons2 a a (List2 a)</div><div><br></div><div>Now I want to define hd for both Cons and Cons2, but not Nil.  Do I use an either type like this?</div><div>hd :: Either (List2!Cons a) (List2!Cons2 a) -&gt; a</div>
<div><br></div><div>It seems like some other syntax would be desirable here, maybe:</div><div>hd :: List2!{Cons, Cons2} a -&gt; a</div><div><br></div><div>How should it work for functions where no type signature is supplied?  Should it infer the type we would now and only enable the subset of constructors when the type is explicit as above?</div>
<div><br></div><div>Jason</div></div>