在Haskell中创建一个Read实例

时间:2012-12-22 21:25:17

标签: parsing haskell typeclass symmetry

我有数据类型

data Time = Time {hour :: Int,
                  minute :: Int
                 }

我已经将Show的实例定义为

instance Show Time where
  show (Time hour minute) = (if hour > 10
                             then (show hour)
                             else ("0" ++ show hour))
                            ++ ":" ++
                            (if minute > 10
                             then (show minute)
                             else ("0" ++ show minute))

07:09

的格式打印出时间

现在,ShowRead之间应该是对称的,所以在阅读之后(但不是真正(我认为)理解)thisthis,并阅读documentation,我提出了以下代码:

instance Read Time where
  readsPrec _ input =
    let hourPart = takeWhile (/= ':')
        minutePart = tail . dropWhile (/= ':')
    in (\str -> [(newTime
                  (read (hourPart str) :: Int)
                  (read (minutePart str) :: Int), "")]) input

这有效,但""部分使它看起来不对。所以我的问题最终是:

任何人都可以向我解释实现Read的正确方法,以便将"07:09"解析为newTime 7 9和/或向我展示吗?

2 个答案:

答案 0 :(得分:18)

我将使用isDigit并保留您对时间的定义。

import Data.Char (isDigit)

data Time = Time {hour :: Int,
                  minute :: Int
                 }

你曾经使用但没有定义newTime,所以我自己写了一个,所以我的代码编译了!

newTime :: Int -> Int -> Time
newTime h m | between 0 23 h && between 0 59 m = Time h m
            | otherwise = error "newTime: hours must be in range 0-23 and minutes 0-59"
     where between low high val = low <= val && val <= high

首先,您的show实例有点不对,因为show $ Time 10 10给出了"010:010"

instance Show Time where
  show (Time hour minute) = (if hour > 9       -- oops
                             then (show hour)
                             else ("0" ++ show hour))
                            ++ ":" ++
                            (if minute > 9     -- oops
                             then (show minute)
                             else ("0" ++ show minute))

我们来看看readsPrec

*Main> :i readsPrec
class Read a where
  readsPrec :: Int -> ReadS a
  ...
    -- Defined in GHC.Read
*Main> :i ReadS
type ReadS a = String -> [(a, String)]
    -- Defined in Text.ParserCombinators.ReadP

这是一个解析器 - 它应该返回不匹配的剩余字符串而不仅仅是"",所以你对""错了是正确的:

*Main> read "03:22" :: Time
03:22
*Main> read "[23:34,23:12,03:22]" :: [Time]
*** Exception: Prelude.read: no parse

它无法解析它,因为你在第一次阅读中丢弃了,23:12,03:22]

让我们重新思考一下我们继续进行输入:

instance Read Time where
  readsPrec _ input =
    let (hours,rest1) = span isDigit input
        hour = read hours :: Int
        (c:rest2) = rest1
        (mins,rest3) = splitAt 2 rest2
        minute = read mins :: Int
        in
      if c==':' && all isDigit mins && length mins == 2 then -- it looks valid
         [(newTime hour minute,rest3)]
       else []                      -- don't give any parse if it was invalid

举例说明

Main> read "[23:34,23:12,03:22]" :: [Time]
[23:34,23:12,03:22]
*Main> read "34:76" :: Time
*** Exception: Prelude.read: no parse
然而,它确实允许“3:45”并将其解释为“03:45”。我不确定这是个好主意,所以也许我们可以添加另一个测试length hours == 2


如果我们这样做的话,我会解决这些问题,所以也许我更喜欢:

instance Read Time where
  readsPrec _ (h1:h2:':':m1:m2:therest) =
    let hour   = read [h1,h2] :: Int  -- lazily doesn't get evaluated unless valid
        minute = read [m1,m2] :: Int
        in
      if all isDigit [h1,h2,m1,m2] then -- it looks valid
         [(newTime hour minute,therest)]
       else []                      -- don't give any parse if it was invalid
  readsPrec _ _ = []                -- don't give any parse if it was invalid

这对我来说实际上看起来更简洁。

这次它不允许"3:45"

*Main> read "3:40" :: Time
*** Exception: Prelude.read: no parse
*Main> read "03:40" :: Time
03:40
*Main> read "[03:40,02:10]" :: [Time]
[03:40,02:10]

答案 1 :(得分:4)

如果readsPrec的输入是一个字符串,其中包含Time的有效表示后的其他一些字符,那么其他字符应作为元组的第二个元素返回。

因此,对于字符串12:34 bla,结果应为[(newTime 12 34, " bla")]。您的实现会导致该输入的错误。这意味着类似read "[12:34]" :: [Time]之类的内容会失败,因为它会以Time作为参数调用readsPrec "12:34]"(因为readList会消耗[,然后使用剩余字符串调用readsPrec,然后检查readsPrec返回的剩余字符串是]还是逗号后跟更多元素。

要修复您的readsPrec,您应该将minutePart重命名为afterColon,然后将其拆分为实际的分钟部分(例如takeWhile isDigit)以及之后发生的任何事情分钟部分。那么在分钟部分之后出现的东西应该作为元组的第二个元素返回。