Haskell Boomerang编译错误

时间:2017-05-05 11:31:31

标签: haskell web-routes-boomerang

所以我觉得我在与Boomerang解析一些AIS数据时有一些乐趣,而且我在第一道障碍时遇到了绊脚石。编译错误令人费解。在我试图解决这个问题之前,我已经在Boomerang中解析了类似的事情。

图书馆很简单。我定义了一些基本类型及其解析器/语法:

import           Control.Category      (id, (.))
import           Control.Monad         (forever)
import           Prelude               hiding (id, (.))
import           System.IO             (hFlush, stdout)
import           Text.Boomerang
import           Text.Boomerang.String
import           Text.Boomerang.TH

data MessageType = AIVDM | AIVDO deriving (Enum, Eq, Show)

data AIS = AIS {
              msgType :: MessageType
          } deriving (Eq, Show)

$(makeBoomerangs ''MessageType)
$(makeBoomerangs ''AIS)

messageTypeP :: StringBoomerang () (MessageType :- ())
messageTypeP = rAIVDM . "!AIVDM" <> rAIVDO . "!AIVDO"

aisP :: StringBoomerang () (AIS :- ())
aisP = rAIS . messageTypeP . lit ","

我现在希望支持在消息类型之后出现的句子计数值;我向Int添加了AIS

data AIS = AIS {
              msgType :: MessageType, sCount :: Int
          } deriving (Eq, Show)

并更改解析器/打印机:

aisP :: StringBoomerang () (AIS :- ())
aisP = rAIS . messageTypeP . lit "," . int

但无法编译:

• Couldn't match type ‘()’ with ‘Int :- ()’
  Expected type: Boomerang
                   StringError String () (MessageType :- (Int :- ()))
    Actual type: Boomerang StringError String () (MessageType :- ())
• In the second argument of ‘(.)’, namely
    ‘messageTypeP . lit "," . int’
  In the expression: rAIS . messageTypeP . lit "," . int
  In an equation for ‘aisP’:
      aisP = rAIS . messageTypeP . lit "," . int

哎哟。请帮帮忙?

1 个答案:

答案 0 :(得分:1)

回旋镖应该是多态的。

messageTypeP :: StringBoomerang r (MessageType :- r)

aisP :: StringBoomerang r (AIS :- r)

解释是r是一堆类型,并且从/向它回放pop / push类型。将r设置为()会强制输入堆栈为空,这会损害这些回旋镖的可重用性。