I am working on a small symbolic library to do some calculations using Haskell.
To represent a symbolic operation, I created this data type:
data MathExpress =
MathDouble Double
| MathAdd MathExpress MathExpress
| MathSoust MathExpress MathExpress
| ...
I was able to create an instance Num, to be able to use the operator +and -for my type MathExpress.
instance Num MathExpress where
(+) (expa) (expb) = MathAdd expa expb
(-) (expa) (expb) = MathSoust expa expb
...
and when I write:
( MathExpress expression ) * MathDouble 2.0
Works!
Now I would like to be able to use +and -with numbers too ( Doubleor Int) to write simpler:
( MathExpress expression ) * 2.0
Is it possible (by creating an instance or something) to make Haskell output 2.0 as MathDouble 2.0?
source
share