haskell - 读取并表示指定要使用的数据类型的输入

标签 haskell polymorphism

我想读取一些数据,这些数据本身指定要使用的数据类型。

例如,我们假设可能存在如下用户输入:

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

我可以轻松地为 PairTriple 编写阅读器:

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)

我想要:

  1. 类型a由调用者选择。
  2. 如果用户的输入与 a 不兼容,
  3. readT 应生成 Nothing
  4. 如果输入有效,
  5. readT 应生成 Just (T a)
  6. 数字应读取为整数或 double ,具体取决于输入。

一个简单的实现

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.

如果删除此检查,错误就会消失,但如何验证用户输入是否与调用者选择的数据类型兼容?解决方案可能是使用单独的 readTIntreadTDouble,但我希望相同的 readT 以与 相同的方式多态工作阅读确实如此。

最佳答案

问题是 undefined::a 中的 areadT 中的 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/

相关文章:

haskell - 在列表列表中,强加相同的长度

recursion - OCaml 显式多态类型注释

java - 依赖注入(inject): templates (/generics) or virtual functions?

haskell - 过滤器卡在无限列表上

haskell - 表示自动机的数据结构

javascript - 功能珍珠 : Implementing trace in JavaScript

Haskell 函数检查数字是否为奇数,而不使用 odd 函数

ruby-on-rails-3.1 - ThinkingSphinx 多态关系问题 |轨道 3.1

c# - 具有多态性的通用接口(interface)来处理对象

java - 接口(interface)方法返回包含不同对象的ArrayList