1{-# OPTIONS_GHC -Wall #-}
2module Optimize.Patterns (optimize) where
4import Control.Applicative
5import Control.Arrow (second)
6import qualified Control.Monad as M
7import qualified Control.Monad.State as State
8import qualified Data.Map as Map
10import qualified Data.Maybe as Maybe
11import qualified Data.Traversable as T
13import qualified AST.Expression.Optimized as Opt
14import qualified AST.Module as Module
15import qualified AST.Module.Name as ModuleName
16import qualified AST.Pattern as P
17import qualified AST.Variable as Var
18import Elm.Utils ((|>))
19import qualified Optimize.Environment as Env
20import qualified Optimize.Patterns.DecisionTree as DT
21import qualified Reporting.Annotation as A
22import qualified Reporting.Region as R
28 -> [(P.CanonicalPattern, Opt.Expr)]
29 -> Env.Optimizer Opt.Expr
30optimize region optExpr optBranches =
32 indexify index (pattern, branch) =
38 case last optBranches of
39 (A.A ann P.Anything, Opt.Call (Opt.Crash _ _) [arg]) ->
40 do name <- Env.freshName
41 let newPattern = A.A ann (P.Var name)
42 let newCall = Opt.Call (Opt.Crash region (Just name)) [arg]
43 return (init optBranches ++ [(newPattern, newCall)])
48 let (patterns, indexedBranches) =
49 unzip (zipWith indexify [0..] crashBranches)
52 DT.compile (error "need a VariantDict") patterns
54 return (inlinedCase optExpr decisionTree indexedBranches)
59 -> DT.DecisionTree DT.Jump
62inlinedCase expr decisionTree jumpTargets =
65 gatherSubstitutions decisionTree
67 (jumps, sharedBranches) =
68 unzip (map (toJumps substitutionDict) jumpTargets)
72 (inlineJumps (Map.fromList jumps) decisionTree)
73 (Maybe.catMaybes sharedBranches)
77 :: DT.DecisionTree DT.Jump
78 -> Map.Map Int [[(String, DT.Path)]]
79gatherSubstitutions decisionTree =
81 DT.Decision _ edges fallback ->
83 |> maybe id (:) fallback
84 |> map gatherSubstitutions
85 |> Map.unionsWith (++)
87 DT.Match (DT.Jump target substitutions) ->
88 Map.singleton target [substitutions]
92 :: Map.Map Int [[(String, DT.Path)]]
94 -> ( (Int, Opt.Jump), Maybe (Int, Opt.Branch) )
95toJumps substitutionDict (target, branch) =
96 case substitutionDict ! target of
98 ( (target, Opt.Inline (Opt.Branch substitutions branch))
103 ( (target, Opt.Jump target)
104 , Just (target, Opt.Branch substitutions branch)
108 error "it is not possible for there to be no substitutions"
112 :: Map.Map Int Opt.Jump
113 -> DT.DecisionTree DT.Jump
114 -> DT.DecisionTree Opt.Jump
115inlineJumps jumpDict decisionTree =
121 DT.Decision test edges fallback ->
122 DT.Decision test (map (second go) edges) (fmap go fallback)
124 DT.Match (DT.Jump target _) ->
125 DT.Match (jumpDict ! target)
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