Hi,<br><br>I'm trying to translate Example 2.3.3 (simple symbolic differentation) from Structure and Interpretation of Computer Programs into Haskell. <br><br>Here is code that works (as far as see):<br>---<br>data Term b = Var String | Const b | Sum (Term b) (Term b) | Prod (Term b) (Term b)
<br><br>newSum (Const a) (Const b) = Const (a+b)<br>newSum (Const 0) t@_ = t<br>newSum t@_ (Const 0) = t<br>newSum a b = Sum a b<br><br>newProd (Const a) (Const b) = Const (a*b)<br>newProd (Const 1) t@_ = t<br>newProd t@_ (Const 1) = t
<br>newProd (Const 0) t@_ = Const 0<br>newProd t@_ (Const 0) = Const 0<br>newProd a b = Prod a b<br><br>deriv (Var x) (Const c) = Const 0<br>deriv (Var x) (Var y)<br> | x == y = Const 1<br> | otherwise = Const 0<br>
deriv x@(Var _) (Sum u v) = newSum (deriv x u) (deriv x v)<br>deriv x@(Var _) (Prod u v) = newSum (newProd u (deriv x v)) (newProd (deriv x u) v)<br><br>--instance Show (Term b) where show = showTerm<br>showTerm (Var x) = x
<br>showTerm (Const c) = show c<br>showTerm (Sum a b) = "(" ++ showTerm a ++ "+" ++ showTerm b ++ ")"<br>showTerm (Prod a b) = "(" ++ showTerm a ++ showTerm b ++ ")"<br>---
<br><br>Where should I put type constraint (Show b) to be able to define Term b as an instance of Show class? <br><br>Actually, I would like to say that Term b is an instance of Show iff b is and not to put constraint on b.
<br><br><br>