[Haskell-cafe] Extension and parameterized classes
mikolaj at well-typed.com
Thu Feb 14 13:14:49 UTC 2019
> 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'm afraid I'm agreeing with the compiler here. When you instantiate this class
class Base a => Extend a where
fromBase :: Base b => b -> a
so that `a` is `BaseExpose x`, then you need to implement
fromBase :: Base b => b -> BaseExpose x
and your fromBase' has a less general type
fromBase' :: Base x => x -> BaseExpose x
and that's it. It's the same issue as when you try
to typecheck `id :: a -> b`. If it type-checked, you could
write (pure) crashing type-correct code.
I don't have enough data, but I guess you don't need `fromBase`
at all. if `a` is `Base`, then it's also `Extend`, as long as you
implement any extra operations. You don't need to coerce it
to `Extend` with `fromBase`.
BTW, as opposed to object oriented programmers,
I think most of us doesn't use the class system
for encapsulation, but the module system instead
(with all its limitations). If the module system is not enough
for you and you are ready for the bleeding edge,
the Backpack may have what you need. Having said that,
I think we usually err on the side of exposing too much,
with all the simplicity and testing ease benefits it provides,
rather than on the side of hiding too much. The user of a library
is free to encapsulate more strongly in his import lists
or by defining an interface module. Only the user knows
his use case.
More information about the Haskell-Cafe