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

时间:2010-11-13 19:05:07

标签: 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 s:

撰写读者
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. 如果用户的输入与readT不兼容,则
  3. Nothing应生成a
  4. 如果输入有效,
  5. readT应生成Just (T a)
  6. 根据输入,数字应按整数或双精度读取。
  7. 天真的实施

    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以与read相同的方式进行多态处理。

1 个答案:

答案 0 :(得分:5)

问题是a中的undefined :: aa签名中的readT不一样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 。 GHC中有一种语言扩展可以实现,称为“ScopedTypeVariables”。一个更便携的解决方案是引入一些额外的代码来明确地将类型绑定在一起,例如:

{{1}}

这是对代码的一个非常快速和肮脏的修改,我可以更优雅地进行更改,但这应该可行。