连续几天我一直在抨击 Haskell 空间泄漏(自然是堆栈溢出类型)。这令人沮丧,因为我试图直接从 CLR 模仿 BFS 算法,这不是自然递归的。注意:我已经启用了 BangPatterns,并且我在每个可能去的地方都放了一个 bang,试图对这个问题进行分支和绑定,但没有任何效果。我以前曾与空间泄漏作斗争,我不愿意放弃并为此而哭泣寻求帮助,但此时我被卡住了。我喜欢在 Haskell 中编码,并且非常了解函数式编程的禅宗,但调试空间泄漏与在满是图钉的地板上滚来滚去一样有趣。
也就是说,我的问题似乎是典型的“蓄能器”类型的空间泄漏。在下面的代码中,堆栈显然是围绕对 bfs' 的调用而建立的。非常感谢任何空间泄漏的提示。
import qualified Data.Map as M
import qualified Data.IntSet as IS
import qualified Data.Sequence as S
import qualified Data.List as DL
data BfsColor = White | Gray | Black deriving Show
data Node =
Node {
neighbors :: !IS.IntSet,
color :: !BfsColor,
depth :: !Int
}
type NodeID = Int
type NodeQueue = S.Seq NodeID
type Graph = M.Map NodeID Node
bfs :: Graph -> NodeID -> Graph
bfs graph start_node =
bfs' (S.singleton start_node) graph
bfs' :: NodeQueue -> Graph -> Graph
bfs' !queue !graph
| S.null queue = graph
| otherwise =
let (u,q1) = pop_left queue
Node children _ n = graph M.! u
(g2,q2) = IS.fold (enqueue_child_at_depth $ n+1) (graph,q1) children
g3 = set_color u Black g2
in bfs' q2 g3
enqueue_child_at_depth :: Int -> NodeID -> (Graph, NodeQueue)
-> (Graph, NodeQueue)
enqueue_child_at_depth depth child (graph,!queue) =
case get_color child graph of
White -> (set_color child Gray $ set_depth child depth graph,
queue S.|> child)
otherwise -> (graph,queue)
pop_left :: NodeQueue -> (NodeID, NodeQueue)
pop_left queue =
let (a,b) = S.splitAt 1 queue
in (a `S.index` 0, b)
set_color :: NodeID -> BfsColor -> Graph -> Graph
set_color node_id c graph =
M.adjust (\node -> node{color=c}) node_id graph
get_color :: NodeID -> Graph -> BfsColor
get_color node_id graph = color $ graph M.! node_id
set_depth :: NodeID -> Int -> Graph -> Graph
set_depth node_id d graph =
M.adjust (\node -> node{depth=d}) node_id graph