1{-# OPTIONS_GHC -Wall #-}
2module Transform.SortDefinitions (sortDefs) where
4import Control.Monad.State
5import Control.Applicative ((<$>),(<*>))
6import qualified Data.Map as Map
7import SourceSyntax.Expression
8import SourceSyntax.Location
9import qualified SourceSyntax.Pattern as P
10import qualified Data.Graph as Graph
11import qualified Data.Set as Set
12import qualified Data.Maybe as Maybe
14ctors :: P.Pattern -> [String]
18 P.PAlias _ p -> ctors p
19 P.PData ctor ps -> ctor : concatMap ctors ps
24free :: String -> State (Set.Set String) ()
25free x = modify (Set.insert x)
27bound :: Set.Set String -> State (Set.Set String) ()
28bound boundVars = modify (\freeVars -> Set.difference freeVars boundVars)
30sortDefs :: LExpr -> LExpr
31sortDefs expr = evalState (reorder expr) Set.empty
33reorder :: LExpr -> State (Set.Set String) LExpr
37 -- Be careful adding and restricting freeVars
38 Var x -> free x >> return expr
41 uncurry Lambda <$> bindingReorder (p,e)
45 Binop op <$> reorder e1 <*> reorder e2
48 Case <$> reorder e <*> mapM bindingReorder cases
52 Data name <$> mapM reorder es
54 -- Just pipe the reorder though
55 Literal _ -> return expr
58 Range <$> reorder e1 <*> reorder e2
61 ExplicitList <$> mapM reorder es
64 App <$> reorder e1 <*> reorder e2
67 MultiIf <$> mapM (\(e1,e2) -> (,) <$> reorder e1 <*> reorder e2) branches
70 Access <$> reorder e <*> return lbl
73 Remove <$> reorder e <*> return lbl
76 Insert <$> reorder e <*> return lbl <*> reorder v
79 Modify <$> reorder e <*> mapM (\(k,v) -> (,) k <$> reorder v) fields
82 Record <$> mapM (\(k,v) -> (,) k <$> reorder v) fields
84 Markdown uid md es -> Markdown uid md <$> mapM reorder es
86 PortOut name st signal -> PortOut name st <$> reorder signal
88 PortIn name st tt handler -> PortIn name st tt <$> reorder handler
90 -- Actually do some reordering
92 do body' <- reorder body
94 -- Sort defs into strongly connected components.This
95 -- allows the programmer to write definitions in whatever
96 -- order they please, we can still define things in order
97 -- and generalize polymorphic functions when appropriate.
98 sccs <- Graph.stronglyConnComp <$> buildDefDict defs
99 let defss = map Graph.flattenSCC sccs
101 -- remove let-bound variables from the context
102 forM_ defs $ \(Definition pattern _ _) -> do
103 bound (P.boundVars pattern)
104 mapM free (ctors pattern)
106 let L _ let' = foldr (\ds bod -> L s (Let ds bod)) body' defss
110bindingReorder :: (P.Pattern, LExpr) -> State (Set.Set String) (P.Pattern, LExpr)
111bindingReorder (pattern,expr) =
112 do expr' <- reorder expr
113 bound (P.boundVars pattern)
114 mapM_ free (ctors pattern)
115 return (pattern, expr')
118reorderAndGetDependencies :: Def -> State (Set.Set String) (Def, [String])
119reorderAndGetDependencies (Definition pattern expr mType) =
120 do globalFrees <- get
121 -- work in a fresh environment
123 expr' <- reorder expr
125 -- merge with global frees
126 modify (Set.union globalFrees)
127 return (Definition pattern expr' mType, Set.toList localFrees)
130-- This also reorders the all of the sub-expressions in the Def list.
131buildDefDict :: [Def] -> State (Set.Set String) [(Def, Int, [Int])]
133 do pdefsDeps <- mapM reorderAndGetDependencies defs
134 return $ realDeps (addKey pdefsDeps)
137 addKey :: [(Def, [String])] -> [(Def, Int, [String])]
138 addKey = zipWith (\n (pdef,deps) -> (pdef,n,deps)) [0..]
140 variableToKey :: (Def, Int, [String]) -> [(String, Int)]
141 variableToKey (Definition pattern _ _, key, _) =
142 [ (var, key) | var <- Set.toList (P.boundVars pattern) ]
144 variableToKeyMap :: [(Def, Int, [String])] -> Map.Map String Int
145 variableToKeyMap pdefsDeps =
146 Map.fromList (concatMap variableToKey pdefsDeps)
148 realDeps :: [(Def, Int, [String])] -> [(Def, Int, [Int])]
149 realDeps pdefsDeps = map convert pdefsDeps
151 varDict = variableToKeyMap pdefsDeps
152 convert (pdef, key, deps) =
153 (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