@@ -1,5 +1,5 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2−module Transform.SortDefinitions (sortDefs) where
2+module Canonicalize.Sort (definitions) where
4import Control.Monad.State
4import Control.Monad.State
5import Control.Applicative ((<$>),(<*>))
5import Control.Applicative ((<$>),(<*>))
@@ -15,31 +15,11 @@ import qualified AST.Variable as V
15import qualified Reporting.Annotation as A
15import qualified Reporting.Annotation as A
18−ctors :: P.CanonicalPattern -> [String]
18+-- STARTING POINT
19−ctors (A.A _ pattern) =
36− P.Data (V.Canonical home name) ps ->
20+definitions :: Canonical.Expr -> Canonical.Expr
37− case home of
21+definitions expression =
38− V.Local -> name : rest
22+ evalState (reorder expression) Set.empty
42− rest = concatMap ctors ps
45free :: String -> State (Set.Set String) ()
25free :: String -> State (Set.Set String) ()
@@ -55,17 +35,7 @@ freeIfLocal (V.Canonical home name) =
55 V.Module _ -> return ()
35 V.Module _ -> return ()
58−bound :: P.CanonicalPattern -> State (Set.Set String) ()
38+-- REORDER EXPRESSIONS
60− let boundVars = P.boundVarSet pattern
62− modify (\freeVars -> Set.difference freeVars boundVars)
65−sortDefs :: Canonical.Expr -> Canonical.Expr
67− evalState (reorder expression) Set.empty
70reorder :: Canonical.Expr -> State (Set.Set String) Canonical.Expr
40reorder :: Canonical.Expr -> State (Set.Set String) Canonical.Expr
71reorder (A.A ann expression) =
41reorder (A.A ann expression) =
@@ -147,7 +117,7 @@ reorder (A.A ann expression) =
147 -- allows the programmer to write definitions in whatever
117 -- allows the programmer to write definitions in whatever
148 -- order they please, we can still define things in order
118 -- order they please, we can still define things in order
149 -- and generalize polymorphic functions when appropriate.
119 -- and generalize polymorphic functions when appropriate.
150− sccs <- Graph.stronglyConnComp <$> buildDefDict defs
120+ sccs <- Graph.stronglyConnComp <$> buildDefGraph defs
151 let defss = map Graph.flattenSCC sccs
121 let defss = map Graph.flattenSCC sccs
153 -- remove let-bound variables from the context
123 -- remove let-bound variables from the context
@@ -161,35 +131,57 @@ reorder (A.A ann expression) =
161 return let'
131 return let'
134+ctors :: P.CanonicalPattern -> [String]
135+ctors (A.A _ pattern) =
152+ P.Data (V.Canonical home name) ps ->
154+ V.Local -> name : rest
158+ rest = concatMap ctors ps
161+bound :: P.CanonicalPattern -> State (Set.Set String) ()
163+ let boundVars = P.boundVarSet pattern
165+ modify (\freeVars -> Set.difference freeVars boundVars)
164bindingReorder
168bindingReorder
165 :: (P.CanonicalPattern, Canonical.Expr)
169 :: (P.CanonicalPattern, Canonical.Expr)
166 -> State (Set.Set String) (P.CanonicalPattern, Canonical.Expr)
170 -> State (Set.Set String) (P.CanonicalPattern, Canonical.Expr)
167−bindingReorder (pattern,expr) =
171+bindingReorder (pattern, expr) =
168 do expr' <- reorder expr
172 do expr' <- reorder expr
169 bound pattern
173 bound pattern
170 mapM_ free (ctors pattern)
174 mapM_ free (ctors pattern)
171 return (pattern, expr')
175 return (pattern, expr')
174−reorderAndGetDependencies
178+-- BUILD DEPENDENCY GRAPH BETWEEN DEFINITIONS
176− -> State (Set.Set String) (Canonical.Def, [String])
177−reorderAndGetDependencies (Canonical.Definition pattern expr mType) =
178− do globalFrees <- get
179− -- work in a fresh environment
181− expr' <- reorder expr
183− -- merge with global frees
184− modify (Set.union globalFrees)
185− return (Canonical.Definition pattern expr' mType, Set.toList localFrees)
188-- This also reorders the all of the sub-expressions in the Def list.
180-- This also reorders the all of the sub-expressions in the Def list.
189−buildDefDict
181+buildDefGraph
190 :: [Canonical.Def]
182 :: [Canonical.Def]
191 -> State (Set.Set String) [(Canonical.Def, Int, [Int])]
183 -> State (Set.Set String) [(Canonical.Def, Int, [Int])]
192−buildDefDict defs =
184+buildDefGraph defs =
193 do pdefsDeps <- mapM reorderAndGetDependencies defs
185 do pdefsDeps <- mapM reorderAndGetDependencies defs
194 return $ realDeps (addKey pdefsDeps)
186 return $ realDeps (addKey pdefsDeps)
@@ -212,3 +204,17 @@ buildDefDict defs =
212 varDict = variableToKeyMap pdefsDeps
204 varDict = variableToKeyMap pdefsDeps
213 convert (pdef, key, deps) =
205 convert (pdef, key, deps) =
214 (pdef, key, Maybe.mapMaybe (flip Map.lookup varDict) deps)
206 (pdef, key, Maybe.mapMaybe (flip Map.lookup varDict) deps)
209+reorderAndGetDependencies
211+ -> State (Set.Set String) (Canonical.Def, [String])
212+reorderAndGetDependencies (Canonical.Definition pattern expr mType) =
213+ do globalFrees <- get
214+ -- work in a fresh environment
216+ expr' <- reorder expr
218+ -- merge with global frees
219+ modify (Set.union globalFrees)
220+ return (Canonical.Definition pattern expr' mType, Set.toList localFrees)