Elementary question about Type Constraints

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

2007/7/9, lassoken
Hi,
I'm trying to translate Example 2.3.3 (simple symbolic differentation) from Structure and Interpretation of Computer Programs into Haskell.
Here is code that works (as far as see): --- data Term b = Var String | Const b | Sum (Term b) (Term b) | Prod (Term b) (Term b) ... --instance Show (Term b) where show = showTerm showTerm (Var x) = x showTerm (Const c) = show c showTerm (Sum a b) = "(" ++ showTerm a ++ "+" ++ showTerm b ++ ")" showTerm (Prod a b) = "(" ++ showTerm a ++ showTerm b ++ ")" ---
Where should I put type constraint (Show b) to be able to define Term b as an instance of Show class?
Actually, I would like to say that Term b is an instance of Show iff b is and not to put constraint on b.
instance Show b => Show (Term b) where ... I believe it is in the Gentle introduction to Haskell.

(Sorry, wrong button again when sending this the first time.) lassoken:
data Term b = Var String | Const b | Sum (Term b) (Term b) | Prod (Term b) (Term b)
newSum (Const a) (Const b) = Const (a+b) newSum (Const 0) t@_ = t newSum t@_ (Const 0) = t newSum a b = Sum a b
Is there any reason to use t@_ instead of just t? If there is, please tell me!
--instance Show (Term b) where show = showTerm showTerm (Var x) = x showTerm (Const c) = show c showTerm (Sum a b) = "(" ++ showTerm a ++ "+" ++ showTerm b ++ ")" showTerm (Prod a b) = "(" ++ showTerm a ++ showTerm b ++ ")" ---
Where should I put type constraint (Show b) to be able to define Term b as an instance of Show class?
The syntax is instance (Show b) => Show (Term b) where ...
Actually, I would like to say that Term b is an instance of Show iff b is and not to put constraint on b.
That's exactly what the constraint (Show b) above does. The arrow can be read as in maths (»implies«), so it corresponds to the »iff« in your sentence. By the way, the following implementation should be more efficient on very large data (if I have understood the corresponding chapter in the Haskell School of Expression correctly): instance (Show b) => Show (Term b) where show x = showTerm x "" showTerm (Var x) = showString x showTerm (Const c) = shows c showTerm (Sum a b) = showChar '(' . showTerm a . showChar '+' . showTerm b . showChar ')' showTerm (Prod a b) = showChar '(' . showTerm a . showTerm b . showChar ')' Malte
participants (4)
-
Daniil Elovkov
-
lassoken
-
Malte Milatz
-
Neil Mitchell