1module Optimize (optimize) where
3import Control.Applicative
4import qualified Control.Monad as M
5import qualified Control.Monad.State as State
6import qualified Data.Traversable as T
8import qualified AST.Expression.General as Expr
9import qualified AST.Expression.Canonical as Can
10import qualified AST.Expression.Optimized as Opt
11import qualified AST.Module as Module
12import qualified AST.Module.Name as ModuleName
13import qualified AST.Pattern as P
14import qualified AST.Variable as Var
15import qualified Optimize.Environment as Env
16import qualified Optimize.Patterns as Patterns
17import qualified Reporting.Annotation as A
18import qualified Reporting.Region as R
21-- OPTIMIZE - turn a canonical module into a list of optimized defs
23optimize :: Module.CanonicalModule -> Module.Optimized
30 flattenLets [] (Module.program (Module.body module_))
33 (concat <$> mapM (optimizeDef (Just home)) defs)
37flattenLets :: [Can.Def] -> Can.Expr -> ([Can.Def], Can.Expr)
38flattenLets defs annExpr@(A.A _ expr) =
41 flattenLets (defs ++ ds) body
49optimizeDef :: Maybe ModuleName.Canonical -> Can.Def -> Env.Optimizer [Opt.Def]
50optimizeDef home (Can.Definition (Can.Facts deps) pattern@(A.A _ ptrn) expression _) =
53 Expr.collectLambdas expression
56 (P.Var name, _ : _) ->
58 do (Env.Env htc uid) <- State.get
59 State.put (Env.Env False uid)
61 (args, body) <- optimizeFunction (Just name) args rawBody
63 (Env.Env isTailCall uid') <- State.get
64 State.put (Env.Env htc uid')
66 let facts = Opt.Facts home deps
71 [ Opt.TailDef facts name args body ]
75 [ Opt.Def facts name (Opt.Function args body) ]
79 do hasTailCall <- State.gets Env._hasTailCall
80 body <- optimizeExpr Nothing rawBody
81 Env.setTailCall hasTailCall
82 return [ Opt.Def (Opt.Facts home deps) name body ]
86 do name <- Env.freshName
87 optBody <- optimizeExpr Nothing rawBody
88 let optDef = Opt.Def (Opt.Facts home deps) name optBody
89 return (optDef : patternToDefs home name pattern)
92 error "there should never be a function where the name is not a P.Var"
99 -> [P.CanonicalPattern]
101 -> Env.Optimizer ([String], Opt.Expr)
102optimizeFunction maybeName patterns givenBody =
103 do (argNames, body) <- M.foldM depatternArgs ([], givenBody) (reverse patterns)
104 optBody <- optimizeExpr (makeContext argNames =<< maybeName) body
105 return (argNames, optBody)
108makeContext :: [String] -> String -> Context
109makeContext argNames name =
110 Just (name, length argNames, argNames)
114 :: ([String], Can.Expr)
115 -> P.CanonicalPattern
116 -> Env.Optimizer ([String], Can.Expr)
117depatternArgs (names, rawExpr) ptrn =
118 do (name, expr) <- depattern ptrn rawExpr
119 return (name : names, expr)
123 :: Maybe ModuleName.Canonical
125 -> P.CanonicalPattern
127patternToDefs home rootName pattern =
133 , Opt.Var (Var.local rootName)
137 ( [ Var.TopLevelVar moduleName rootName ]
138 , Opt.Var (Var.topLevel moduleName rootName)
141 toDef (name, accessExpr) =
142 Opt.Def (Opt.Facts home deps) name accessExpr
144 map toDef (substitutions rootExpr pattern)
147substitutions :: Opt.Expr -> P.CanonicalPattern -> [(String, Opt.Expr)]
148substitutions expr (A.A _ pattern) =
157 map (\name -> (name, Opt.Access expr name)) fields
162 P.Alias alias realPattern ->
163 (alias, expr) : substitutions expr realPattern
166 concat (zipWith (substitutions . Opt.DataAccess expr) [0..] patterns)
169-- OPTIMIZE EXPRESSIONS
171type Context = Maybe (String, Int, [String])
174{-| This optimizer detects tail calls. The `Context` holds a function name,
175function arity, and list of argument names. If a tail call is possible, the
176`Context` will be filled in and we check any function we run into. If we go
177down a route that cannot possibly have tail calls, we remove the context.
179optimizeExpr :: Context -> Can.Expr -> Env.Optimizer Opt.Expr
180optimizeExpr context annExpr@(A.A region expression) =
184 -- this *might* find a tail call, use this when something is a potential
189 -- this will never find tail calls, use this when you are working with
190 -- expressions that cannot be turned into tail calls
194 pure (Opt.Literal lit)
197 if name == Var.inCore ["Debug"] "crash" then
198 pure (Opt.Crash region Nothing)
203 Expr.Range lowExpr highExpr ->
205 <$> justConvert lowExpr
206 <*> justConvert highExpr
208 Expr.ExplicitList elements ->
210 <$> T.traverse justConvert elements
212 Expr.Binop op leftExpr rightExpr ->
213 optimizeBinop context region op leftExpr rightExpr
218 Expr.collectLambdas annExpr
220 uncurry Opt.Function <$> optimizeFunction Nothing patterns body
225 Expr.collectApps annExpr
227 case isTailCall context func (length args) of
228 Just (name, argNames) ->
229 do optArgs <- T.traverse justConvert args
231 return (Opt.TailCall name argNames optArgs)
234 do hasTailCall <- State.gets Env._hasTailCall
235 optFunc <- justConvert func
236 optArgs <- T.traverse justConvert args
237 Env.setTailCall hasTailCall
238 return (Opt.Call optFunc optArgs)
240 Expr.If branches finally ->
242 crawlBranch (cond,branch) =
243 (,) <$> justConvert cond <*> keepLooking branch
246 <$> T.traverse crawlBranch branches
247 <*> keepLooking finally
249 Expr.Let defs body ->
250 do optDefs <- concat <$> T.traverse (optimizeDef Nothing) defs
251 Opt.Let optDefs <$> keepLooking body
253 Expr.Case expr branches ->
254 do optExpr <- optimizeExpr Nothing expr
255 optBranches <- T.traverse (mapSnd (optimizeExpr context)) branches
256 Patterns.optimize region optExpr optBranches
258 Expr.Data name exprs ->
259 Opt.Data name <$> T.traverse justConvert exprs
261 Expr.Access record field ->
263 <$> justConvert record
266 Expr.Update record fields ->
268 <$> justConvert record
269 <*> T.traverse (mapSnd justConvert) fields
271 Expr.Record fields ->
273 <$> T.traverse (mapSnd justConvert) fields
275 Expr.GLShader uid src gltipe ->
276 pure (Opt.GLShader uid src gltipe)
282 pure (Expr.In name tipe)
284 Expr.Out name expr tipe ->
285 (\e -> Expr.Out name e tipe) <$> justConvert expr
287 Expr.Task name expr tipe ->
288 (\e -> Expr.Task name e tipe) <$> justConvert expr
291mapSnd :: M.Functor box => (a -> box b) -> (x, a) -> box (x, b)
298isTailCall :: Context -> Can.Expr -> Int -> Maybe (String, [String])
299isTailCall context (A.A _ function) arity =
300 case (context, function) of
301 ( Just (ctxName, ctxArity, argNames), Expr.Var (Var.Canonical home name) ) ->
303 if name == ctxName && arity == ctxArity && Var.isLocalHome home then
304 Just (name, argNames)
314-- given a pattern and an expression, push the actual pattern matching into the
315-- expression as a case-expression if necessary. The pattern compiler will work
318depattern :: P.CanonicalPattern -> Can.Expr -> Env.Optimizer (String, Can.Expr)
319depattern canPattern@(A.A _ pattern) expr =
322 A.A (error "the annotation added in 'depattern' should not be observed!")
324 caseExpr e branches =
325 ann (Expr.Case e branches)
328 ann (Expr.Var (Var.local name))
331 do name <- Env.freshName
334 , caseExpr (var name) [ (canPattern, expr) ]
348 do name <- Env.freshName
366 -> Env.Optimizer Opt.Expr
367optimizeBinop context region op leftExpr rightExpr =
370 binop = ann (Expr.Binop op leftExpr rightExpr)
372 if op == forwardApply then
374 optimizeExpr context (collect id left forwardApply binop)
376 else if op == backwardApply then
378 optimizeExpr context (collect id right backwardApply binop)
380 else if op == forwardCompose then
382 do var <- Env.freshName
383 let makeRoot func = ann (Expr.App func (ann (Expr.Var (Var.local var))))
384 let body = collect makeRoot left forwardCompose binop
385 optimizeExpr context (ann (Expr.Lambda (ann (P.Var var)) body))
387 else if op == backwardCompose then
389 do var <- Env.freshName
390 let makeRoot func = ann (Expr.App func (ann (Expr.Var (Var.local var))))
391 let body = collect makeRoot right backwardCompose binop
392 optimizeExpr context (ann (Expr.Lambda (ann (P.Var var)) body))
394 else if op == Var.Canonical Var.BuiltIn "::" then
396 optimizeExpr context (ann (Expr.Data "::" [ leftExpr, rightExpr ]))
401 <$> optimizeExpr Nothing leftExpr
402 <*> optimizeExpr Nothing rightExpr
405-- left-associative ((x |> f) |> g)
406forwardApply :: Var.Canonical
408 Var.inCore ["Basics"] "|>"
411-- right-associative (g <| (f <| x))
412backwardApply :: Var.Canonical
414 Var.inCore ["Basics"] "<|"
417-- left-associative ((f >> g) >> h)
418forwardCompose :: Var.Canonical
420 Var.inCore ["Basics"] ">>"
423-- right-associative (h << (g << f))
424backwardCompose :: Var.Canonical
426 Var.inCore ["Basics"] "<<"
430 Can.Expr -> Can.Expr -> (Can.Expr, Can.Expr)
443collect :: (Can.Expr -> Can.Expr) -> Assoc -> Var.Canonical -> Can.Expr -> Can.Expr
444collect makeRoot assoc desiredOp annExpr@(A.A ann expr) =
446 Expr.Binop op leftExpr rightExpr | op == desiredOp ->
448 (func, arg) = assoc leftExpr rightExpr
450 A.A ann (Expr.App func (collect makeRoot assoc desiredOp arg))
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