@@ -16,6 +16,7 @@ import qualified AST.Pattern as Pattern
1616import qualified AST.Variable as Var
1717import Elm.Utils ((|>))
1818import Nitpick.Pattern (Pattern(..), fromCanonicalPattern)
19+import qualified Optimize.Patterns.DecisionTree as Opt
1920import qualified Reporting.Annotation as A
2021import qualified Reporting.Error.Pattern as Error
2122import qualified Reporting.Region as Region
@@ -25,31 +26,40 @@ import qualified Reporting.Result as Result
2526patternMatches
2627 :: Module.Interfaces
2728 -> Module.CanonicalModule
28− -> Result.Result w Error.Error ()
29+ -> Result.Result w Error.Error Opt.VariantDict
2930patternMatches interfaces modul =
3031 let
3132 name = Module.name modul
3233 body = Module.body modul
34+ tagDict =
35+ toTagDict interfaces name (Module.datatypes body)
3336 in
34− checkExpression
35− (toTagDict interfaces name (Module.datatypes body))
36− (Module.program body)
37+ const (Map.map (Map.map length) tagDict)
38+ <$> checkExpression tagDict (Module.program body)
3739
3840
3941-- TAG DICT
4042
4143type TagDict =
42− Map.Map Var.Home (Map.Map Tag [(Tag, Int)])
44+ Map.Map Var.Home (Map.Map Tag [TagInfo])
4345
4446
4547type Tag = String
4648
4749
50+data TagInfo = TagInfo
51+ { _tag :: Tag
52+ , _arity :: Int
53+ }
54+
55+
4856toTagDict :: Module.Interfaces -> ModuleName.Canonical -> Module.ADTs -> TagDict
4957toTagDict interfaces localName localAdts =
5058 let
5159 listTags =
52− [ ("::", 2), ("[]", 0) ]
60+ [ TagInfo "::" 2
61+ , TagInfo "[]" 0
62+ ]
5363
5464 builtinDict =
5565 Map.singleton Var.BuiltIn $
@@ -67,12 +77,12 @@ toTagDict interfaces localName localAdts =
6777 Map.union builtinDict interfaceDict
6878
6979
70−toTagMapping :: Module.ADTs -> Map.Map Tag ([(Tag,Int)])
80+toTagMapping :: Module.ADTs -> Map.Map Tag [TagInfo]
7181toTagMapping adts =
7282 let
7383 toTagAndArity (_tvars, tagInfoList) =
7484 let
75− info = map (\(tag, args) -> (tag, length args)) tagInfoList
85+ info = map (\(tag, args) -> TagInfo tag (length args)) tagInfoList
7686 in
7787 map (second (const info)) tagInfoList
7888 in
@@ -81,7 +91,7 @@ toTagMapping adts =
8191 |> Map.fromList
8292
8393
84−lookupOtherTags :: Var.Canonical -> TagDict -> [(Tag, Int)]
94+lookupOtherTags :: Var.Canonical -> TagDict -> [TagInfo]
8595lookupOtherTags (Var.Canonical home name) tagDict =
8696 case Map.lookup name =<< Map.lookup home tagDict of
8797 Just otherTags ->
@@ -89,7 +99,7 @@ lookupOtherTags (Var.Canonical home name) tagDict =
8999
90100 Nothing ->
91101 if Help.isTuple name then
92− [(name, read (drop 6 name))]
102+ [ TagInfo name (read (drop 6 name)) ]
93103 else
94104 error
95105 "Since the Nitpick phase happens after canonicalization and type \
@@ -339,8 +349,8 @@ complementData tagDict tag patterns =
339349 tagComplements ++ argComplements
340350
341351
342−tagToPattern :: Var.Canonical -> (Tag, Int) -> Maybe Pattern
343−tagToPattern (Var.Canonical home rootTag) (tag, arity) =
352+tagToPattern :: Var.Canonical -> TagInfo -> Maybe Pattern
353+tagToPattern (Var.Canonical home rootTag) (TagInfo tag arity) =
344354 if rootTag == tag then
345355 Nothing
346356 else