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

Evan Laforge qdunkan at gmail.com
Fri Jun 17 00:50:50 CEST 2011


I've done something perhaps similar in that I have a couple of signal
types, backed by (X, Y) vectors with Y values of different types, but
the same X type.  So they can share a fair amount of implementation
that depends only on X.  Still more could be shared if I could know a
"zero" value for each Y, so I wrote a SignalBase module:

class (Eq y) => Y y where
    zero_y :: y
    -- anything else that lets you share code

class (Storable.Storable (X, y), Y y) => Signal y

type SigVec y = V.Vector (X, y)

Now the SignalBase functions take '(Signal y) => SigVec y'.  The
specific signals contain a SigVec, and the functions whose
implementations can be shared are just one line:

at :: X -> Signal y -> Y
at x sig = SignalBase.at x (sig_vec sig)

The 'y' parameter to Signal is unrelated, I use it for a phantom type
to distinguish between signals of the same implementation but
different logical meaning, but 'at' applies to all of them.

On Thu, Jun 16, 2011 at 2:16 PM, gutti <philipp.guttenberg at gmx.net> wrote:
> Hi David,
>
> thanks for the links. I had a lok at the OO-paper some time ago already,
> heard however that its quite unusual and rather tricky to do OO-style
> programming in Haskell. So I'm looking for suggestions how to tackle this
> problem in a functional way.
>
> Cheers Phil
>
> --
> View this message in context: http://haskell.1045720.n5.nabble.com/Data-Type-Inheritance-ala-OO-Inheritence-howto-best-in-Haskell-tp4494800p4496726.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list