It has given me an infinite loop and out-of-memory exception. I tried different variations but not found the valid one and don&#39;t know what to do next.<br><br>
<div class="gmail_quote">2010/9/1 jean verdier <span dir="ltr">&lt;<a href="mailto:verdier.jean@gmail.com">verdier.jean@gmail.com</a>&gt;</span><br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">I think that you need to reduce inner expressions before simplification.<br>
<div class="im"><br>s rdc (Add a b) =  rdc a `Add` rdc b<br><br></div>will not reduce the Add result. What you need is something like<br><br>s rdc (Add a b) =  rdc (rdc a `Add` rdc b)<br><br>that might not terminate but is your target.<br>

<div>
<div></div>
<div class="h5"><br><br>On Wed, 2010-09-01 at 15:19 +0600, Alexander.Vladislav.Popov wrote:<br>&gt; Hi.<br>&gt;<br>&gt; Please, tell me where I&#39;m wrong and how to improve my approach.<br>&gt;<br>&gt; I&#39;m trying to simplify algebraic expressions this way:<br>
&gt;<br>&gt; import Data.Ratio<br>&gt;<br>&gt; data Func = Const (Ratio Int)<br>&gt;          | Pow (Ratio Int)<br>&gt;          | Add Func Func<br>&gt;          | Mul Func Func<br>&gt;<br>&gt; instance Show Func where<br>
&gt;    show (Const n) = &quot;(&quot; ++ show n ++ &quot;)&quot;<br>&gt;    show (Pow n) | n == 0 = &quot;1&quot;<br>&gt;                 | n == 1 = &quot;x&quot;<br>&gt;                 | otherwise = &quot;(x**(&quot; ++ show n ++ &quot;))&quot;<br>
&gt;    show (Add t1 t2) =&quot;(&quot; ++ (show t1) ++ &quot;+&quot; ++ (show t2) ++ &quot;)&quot;<br>&gt;    show (Mul t1 t2) =&quot;(&quot; ++ (show t1) ++ &quot;*&quot; ++ (show t2) ++ &quot;)&quot;<br>&gt;<br>&gt;<br>
&gt; deriv (Const _) = Const 0<br>&gt; deriv (Pow 1) = Const 1<br>&gt; deriv (Pow n) = Const n `Mul` Pow (n-1)<br>&gt; deriv (Add a b) = deriv a `Add` deriv b<br>&gt; deriv (Mul a b) = Add (deriv a `Mul` b) (a `Mul` deriv b)<br>
&gt;<br>&gt; p0 = Const 1<br>&gt; p1 = p0 `Add` (Mul (Pow 1) (Const 2))<br>&gt; p2 = p1 `Add` (Mul (Pow 2) (Const 3))<br>&gt;<br>&gt;<br>&gt; s rdc (Const x) = Const x<br>&gt; s rdc (Pow 0) = Const 1<br>&gt; s rdc (Pow x) = Pow x<br>
&gt; s rdc (Add (Const a) (Const b)) = Const (a+b)<br>&gt; s rdc (Mul (Const 0) _) = Const 0<br>&gt; s rdc (Mul _ (Const 0)) = Const 0<br>&gt; s rdc (Mul (Const a) (Const b)) = Const (a*b)<br>&gt; s rdc (Mul (Pow n) (Pow m)) = Pow (n+m)<br>
&gt;<br>&gt; s rdc (Add x (Const 0)) =  rdc x<br>&gt; s rdc (Add (Const 0) x) =  rdc x<br>&gt; s rdc (Mul (Const m) (Mul (Const n) x)) = rdc $ Mul (Const (n*m)) (rdc<br>&gt; x)<br>&gt; s rdc (Mul x (Const 1)) =  rdc x<br>
&gt; s rdc (Mul x (Const a)) =  rdc $ Mul (Const a) (rdc x)<br>&gt; s rdc (Mul (Const 1) x) =  rdc x<br>&gt; s rdc (Mul x (Add a b)) = Mul (rdc x) (rdc a) `Add` Mul (rdc x) (rdc<br>&gt; b)<br>&gt; s rdc (Mul (Add a b) x) = Mul (rdc a) (rdc x) `Add` Mul (rdc b) (rdc<br>
&gt; x)<br>&gt; s rdc (Mul a b) =  rdc a `Mul` rdc b<br>&gt; s rdc (Add a b) =  rdc a `Add` rdc b<br>&gt;<br>&gt; fix f = f (fix f)<br>&gt;<br>&gt; The result I got is :<br>&gt;<br>&gt; *Main&gt; fix s $ deriv p2<br>&gt; (((2 % 1)+(0 % 1))+(((6 % 1)*x)+(0 % 1)))<br>
&gt;<br>&gt; instead of the anticipated expression ((2 % 1)+((6 % 1)*x)).<br>&gt; And worst of all,  I must apply (fix s) repeatedly to achieve correct<br>&gt; answer:<br>&gt;<br>&gt; *Main&gt; fix s $ fix s $ deriv p2<br>
&gt; ((2 % 1)+((6 % 1)*x)).<br>&gt;<br>&gt; I&#39;ll be very much appriciated for any help and useful links.<br></div></div>&gt; _______________________________________________<br>&gt; Beginners mailing list<br>&gt; <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
&gt; <a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br><br><br></blockquote></div><br>