From: Mart Lubbers Date: Mon, 18 Mar 2019 14:13:41 +0000 (+0100) Subject: cleanup X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=194b34ae4216c864d341f2dfe2055d0f3c09ed33;p=minfp.git cleanup --- diff --git a/scc.icl b/scc.icl index 8c879cf..0014f50 100644 --- a/scc.icl +++ b/scc.icl @@ -3,36 +3,35 @@ implementation module scc import StdEnv, StdMaybe import Data.Map => qualified updateAt -:: St a = {nextindex :: Int, stack :: [a], map :: Map a Annot, sccs :: [[a]]} +:: St a = {nextindex :: Int, stack :: [a], map :: Map a Annot, sccs :: [[a]]} :: Annot = {index :: Int, lowlink :: Int, onstack :: Bool} scc :: [(a, [a])] -> [[a]] | Eq, Ord a -scc nodes = reverse (foldr strongconnect {nextindex=0,stack=[],map=newMap,sccs=[]} nodes).sccs +scc nodes = reverse (foldr (strongconnect nodes) {nextindex=zero,stack=[],map=newMap,sccs=[]} nodes).sccs where -// strongconnect :: (a, [a]) (St a) -> St a | Eq, Ord a - strongconnect (v, suc) s + strongconnect :: [(a, [a])] (a, [a]) (St a) -> St a | Eq, Ord a + strongconnect nodes (v, suc) s | isJust (get v s.map) = s - # s = foldr processSucc + # s = foldr (processSucc nodes v) { s & map = put v {index=s.nextindex, lowlink=s.nextindex, onstack=True} s.map , stack = [v:s.stack] - , nextindex = s.nextindex + 1 + , nextindex = inc s.nextindex } suc # (Just a) = get v s.map - | a.index == a.lowlink - # (scc, [sl:stack]) = span ((<>) v) s.stack - # scc = scc ++ [sl] - = { s & sccs = [scc:s.sccs] - , stack = stack - , map = foldr (alter \(Just s)->Just {s & onstack=False}) s.map scc - } - = s + | a.index <> a.lowlink = s + # (scc, [sl:stack]) = span ((<>) v) s.stack + # scc = scc ++ [sl] + = { s & sccs = [scc:s.sccs] + , stack = stack + , map = foldr (alter \(Just s)->Just {s & onstack=False}) s.map scc + } where -// processSucc :: a (St a) -> St a | Eq, Ord a - processSucc w s = case get w s.map of + processSucc :: [(a, [a])] a a (St a) -> St a | Eq, Ord a + processSucc nodes v w s = case get w s.map of Nothing - # n = [l\\l=:(n, _)<-nodes | n == w] + # n = filter ((==)w o fst) nodes | n =: [] = s - # s = strongconnect (hd n) s + # s = strongconnect nodes (hd n) s # (Just aw) = get w s.map # (Just av) = get v s.map = {s & map=put v {av & lowlink=min av.lowlink aw.lowlink} s.map}