【发布时间】:2016-11-06 22:40:04
【问题描述】:
我有以下自定义数据类型:
data FirstPair' a b = FirstPair a b deriving (Show, Ord, Eq)
type FirstPair a = FirstPair' a a
data SecondPair' a b = SecondPair a b deriving (Show, Ord, Eq)
type SecondPair a = SecondPair' a a
我正在尝试为我的函数创建一个 GADT 结构:
data Generator a where
Success :: FirstPair a -> Generator (SecondPair a)
Fail :: FirstPair a -> Generator Bool
myFunction :: Generator a -> a
myFunction (Success fp) = SecondPair "21" "24"
myFunction (Fail fp) = False
'Generator'类型的作用是让我强制'myFunction'在'Success'被传递给它时返回'SecondPair'的实例,如果'Fail'被传递给它,'False'。
但是,我收到了这个错误:
"Could not deduce: a1 ~ [Char] from the context: a ~ SecondPair' a1 a1 bound by a pattern with constructor: Success :: forall a. FirstPair a -> Generator (SecondPair a)"
我在这里做错了什么?
【问题讨论】:
-
您的问题似乎缺少一些代码。您的错误引用了
ContextaPair',但您没有提及。你能发布你的实际代码吗? -
您仍然没有运行您发布的相同代码。您的代码中没有
[Char]值。