我有一个带有“隐藏”(推断)类型和具体值的数据类型。现在我尝试实现一个改变这两个但却无法通过GHC的函数。
我的示例代码是:
data T tag val = T val
data A = A
data B = B
mkIntVal :: T a b -> T Int b
mkIntVal (T x) = T x
mkCharVal :: T a b -> T Char b
mkCharVal (T x) = T x
convert :: T Int a -> T Char b
convert (T A) = mkCharVal $ T B
convert (T B) = mkCharVal $ T A
它产生的错误是:
test.hs:13:12:
Couldn't match type `A' with `B'
In the pattern: A
In the pattern: T A
In an equation for `convert': convert (T A) = mkCharVal $ T B
test.hs:13:17:
Couldn't match type `B' with `A'
Expected type: T Char b
Actual type: T Char B
In the expression: mkCharVal $ T B
In an equation for `convert': convert (T A) = mkCharVal $ T B
要做这项工作需要做些什么?我是否必须更改数据结构?
我正在尝试扩展Don Stewart
的解决方案以处理多态数据类型。我一直在玩实例定义,但最有希望看到的是:
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
data C a = C a deriving Show
class Convertable inVal outVal outTag | outVal -> outTag where
convert :: T Int inVal -> T outTag outVal
instance Convertable A B Char where
convert (T A) = mkCharVal $ T B
instance Convertable B A Char where
convert (T B) = mkCharVal $ T A
instance Convertable a b Char => Convertable (C a) (C (T Char b)) Char where
convert (T (C val)) = mkCharVal $ T (C (convert val)) -- line 29
但是这给了我另一个错误信息:
test.hs:29:57:
Could not deduce (a ~ T Int inVal0)
from the context (Convertable a b Char)
bound by the instance declaration at test.hs:28:10-70
`a' is a rigid type variable bound by
the instance declaration at test.hs:28:22
In the first argument of `convert', namely `val'
In the first argument of `C', namely `(convert val)'
In the first argument of `T', namely `(C (convert val))'
正如唐所说,我应该有可能对如何实施这一点感兴趣。
经过更多'玩'后,我终于想出了一些有用的东西。这看起来对你好吗?
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE OverlappingInstances #-}
data T tag val = T val deriving Show
data A = A deriving Show
data B = B deriving Show
data C a = C a deriving Show
class Convertable inTag inVal outTag outVal | inTag -> outTag, inVal -> outVal
where
convert :: T inTag inVal -> T outTag outVal
instance Convertable Int A Char B where
convert (T A) = T B
instance Convertable Int B Char A where
convert (T B) = T A
instance (Convertable Int (T Int a) Char (T Char b), Convertable Int a Char b)
=> Convertable Int (C (T Int a)) Char (C (T Char b)) where
convert (T (C x)) = T (C (convert x))
instance Convertable Int (C (T Int A)) Char (C (T Char B)) where
convert (T (C x)) = T (C (convert x))
instance Convertable Int (C (T Int B)) Char (C (T Char A)) where
convert (T (C x)) = T (C (convert x))
用法:
*Main> convert $ mkIntVal $ T $ C $ mkIntVal $ T A
T (C (T B))
*Main> :t it
it :: T Char (C (T Char B))
答案 0 :(得分:1)
convert
函数的每个案例都有不同的冲突类型:
convertA :: T t A -> T Char B
convertA (T A) = mkCharVal $ T B
convertB :: T t B -> T Char A
convertB (T B) = mkCharVal $ T A
你可以通过类型类来统一这些,
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
class C a b c | b -> c where
convert :: T t a -> T c b
instance C A B Char where
convert (T A) = mkCharVal (T B)
instance C B A Char where
convert (T B) = mkCharVal (T A)
如果你真的希望在不同类型的单一功能,转换到不同的方向。请注意如何将T
与标记一起使用,将其丢弃,并使用由值类型确定的新标记替换标记和值。