gpt4 book ai didi

haskell - Simon Marlow 书中的严格评估示例 "Parallel and Concurrent Programming in Haskell"

转载 作者:行者123 更新时间:2023-12-02 18:45:23 24 4
gpt4 key购买 nike

Simon Marlow 在他的书《Haskell 中的并行和并发编程》中写道:

The insert operation hadthis line:

putMVar m (Map.insert name number book)

This places in the MVar the unevaluated expression Map.insert name number book.If we were to do many insert operations consecutively, the MVar would build up a large chain of unevaluated expressions.To get brief locking and no space leaks, we need to usea trick:

let book' = Map.insert name number book
putMVar m book'
seq book' (return ())

With this sequence, we’re storing an unevaluated expression in the MVar, but it is evaluated immediately after the putMVar.

我不明白。 seq a b 运算以弱头范式计算 a。所以就会出现未评估的表达。正如我所看到的,只有 Map 构造函数将被评估,并且所有内容都将被不评估。

最佳答案

As I can see only the Map constructor will be evaluated and all of it contents will be unevaluated.

在内部,Map 类型是使用严格树实现的。要么评估整个树干,要么不评估。这是库代码的片段:

data Map k a  = Bin {-# UNPACK #-} !Size !k a !(Map k a) !(Map k a)
| Tip

严格注释 (!) 防止未计算的值存储为子树。因此,如果我们将 Map k a 值评估为弱头范式,我们实际上完全评估了树干。

关于haskell - Simon Marlow 书中的严格评估示例 "Parallel and Concurrent Programming in Haskell",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67519876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com