1{-# OPTIONS_GHC -Wall #-}
2module Optimize.Patterns (optimize) where
4import Control.Arrow (second)
5import qualified Data.Map as Map
7import qualified Data.Maybe as Maybe
9import qualified AST.Expression.Optimized as Opt
10import qualified AST.Pattern as P
11import Elm.Utils ((|>))
12import qualified Optimize.Environment as Env
13import qualified Optimize.Patterns.DecisionTree as DT
14import qualified Reporting.Annotation as A
15import qualified Reporting.Region as R
22 -> [(P.CanonicalPattern, Opt.Expr)]
23 -> Env.Optimizer Opt.Expr
24optimize variantDict region optExpr optBranches =
26 indexify index (pattern, branch) =
32 case last optBranches of
33 (A.A ann P.Anything, Opt.Call (Opt.Crash _ _) [arg]) ->
34 do name <- Env.freshName
35 let newPattern = A.A ann (P.Var name)
36 let newCall = Opt.Call (Opt.Crash region (Just name)) [arg]
37 return (init optBranches ++ [(newPattern, newCall)])
42 let (patterns, indexedBranches) =
43 unzip (zipWith indexify [0..] crashBranches)
46 DT.compile variantDict patterns
48 tempName <- Env.freshName
52 [ Opt.Def Opt.dummyFacts tempName optExpr ]
53 (inlinedCase tempName decisionTree indexedBranches)
58 -> DT.DecisionTree DT.Jump
61inlinedCase name decisionTree jumpTargets =
64 gatherSubstitutions decisionTree
66 (jumps, sharedBranches) =
67 unzip (map (toJumps substitutionDict) jumpTargets)
71 (inlineJumps (Map.fromList jumps) decisionTree)
72 (Maybe.catMaybes sharedBranches)
76 :: DT.DecisionTree DT.Jump
77 -> Map.Map Int [[(String, DT.Path)]]
78gatherSubstitutions decisionTree =
80 DT.Decision _ edges fallback ->
82 |> maybe id (:) fallback
83 |> map gatherSubstitutions
84 |> Map.unionsWith (++)
86 DT.Match (DT.Jump target substitutions) ->
87 Map.singleton target [substitutions]
91 :: Map.Map Int [[(String, DT.Path)]]
93 -> ( (Int, Opt.Jump), Maybe (Int, Opt.Branch) )
94toJumps substitutionDict (target, branch) =
95 case substitutionDict ! target of
97 ( (target, Opt.Inline (Opt.Branch substitutions branch))
102 ( (target, Opt.Jump target)
103 , Just (target, Opt.Branch substitutions branch)
107 error "it is not possible for there to be no substitutions"
111 :: Map.Map Int Opt.Jump
112 -> DT.DecisionTree DT.Jump
113 -> DT.DecisionTree Opt.Jump
114inlineJumps jumpDict decisionTree =
120 DT.Decision test edges fallback ->
121 DT.Decision test (map (second go) edges) (fmap go fallback)
123 DT.Match (DT.Jump target _) ->
124 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