<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Alberto G. Corona</b> <span dir="ltr">&lt;<a href="mailto:agocorona@gmail.com">agocorona@gmail.com</a>&gt;</span><br>
Date: 2009/3/13<br>Subject: Re: [Haskell-cafe] DSLs with {in,}equalities<br>To: Adam Vogt &lt;<a href="mailto:vogt.adam@gmail.com">vogt.adam@gmail.com</a>&gt;<br><br><br>You need an expression evaluator:<br><div><br></div>
<div>with </div><div><span style="border-collapse:collapse">(+) (Const a) (Const b)= Const (a+b)</span><br></div><div class="im"><div><span style="border-collapse:collapse">(*) (Const a) (Const b)= Const (a*b)<br>
</span></div><div><br></div></div><div>eval :: Exp -&gt; Integer</div><div><br></div><div>eval (Const i)= i</div><div>eval ( Plus e1 e2)= eval e1 + eval e2 ..</div><div>eval( Mul ....</div><div><br></div><div>and</div><div>
<br>
</div><div>instance Ord Expr where</div><div> (&lt;) expr1 expr2 = eval expr1 &lt; eval expr2</div><div><br></div><div><br></div><div>by the way:</div><div><br></div><div>simplify expr= Const (eval expr)</div><div><br></div>

<div><br></div><div><br></div><div>......................................</div><div><div></div><div class="h5"><div><br><div class="gmail_quote">2009/3/13 Alberto G. Corona <span dir="ltr">&lt;<a href="mailto:agocorona@gmail.com" target="_blank">agocorona@gmail.com</a>&gt;</span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Sorry<div><div>(Const a) &lt; (Const b) = a &lt; b</div><div><br></div><div>also</div><div><br></div><div>(*) (Const a) (Const b)= Const (a*b)<br>

</div><div><br></div><br><div class="gmail_quote">2009/3/13 Alberto G. Corona <span dir="ltr">&lt;<a href="mailto:agocorona@gmail.com" target="_blank">agocorona@gmail.com</a>&gt;</span><div><div></div><div><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><span style="border-collapse:collapse;color:rgb(80, 0, 80)">&gt;(&lt;) :: (Ord a) =&gt; a -&gt; a -&gt; Bool<br>
</span><br></div><div>what´s the problem?</div><div><br></div><div>make your Expr an instance of Ord. </div>
<div><br></div><div>By the way</div><div><div><br></div><div><span style="border-collapse:collapse">&gt; instance Num Expr where<br>&gt; fromInterger = Const<br>&gt; (+) = Plus<br>&gt; (*) = Times</span><br>
</div><div><br></div></div><div>does not work. you have not defined (+) and (*) for Const Integer. </div><div><br></div><div>(+) (Const a) (Const b)= Const (a+b)</div><div><br></div><div>With this you have an evaluator.</div>


<div>
<br></div><div>In the same way:</div><div><br></div><div>(Const a) &lt; (Const b) = Const (a &lt; b)</div><div><br></div><div><br></div><div><br></div><div><br><div class="gmail_quote">2009/3/12 Adam Vogt <span dir="ltr">&lt;<a href="mailto:vogt.adam@gmail.com" target="_blank">vogt.adam@gmail.com</a>&gt;</span><div>


<div></div><div><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This seems to be in ghc for those reasons:<br>
<a href="http://www.haskell.org/haskellwiki/Quasiquotation" target="_blank">http://www.haskell.org/haskellwiki/Quasiquotation</a><br>
<div><div></div><div><br>
* On Monday, March 02 2009, Andrew Hunter wrote:<br>
<br>
&gt;Several times now I&#39;ve had to define an EDSL for working with<br>
&gt;(vaguely) numeric expressions.  For stuff like 2*X+Y, this is easy,<br>
&gt;looking pretty much like:<br>
&gt;<br>
&gt;&gt; data Expr = Const Integer | Plus Expr Expr | Times Expr Expr<br>
&gt;&gt;<br>
&gt;&gt; instance Num Expr where<br>
&gt;&gt; fromInterger = Const<br>
&gt;&gt; (+) = Plus<br>
&gt;&gt; (*) = Times<br>
&gt;<br>
&gt;&amp;c.  This lets me get a perfectly nice AST, which is what I want.<br>
&gt;When I want to be able to express and work with inequalities and<br>
&gt;equalities, this breaks.  Suppose I want to write 2*X + Y &lt; 3.  I<br>
&gt;either have to:<br>
&gt;<br>
&gt;a) Hide Prelude.(&lt;) and define a simple &lt; that builds the AST term I want.<br>
&gt;b) Come up with a new symbol for it that doesn&#39;t look totally awful.<br>
&gt;<br>
&gt;Neither of these work decently well.  Hiding Eq and Ord operators,<br>
&gt;which is what I effectively have to do for a), is pretty much a<br>
&gt;nonstarter--we&#39;ll have to use them too much for that to be practical.<br>
&gt;<br>
&gt;On the other hand, b) works...but is about as ugly as it gets.  We<br>
&gt;have lots and lots of symbols that are already taken for important<br>
&gt;purposes that are syntactically &quot;near&quot; &lt;,&lt;=,==, and the like: &lt;&lt; and<br>
&gt;&gt;&gt; and &gt;&gt;= for monads, &gt;&gt;&gt; for arrows, etc.  There...are not good<br>
&gt;choices that I know of for the symbols that don&#39;t defeat the purpose<br>
&gt;of making a nice clean EDSL for expressions; I might as well use 3*X +<br>
&gt;Y `lessthan` 3, which is just not cool.<br>
&gt;<br>
&gt;Does anyone know of a good solution, here?  Are there good<br>
&gt;substitutions for all the six operators that are important<br>
&gt;(&lt;,&gt;,&gt;=,&lt;=,==,/=), that are close enough to be pretty-looking but not<br>
&gt;used for other important modules?<br>
&gt;<br>
&gt;Better yet, though a little harder, is there a nice type trick I&#39;m not<br>
&gt;thinking of?  This works for Num methods but not for Ord methods<br>
&gt;because:<br>
&gt;<br>
&gt;(+) :: (Num a) =&gt; a -&gt; a -&gt; a<br>
&gt;(&lt;) :: (Ord a) =&gt; a -&gt; a -&gt; Bool<br>
&gt;<br>
&gt;i.e. the return type of comparisons is totally fixed.  I don&#39;t suppose<br>
&gt;there&#39;s a good way to...well, I don&#39;t know what the *right* answer is,<br>
&gt;but maybe define a new typeclass with a more flexible type for &lt; that<br>
&gt;lets both standard types return Bool and my expressions return Expr?<br>
&gt;Any good solution would be appreciated.<br>
&gt;<br>
&gt;Thanks,<br>
&gt;AHH<br>
&gt;_______________________________________________<br>
&gt;Haskell-Cafe mailing list<br>
&gt;<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
&gt;<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div></div></div><br></div>
</blockquote></div></div></div><br></div>
</blockquote></div><br></div>
</div></div></div><br>