on ade0339 help sign in
elk/compiler/Transform/SortDefinitions.hs
149 lines 4.8 kB history raw
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]
15ctors pattern =
16 case pattern of
17 P.PVar _ -> []
18 P.PAlias _ p -> ctors p
19 P.PData ctor ps -> ctor : concatMap ctors ps
20 P.PRecord _ -> []
21 P.PAnything -> []
22 P.PLiteral _ -> []
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
34reorder (L s expr) =
35 L s <$>
36 case expr of
37 -- Be careful adding and restricting freeVars
38 Var x -> free x >> return expr
40 Lambda p e ->
41 uncurry Lambda <$> bindingReorder (p,e)
43 Binop op e1 e2 ->
44 do free op
45 Binop op <$> reorder e1 <*> reorder e2
47 Case e cases ->
48 Case <$> reorder e <*> mapM bindingReorder cases
50 Data name es ->
51 do free name
52 Data name <$> mapM reorder es
54 -- Just pipe the reorder though
55 Literal _ -> return expr
57 Range e1 e2 ->
58 Range <$> reorder e1 <*> reorder e2
60 ExplicitList es ->
61 ExplicitList <$> mapM reorder es
63 App e1 e2 ->
64 App <$> reorder e1 <*> reorder e2
66 MultiIf branches ->
67 MultiIf <$> mapM (\(e1,e2) -> (,) <$> reorder e1 <*> reorder e2) branches
69 Access e lbl ->
70 Access <$> reorder e <*> return lbl
72 Remove e lbl ->
73 Remove <$> reorder e <*> return lbl
75 Insert e lbl v ->
76 Insert <$> reorder e <*> return lbl <*> reorder v
78 Modify e fields ->
79 Modify <$> reorder e <*> mapM (\(k,v) -> (,) k <$> reorder v) fields
81 Record fields ->
82 Record <$> mapM (\(k,v) -> (,) k <$> reorder v) fields
84 Markdown uid md es -> Markdown uid md <$> mapM reorder es
86 -- Actually do some reordering
87 Let defs body ->
88 do body' <- reorder body
90 -- Sort defs into strongly connected components.This
91 -- allows the programmer to write definitions in whatever
92 -- order they please, we can still define things in order
93 -- and generalize polymorphic functions when appropriate.
94 sccs <- Graph.stronglyConnComp <$> buildDefDict defs
95 let defss = map Graph.flattenSCC sccs
97 -- remove let-bound variables from the context
98 forM_ defs $ \(Definition pattern _ _) -> do
99 bound (P.boundVars pattern)
100 mapM free (ctors pattern)
102 let L _ let' = foldr (\ds bod -> L s (Let ds bod)) body' defss
104 return let'
106bindingReorder :: (P.Pattern, LExpr) -> State (Set.Set String) (P.Pattern, LExpr)
107bindingReorder (pattern,expr) =
108 do expr' <- reorder expr
109 bound (P.boundVars pattern)
110 mapM_ free (ctors pattern)
111 return (pattern, expr')
114reorderAndGetDependencies :: Def -> State (Set.Set String) (Def, [String])
115reorderAndGetDependencies (Definition pattern expr mType) =
116 do globalFrees <- get
117 -- work in a fresh environment
118 put Set.empty
119 expr' <- reorder expr
120 localFrees <- get
121 -- merge with global frees
122 modify (Set.union globalFrees)
123 return (Definition pattern expr' mType, Set.toList localFrees)
126-- This also reorders the all of the sub-expressions in the Def list.
127buildDefDict :: [Def] -> State (Set.Set String) [(Def, Int, [Int])]
128buildDefDict defs =
129 do pdefsDeps <- mapM reorderAndGetDependencies defs
130 return $ realDeps (addKey pdefsDeps)
132 where
133 addKey :: [(Def, [String])] -> [(Def, Int, [String])]
134 addKey = zipWith (\n (pdef,deps) -> (pdef,n,deps)) [0..]
136 variableToKey :: (Def, Int, [String]) -> [(String, Int)]
137 variableToKey (Definition pattern _ _, key, _) =
138 [ (var, key) | var <- Set.toList (P.boundVars pattern) ]
140 variableToKeyMap :: [(Def, Int, [String])] -> Map.Map String Int
141 variableToKeyMap pdefsDeps =
142 Map.fromList (concatMap variableToKey pdefsDeps)
144 realDeps :: [(Def, Int, [String])] -> [(Def, Int, [Int])]
145 realDeps pdefsDeps = map convert pdefsDeps
146 where
147 varDict = variableToKeyMap pdefsDeps
148 convert (pdef, key, deps) =
149 (pdef, key, Maybe.mapMaybe (flip Map.lookup varDict) deps)