diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-04-20 06:49:53 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-04-20 06:49:53 +0200 |
commit | 407e7257eb8b7a377103db2c0f632c6c8dec1fe3 (patch) | |
tree | c005690932ba23e8413223575e5c50ccf5ce4a4a /src | |
parent | 3ff3ed937f7a47e53d668dbaa3495d5b43fe8ea2 (diff) |
refactoring
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 16 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Parser.hs | 141 |
2 files changed, 76 insertions, 81 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index cc03e5fb..560bee3c 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -515,6 +515,14 @@ varType err n_ env = f n_ env where | |||
515 | f n (ELet2 _ (x, t) es) = if n == 0 then (BLam Visible{-??-}, up 1 t) else second (up 1) $ f (n-1) es | 515 | f n (ELet2 _ (x, t) es) = if n == 0 then (BLam Visible{-??-}, up 1 t) else second (up 1) $ f (n-1) es |
516 | f n e = either (error $ "varType: " ++ err ++ "\n" ++ show n_ ++ "\n" ++ ppShow env) (f n) $ parent e | 516 | f n e = either (error $ "varType: " ++ err ++ "\n" ++ show n_ ++ "\n" ++ ppShow env) (f n) $ parent e |
517 | 517 | ||
518 | substS :: Int -> ExpType -> SExp2 -> SExp2 | ||
519 | substS j x = mapS__ (\_ _ _ -> error "substS: TODO") (const . SGlobal) f2 ((+1) *** up 1) (j, x) | ||
520 | where | ||
521 | f2 sn i (j, x) = case compare i j of | ||
522 | GT -> SVar sn $ i - 1 | ||
523 | LT -> SVar sn i | ||
524 | EQ -> STyped (fst sn) x | ||
525 | |||
518 | -------------------------------------------------------------------------------- reduction | 526 | -------------------------------------------------------------------------------- reduction |
519 | evalCaseFun a ps (Con n@(ConName _ i _) _ vs) | 527 | evalCaseFun a ps (Con n@(ConName _ i _) _ vs) |
520 | | i /= (-1) = foldl app_ (ps !!! (i + 1)) vs | 528 | | i /= (-1) = foldl app_ (ps !!! (i + 1)) vs |
@@ -934,8 +942,8 @@ inferN_ tellTrace = infer where | |||
934 | STyped si et -> focus_' te exp et | 942 | STyped si et -> focus_' te exp et |
935 | SGlobal (si, s) -> focus_' te exp =<< getDef te si s | 943 | SGlobal (si, s) -> focus_' te exp =<< getDef te si s |
936 | SLet le a b -> infer (ELet1 le te b{-in-}) a{-let-} -- infer te SLamV b `SAppV` a) | 944 | SLet le a b -> infer (ELet1 le te b{-in-}) a{-let-} -- infer te SLamV b `SAppV` a) |
937 | SApp si h a b -> infer (EApp1 (si `validate` [sourceInfo a, sourceInfo b]) h te b) a | 945 | SApp_ si h a b -> infer (EApp1 (si `validate` [sourceInfo a, sourceInfo b]) h te b) a |
938 | SBind si h _ a b -> infer ((if h /= BMeta then CheckType_ (sourceInfo exp) TType else id) $ EBind1 si h te $ (if isPi h then TyType else id) b) a | 946 | SBind_ si h _ a b -> infer ((if h /= BMeta then CheckType_ (sourceInfo exp) TType else id) $ EBind1 si h te $ (if isPi h then TyType else id) b) a |
939 | 947 | ||
940 | checkN :: Env -> SExp2 -> Type -> IM m ExpType' | 948 | checkN :: Env -> SExp2 -> Type -> IM m ExpType' |
941 | checkN te x t = tellTrace "check" (showEnvSExpType te x t) $ checkN_ te x t | 949 | checkN te x t = tellTrace "check" (showEnvSExpType te x t) $ checkN_ te x t |
@@ -959,7 +967,7 @@ inferN_ tellTrace = infer where | |||
959 | = infer te $ x `SAppV` SLamV (SLamV (STyped mempty (subst (n'+2) (Var 1) $ up1_ (n'+3) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` SVar siv v | 967 | = infer te $ x `SAppV` SLamV (SLamV (STyped mempty (subst (n'+2) (Var 1) $ up1_ (n'+3) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` SVar siv v |
960 | -} | 968 | -} |
961 | | SRHS x <- e = checkN (ELabelEnd te) x t | 969 | | SRHS x <- e = checkN (ELabelEnd te) x t |
962 | | SApp si h a b <- e = infer (CheckAppType si h t te b) a | 970 | | SApp_ si h a b <- e = infer (CheckAppType si h t te b) a |
963 | | SLam h a b <- e, Pi h' x y <- t, h == h' = do | 971 | | SLam h a b <- e, Pi h' x y <- t, h == h' = do |
964 | tellType e t | 972 | tellType e t |
965 | let same = checkSame te a x | 973 | let same = checkSame te a x |
@@ -989,7 +997,7 @@ inferN_ tellTrace = infer where | |||
989 | checkSame te (Wildcard _) a = True | 997 | checkSame te (Wildcard _) a = True |
990 | checkSame te (SGlobal (_,"'Type")) TType = True | 998 | checkSame te (SGlobal (_,"'Type")) TType = True |
991 | checkSame te SType TType = True | 999 | checkSame te SType TType = True |
992 | checkSame te (SBind _ BMeta _ SType (STyped _ (Var 0, _))) a = True | 1000 | checkSame te (SBind_ _ BMeta _ SType (STyped _ (Var 0, _))) a = True |
993 | checkSame te a b = error $ "checkSame: " ++ show (a, b) | 1001 | checkSame te a b = error $ "checkSame: " ++ show (a, b) |
994 | 1002 | ||
995 | hArgs (Pi Hidden _ b) = 1 + hArgs b | 1003 | hArgs (Pi Hidden _ b) = 1 + hArgs b |
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs index d8ebd116..008ba116 100644 --- a/src/LambdaCube/Compiler/Parser.hs +++ b/src/LambdaCube/Compiler/Parser.hs | |||
@@ -21,7 +21,7 @@ module LambdaCube.Compiler.Parser | |||
21 | , Up (..), up1, up | 21 | , Up (..), up1, up |
22 | , Doc, shLam, shApp, shLet, shLet_, shAtom, shAnn, shVar, epar, showDoc, showDoc_, sExpDoc, shCstr, shTuple | 22 | , Doc, shLam, shApp, shLet, shLet_, shAtom, shAnn, shVar, epar, showDoc, showDoc_, sExpDoc, shCstr, shTuple |
23 | , mtrace | 23 | , mtrace |
24 | , trSExp', usedS, substSG0, substS | 24 | , trSExp', usedS, substSG0, mapS__ |
25 | , Stmt (..), Export (..), ImportItems (..) | 25 | , Stmt (..), Export (..), ImportItems (..) |
26 | , DesugarInfo | 26 | , DesugarInfo |
27 | ) where | 27 | ) where |
@@ -34,6 +34,8 @@ import Data.String | |||
34 | import Data.Function | 34 | import Data.Function |
35 | import qualified Data.Map as Map | 35 | import qualified Data.Map as Map |
36 | import qualified Data.Set as Set | 36 | import qualified Data.Set as Set |
37 | import qualified Data.IntMap as IM | ||
38 | import qualified Data.IntSet as IS | ||
37 | import Control.Monad.Except | 39 | import Control.Monad.Except |
38 | import Control.Monad.Reader | 40 | import Control.Monad.Reader |
39 | import Control.Monad.Writer | 41 | import Control.Monad.Writer |
@@ -80,8 +82,8 @@ try = try_ | |||
80 | data SExp' a | 82 | data SExp' a |
81 | = SLit SI Lit | 83 | = SLit SI Lit |
82 | | SGlobal SIName | 84 | | SGlobal SIName |
83 | | SApp SI Visibility (SExp' a) (SExp' a) | 85 | | SApp_ SI Visibility (SExp' a) (SExp' a) |
84 | | SBind SI Binder (SData SIName){-parameter name-} (SExp' a) (SExp' a) | 86 | | SBind_ SI Binder (SData SIName){-parameter name-} (SExp' a) (SExp' a) |
85 | | SVar_ (SData SIName) !Int | 87 | | SVar_ (SData SIName) !Int |
86 | | SLet_ SI (SData SIName) (SExp' a) (SExp' a) -- let x = e in f --> SLet e f{-x is Var 0-} | 88 | | SLet_ SI (SData SIName) (SExp' a) (SExp' a) -- let x = e in f --> SLet e f{-x is Var 0-} |
87 | | STyped SI a | 89 | | STyped SI a |
@@ -102,23 +104,23 @@ dummyName s = (debugSI s, "v_" ++ s) | |||
102 | dummyName' = SData . dummyName | 104 | dummyName' = SData . dummyName |
103 | sVar = SVar . dummyName | 105 | sVar = SVar . dummyName |
104 | 106 | ||
105 | pattern SBind' v x a b <- SBind _ v x a b | 107 | pattern SBind v x a b <- SBind_ _ v x a b |
106 | where SBind' v x a b = SBind (sourceInfo a <> sourceInfo b) v x a b | 108 | where SBind v x a b = SBind_ (sourceInfo a <> sourceInfo b) v x a b |
107 | pattern SPi h a b <- SBind' (BPi h) _ a b | 109 | pattern SPi h a b <- SBind (BPi h) _ a b |
108 | where SPi h a b = SBind' (BPi h) (dummyName' "SPi") a b | 110 | where SPi h a b = SBind (BPi h) (dummyName' "SPi") a b |
109 | pattern SLam h a b <- SBind' (BLam h) _ a b | 111 | pattern SLam h a b <- SBind (BLam h) _ a b |
110 | where SLam h a b = SBind' (BLam h) (dummyName' "SLam") a b | 112 | where SLam h a b = SBind (BLam h) (dummyName' "SLam") a b |
111 | pattern Wildcard t <- SBind' BMeta _ t (SVar _ 0) | 113 | pattern Wildcard t <- SBind BMeta _ t (SVar _ 0) |
112 | where Wildcard t = SBind' BMeta (dummyName' "Wildcard") t (sVar "Wildcard2" 0) | 114 | where Wildcard t = SBind BMeta (dummyName' "Wildcard") t (sVar "Wildcard2" 0) |
113 | pattern SLet n a b <- SLet_ _ (SData n) a b | 115 | pattern SLet n a b <- SLet_ _ (SData n) a b |
114 | where SLet n a b = SLet_ (sourceInfo a <> sourceInfo b) (SData n) a b | 116 | where SLet n a b = SLet_ (sourceInfo a <> sourceInfo b) (SData n) a b |
115 | pattern SLamV a = SLam Visible (Wildcard SType) a | 117 | pattern SLamV a = SLam Visible (Wildcard SType) a |
116 | pattern SVar a b = SVar_ (SData a) b | 118 | pattern SVar a b = SVar_ (SData a) b |
117 | 119 | ||
118 | pattern SApp' h a b <- SApp _ h a b | 120 | pattern SApp h a b <- SApp_ _ h a b |
119 | where SApp' h a b = SApp (sourceInfo a <> sourceInfo b) h a b | 121 | where SApp h a b = SApp_ (sourceInfo a <> sourceInfo b) h a b |
120 | pattern SAppH a b = SApp' Hidden a b | 122 | pattern SAppH a b = SApp Hidden a b |
121 | pattern SAppV a b = SApp' Visible a b | 123 | pattern SAppV a b = SApp Visible a b |
122 | pattern SAppV2 f a b = f `SAppV` a `SAppV` b | 124 | pattern SAppV2 f a b = f `SAppV` a `SAppV` b |
123 | 125 | ||
124 | infixl 2 `SAppV`, `SAppH` | 126 | infixl 2 `SAppV`, `SAppH` |
@@ -147,10 +149,10 @@ getParamsS x = ([], x) | |||
147 | 149 | ||
148 | pattern AppsS :: SExp' a -> [(Visibility, SExp' a)] -> SExp' a | 150 | pattern AppsS :: SExp' a -> [(Visibility, SExp' a)] -> SExp' a |
149 | pattern AppsS f args <- (getApps -> (f, args)) | 151 | pattern AppsS f args <- (getApps -> (f, args)) |
150 | where AppsS = foldl $ \a (v, b) -> SApp' v a b | 152 | where AppsS = foldl $ \a (v, b) -> SApp v a b |
151 | 153 | ||
152 | getApps = second reverse . run where | 154 | getApps = second reverse . run where |
153 | run (SApp _ h a b) = second ((h, b):) $ run a | 155 | run (SApp h a b) = second ((h, b):) $ run a |
154 | run x = (x, []) | 156 | run x = (x, []) |
155 | 157 | ||
156 | -- todo: remove | 158 | -- todo: remove |
@@ -159,8 +161,8 @@ downToS err n m = [sVar (err ++ "_" ++ show i) (n + i) | i <- [m-1, m-2..0]] | |||
159 | instance SourceInfo (SExp' a) where | 161 | instance SourceInfo (SExp' a) where |
160 | sourceInfo = \case | 162 | sourceInfo = \case |
161 | SGlobal (si, _) -> si | 163 | SGlobal (si, _) -> si |
162 | SBind si _ _ _ _ -> si | 164 | SBind_ si _ _ _ _ -> si |
163 | SApp si _ _ _ -> si | 165 | SApp_ si _ _ _ -> si |
164 | SLet_ si _ _ _ -> si | 166 | SLet_ si _ _ _ -> si |
165 | SVar (si, _) _ -> si | 167 | SVar (si, _) _ -> si |
166 | STyped si _ -> si | 168 | STyped si _ -> si |
@@ -168,8 +170,8 @@ instance SourceInfo (SExp' a) where | |||
168 | 170 | ||
169 | instance SetSourceInfo (SExp' a) where | 171 | instance SetSourceInfo (SExp' a) where |
170 | setSI si = \case | 172 | setSI si = \case |
171 | SBind _ a b c d -> SBind si a b c d | 173 | SBind_ _ a b c d -> SBind_ si a b c d |
172 | SApp _ a b c -> SApp si a b c | 174 | SApp_ _ a b c -> SApp_ si a b c |
173 | SLet_ _ le a b -> SLet_ si le a b | 175 | SLet_ _ le a b -> SLet_ si le a b |
174 | SVar (_, n) i -> SVar (si, n) i | 176 | SVar (_, n) i -> SVar (si, n) i |
175 | STyped _ t -> STyped si t | 177 | STyped _ t -> STyped si t |
@@ -212,9 +214,9 @@ foldS | |||
212 | foldS h g f = fs | 214 | foldS h g f = fs |
213 | where | 215 | where |
214 | fs i = \case | 216 | fs i = \case |
215 | SApp _ _ a b -> fs i a <> fs i b | 217 | SApp _ a b -> fs i a <> fs i b |
216 | SLet _ a b -> fs i a <> fs (i+1) b | 218 | SLet _ a b -> fs i a <> fs (i+1) b |
217 | SBind _ _ _ a b -> fs i a <> fs (i+1) b | 219 | SBind_ _ _ _ a b -> fs i a <> fs (i+1) b |
218 | STyped si x -> h i si x | 220 | STyped si x -> h i si x |
219 | SVar sn j -> f sn j i | 221 | SVar sn j -> f sn j i |
220 | SGlobal sn -> g sn i | 222 | SGlobal sn -> g sn i |
@@ -236,22 +238,14 @@ mapS__ | |||
236 | -> SExp' a | 238 | -> SExp' a |
237 | mapS__ hh gg f2 h = g where | 239 | mapS__ hh gg f2 h = g where |
238 | g i = \case | 240 | g i = \case |
239 | SApp si v a b -> SApp si v (g i a) (g i b) | 241 | SApp_ si v a b -> SApp_ si v (g i a) (g i b) |
240 | SLet_ si x a b -> SLet_ si x (g i a) (g (h i) b) | 242 | SLet_ si x a b -> SLet_ si x (g i a) (g (h i) b) |
241 | SBind si k si' a b -> SBind si k si' (g i a) (g (h i) b) | 243 | SBind_ si k si' a b -> SBind_ si k si' (g i a) (g (h i) b) |
242 | SVar sn j -> f2 sn j i | 244 | SVar sn j -> f2 sn j i |
243 | SGlobal sn -> gg sn i | 245 | SGlobal sn -> gg sn i |
244 | STyped si x -> hh i si x | 246 | STyped si x -> hh i si x |
245 | x@SLit{} -> x | 247 | x@SLit{} -> x |
246 | 248 | ||
247 | substS :: Up a => Int -> a -> SExp' a -> SExp' a | ||
248 | substS j x = mapS__ (\_ _ _ -> error "substS: TODO") (const . SGlobal) f2 ((+1) *** up 1) (j, x) | ||
249 | where | ||
250 | f2 sn i (j, x) = case compare i j of | ||
251 | GT -> SVar sn $ i - 1 | ||
252 | LT -> SVar sn i | ||
253 | EQ -> STyped (fst sn) x | ||
254 | |||
255 | rearrangeS :: (Int -> Int) -> SExp -> SExp | 249 | rearrangeS :: (Int -> Int) -> SExp -> SExp |
256 | rearrangeS f = mapS__ (\_ _ -> elimVoid) (const . SGlobal) (\sn j i -> SVar sn $ if j < i then j else i + f (j - i)) (+1) 0 | 250 | rearrangeS f = mapS__ (\_ _ -> elimVoid) (const . SGlobal) (\sn j i -> SVar sn $ if j < i then j else i + f (j - i)) (+1) 0 |
257 | 251 | ||
@@ -266,9 +260,7 @@ instance Up Void where | |||
266 | fold _ _ = elimVoid | 260 | fold _ _ = elimVoid |
267 | 261 | ||
268 | instance Up a => Up (SExp' a) where | 262 | instance Up a => Up (SExp' a) where |
269 | up_ n = mapS' (\sn j i -> SVar sn $ if j < i then j else j+n) (+1) | 263 | up_ n = mapS__ (\i si x -> STyped si $ up_ n i x) (const . SGlobal) (\sn j i -> SVar sn $ if j < i then j else j+n) (+1) |
270 | where | ||
271 | mapS' = mapS__ (\i si x -> STyped si $ up_ n i x) (const . SGlobal) | ||
272 | 264 | ||
273 | fold f = foldS (\i si x -> fold f i x) mempty $ \sn j i -> f j i | 265 | fold f = foldS (\i si x -> fold f i x) mempty $ \sn j i -> f j i |
274 | 266 | ||
@@ -283,9 +275,9 @@ dbff ns e = foldr substSG0 e ns | |||
283 | trSExp :: (a -> b) -> SExp' a -> SExp' b | 275 | trSExp :: (a -> b) -> SExp' a -> SExp' b |
284 | trSExp f = g where | 276 | trSExp f = g where |
285 | g = \case | 277 | g = \case |
286 | SApp si v a b -> SApp si v (g a) (g b) | 278 | SApp_ si v a b -> SApp_ si v (g a) (g b) |
287 | SLet_ si x a b -> SLet_ si x (g a) (g b) | 279 | SLet_ si x a b -> SLet_ si x (g a) (g b) |
288 | SBind si k si' a b -> SBind si k si' (g a) (g b) | 280 | SBind_ si k si' a b -> SBind_ si k si' (g a) (g b) |
289 | SVar sn j -> SVar sn j | 281 | SVar sn j -> SVar sn j |
290 | SGlobal sn -> SGlobal sn | 282 | SGlobal sn -> SGlobal sn |
291 | SLit si l -> SLit si l | 283 | SLit si l -> SLit si l |
@@ -617,7 +609,7 @@ parsePat = \case | |||
617 | 609 | ||
618 | mkListPat TypeNS [p] = PCon (debugSI "mkListPat4", "'List") [ParPat [p]] | 610 | mkListPat TypeNS [p] = PCon (debugSI "mkListPat4", "'List") [ParPat [p]] |
619 | mkListPat ns (p: ps) = PCon (debugSI "mkListPat2", "Cons") $ map (ParPat . (:[])) [p, mkListPat ns ps] | 611 | mkListPat ns (p: ps) = PCon (debugSI "mkListPat2", "Cons") $ map (ParPat . (:[])) [p, mkListPat ns ps] |
620 | mkListPat _ [] = PCon (debugSI "mkListPat3", "Nil") [] | 612 | mkListPat _ [] = PCon (debugSI "mkListPat3", "Nil") [] |
621 | 613 | ||
622 | --mkTupPat :: [Pat] -> Pat | 614 | --mkTupPat :: [Pat] -> Pat |
623 | mkTupPat ns [PParens x] = ff [x] | 615 | mkTupPat ns [PParens x] = ff [x] |
@@ -941,38 +933,34 @@ defined defs = ("'Type":) $ flip foldMap defs $ \case | |||
941 | data StmtNode = StmtNode | 933 | data StmtNode = StmtNode |
942 | { snId :: !Int | 934 | { snId :: !Int |
943 | , snValue :: Stmt | 935 | , snValue :: Stmt |
944 | , snChildren :: Set.Set StmtNode | 936 | , snChildren :: [StmtNode] |
945 | , snRevChildren :: Set.Set StmtNode | 937 | , snRevChildren :: [StmtNode] |
946 | , snDef :: Set.Set SIName | ||
947 | } | 938 | } |
948 | 939 | ||
949 | instance Eq StmtNode where (==) = (==) `on` snId | ||
950 | instance Ord StmtNode where compare = compare `on` snId | ||
951 | |||
952 | sortDefs :: DesugarInfo -> [Stmt] -> [Stmt] | 940 | sortDefs :: DesugarInfo -> [Stmt] -> [Stmt] |
953 | sortDefs ds xs = concatMap (desugarMutual ds . map snValue) $ scc (Set.toList . snChildren) (Set.toList . snRevChildren) nodes | 941 | sortDefs ds xs = concatMap (desugarMutual ds . map snValue) $ scc snId snChildren snRevChildren nodes |
954 | where | 942 | where |
955 | nodes = zipWith4 mkNode [0..] xs (def <$> xs) (need <$> xs) | 943 | nodes = zipWith mkNode [0..] xs |
956 | |||
957 | mkNode i s def need = n | ||
958 | where | 944 | where |
959 | n = StmtNode i s (Set.fromList $ filter (not . Set.null . (`Set.intersection` need) . snDef) nodes) | 945 | mkNode i s = StmtNode i s (nubBy ((==) `on` snId) $ catMaybes $ (`Map.lookup` defMap) <$> Set.toList (need s)) |
960 | (Set.fromList $ filter (Set.member n . snChildren) nodes) | 946 | (fromMaybe [] $ IM.lookup i revMap) |
961 | def | 947 | |
962 | 948 | need :: Stmt -> Set.Set SIName | |
963 | need :: Stmt -> Set.Set SIName | 949 | need = \case |
964 | need = \case | 950 | PrecDef{} -> mempty |
965 | PrecDef{} -> mempty | 951 | Let _ mt e -> foldMap freeS' mt <> freeS' e |
966 | Let _ mt e -> foldMap freeS' mt <> freeS' e | 952 | Data _ ps t _ cs -> foldMap (freeS' . snd) ps <> freeS' t <> foldMap (freeS' . snd) cs |
967 | Data _ ps t _ cs -> foldMap (freeS' . snd) ps <> freeS' t <> foldMap (freeS' . snd) cs | 953 | where |
968 | where | 954 | freeS' = Set.fromList . freeS |
969 | freeS' = Set.fromList . freeS | ||
970 | 955 | ||
971 | def :: Stmt -> Set.Set SIName | 956 | revMap = mconcat [IM.singleton (snId c) [n] | n <- nodes, c <- snChildren n] |
972 | def = \case | 957 | |
973 | PrecDef{} -> mempty | 958 | defMap = Map.fromList [(s, n) | n <- nodes, s <- def $ snValue n] |
974 | Let n _ _ -> Set.singleton n | 959 | where |
975 | Data n _ _ _ cs -> Set.singleton n <> Set.fromList (map fst cs) | 960 | def = \case |
961 | PrecDef{} -> mempty | ||
962 | Let n _ _ -> [n] | ||
963 | Data n _ _ _ cs -> n: map fst cs | ||
976 | 964 | ||
977 | desugarMutual _ [x] = [x] | 965 | desugarMutual _ [x] = [x] |
978 | desugarMutual ds xs = xs | 966 | desugarMutual ds xs = xs |
@@ -997,31 +985,30 @@ type Children k = k -> [k] | |||
997 | 985 | ||
998 | data Task a = Return a | Visit a | 986 | data Task a = Return a | Visit a |
999 | 987 | ||
1000 | scc :: Ord k => Children k -> Children k -> [k]{-roots-} -> [[k]] | 988 | scc :: forall k . (k -> Int) -> Children k -> Children k -> [k]{-roots-} -> [[k]] |
1001 | scc children revChildren | 989 | scc key children revChildren |
1002 | = filter (not . null) . uncurry (revMapWalk revChildren) . revPostOrderWalk children | 990 | = filter (not . null) . uncurry (revMapWalk revChildren) . revPostOrderWalk children |
1003 | where | 991 | where |
1004 | revPostOrderWalk :: Ord k => Children k -> [k] -> (Set.Set k, [k]) | 992 | revPostOrderWalk :: Children k -> [k] -> (IS.IntSet, [k]) |
1005 | revPostOrderWalk children = collect Set.empty [] . map Visit where | 993 | revPostOrderWalk children = collect IS.empty [] . map Visit where |
1006 | 994 | ||
1007 | collect s acc [] = (s, acc) | 995 | collect s acc [] = (s, acc) |
1008 | collect s acc (Return h: t) = collect s (h: acc) t | 996 | collect s acc (Return h: t) = collect s (h: acc) t |
1009 | collect s acc (Visit h: t) | 997 | collect s acc (Visit h: t) |
1010 | | h `Set.member` s = collect s acc t | 998 | | key h `IS.member` s = collect s acc t |
1011 | | otherwise = collect (Set.insert h s) acc $ map Visit (children h) ++ Return h: t | 999 | | otherwise = collect (IS.insert (key h) s) acc $ map Visit (children h) ++ Return h: t |
1012 | 1000 | ||
1013 | revMapWalk :: Ord k => Children k -> Set.Set k -> [k] -> [[k]] | 1001 | revMapWalk :: Children k -> IS.IntSet -> [k] -> [[k]] |
1014 | revMapWalk children = f [] | 1002 | revMapWalk children = f [] |
1015 | where | 1003 | where |
1016 | f acc s [] = acc | 1004 | f acc s [] = acc |
1017 | f acc s (h:t) = f (c: acc) s' t | 1005 | f acc s (h:t) = f (c: acc) s' t |
1018 | where (s', c) = collect s [] [h] | 1006 | where (s', c) = collect s [] [h] |
1019 | 1007 | ||
1020 | -- collect :: Set a -> [a] -> [a] -> (Set a, [a]) | ||
1021 | collect s acc [] = (s, acc) | 1008 | collect s acc [] = (s, acc) |
1022 | collect s acc (h:t) | 1009 | collect s acc (h:t) |
1023 | | not (h `Set.member` s) = collect s acc t | 1010 | | not (key h `IS.member` s) = collect s acc t |
1024 | | otherwise = collect (Set.delete h s) (h: acc) (children h ++ t) | 1011 | | otherwise = collect (IS.delete (key h) s) (h: acc) (children h ++ t) |
1025 | 1012 | ||
1026 | 1013 | ||
1027 | ------------------------------------------------------------------------ | 1014 | ------------------------------------------------------------------------ |
@@ -1215,9 +1202,9 @@ sExpDoc = \case | |||
1215 | SGlobal (_,s) -> pure $ shAtom s | 1202 | SGlobal (_,s) -> pure $ shAtom s |
1216 | SAnn a b -> shAnn ":" False <$> sExpDoc a <*> sExpDoc b | 1203 | SAnn a b -> shAnn ":" False <$> sExpDoc a <*> sExpDoc b |
1217 | TyType a -> shApp Visible (shAtom "tyType") <$> sExpDoc a | 1204 | TyType a -> shApp Visible (shAtom "tyType") <$> sExpDoc a |
1218 | SApp _ h a b -> shApp h <$> sExpDoc a <*> sExpDoc b | 1205 | SApp h a b -> shApp h <$> sExpDoc a <*> sExpDoc b |
1219 | Wildcard t -> shAnn ":" True (shAtom "_") <$> sExpDoc t | 1206 | Wildcard t -> shAnn ":" True (shAtom "_") <$> sExpDoc t |
1220 | SBind _ h _ a b -> join $ shLam (used 0 b) h <$> sExpDoc a <*> pure (sExpDoc b) | 1207 | SBind_ _ h _ a b -> join $ shLam (used 0 b) h <$> sExpDoc a <*> pure (sExpDoc b) |
1221 | SLet _ a b -> shLet_ (sExpDoc a) (sExpDoc b) | 1208 | SLet _ a b -> shLet_ (sExpDoc a) (sExpDoc b) |
1222 | STyped _ _{-(e,t)-} -> pure $ shAtom "<<>>" -- todo: expDoc e | 1209 | STyped _ _{-(e,t)-} -> pure $ shAtom "<<>>" -- todo: expDoc e |
1223 | SVar _ i -> shAtom <$> shVar i | 1210 | SVar _ i -> shAtom <$> shVar i |