@@ -29,8 +29,10 @@ import qualified Reporting.Annotation as A
2929type CPattern = P.Canonical
3030
3131
32+
3233-- COMPILE CASES
3334
35+
3436{-| Users of this module will mainly interact with this function. It takes
3537some normal branches and gives out a decision tree that has "labels" at all
3638the leafs and a dictionary that maps these "labels" to the code that should
@@ -66,8 +68,10 @@ type VariantDict =
6668 Map.Map Var.Home (Map.Map Text Int)
6769
6870
71+
6972-- DECISION TREES
7073
74+
7175data DecisionTree
7276 = Match Int
7377 | Decision
@@ -92,8 +96,10 @@ data Path
9296 deriving (Eq)
9397
9498
99+
95100-- PATH HELPERS
96101
102+
97103add :: Path -> Path -> Path
98104add path finalLink =
99105 case path of
@@ -118,8 +124,10 @@ subPositions path patterns =
118124 patterns
119125
120126
127+
121128-- ACTUALLY BUILD DECISION TREES
122129
130+
123131data Branch =
124132 Branch
125133 { _goal :: Int
@@ -191,8 +199,10 @@ getArity variantDict (Var.Canonical home name) =
191199 \inference, it is impossible that a pattern cannot be found."
192200
193201
202+
194203-- FLATTEN PATTERNS
195204
205+
196206{-| Flatten type aliases and use the VariantDict to figure out when a tag is
197207the only variant so we can skip doing any tests on it.
198208-}
@@ -229,8 +239,10 @@ flatten variantDict pathPattern@(path, A.A ann pattern) =
229239 [pathPattern]
230240
231241
242+
232243-- SUCCESSFULLY MATCH
233244
245+
234246{-| If the first branch has no more "decision points" we can finally take that
235247path. If that is the case we give the resulting label and a mapping from free
236248variables to "how to get their value". So a pattern like (Just (x,_)) will give
@@ -246,8 +258,10 @@ checkForMatch branches =
246258 Nothing
247259
248260
261+
249262-- GATHER OUTGOING EDGES
250263
264+
251265gatherEdges :: VariantDict -> [Branch] -> Path -> ([(Test, [Branch])], [Branch])
252266gatherEdges variantDict branches path =
253267 let
@@ -266,8 +280,10 @@ gatherEdges variantDict branches path =
266280 ( allEdges, fallbacks )
267281
268282
283+
269284-- FIND RELEVANT TESTS
270285
286+
271287testsAtPath :: Path -> [Branch] -> [Test]
272288testsAtPath selectedPath branches =
273289 let
@@ -312,8 +328,10 @@ testAtPath selectedPath (Branch _ pathPatterns) =
312328 Nothing
313329
314330
331+
315332-- BUILD EDGES
316333
334+
317335edgesFor :: Path -> [Branch] -> Test -> (Test, [Branch])
318336edgesFor path branches test =
319337 ( test
@@ -369,8 +387,10 @@ extract selectedPath pathPatterns =
369387 Just (first : start, foundPattern, end)
370388
371389
390+
372391-- FIND IRRELEVANT BRANCHES
373392
393+
374394isIrrelevantTo :: Path -> Branch -> Bool
375395isIrrelevantTo selectedPath (Branch _ pathPatterns) =
376396 case List.lookup selectedPath pathPatterns of
@@ -403,8 +423,10 @@ needsTests (A.A _ pattern) =
403423 True
404424
405425
426+
406427-- PICK A PATH
407428
429+
408430pickPath :: VariantDict -> [Branch] -> Path
409431pickPath variantDict branches =
410432 let
@@ -453,8 +475,10 @@ bests allPaths =
453475 snd (List.foldl' gatherMinimum (headWeight, [headPath]) weightedPaths)
454476
455477
478+
456479-- PATH PICKING HEURISTICS
457480
481+
458482smallDefaults :: [Branch] -> Path -> Int
459483smallDefaults branches path =
460484 length (filter (isIrrelevantTo path) branches)