The following code/definitions were given:
{-# OPTIONS_GHC -Wall #-}
module SQMULID where
import Data.Char
import Data.List
import Data.Maybe
import Test.HUnit
import Test.QuickCheck
import Test.QuickCheck.Gen
class Ring a where
addId :: a -- additive identity
addInv :: a -> a -- additive inverse
mulId :: a -- multiplicative identity
add :: a -> a -> a -- addition
mul :: a -> a -> a -- multiplication
-- | A datatype for storing and manipulating ring expressions.
data RingExpr a = Lit a
| AddId
| AddInv (RingExpr a)
| MulId
| Add (RingExpr a) (RingExpr a)
| Mul (RingExpr a) (RingExpr a)
deriving (Show, Eq)
instance Ring (RingExpr a) where
addId = AddId
addInv = AddInv
mulId = MulId
add = Add
mul = Mul
-- The canonical instance for integers:
instance Ring Integer where
addId = 0
addInv = negate
mulId = 1
add = (+)
mul = (*)
And hereHere is my implementation.
The following code/definitions were given:
{-# OPTIONS_GHC -Wall #-}
module SQMULID where
import Data.Char
import Data.List
import Data.Maybe
import Test.HUnit
import Test.QuickCheck
import Test.QuickCheck.Gen
class Ring a where
addId :: a -- additive identity
addInv :: a -> a -- additive inverse
mulId :: a -- multiplicative identity
add :: a -> a -> a -- addition
mul :: a -> a -> a -- multiplication
-- | A datatype for storing and manipulating ring expressions.
data RingExpr a = Lit a
| AddId
| AddInv (RingExpr a)
| MulId
| Add (RingExpr a) (RingExpr a)
| Mul (RingExpr a) (RingExpr a)
deriving (Show, Eq)
instance Ring (RingExpr a) where
addId = AddId
addInv = AddInv
mulId = MulId
add = Add
mul = Mul
-- The canonical instance for integers:
instance Ring Integer where
addId = 0
addInv = negate
mulId = 1
add = (+)
mul = (*)