Your data definition is a lateral move, both constructors A and B are instances of type A, where what you are looking for is type B, which is type A, not just an instance of type A.
Try:
data Foo = Foo {field :: Type} newtype Bar = Bar Foo fromBar Bar x = x toBar x = Bar x
This will allow you to declare all kinds of special functions in the type panel that will not be applied to Foo, as well as work with the bar using functions designed for the Foo type.
If you want to extend the Foo data parameters with additional information in Bar, you can use the data declaration and has-a relationship. This is not as desirable as the new type, since newtype is lazily evaluated where the data declaration is strict. Nonetheless:
data Foo = Foo {field :: Type} data Bar = Bar {fooField :: Foo, moreField :: Type}
As sepp2k points out, what you really want to do to determine the βnatureβ of a type is not by how it looks, but by how it works. We do this in Haskell by defining a type class and having an instance of this class.
Hope this helps.
source share