module TerraHS.Algebras.Base.Attribute
(
Attribute (..),
Attributes (..),
Values (..),
toString,
Values (..),
Value (..),
Property,
getValuebyName
)
where
class (Values v) => Attributes a v | a -> v where
getName :: a -> String
getValue :: a -> v
type Property = (String, Value)
data Attribute = Attr Property deriving (Show)
data Value
= StValue String
| DbValue Double
| InValue Int
| Undefined
deriving (Eq, Show)
instance Attributes Attribute Value where
getName ( Attr (name, val) ) = name
getValue ( Attr (name, val) ) = val
getValuebyName :: [Attribute] -> String -> Value
getValuebyName [] _ = error "attribute not found"
getValuebyName (x:xs) str
|((getName x) == str ) = getValue x
| otherwise = getValuebyName xs str
instance Num Value where
(+) (DbValue v1) (DbValue v2) = ( DbValue (v1 + v2) )
(+) (InValue v1) (InValue v2) = ( InValue (v1 + v2) )
(+) (StValue v1) (StValue v2) = ( StValue (v1 ++ v2) )
(+) Undefined _ = Undefined
(+) _ Undefined = Undefined
() (DbValue v1) (DbValue v2) = ( DbValue (v1 v2) )
() (InValue v1) (InValue v2) = ( InValue (v1 v2) )
() Undefined _ = Undefined
() _ Undefined = Undefined
(*) (DbValue v1) (DbValue v2) = ( DbValue (v1 v2) )
(*) (InValue v1) (InValue v2) = ( InValue (v1 v2) )
(*) Undefined _ = Undefined
(*) _ Undefined = Undefined
class Values a where
toString :: a -> String
instance Values Value where
toString (StValue v) = v
toString (DbValue v) = (show v)
toString (DbValue v) = (show v)
toString (InValue v) = (show v)