因使用“打印”而导致类型变量“ a0”不明确

时间:2019-06-17 05:29:16

标签: haskell functional-programming

我具有以下数据结构,并且我正在尝试为其编写打印机:

data CTypeF a 
    = CVarF Int 
    | CArrF a a
    | CIntF
    | CBoolF
    deriving (Eq, Data, Show, Functor, Foldable, Traversable)  

以下内容给我一个错误:

my_test = do
    let c0 = CIntF
    (print CIntF)

这是错误消息:

• Ambiguous type variable ‘a0’ arising from a use of ‘print’
      prevents the constraint ‘(Show a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Show Constr -- Defined in ‘Data.Data’
        instance Show ConstrRep -- Defined in ‘Data.Data’
        instance Show DataRep -- Defined in ‘Data.Data’
        ...plus 40 others
        ...plus 166 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
• In a stmt of a 'do' block: (print CIntF)
          In the expression:
            do let c0 = CIntF
               (print CIntF)

我看到了以下问题Ambiguous type variable ‘b1’ arising from a use of ‘print’

并修改了我的代码以执行(print CIntF :: CTypeF),但我得到了:

• Expecting one more argument to ‘CTypeF’
      Expected a type, but ‘CTypeF’ has kind ‘* -> *’

我对这个问题有点迷茫。有人可以指出我在这里做错了吗?

2 个答案:

答案 0 :(得分:5)

如错误消息所述,CTypeF接受一个参数。 CTypeF不是类型,但例如CTypeF ()CTypeF IntCTypeF [(String, Double)]是。

这种情况类似于print Nothing,并尝试通过添加(Nothing :: Maybe)来解决。您需要指定Maybe ()Maybe Int或...

尝试

my_test = do
    let c0 = CIntF :: CTypeF ()
    print c0

答案 1 :(得分:1)

问题是,正如Haskell所看到的那样,您没有告诉它a中的CTypeF a是什么,至少不足以打印出来。您可以尝试print (CIntF :: CTypeF ())