Type constraints on all type family instances

后端 未结 3 954
-上瘾入骨i
-上瘾入骨i 2021-01-02 08:36

I suppose what I want is impossible without Template Haskell but I\'ll ask anyway.

I have an interface for types like Data.Set and Data.IntSet

3条回答
  •  迷失自我
    2021-01-02 09:14

    Using GHC 7.4's constraint kinds you could have something like

    type EfficientSetLike a = (SetLike (EfficientSet a),Elem (EfficientSet a) ~ a)
    

    You can (with appropriate extensions) get constraints like this in earlier versions of GHC

    class (SetLike (EfficientSet a),Elem (EfficientSet a) ~ a) => EfficientSetLike a 
    instance (SetLike (EfficientSet a),Elem (EfficientSet a) ~ a) => EfficientSetLike a 
    

    But, the new style type declaration is much nicer.

    I'm not exactly sure what you are looking for, but it sounds like you just want easier to write/understand constraint signatures, in which case this will work.

提交回复
热议问题