我想读取一些数据,这些数据本身指定要使用的数据类型。
例如,我们假设可能存在如下用户输入:
integer pair 1 2
integer triple 1 2 3
real pair 1 2
real triple 1 2 3
并且有一个数据类型来表示它:
data (MValue a) => T a = TP (Pair a) | TT (Triple a)
deriving (Show, Eq)
data Pair a = Pair a a deriving (Show, Eq)
data Triple a = Triple a a a deriving (Show, Eq)
其中允许的值类型必须属于 MValue
类:
class (Num a, Read a) => MValue a where
typename :: a -> String
readval :: [String] -> Maybe a
instance MValue Int where
typename _ = "integer"
readval [s] = maybeRead s
readval _ = Nothing
instance MValue Double where
typename _ = "real"
readval [s] = maybeRead s
readval _ = Nothing
maybeRead s =
case reads s of
[(x,_)] -> Just x
_ -> Nothing
我可以轻松地为 Pair
和 Triple
编写阅读器:
readPair (w1:w2:[]) = Pair <$> maybeRead w1 <*> maybeRead w2
readTriple (w1:w2:w3:[]) = Triple <$> maybeRead w1 <*> maybeRead w2 <*> maybeRead w3
问题是如何为整个T a
类型编写多态读取器?:
readT :: (MValue a, Read a) => String -> Maybe (T a)
我想要:
- 类型
a
由调用者选择。
如果用户的输入与 readT
应生成Nothing
。
如果输入有效,readT
应生成Just (T a)
。- 数字应读取为整数或 double ,具体取决于输入。
a
不兼容,一个简单的实现
readT :: (MValue a, Read a) => String -> Maybe (T a)
readT s =
case words s of
(tp:frm:rest) ->
if tp /= typename (undefined :: a)
then Nothing
else case frm of
"pair" -> TP <$> readPair rest
"triple" -> TT <$> readTriple rest
_ -> Nothing
_ -> Nothing
在行 if tp/= typename (undefined::a)
:
rd.hs:45:17:
Ambiguous type variable `a' in the constraint:
`MValue a' arising from a use of `typename' at rd.hs:45:17-41
Probable fix: add a type signature that fixes these type variable(s)
Failed, modules loaded: none.
如果删除此检查,错误就会消失,但如何验证用户输入是否与调用者选择的数据类型兼容?解决方案可能是使用单独的 readTInt
和 readTDouble
,但我希望相同的 readT
以与 相同的方式多态工作阅读
确实如此。
最佳答案
问题是 undefined::a
中的 a
与 readT
中的 a
不一样code> 的签名。 GHC 中有一个语言扩展可以实现这一点,称为“ScopedTypeVariables”。更可移植的修复方法是引入一些额外的代码来显式地将类型绑定(bind)在一起,例如:
readT :: (MValue a, Read a) => String -> Maybe (T a)
readT s = result
where
result =
case words s of
(tp:frm:rest) ->
if tp /= typename ((const :: a -> Maybe (T a) -> a) undefined result)
then Nothing
else case frm of
"pair" -> TP <$> readPair rest
"triple" -> TT <$> readTriple rest
_ -> Nothing
_ -> Nothing
这是对代码的非常快速和肮脏的修改,我认为这些更改可以更优雅地进行,但这应该可行。
关于haskell - 读取并表示指定要使用的数据类型的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4174187/