1{-# OPTIONS_GHC -Wall #-}
2module Transform.SortDefinitions (sortDefs) where
4import Control.Monad.State
5import Control.Applicative ((<$>),(<*>))
6import qualified Data.Graph as Graph
7import qualified Data.Map as Map
8import qualified Data.Maybe as Maybe
9import qualified Data.Set as Set
12import AST.Expression.General (Expr'(..))
13import qualified AST.Expression.Canonical as Canonical
14import qualified AST.Pattern as P
15import qualified AST.Variable as V
18ctors :: P.CanonicalPattern -> [String]
22 P.Alias _ p -> ctors p
26 P.Data (V.Canonical home name) ps ->
28 V.Local -> name : rest
32 rest = concatMap ctors ps
35free :: String -> State (Set.Set String) ()
40freeIfLocal :: V.Canonical -> State (Set.Set String) ()
41freeIfLocal (V.Canonical home name) =
44 V.BuiltIn -> return ()
45 V.Module _ -> return ()
48bound :: Set.Set String -> State (Set.Set String) ()
50 modify (\freeVars -> Set.difference freeVars boundVars)
53sortDefs :: Canonical.Expr -> Canonical.Expr
55 evalState (reorder expression) Set.empty
58reorder :: Canonical.Expr -> State (Set.Set String) Canonical.Expr
59reorder (A ann expression) =
62 -- Be careful adding and restricting freeVars
67 Lambda pattern body ->
68 uncurry Lambda <$> bindingReorder (pattern,body)
70 Binop op leftExpr rightExpr ->
72 Binop op <$> reorder leftExpr <*> reorder rightExpr
75 Case <$> reorder expr <*> mapM bindingReorder cases
79 Data name <$> mapM reorder exprs
81 -- Just pipe the reorder though
85 Range lowExpr highExpr ->
86 Range <$> reorder lowExpr <*> reorder highExpr
89 ExplicitList <$> mapM reorder es
92 App <$> reorder func <*> reorder arg
95 MultiIf <$> mapM (\(cond,branch) -> (,) <$> reorder cond <*> reorder branch) branches
97 Access record field ->
98 Access <$> reorder record <*> return field
100 Remove record field ->
101 Remove <$> reorder record <*> return field
103 Insert record field expr ->
104 Insert <$> reorder record <*> return field <*> reorder expr
106 Modify record fields ->
109 <*> mapM (\(field,expr) -> (,) field <$> reorder expr) fields
113 <$> mapM (\(field,expr) -> (,) field <$> reorder expr) fields
119 return $ Input name tipe
121 Output name tipe signal ->
122 Output name tipe <$> reorder signal
124 Loopback name tipe maybeExpr ->
125 Loopback name tipe <$>
126 maybe (return Nothing) (fmap Just . reorder) maybeExpr
128 -- Actually do some reordering
130 do body' <- reorder body
132 -- Sort defs into strongly connected components.This
133 -- allows the programmer to write definitions in whatever
134 -- order they please, we can still define things in order
135 -- and generalize polymorphic functions when appropriate.
136 sccs <- Graph.stronglyConnComp <$> buildDefDict defs
137 let defss = map Graph.flattenSCC sccs
139 -- remove let-bound variables from the context
140 forM_ defs $ \(Canonical.Definition pattern _ _) -> do
141 bound (P.boundVars pattern)
142 mapM free (ctors pattern)
145 foldr (\ds bod -> A ann (Let ds bod)) body' defss
151 :: (P.CanonicalPattern, Canonical.Expr)
152 -> State (Set.Set String) (P.CanonicalPattern, Canonical.Expr)
153bindingReorder (pattern,expr) =
154 do expr' <- reorder expr
155 bound (P.boundVars pattern)
156 mapM_ free (ctors pattern)
157 return (pattern, expr')
160reorderAndGetDependencies
162 -> State (Set.Set String) (Canonical.Def, [String])
163reorderAndGetDependencies (Canonical.Definition pattern expr mType) =
164 do globalFrees <- get
165 -- work in a fresh environment
167 expr' <- reorder expr
169 -- merge with global frees
170 modify (Set.union globalFrees)
171 return (Canonical.Definition pattern expr' mType, Set.toList localFrees)
174-- This also reorders the all of the sub-expressions in the Def list.
177 -> State (Set.Set String) [(Canonical.Def, Int, [Int])]
179 do pdefsDeps <- mapM reorderAndGetDependencies defs
180 return $ realDeps (addKey pdefsDeps)
182 addKey :: [(Canonical.Def, [String])] -> [(Canonical.Def, Int, [String])]
184 zipWith (\n (pdef,deps) -> (pdef,n,deps)) [0..]
186 variableToKey :: (Canonical.Def, Int, [String]) -> [(String, Int)]
187 variableToKey (Canonical.Definition pattern _ _, key, _) =
188 [ (var, key) | var <- Set.toList (P.boundVars pattern) ]
190 variableToKeyMap :: [(Canonical.Def, Int, [String])] -> Map.Map String Int
191 variableToKeyMap pdefsDeps =
192 Map.fromList (concatMap variableToKey pdefsDeps)
194 realDeps :: [(Canonical.Def, Int, [String])] -> [(Canonical.Def, Int, [Int])]
196 map convert pdefsDeps
198 varDict = variableToKeyMap pdefsDeps
199 convert (pdef, key, deps) =
200 (pdef, key, Maybe.mapMaybe (flip Map.lookup varDict) deps)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list