箭头/HXT 和类型签名

发布于 2024-12-21 04:09:16 字数 716 浏览 7 评论 0原文

我正在尝试学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花想c 2024-12-28 04:09:16

readXml 的定义中使用 runX 将箭头“转换”为函数,并在 parseItem 的定义中使用 arr 将该函数转换回函数又是一个箭头。现在,这样做就可以了,除了 readString 返回一个 IOStateArrowIOSLA 的特殊类型别名 - IO State List Arrow ),它不仅应该被视为一个 Arrow,更具体地说,应该被视为一个 IOArrow;同时,您可以使用 arr 重新包装它,将其视为纯 Arrow

这里有两个选择:

  1. 使 readXml = readString [withValidate no],以便 readXml :: String -> IOStateArrow sb XmlTree。然后你就可以做 ... <<< parseItem 中的 readXml str
  2. 使用 arrIO 将 readXml 提升为 IO 箭头,这样您就可以按照您想要的方式使用它。

在这种情况下我会使用选项 1,因为如果没有特殊原因,执行此箭头包装-展开似乎是多余的。

Using runX in the definition of readXml "converts" an arrow into a function, and using arr in the definition of parseItem converts that function back into an arrow again. Now, doing it like this would be fine, except that readString returns an IOStateArrow (a special type alias for the IOSLA - IO State List Arrow), which should be treated not only as an Arrow, but more specifically as an IOArrow; meanwhile, you are treating it as a pure Arrow by rewrapping it using arr.

You have two options here:

  1. Make readXml = readString [withValidate no], so that readXml :: String -> IOStateArrow s b XmlTree. Then you can just do ... <<< readXml str in parseItem.
  2. Use 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文