我无法在几个模糊的类型变量错误问题中找到我的问题的答案。
基本上我想将类型信息带到值级别。此示例中的最后一行失败。
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
module Test where
data Typ = TInteger | TString deriving Show
data Empty = Empty
data a ## b = Cons a b
class Typical a b | a -> b where
typical :: a -> b
instance Typical Empty [Typ] where
typical _ = []
instance Typical Integer Typ where
typical _ = TInteger
instance Typical String Typ where
typical _ = TString
instance (Typical a Typ, Typical b [Typ]) => Typical (a ## b) [Typ] where
typical _ = typical (undefined :: a) : typical (undefined :: b)
以下是第一条错误消息:
Test.hs:27:17:
Could not deduce (Typical a0 Typ) arising from a use of `typical'
from the context (Typical a Typ, Typical b [Typ])
bound by the instance declaration at Test.hs:26:10-67
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Typical String Typ -- Defined at Test.hs:23:10
instance Typical Integer Typ -- Defined at Test.hs:20:10
Possible fix: add an instance declaration for (Typical a0 Typ)
In the first argument of `(:)', namely `typical (undefined :: a)'
In the expression:
typical (undefined :: a) : typical (undefined :: b)
In an equation for `typical':
typical _ = typical (undefined :: a) : typical (undefined :: b)
我只是不明白。
这里a0
是什么?可能是我最后一行的a
没有用最后一行的那个标识吗?
我应该在哪里放置类型签名?为什么?
请赐教!
答案 0 :(得分:5)
好的,我有一个解决方案,但我不知道这是否是最干净的解决方法。
添加{-# LANGUAGE ScopedTypeVariables #-}
会使代码编译。这样就可以从错误消息中识别a0
a
(对应于代码最后两行的a
)。
请评论!