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
10import SourceSyntax.Annotation
11import SourceSyntax.Expression
12import qualified SourceSyntax.Pattern as P
13import qualified SourceSyntax.Variable as V
15ctors :: P.Pattern -> [String]
19 P.Alias _ p -> ctors p
20 P.Data ctor ps -> ctor : concatMap ctors ps
25free :: String -> State (Set.Set String) ()
26free x = modify (Set.insert x)
28bound :: Set.Set String -> State (Set.Set String) ()
29bound boundVars = modify (\freeVars -> Set.difference freeVars boundVars)
31sortDefs :: Expr -> Expr
32sortDefs expr = evalState (reorder expr) Set.empty
34reorder :: Expr -> State (Set.Set String) Expr
38 -- Be careful adding and restricting freeVars
39 Var (V.Raw x) -> free x >> return expr
42 uncurry Lambda <$> bindingReorder (p,e)
46 Binop op <$> reorder e1 <*> reorder e2
49 Case <$> reorder e <*> mapM bindingReorder cases
53 Data name <$> mapM reorder es
55 -- Just pipe the reorder though
56 Literal _ -> return expr
59 Range <$> reorder e1 <*> reorder e2
62 ExplicitList <$> mapM reorder es
65 App <$> reorder e1 <*> reorder e2
68 MultiIf <$> mapM (\(e1,e2) -> (,) <$> reorder e1 <*> reorder e2) branches
71 Access <$> reorder e <*> return lbl
74 Remove <$> reorder e <*> return lbl
77 Insert <$> reorder e <*> return lbl <*> reorder v
80 Modify <$> reorder e <*> mapM (\(k,v) -> (,) k <$> reorder v) fields
83 Record <$> mapM (\(k,v) -> (,) k <$> reorder v) fields
85 Markdown uid md es -> Markdown uid md <$> mapM reorder es
87 PortOut name st signal -> PortOut name st <$> reorder signal
89 PortIn name st -> return $ PortIn name st
91 -- Actually do some reordering
93 do body' <- reorder body
95 -- Sort defs into strongly connected components.This
96 -- allows the programmer to write definitions in whatever
97 -- order they please, we can still define things in order
98 -- and generalize polymorphic functions when appropriate.
99 sccs <- Graph.stronglyConnComp <$> buildDefDict defs
100 let defss = map Graph.flattenSCC sccs
102 -- remove let-bound variables from the context
103 forM_ defs $ \(Definition pattern _ _) -> do
104 bound (P.boundVars pattern)
105 mapM free (ctors pattern)
107 let A _ let' = foldr (\ds bod -> A ann (Let ds bod)) body' defss
111bindingReorder :: (P.Pattern, Expr) -> State (Set.Set String) (P.Pattern, Expr)
112bindingReorder (pattern,expr) =
113 do expr' <- reorder expr
114 bound (P.boundVars pattern)
115 mapM_ free (ctors pattern)
116 return (pattern, expr')
119reorderAndGetDependencies :: Def -> State (Set.Set String) (Def, [String])
120reorderAndGetDependencies (Definition pattern expr mType) =
121 do globalFrees <- get
122 -- work in a fresh environment
124 expr' <- reorder expr
126 -- merge with global frees
127 modify (Set.union globalFrees)
128 return (Definition pattern expr' mType, Set.toList localFrees)
131-- This also reorders the all of the sub-expressions in the Def list.
132buildDefDict :: [Def] -> State (Set.Set String) [(Def, Int, [Int])]
134 do pdefsDeps <- mapM reorderAndGetDependencies defs
135 return $ realDeps (addKey pdefsDeps)
138 addKey :: [(Def, [String])] -> [(Def, Int, [String])]
139 addKey = zipWith (\n (pdef,deps) -> (pdef,n,deps)) [0..]
141 variableToKey :: (Def, Int, [String]) -> [(String, Int)]
142 variableToKey (Definition pattern _ _, key, _) =
143 [ (var, key) | var <- Set.toList (P.boundVars pattern) ]
145 variableToKeyMap :: [(Def, Int, [String])] -> Map.Map String Int
146 variableToKeyMap pdefsDeps =
147 Map.fromList (concatMap variableToKey pdefsDeps)
149 realDeps :: [(Def, Int, [String])] -> [(Def, Int, [Int])]
150 realDeps pdefsDeps = map convert pdefsDeps
152 varDict = variableToKeyMap pdefsDeps
153 convert (pdef, key, deps) =
154 (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