Expression tree in Haskell with support for differentiation A simple binary expression tree in Haskell without operator precedence and without parentheses.

Any comments would be much appreciated.

data Tree a= Const a
       | X    
       | Plus (Tree a) (Tree a)
       | Mult (Tree a) (Tree a)
       | Minus (Tree a) (Tree a)
       | Div (Tree a) (Tree a)
       | Power (Tree a) (Tree a)
       | Cos (Tree a)
       | Sin (Tree a)
deriving Show

eval :: ( Eq t, Floating t) =&gt; Tree t-&gt; t -&gt; Maybe t
eval (Const a) x =  Just  (a)
eval X x =  Just x
eval (Plus a b) x = (+) &lt;$&gt; eval a x &lt;*&gt; eval b x
eval (Mult a b) x =  (*) &lt;$&gt; eval a x &lt;*&gt; eval b x
eval (Minus a b) x =  (-) &lt;$&gt; eval a x &lt;*&gt; eval b x
eval (Power a b) x =  (**) &lt;$&gt; eval a x &lt;*&gt; eval b x
eval (Div a b) x =  if (eval b x) == (Just 0) then Nothing else (/) &lt;$&gt; eval a x &lt;*&gt; eval b x
eval (Cos a) x =(cos) &lt;$&gt; eval a x
eval (Sin a) x = (cos) &lt;$&gt; eval a x


dif :: ( Eq t, Floating t) =&gt; Tree t-&gt;Tree t
dif (Const a)  = Const 0
dif X  =  Const 1
dif (Plus a b)  =Plus (dif a) (dif b )
dif (Mult a b)  =  Plus (Mult (dif a) b) (Mult  a (dif b))
dif (Minus a b)  =  Minus (dif a) (dif b)
dif (Div a b) =  Div (Minus (Mult (dif a) b) (Mult  a (dif b))) (Mult b b)
dif (Power (Const a) (Const b)) = Const 0
dif (Power a (Const b)) = Mult (Const b) (Power X (Minus (Const b) (Const 1)))
dif (Cos a) = Minus (Const 0) (Mult (dif a) (Sin a))
dif (Sin a) = (Mult (dif a) (Cos a))
You have a copy-and-paste error here:


eval (Cos a) x =(cos) &lt;$&gt; eval a x
eval (Sin a) x = (cos) &lt;$&gt; eval a x