----- Begin Included Message ----- >From [EMAIL PROTECTED] Tue Feb 6 11:07 MET 1996 Date: Tue, 6 Feb 1996 11:07:26 +0100 From: [EMAIL PROTECTED] (Denis WALLEZ) To: [EMAIL PROTECTED] Subject: fully defined type vs. class , as a result of function Hi, I was wondering how it is possible to use classes in a project I have to make symbolic calculus in Haskell. Since I have to do it in several programming languages, I want to apply quite the same basic design, an expression is a tree: a+b => + / \ a b Well, the interest of symbolic calculus is the modification of the trees WITHOUT evaluation ; for instance what_s_the_slope ( tree=> a, variable => a) = 1 or simplify(tree=>(a-0)) = (leaf a) I started a little exemple like this: >class Derivable a where > derive :: (Derivable b)=> a -> b I make the hypothesis that the result of derivation is still derivable, BUT not necessarily the same kind as the initial upper node value: (fg)'=(f'g)+(fg') the upper node of tree is first a multiplication, but the second is an addition... no conservation. >data Op_bin = Plus (Op_bin) (Op_bin) > | Times (Op_bin) (Op_bin) >instance Derivable Op_bin where > derive (Plus a b) = Plus (derive a) (derive b) > derive (Times a b) = Plus (Times (derive a) b) (Times a (derive b)) This seem quite coherent. I thought I could then define data Op_1 = Val_int (Int) | Val_var [Char] | Val_float (Float) instance Derivable Op_1 where derive (Val_var x) | x=="x" = Val_int 1 | otherwise = Val_int 0 derive _ = Val_int 0 But no way to do this: (with HUGS, which is declared fully implementing Haskell) Reading script file "test4.hs": Type checking ERROR "test4.hs" (line 8): Declared type too general *** Expression : derive *** Declared type : Opbin -> a *** Inferred type : Opbin -> Opbin This is not OK. If I make another class with a 'simplify' function, the result simplify(a+b)=(a+b) with no hypothesis on a and b, but symplify(1+2)=Val_int 3... so I NEED to make my declarations such as I can return ANY type in the class With_simplify. Since the idea I had was to simplify the result of the naive derivation, before returning it (i.e. Op_bin should be also an instance of With_simplify), I NEED the 'general' declaration for the derivation. How can I achieve this ? Does Haskell need ANYWAY a fully defined type when defining the type of the result of a function ? If there's a way to round the difficulty, what is it, or do you have any pointer ? Thanks a lot in advance. Denis. Denis WALLEZ --- TELECOM BRETAGNE \ 3A-IPR \ technopole Brest-Iroise (98 00 18 12) --- 29280 PLOUZANE [EMAIL PROTECTED] fax 98 00 11 90 (preciser: "pour WALLEZ- 3A") Team Ada --- e-mail >> fax >> phone ----- End Included Message -----