on master help sign in
Move SortDefinitions into Canonicalize/
8c77cdbEvan Czaplicki
elm-compiler.cabal+1−1
@@ -61,6 +61,7 @@ Library
61 Canonicalize.Environment,
61 Canonicalize.Environment,
62 Canonicalize.Port,
62 Canonicalize.Port,
63 Canonicalize.Setup,
63 Canonicalize.Setup,
64+ Canonicalize.Sort,
64 Canonicalize.Type,
65 Canonicalize.Type,
65 Canonicalize.Variable,
66 Canonicalize.Variable,
66 Compile,
67 Compile,
@@ -73,7 +74,6 @@ Library
73 Generate.Cases,
74 Generate.Cases,
74 Transform.AddDefaultImports,
75 Transform.AddDefaultImports,
75 Transform.Interface,
76 Transform.Interface,
76− Transform.SortDefinitions,
77 Transform.Substitute,
77 Transform.Substitute,
78 Parse.Binop,
78 Parse.Binop,
79 Parse.Declaration,
79 Parse.Declaration,
src/Canonicalize.hs+2−2
@@ -27,9 +27,9 @@ import qualified Canonicalize.Environment as Env
27import qualified Canonicalize.Port as Port
27import qualified Canonicalize.Port as Port
28import qualified Canonicalize.Result as Result
28import qualified Canonicalize.Result as Result
29import qualified Canonicalize.Setup as Setup
29import qualified Canonicalize.Setup as Setup
30+import qualified Canonicalize.Sort as Sort
30import qualified Canonicalize.Type as Canonicalize
31import qualified Canonicalize.Type as Canonicalize
31import qualified Canonicalize.Variable as Canonicalize
32import qualified Canonicalize.Variable as Canonicalize
32−import qualified Transform.SortDefinitions as Transform
33import qualified Transform.Declaration as Transform
33import qualified Transform.Declaration as Transform
34
34
35
35
@@ -87,7 +87,7 @@ moduleHelp interfaces modul@(Module.Module _ _ exports (defaults, imports) decls
87 { program =
87 { program =
88 let expr = Transform.toExpr (Module.names modul) decls
88 let expr = Transform.toExpr (Module.names modul) decls
89 in
89 in
90− Transform.sortDefs (dummyLet expr)
90+ Sort.definitions (dummyLet expr)
91
91
92 , types =
92 , types =
93 Map.empty
93 Map.empty
src/Transform/SortDefinitions.hs → src/Canonicalize/Sort.hsrenamed+59−53
@@ -1,5 +1,5 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2−module Transform.SortDefinitions (sortDefs) where
2+module Canonicalize.Sort (definitions) where
3
3
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
16
16
17
17
18−ctors :: P.CanonicalPattern -> [String]
18+-- STARTING POINT
19−ctors (A.A _ pattern) =
20− case pattern of
21− P.Var _ ->
22− []
23−
24− P.Alias _ p ->
25− ctors p
26−
27− P.Record _ ->
28− []
29−
30− P.Anything ->
31− []
32−
33− P.Literal _ ->
34− []
35
19
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
39− V.BuiltIn -> rest
40− V.Module _ -> rest
41− where
42− rest = concatMap ctors ps
43
23
44
24
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 ()
56
36
57
37
58−bound :: P.CanonicalPattern -> State (Set.Set String) ()
38+-- REORDER EXPRESSIONS
59−bound pattern =
60− let boundVars = P.boundVarSet pattern
61− in
62− modify (\freeVars -> Set.difference freeVars boundVars)
63−
64−
65−sortDefs :: Canonical.Expr -> Canonical.Expr
66−sortDefs expression =
67− evalState (reorder expression) Set.empty
68−
69
39
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
152
122
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'
162
132
163
133
134+ctors :: P.CanonicalPattern -> [String]
135+ctors (A.A _ pattern) =
136+ case pattern of
137+ P.Var _ ->
138+ []
139+
140+ P.Alias _ p ->
141+ ctors p
142+
143+ P.Record _ ->
144+ []
145+
146+ P.Anything ->
147+ []
148+
149+ P.Literal _ ->
150+ []
151+
152+ P.Data (V.Canonical home name) ps ->
153+ case home of
154+ V.Local -> name : rest
155+ V.BuiltIn -> rest
156+ V.Module _ -> rest
157+ where
158+ rest = concatMap ctors ps
159+
160+
161+bound :: P.CanonicalPattern -> State (Set.Set String) ()
162+bound pattern =
163+ let boundVars = P.boundVarSet pattern
164+ in
165+ modify (\freeVars -> Set.difference freeVars boundVars)
166+
167+
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')
172
176
173
177
174−reorderAndGetDependencies
178+-- BUILD DEPENDENCY GRAPH BETWEEN DEFINITIONS
175− :: Canonical.Def
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
180− put Set.empty
181− expr' <- reorder expr
182− localFrees <- get
183− -- merge with global frees
184− modify (Set.union globalFrees)
185− return (Canonical.Definition pattern expr' mType, Set.toList localFrees)
186−
187
179
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)
195 where
187 where
@@ -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)
207+
208+
209+reorderAndGetDependencies
210+ :: Canonical.Def
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
215+ put Set.empty
216+ expr' <- reorder expr
217+ localFrees <- get
218+ -- merge with global frees
219+ modify (Set.union globalFrees)
220+ return (Canonical.Definition pattern expr' mType, Set.toList localFrees)