I'm learning Overloading in haskell and I have a few problems trying to overload show and num clases. I work with a new recursive data and I have this functions:
module Nat where
data Nat = Zero | Suc Nat
--Zero = 0
--Suc (Suc zero) = 1
--suc (suc(suc zero)) = 2
--nat2int:: Nat -> Int
nat2int Zero = 0
nat2int (Suc x) = 1 Prelude.+ nat2int x
--suma: Nat -> Nat -> Nat
suma Zero b = b --addition
suma (Suc a) b = Suc (suma a b)
--producto: Nat -> Nat -> Nat
producto Zero _ = Zero --product
producto (Suc m) n = suma n (producto m n)
The functions work correctly, but when i try to create 2 instances:
module Sobrecarga where
import Nat
instance Show Nat where
show Zero = "0"
show (Suc x) = Prelude.show (nat2int (Suc x))
instance Num Nat where
(+) a b = suma a b
(*) a b = producto a b
i got this 2 warnings and if y try to use show, sumar o producto, the ghci compiler give me an error:
Sobrecarga.hs:3:10: warning: [-Wmissing-methods]
* No explicit implementation for
either `showsPrec' or `Prelude.show'
* In the instance declaration for `Show Nat'
|
3 | instance Show Nat where
| ^^^^^^^^
Sobrecarga.hs:8:10: warning: [-Wmissing-methods]
* No explicit implementation for
`Prelude.+', `Prelude.*', `abs', `signum',
`fromInteger', and (either `negate' or `-')
* In the instance declaration for `Num Nat'
*Sobrecarga> Zero + Zero
<interactive>:65:6: error:
Ambiguous occurrence `+'
It could refer to either `Prelude.+',
imported from `Prelude' at Sobrecarga.hs:1:8-17
(and originally defined in `GHC.Num')
or `Sobrecarga.+', defined at Sobrecarga.hs:7:1
Have you any solution to fix the amibiguity?