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

Lan*_*ana 1 haskell functional-programming

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

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

以下给我一个错误:

my_test = do
    let c0 = CIntF
    (print CIntF)
Run Code Online (Sandbox Code Playgroud)

这是错误消息:

• 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)
Run Code Online (Sandbox Code Playgroud)

我看到以下问题是由于使用'print'而产生的歧义类型变量'b1'

并修改了我的代码来做,(print CIntF :: CTypeF)但是我得到了:

• Expecting one more argument to ‘CTypeF’
      Expected a type, but ‘CTypeF’ has kind ‘* -> *’
Run Code Online (Sandbox Code Playgroud)

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

mel*_*ene 5

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

这种情况类似于,print Nothing并尝试通过添加进行修复(Nothing :: Maybe)。您需要指定Maybe ()or Maybe Int或...

尝试

my_test = do
    let c0 = CIntF :: CTypeF ()
    print c0
Run Code Online (Sandbox Code Playgroud)