<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Alberto G. Corona</b> <span dir="ltr"><<a href="mailto:agocorona@gmail.com">agocorona@gmail.com</a>></span><br>
Date: 2009/3/13<br>Subject: Re: [Haskell-cafe] DSLs with {in,}equalities<br>To: Adam Vogt <<a href="mailto:vogt.adam@gmail.com">vogt.adam@gmail.com</a>><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 -> 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> (<) expr1 expr2 = eval expr1 < 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"><<a href="mailto:agocorona@gmail.com" target="_blank">agocorona@gmail.com</a>></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) < (Const b) = a < 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"><<a href="mailto:agocorona@gmail.com" target="_blank">agocorona@gmail.com</a>></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)">>(<) :: (Ord a) => a -> a -> 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">> instance Num Expr where<br>> fromInterger = Const<br>> (+) = Plus<br>> (*) = 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) < (Const b) = Const (a < 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"><<a href="mailto:vogt.adam@gmail.com" target="_blank">vogt.adam@gmail.com</a>></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>
>Several times now I've had to define an EDSL for working with<br>
>(vaguely) numeric expressions. For stuff like 2*X+Y, this is easy,<br>
>looking pretty much like:<br>
><br>
>> data Expr = Const Integer | Plus Expr Expr | Times Expr Expr<br>
>><br>
>> instance Num Expr where<br>
>> fromInterger = Const<br>
>> (+) = Plus<br>
>> (*) = Times<br>
><br>
>&c. This lets me get a perfectly nice AST, which is what I want.<br>
>When I want to be able to express and work with inequalities and<br>
>equalities, this breaks. Suppose I want to write 2*X + Y < 3. I<br>
>either have to:<br>
><br>
>a) Hide Prelude.(<) and define a simple < that builds the AST term I want.<br>
>b) Come up with a new symbol for it that doesn't look totally awful.<br>
><br>
>Neither of these work decently well. Hiding Eq and Ord operators,<br>
>which is what I effectively have to do for a), is pretty much a<br>
>nonstarter--we'll have to use them too much for that to be practical.<br>
><br>
>On the other hand, b) works...but is about as ugly as it gets. We<br>
>have lots and lots of symbols that are already taken for important<br>
>purposes that are syntactically "near" <,<=,==, and the like: << and<br>
>>> and >>= for monads, >>> for arrows, etc. There...are not good<br>
>choices that I know of for the symbols that don't defeat the purpose<br>
>of making a nice clean EDSL for expressions; I might as well use 3*X +<br>
>Y `lessthan` 3, which is just not cool.<br>
><br>
>Does anyone know of a good solution, here? Are there good<br>
>substitutions for all the six operators that are important<br>
>(<,>,>=,<=,==,/=), that are close enough to be pretty-looking but not<br>
>used for other important modules?<br>
><br>
>Better yet, though a little harder, is there a nice type trick I'm not<br>
>thinking of? This works for Num methods but not for Ord methods<br>
>because:<br>
><br>
>(+) :: (Num a) => a -> a -> a<br>
>(<) :: (Ord a) => a -> a -> Bool<br>
><br>
>i.e. the return type of comparisons is totally fixed. I don't suppose<br>
>there's a good way to...well, I don't know what the *right* answer is,<br>
>but maybe define a new typeclass with a more flexible type for < that<br>
>lets both standard types return Bool and my expressions return Expr?<br>
>Any good solution would be appreciated.<br>
><br>
>Thanks,<br>
>AHH<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>
_______________________________________________<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>