首页 > 解决方案 > Haskell Show 实例

问题描述

我有这种数据类型data SigT a p = ExistT a p,我想Show为它写一个指定的。我试过这个:

instance Show (SigT Integer Integer) where
  show (ExistT q r) = "quotient = " ++ show q ++ "remainder = " ++ show r

但我得到这个错误:

EXAMPLE_05_Driver.hs:8:10: error:
    • Illegal instance declaration for ‘Show (SigT Integer Integer)’
        (All instance types must be of the form (T a1 ... an)
         where a1 ... an are *distinct type variables*,
         and each type variable appears at most once in the instance head.
         Use FlexibleInstances if you want to disable this.)
    • In the instance declaration for ‘Show (SigT Integer Integer)’

标签: haskell

解决方案


您是否阅读过错误消息的这一部分:“如果您想禁用此功能,请使用 FlexibleInstances。”?这是指FlexibleInstances语言扩展,您可以通过编写

{-# LANGUAGE FlexibleInstances #-}

在源文件之上,或者(如果你在 ghci 中)通过编写:set -XFlexibleInstances


推荐阅读