on fbf8daf help sign in
elk/compiler/Transform/SortDefinitions.hs
154 lines 5.0 kB history raw
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]
16ctors pattern =
17 case pattern of
18 P.Var _ -> []
19 P.Alias _ p -> ctors p
20 P.Data ctor ps -> ctor : concatMap ctors ps
21 P.Record _ -> []
22 P.Anything -> []
23 P.Literal _ -> []
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
35reorder (A ann expr) =
36 A ann <$>
37 case expr of
38 -- Be careful adding and restricting freeVars
39 Var (V.Raw x) -> free x >> return expr
41 Lambda p e ->
42 uncurry Lambda <$> bindingReorder (p,e)
44 Binop op e1 e2 ->
45 do free op
46 Binop op <$> reorder e1 <*> reorder e2
48 Case e cases ->
49 Case <$> reorder e <*> mapM bindingReorder cases
51 Data name es ->
52 do free name
53 Data name <$> mapM reorder es
55 -- Just pipe the reorder though
56 Literal _ -> return expr
58 Range e1 e2 ->
59 Range <$> reorder e1 <*> reorder e2
61 ExplicitList es ->
62 ExplicitList <$> mapM reorder es
64 App e1 e2 ->
65 App <$> reorder e1 <*> reorder e2
67 MultiIf branches ->
68 MultiIf <$> mapM (\(e1,e2) -> (,) <$> reorder e1 <*> reorder e2) branches
70 Access e lbl ->
71 Access <$> reorder e <*> return lbl
73 Remove e lbl ->
74 Remove <$> reorder e <*> return lbl
76 Insert e lbl v ->
77 Insert <$> reorder e <*> return lbl <*> reorder v
79 Modify e fields ->
80 Modify <$> reorder e <*> mapM (\(k,v) -> (,) k <$> reorder v) fields
82 Record 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
92 Let defs body ->
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
109 return let'
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
123 put Set.empty
124 expr' <- reorder expr
125 localFrees <- get
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])]
133buildDefDict defs =
134 do pdefsDeps <- mapM reorderAndGetDependencies defs
135 return $ realDeps (addKey pdefsDeps)
137 where
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
151 where
152 varDict = variableToKeyMap pdefsDeps
153 convert (pdef, key, deps) =
154 (pdef, key, Maybe.mapMaybe (flip Map.lookup varDict) deps)