箭头/HXT 和类型签名
我正在尝试学习 Haskell 中的 Arrow,因此我正在使用基于 XML 的 HXT 库的箭头编写一个简单的应用程序。 HXT wiki 和教程中的示例放弃了函数类型签名。然而,我非常喜欢类型,并且正在尝试研究如何使用它们。这就是我遇到的绊脚石。给定这些函数:
readXml str = runX (readString [withValidate no] str)
atTag tag = deep (isElem >>> hasName tag)
我认为应该为它们分配以下签名:
readXml ∷ String → IO [XmlTree]
atTag ∷ ArrowXml a ⇒ String → a XmlTree XmlTree
我正在尝试使用箭头语法将它们连接在一起:
parseItem = proc str -> do
desc <- text <<< atTag "description" <<< arr readXml -< str
...
但是,如果我的类型签名是正确的(GHC 没有抱怨),我需要一种方法其中结合 monad 语法和箭头语法来获取 XmlTree
并返回到 IO
。
我不确定如何继续。有人有任何见解吗?
I am trying to learn Arrows in Haskell, so I am writing a simple application with the arrow based HXT library for XML. The examples in the HXT wikis and tutorials forgo function type signatures. However, I am quite fond of types and am trying to work out how to use them. This is where I have met a stumbling block. Given these functions:
readXml str = runX (readString [withValidate no] str)
atTag tag = deep (isElem >>> hasName tag)
I figure they should be assigned the following signatures:
readXml ∷ String → IO [XmlTree]
atTag ∷ ArrowXml a ⇒ String → a XmlTree XmlTree
I am trying to hook these together using arrow syntax as such:
parseItem = proc str -> do
desc <- text <<< atTag "description" <<< arr readXml -< str
...
However, if my my type-signatures are correct (GHC hasn't complained), I would need a way in which to combine monad syntax and arrow syntax to get the XmlTree
out of and returned to IO
.
I am unsure how to proceed. Anyone have any insights?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
readXml
的定义中使用runX
将箭头“转换”为函数,并在 parseItem 的定义中使用arr
将该函数转换回函数又是一个箭头。现在,这样做就可以了,除了readString
返回一个IOStateArrow
(IOSLA
的特殊类型别名 - IO State List Arrow ),它不仅应该被视为一个Arrow
,更具体地说,应该被视为一个IOArrow
;同时,您可以使用arr
重新包装它,将其视为纯Arrow
。这里有两个选择:
readXml = readString [withValidate no]
,以便readXml :: String -> IOStateArrow sb XmlTree
。然后你就可以做... <<<
。parseItem
中的 readXml str在这种情况下我会使用选项 1,因为如果没有特殊原因,执行此箭头包装-展开似乎是多余的。
Using
runX
in the definition ofreadXml
"converts" an arrow into a function, and usingarr
in the definition of parseItem converts that function back into an arrow again. Now, doing it like this would be fine, except thatreadString
returns anIOStateArrow
(a special type alias for theIOSLA
- IO State List Arrow), which should be treated not only as anArrow
, but more specifically as anIOArrow
; meanwhile, you are treating it as a pureArrow
by rewrapping it usingarr
.You have two options here:
readXml = readString [withValidate no]
, so thatreadXml :: String -> IOStateArrow s b XmlTree
. Then you can just do... <<< readXml str
inparseItem
.arrIO
to lift readXml into an IO arrow, which lets you use it the way you intended.I would use option 1 in this case, since it seems superfluous to do this arrow-wrapping-unwrapping if there's no special reason for it.