Hello while doing examples from Real World Haskell book i have encountered this example and i can not understand what it does mean and how it does work:instance Num a=>Num (SymbolicManip a)
In this case i should translate to somehting like : "For Num instance of type SymbolicManip
there is a constraint regarding its field of type a
,which is : a
being an instance of Num itself"?
Can someone please tell me if i interpreted it right or explain?
Why wouldn't instance Num (SymbolicManip a)
be enough?
-- The "operators" that we're going to support
data Op = Plus | Minus | Mul | Div | Pow
deriving (Eq, Show)
{- The core symbolic manipulation type -}
data SymbolicManip a =
Number a -- Simple number, such as 5
| Arith Op (SymbolicManip a) (SymbolicManip a)
deriving (Eq, Show)
{- SymbolicManip will be an instance of Num. Define how the Num
operations are handled over a SymbolicManip. This will implement things
like (+) for SymbolicManip. -}
instance Num a => Num (SymbolicManip a) where
a + b = Arith Plus a b
a - b = Arith Minus a b
a * b = Arith Mul a b
negate a = Arith Mul (Number (-1)) a
abs a = error "abs is unimplemented"
signum _ = error "signum is unimplemented"
fromInteger i = Number (fromInteger i)
P.S All code is from the book (chapter 13 - subchapter-Extended Example-Numeric Types)