How to put constraints on type variable of kind `Constraint`?

前端 未结 3 2252
北海茫月
北海茫月 2021-02-19 05:07

I\'m playing around with the ConstraintKinds extension of GHC. I have the following data type, which is just a box for things fulfilling some one parameter constrai

3条回答
  •  无人及你
    2021-02-19 05:59

    The new QuantifiedConstraints extension allows this.

    class (a => b) => Implies a b where
    instance (a => b) => Implies a b where
    instance (forall a. c a `Implies` Show a) => Show (Some c) where
      show (Some x) = show x
    

    Within the body of the Show instance, it is as if there is a

    instance forall a. Implies (c a) (Show a)
    

    in scope. If you then have T :: Type and know c T, then the superclass of c T => Show T of the specialized Implies (c T) (Show T) instance allows you to derive Show T. It is necessary to use Implies instead of a straight forall a. c a => Show a constraint. This incorrect constraint acts like

    instance forall a. c a => Show a
    

    which overlaps with every Show instance, causing weird breakage. Forcing an indirection through the superclass of an otherwise useless constraint fixes everything.

提交回复
热议问题