如何将重叠编译指示应用于派生实例

How to apply overlapping pragma to derived instance

Pragma OverlappingInstances 在 GHC 中已经弃用了很长一段时间并且 OVERLAPPING pragma 是它的替代品。

instance {-# OVERLAPPING #- } ...

尽管这不是 Haskell 中定义 class 实例的唯一方法。 我无法通过派生来定义重叠实例,也无法避免关于已弃用的 OverlappingInstances 的讨厌警告。

None 以下情况有效:

  deriving {-# OVERLAPPING #-} (Lift)
  deriving ({-# OVERLAPPING #-} Lift)

对于此类实例以及需要专用实例上下文的实例,您需要使用 StandaloneDeriving

{-# language StandaloneDeriving, FlexibleInstances #-}

data T a = T

deriving instance {-# OVERLAPPING #-} Show (T Int)
instance {-# OVERLAPPABLE #-} Show (T a) where
  show ~T = "Tee-hee"

main = do
  print (T :: T Int)
  print (T :: T Char)