如何在 Haskell 中处理无限的 IO 对象列表?
我正在编写一个从文件列表中读取的程序。每个文件要么包含到下一个文件的链接,要么标记它是链的末尾。
作为 Haskell 的新手,处理这个问题的惯用方法似乎是为此目的提供一个可能文件的惰性列表,到目前为止
getFirstFile :: String -> DataFile
getNextFile :: Maybe DataFile -> Maybe DataFile
loadFiles :: String -> [Maybe DataFile]
loadFiles = iterate getNextFile . Just . getFirstFile
getFiles :: String -> [DataFile]
getFiles = map fromJust . takeWhile isJust . loadFiles
,一切都很好。唯一的问题是,由于 getFirstFile 和 getNextFile 都需要打开文件,因此我需要将它们的结果放在 IO monad 中。这给出了修改后的形式
getFirstFile :: String -> IO DataFile
getNextFile :: Maybe DataFile -> IO (Maybe DataFile)
loadFiles :: String -> [IO Maybe DataFile]
loadFiles = iterate (getNextFile =<<) . Just . getFirstFile
getFiles :: String -> IO [DataFile]
getFiles = liftM (map fromJust . takeWhile isJust) . sequence . loadFiles
。问题在于,由于 iterate 返回一个无限列表,因此序列变成了无限循环。我不知道如何从这里继续。是否有一种更惰性的序列形式不会命中所有列表元素?我是否应该重新调整映射并 takeWhile 以便在每个列表元素的 IO monad 内进行操作?或者我是否需要放弃整个无限列表过程并编写一个递归函数来手动终止列表?
I'm writing a program that reads from a list of files. The each file either contains a link to the next file or marks that it's the end of the chain.
Being new to Haskell, it seemed like the idiomatic way to handle this is is a lazy list of possible files to this end, I have
getFirstFile :: String -> DataFile
getNextFile :: Maybe DataFile -> Maybe DataFile
loadFiles :: String -> [Maybe DataFile]
loadFiles = iterate getNextFile . Just . getFirstFile
getFiles :: String -> [DataFile]
getFiles = map fromJust . takeWhile isJust . loadFiles
So far, so good. The only problem is that, since getFirstFile and getNextFile both need to open files, I need their results to be in the IO monad. This gives the modified form of
getFirstFile :: String -> IO DataFile
getNextFile :: Maybe DataFile -> IO (Maybe DataFile)
loadFiles :: String -> [IO Maybe DataFile]
loadFiles = iterate (getNextFile =<<) . Just . getFirstFile
getFiles :: String -> IO [DataFile]
getFiles = liftM (map fromJust . takeWhile isJust) . sequence . loadFiles
The problem with this is that, since iterate returns an infinite list, sequence becomes an infinite loop. I'm not sure how to proceed from here. Is there a lazier form of sequence that won't hit all of the list elements? Should I be rejiggering the map and takeWhile to be operating inside the IO monad for each list element? Or do I need to drop the whole infinite list process and write a recursive function to terminate the list manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
朝着正确方向迈出的一步
令我困惑的是
getNextFile
。和我一起进入一个简化的世界,我们还没有处理 IO。类型是Maybe DataFile ->也许是数据文件。在我看来,这应该只是
DataFile ->也许是DataFile
,我会在这种调整可能的假设下进行操作。 这看起来很适合unfoldr
。我要做的第一件事是制作我自己的 Expandr 简化版本,它不太通用,但使用起来更简单。现在类型
f :: a ->也许 a
匹配getNextFile :: DataFile ->也许 DataFile
很漂亮,对吧?
unfoldr
很像iterate
,只不过一旦它命中Nothing
,它就会终止列表。现在,我们有一个问题。 IO。我们怎样才能用 IO 来做同样的事情呢?甚至不要考虑“不得命名的函数”。我们需要一个增强的展开器来处理这个问题。幸运的是,unfoldr 的源代码< /a> 可供我们使用。
现在我们需要什么?健康剂量的
IO
。liftM2 Expandr
几乎为我们提供了正确的类型,但这次不会完全削减它。实际的解决方案
这是一个相当简单的转换;我想知道是否有一些组合器可以完成同样的任务。
有趣的事实:我们现在可以定义
unfoldr fb = runIdentity $unfoldrM (return . f) b
让我们再次定义一个简化的
myUnfoldrM
,我们只需添加一个liftM 在那里:
现在我们已经准备好了,就像以前一样。
顺便说一下,我使用 data DataFile = NoClueWhatGoesHere 以及
getFirstFile
和getNextFile
的类型签名对所有这些进行了类型检查,并将它们的定义设置为未定义
。[edit] 更改了
myUnfoldr
和myUnfoldrM
使其行为更像iterate
,包括结果列表中的初始值。[编辑]关于展开的其他见解:
如果您很难理解展开,Collatz 序列 可能是最简单的例子之一。
请记住,
myUnfoldr
是“下一个种子”和“当前输出值”相同的情况下的简化展开,就像 collatz 的情况一样。考虑到myUnfoldr
在unfoldr
和tuplfy x = (x,x)
方面的简单定义,这种行为应该很容易看出。更多,大多是不相关的想法
其余的与这个问题完全无关,但我就是忍不住沉思。我们可以用
myUnfoldrM
来定义myUnfoldr
:看起来很熟悉吗?我们甚至可以抽象这个模式:
形式的函数
sinkM
应该能够“下沉”(与“提升”相反)任何Monad m =>; 。 (a→mb)→一个-> m c 。
因为这些函数中的
Monad m
可以与sinkM
的Identity
monad 约束统一。但是, 我没有看到任何sinkM
实际上有用的东西。A step in the right direction
What puzzles me is
getNextFile
. Step into a simplified world with me, where we're not dealing with IO yet. The type isMaybe DataFile -> Maybe DataFile
. In my opinion, this should simply beDataFile -> Maybe DataFile
, and I will operate under the assumption that this adjustment is possible. And that looks like a good candidate forunfoldr
. The first thing I am going to do is make my own simplified version of unfoldr, which is less general but simpler to use.Now the type
f :: a -> Maybe a
matchesgetNextFile :: DataFile -> Maybe DataFile
Beautiful, right?
unfoldr
is a lot likeiterate
, except once it hitsNothing
, it terminates the list.Now, we have a problem.
IO
. How can we do the same thing withIO
thrown in there? Don't even think about The Function Which Shall Not Be Named. We need a beefed up unfoldr to handle this. Fortunately, the source for unfoldr is available to us.Now what do we need? A healthy dose of
IO
.liftM2 unfoldr
almost gets us the right type, but won't quite cut it this time.An actual solution
It is a rather straightforward transformation; I wonder if there is some combinator that could accomplish the same.
Fun fact: we can now define
unfoldr f b = runIdentity $ unfoldrM (return . f) b
Let's again define a simplified
myUnfoldrM
, we just have to sprinkle in aliftM
in there:And now we're all set, just like before.
By the way, I typechecked all of these with
data DataFile = NoClueWhatGoesHere
, and the type signatures forgetFirstFile
andgetNextFile
, with their definitions set toundefined
.[edit] changed
myUnfoldr
andmyUnfoldrM
to behave more likeiterate
, including the initial value in the list of results.[edit] Additional insight on unfolds:
If you have a hard time wrapping your head around unfolds, the Collatz sequence is possibly one of the simplest examples.
Remember,
myUnfoldr
is a simplified unfold for the cases where the "next seed" and the "current output value" are the same, as is the case for collatz. This behavior should be easy to see givenmyUnfoldr
's simple definition in terms ofunfoldr
andtuplefy x = (x,x)
.More, mostly unrelated thoughts
The rest has absolutely nothing to do with the question, but I just couldn't resist musing. We can define
myUnfoldr
in terms ofmyUnfoldrM
:Look familiar? We can even abstract this pattern:
sinkM
should work to "sink" (opposite of "lift") any function of the formMonad m => (a -> m b) -> a -> m c
.since the
Monad m
in those functions can be unified with theIdentity
monad constraint ofsinkM
. However, I don't see anything thatsinkM
would actually be useful for.产量:
Yields:
正如您所注意到的,IO 结果不能是惰性的,因此您不能(轻松)使用 IO 构建无限列表。然而,有一个出路,在
unsafeInterleaveIO
;有了这个,您可以执行以下操作:不过,这里要小心很重要 - 您只是将 ioList 的结果推迟到将来某个不可预测的时间。事实上,它可能永远不会运行。所以当你像这样聪明™时要非常小心。
就我个人而言,我只会构建一个手动递归函数。
As you have noticed, IO results can't be lazy, so you can't (easily) build an infinite list using IO. There is a way out, however, in
unsafeInterleaveIO
; with this, you can do something like:It's important to be careful here, though - you've just deferred the results of
ioList
to some unpredictable time in the future. It may never be run at all, in fact. So be very careful when you're being Clever™ like this.Personally, I would just build a manual recursive function.
惰性和 I/O 是一个棘手的组合。使用
unsafeInterleaveIO
是在 IO monad 中生成惰性列表的一种方法(这是标准getContents
、readFile
等使用的技术) 。然而,尽管这很方便,但它会将纯代码暴露给可能的 I/O 错误,并使释放资源(例如文件句柄)变得不确定。这就是为什么现在大多数“严肃”的 Haskell 应用程序(尤其是那些关心效率的应用程序)使用称为 Enumerators 和 Iteratees 的东西来进行流式 I/O。 Hackage 中实现这一概念的一个库是enumerator
。您可能很乐意在应用程序中使用惰性 I/O,但我想我仍然会以此作为解决此类问题的另一种方法的示例。您可以在 此处 和 此处。
例如,您的数据文件流可以实现为枚举器,如下所示:
Laziness and I/O are a tricky combination. Using
unsafeInterleaveIO
is one way to produce lazy lists in the IO monad (and this is the technique used by the standardgetContents
,readFile
and friends). However, as convenient as this is, it exposes pure code to possible I/O errors and makes makes releasing resources (such as file handles) non-deterministic. This is why most "serious" Haskell applications (especially those concerned with efficiency) nowadays use things called Enumerators and Iteratees for streaming I/O. One library in Hackage that implements this concept isenumerator
.You are probably fine with using lazy I/O in your application, but I thought I'd still give this as an example of another way to approach these kind of problems. You can find more in-depth tutorials about iteratees here and here.
For example, your stream of DataFiles could be implemented as an Enumerator like this: