1{-# OPTIONS_GHC -Wall #-}
2module Optimize.Patterns.DecisionTree where
3{- To learn more about how this works, definitely read through:
5 "When Do Match-Compilation Heuristics Matter?"
7by Kevin Scott and Norman Ramsey. The rough idea is that we start with a simple
8list of patterns and expressions, and then turn that into a "decision tree"
9that requires as few tests as possible to make it to a leaf. Read the paper, it
10explains this extraordinarily well! We are currently using the same heuristics
11as SML/NJ to get nice trees.
14import Control.Arrow (second)
15import Data.Function (on)
16import qualified Data.List as List
17import qualified Data.Map as Map
18import qualified Data.Set as Set
19import qualified Data.Maybe as Maybe
21import qualified AST.Helpers as Help
22import qualified AST.Literal as L
23import qualified AST.Pattern as P
24import qualified AST.Variable as Var
25import qualified Reporting.Annotation as A
28type CPattern = P.CanonicalPattern
33{-| Users of this module will mainly interact with this function. It takes
34some normal branches and gives out a decision tree that has "labels" at all
35the leafs and a dictionary that maps these "labels" to the code that should
38If 2 or more leaves point to the same label, we need to do some tricks in JS to
39make that work nicely. When is JS getting goto?! ;) That is outside the scope
42compile :: VariantDict -> [(CPattern, Int)] -> DecisionTree Jump
43compile variantDict rawBranches =
45 format (pattern, index) =
46 Branch index [(Empty, pattern)]
48 toDecisionTree variantDict (map format rawBranches)
51{-| When a certain union type is defined, you specify a certain number of tags.
52This helps us do a few optimizations:
54 * If there is only one possible tag, we can always skip checking what it is.
55 Tuples are a common example of this.
56 * If we use all possible tags, we can skip doing the last test. If we have
57 checked N-1 of N tags, there is no need to test the Nth, we know its the
60So this dictionary maps tags to the number of variants that exist, so it'll
61contain things like [ ("Just", 2), ("Nothing", 2), ("_Tuple2", 1), ... ] which
62we can use for these optimizations.
65 Map.Map Var.Home (Map.Map String Int)
74 , _edges :: [(Test, DecisionTree a)]
75 , _default :: Maybe (DecisionTree a)
81 , _substitutions :: [(String, Path)]
86 = Constructor Var.Canonical
101add :: Path -> Path -> Path
108 error "nothing should be added to an alias path"
110 Position index subpath ->
111 Position index (add subpath finalLink)
113 Field name subpath ->
114 Field name (add subpath finalLink)
117subPositions :: Path -> [CPattern] -> [(Path, CPattern)]
118subPositions path patterns =
120 (\index pattern -> (add path (Position index Empty), pattern))
125-- ACTUALLY BUILD DECISION TREES
130 , _patterns :: [(Path, CPattern)]
134toDecisionTree :: VariantDict -> [Branch] -> DecisionTree Jump
135toDecisionTree variantDict rawBranches =
138 map (flattenPatterns variantDict) rawBranches
140 case checkForMatch branches of
141 Just (goal, substitutions) ->
142 Match (Jump goal substitutions)
147 smallDefaults branches
150 testsAtPath path branches
153 map (edgesFor path branches) relevantTests
155 (rawEdges, unexplored) =
156 if isComplete variantDict relevantTests then
158 , snd (last allRawEdges)
163 , filter (isIrrelevantTo path) branches
167 map (second (toDecisionTree variantDict)) rawEdges
169 case (edges, unexplored) of
170 ([(_tag, decisionTree)], []) ->
174 Decision path edges Nothing
177 Decision path edges (Just (toDecisionTree variantDict unexplored))
180isComplete :: VariantDict -> [Test] -> Bool
181isComplete variantDict tests =
184 getArity variantDict var == length tests
186 Literal (L.Boolean _) ->
193getArity :: VariantDict -> Var.Canonical -> Int
194getArity variantDict (Var.Canonical home name) =
195 case Map.lookup name =<< Map.lookup home variantDict of
200 if Help.isTuple name then
205 "Since the Optimize phase happens after canonicalization and type \
206 \inference, it is impossible that a pattern cannot be found."
211{-| Flatten type aliases and use the VariantDict to figure out when a tag is
212the only variant so we can skip doing any tests on it.
214flattenPatterns :: VariantDict -> Branch -> Branch
215flattenPatterns variantDict (Branch goal pathPatterns) =
216 Branch goal (concatMap (flatten variantDict) pathPatterns)
219flatten :: VariantDict -> (Path, CPattern) -> [(Path, CPattern)]
220flatten variantDict pathPattern@(path, A.A ann pattern) =
228 P.Alias alias realPattern ->
229 [ (add path Alias, A.A ann (P.Var alias))
230 , (path, realPattern)
236 P.Data tag patterns ->
237 if getArity variantDict tag == 1 then
238 concatMap (flatten variantDict) (subPositions path patterns)
249{-| If the first branch has no more "decision points" we can finally take that
250path. If that is the case we give the resulting label and a mapping from free
251variables to "how to get their value". So a pattern like (Just (x,_)) will give
252us something like ("x" => value.0.0)
254checkForMatch :: [Branch] -> Maybe (Int, [(String, Path)])
255checkForMatch branches =
257 Branch goal patterns : _ ->
258 (,) goal `fmap` concat `fmap` mapM getSubstitution patterns
264getSubstitution :: (Path, CPattern) -> Maybe [(String, Path)]
265getSubstitution (path, A.A _ pattern) =
274 error "aliases should never reach 'getSubstitution' function"
277 Just (map (\name -> (name, add path (Field name Empty))) fields)
286-- FIND RELEVANT TESTS
288testsAtPath :: Path -> [Branch] -> [Test]
289testsAtPath selectedPath branches =
292 Maybe.mapMaybe (testAtPath selectedPath) branches
294 skipVisited test curr@(uniqueTests, visitedTests) =
295 if Set.member test visitedTests then
299 , Set.insert test visitedTests
302 fst (foldr skipVisited ([], Set.empty) allTests)
305testAtPath :: Path -> Branch -> Maybe Test
306testAtPath selectedPath (Branch _ pathPatterns) =
307 case List.lookup selectedPath pathPatterns of
311 Just (A.A _ pattern) ->
314 Just (Constructor name)
323 error "aliases should never reach 'testAtPath' function"
334edgesFor :: Path -> [Branch] -> Test -> (Test, [Branch])
335edgesFor path branches test =
337 , Maybe.mapMaybe (toRelevantBranch test path) branches
341toRelevantBranch :: Test -> Path -> Branch -> Maybe Branch
342toRelevantBranch test path branch@(Branch goal pathPatterns) =
343 case extract path pathPatterns of
344 Just (start, A.A _ pattern, end) ->
346 P.Data name patterns ->
347 if test == Constructor name then
348 Just (Branch goal (start ++ subPositions path patterns ++ end))
354 if test == Literal lit then
355 Just (Branch goal (start ++ end))
369 -> [(Path, CPattern)]
370 -> Maybe ([(Path, CPattern)], CPattern, [(Path, CPattern)])
371extract selectedPath pathPatterns =
376 first@(path, pattern) : rest ->
377 if path == selectedPath then
378 Just ([], pattern, rest)
381 case extract selectedPath rest of
385 Just (start, foundPattern, end) ->
386 Just (first : start, foundPattern, end)
389-- FIND IRRELEVANT BRANCHES
391isIrrelevantTo :: Path -> Branch -> Bool
392isIrrelevantTo selectedPath (Branch _ pathPatterns) =
393 case List.lookup selectedPath pathPatterns of
398 not (needsTests pattern)
401needsTests :: CPattern -> Bool
402needsTests (A.A _ pattern) =
411 error "aliases should never reach 'isIrrelevantTo' function"
423-- PATH PICKING HEURISTICS
425smallDefaults :: [Branch] -> Path
426smallDefaults branches =
429 Maybe.mapMaybe isChoicePath (concatMap _patterns branches)
432 map (\path -> (path, length (filter (isIrrelevantTo path) branches))) allPaths
434 fst (List.minimumBy (compare `on` snd) weightedPaths)
437isChoicePath :: (Path, CPattern) -> Maybe Path
438isChoicePath (path, pattern) =
439 if needsTests pattern then
445-- smallBranchingFactor :: [Branch] -> Path
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