[Haskell-cafe] Extension and parameterized classes

Pierre_van_der_Laar (Functional Account) pierre.van.de.laar at philips.com
Tue Feb 12 12:48:53 UTC 2019

Dear Haskell experts,

I working  on a large Haskell project on Model Based Testing (see https://github.com/TorXakis/TorXakis)
and I would like to get feedback on a design issue I have related to extension and parameterized classes.
I will describe a small simplified example to illustrate the issue
and I appreciate any feedback.

I have a class that extends another class as follows:

class Base a => Extend a where
    -- | Constructor from Base
    fromBase :: Base b => b -> a

I can define an instance of Extend that hides the Base implementation as follows:

{-# LANGUAGE ExistentialQuantification #-}
data BaseHidden = forall a . Base a =>
                            BaseHidden { _base :: a
                                       , ... -- additional extension info

instance Extend BaseHidden where
    fromBase b = BaseHidden b ... -- additional extension initialization

I can define a parameterized data class of which the parameter is the type of the Base class as follows:
data BaseExpose a = BaseExpose { _base :: a
                               , ... -- additional extension info

With this parameterized data class, I can make the following fromBase' function
fromBase' :: Base a => a -> BaseExpose a
fromBase' a = BaseExpose a ... -- additional extension initialization

My question is can I make BaseExpose an instance of Extend?

I am aware that the signature of fromBase states that for all 'b' of Base
an Extend of type 'a' must be able to be made.
Yet, in my case 'a' is a parameterized class 'BaseExpose x' and
since the type of Extend is provided (it is a constructor)
I think it is acceptable that 'BaseExpose b' is returned as type.
I have looked at some extensions, such as InstanceSigs, yet I found no solution.

Does anybody on this mailing list have an answer or solution for me?
Also experience in a large project related to maintenance for the two alternatives (hiding or exposing types) is appreciated!

Thanks in advance,
    Pierre van de Laar

The information contained in this message may be confidential and legally protected under applicable law. The message is intended solely for the addressee(s). If you are not the intended recipient, you are hereby notified that any use, forwarding, dissemination, or reproduction of this message is strictly prohibited and may be unlawful. If you are not the intended recipient, please contact the sender by return e-mail and destroy all copies of the original message.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190212/ef2a4670/attachment.html>

More information about the Haskell-Cafe mailing list