For the record, the error is:
* Illegal polymorphic type: forall a. a
* In the type instance declaration for `Failure'
In the instance declaration for `Failable (Maybe c)'
I don’t think there is a way to fix that error. It just seems to be a limitation of how associated types work for type classes. But I don’t know exactly why this restriction is in place.
But even if it did work, then it probably wouldn’t do what you expect. You’d get:
initWithFailure :: (forall a. a) -> Maybe c
forall a. a means that the value must be able to be of any type, which is only true for
undefined. No real value can have that type.
You probably wanted the type:
initWithFailure :: forall a. (a -> Maybe c)
Which means that the function can accept any type as input.
I don’t think there is a simple way to get that to work. I can only think of this now:
import Data.Kind (Constraint)
class Failable c where
type Failure c a :: Constraint
initWithFailure :: Failure c a => a -> Maybe c
instance Failable c where
type Failure (Maybe _) _ = ()
initWithFailure :: a -> Maybe c
initWithFailure _ = Nothing