Suppose I have these definitions of a data in Haskell.
data Point a = Point a a
data Piece a = One a | Two a
and I want to have one function like
place :: Num a => Piece (Point a) -> Point a
place (Piece x) = x
how do I do this since haskell doesn't allow functions of that particular form.
Solution
The problem was my data definition (many years of empirical programming is interfering here...). I wanted my Piece data to effectively be 2 different things, a Piece and also a kind of piece, I tried to achieve this with inheritance which was just a bad approach. Separating data in the following way solved the problem.
data PieceKind = One | Two | ... | n
data Piece a b = Piece a PieceKind
This way my Piece effectively has two attributes "a" (in my case this is a position of a piece) and also PieceKind, this way I was able to write the place function without having to repeat it for every kind of piece in the following way:
place :: Num a => Piece (Point a) PieceKind -> Point a
place (Piece x _) = x
In addition I was also able to write functions for a particular kind of a Piece:
place :: Num a => Piece (Point a) PieceKind -> Point a
place (Piece _ One) = ...
and this is what I really wanted.
place (One x) = x; place (Two x) = x?Point.