Data Type Inheritance ala OO - Inheritence -- howto best in Haskell ?

6 messages
Open this post in threaded view
|
Report Content as Inappropriate

Data Type Inheritance ala OO - Inheritence -- howto best in Haskell ?

 Dear all, I'm created a timeSignal datatype as container around a "Vector Double" data type (see simple code below) and subsequently started to instanciate Num & Eq to be able to perform operations on it. Additionally I want store ifno like an index, time information and eventually an inheritence log (the log is not yet in there).   As I will in the end need up to 10 different datatypes, however using slightly different content (time signal, single value, distribution, ...) I ask myself, how I could define a super data-type with sub-data-types to inherit, but then also overload certain functions (like u would do in OO). What is best way in haskell to achieve this ? (I'm unsure wether haskell classes are what I'm looking for) Cheers Phil ########## Code below import qualified Data.Vector.Unboxed as V data TimeSig = TimeSig Int Double (V.Vector Double) -- signal Index timeStep data getVect :: TimeSig -> (V.Vector Double) getVect (TimeSig idx dt vect)= vect getIdx :: TimeSig -> Int getIdx (TimeSig idx dt vect) = idx getdt :: TimeSig -> Double getdt (TimeSig idx dt vect) = dt pzipWith :: (Double -> Double -> Double) -> TimeSig -> TimeSig -> TimeSig pzipWith f p1 p2 =  TimeSig idx dt vect               where                 vect = V.zipWith f (getVect p1)  (getVect p2)                 idx = getIdx p1                 dt = getdt p1   pmap :: (Double -> Double) -> TimeSig -> TimeSig pmap f p = TimeSig (getIdx p) (getdt p) (V.map f (getVect p)) instance Num TimeSig       where       (+) p1 p2 = pzipWith (+) p1 p2       (-) p1 p2 = pzipWith (-) p1 p2       negate p1 = pmap negate p1       abs p1 = pmap abs p1       (*) p1 p2 = pzipWith (*) p1 p2 instance Eq TimeSig where             (==) p1 p2 = (==) (getVect p1) (getVect p2) instance Show TimeSig where     show (TimeSig idx dt vect) = "TimeSignal Nr: " ++ show idx ++ "  dt: " ++ show dt ++ " val:" ++ show vect           main = do         let p = TimeSig 5 0.1 (V.fromList [0..10::Double])         putStrLn (show p)         putStrLn (show (p+p))
Open this post in threaded view
|
Report Content as Inappropriate

Re: Data Type Inheritance ala OO - Inheritence -- howto best in Haskell ?

Open this post in threaded view
|
Report Content as Inappropriate

Re: Data Type Inheritance ala OO - Inheritence -- howto best in Haskell ?

 CONTENTS DELETED The author has deleted this message.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Data Type Inheritance ala OO - Inheritence -- howto best in Haskell ?

Open this post in threaded view
|
Report Content as Inappropriate

Re: Data Type Inheritance ala OO - Inheritence -- howto best in Haskell ?

 Hi Evan, that looks very interesting indeed - as still newby I try to understand: - u create a class "Y" to tackle the "zero" problem for different kinds of y vectors in a common way - u create a "Signal" class for the Y- signal inheriting interfaces from y and Storable.Storable Then u nest X and Y in a V.Vector - array and give it the type synonym SigVec. In all of this X and Y are ur types (or type synonyms) and y is a help variable. Then u define the function at -- and that where my understanding fails ? - this function takes X and sigVec and give u Y ? -- do u use it to applies all functions on SigVec on Y ? - don't u need to create instances somewhere in the process and wouldn't u then have to write all the code during the instance declaration ? Cheers Phil P.S.: - some generic question: - I read using composite datatype using the "data" keyword makes code rather slow - is nesting better ? : e.g. "data Signal Double v.Vector"  versus "newtype (Double, v.Vector) - which nesting structure is most efficient  - touples, lists, V.vector, ... - alternatively I'm thinking of an "external parallel list" to store information about the signals