@@ -23,6 +23,8 @@ import qualified Data.Maybe as Maybe
23import qualified Data.Text as Text
23import qualified Data.Text as Text
25import qualified AST.Canonical as Can
25import qualified AST.Canonical as Can
26+import qualified AST.Module.Name as ModuleName
27+import qualified Data.Index as Index
26import qualified Elm.Name as N
28import qualified Elm.Name as N
27import qualified Reporting.Annotation as A
29import qualified Reporting.Annotation as A
28import qualified Reporting.Region as R
30import qualified Reporting.Region as R
@@ -35,8 +37,7 @@ import qualified Reporting.Region as R
35data Pattern
37data Pattern
36 = Anything
38 = Anything
37 | Literal Literal
39 | Literal Literal
38− | Ctor Can.CtorAlts N.Name [Pattern]
40+ | Ctor Can.Union N.Name [Pattern]
42data Literal
43data Literal
@@ -63,16 +64,16 @@ simplify (A.At _ pattern) =
65 Can.PUnit ->
66 Can.PUnit ->
66− Ctor unitAlts unitName []
67+ Ctor unit unitName []
68 Can.PTuple a b Nothing ->
69 Can.PTuple a b Nothing ->
69− Ctor pairAlts pairName [ simplify a, simplify b ]
70+ Ctor pair pairName [ simplify a, simplify b ]
71 Can.PTuple a b (Just c) ->
72 Can.PTuple a b (Just c) ->
72− Ctor tripleAlts tripleName [ simplify a, simplify b, simplify c ]
73+ Ctor triple tripleName [ simplify a, simplify b, simplify c ]
74− Can.PCtor _ _ _ alts name _ args ->
75+ Can.PCtor _ _ union name _ args ->
75− Ctor alts name $
76+ Ctor union name $
76 map (\(Can.PatternCtorArg _ _ arg) -> simplify arg) args
77 map (\(Can.PatternCtorArg _ _ arg) -> simplify arg) args
78 Can.PList entries ->
79 Can.PList entries ->
@@ -96,41 +97,63 @@ simplify (A.At _ pattern) =
97cons :: Can.Pattern -> Pattern -> Pattern
98cons :: Can.Pattern -> Pattern -> Pattern
98cons hd tl =
99cons hd tl =
99− Ctor listAlts consName [ simplify hd, tl ]
100+ Ctor list consName [ simplify hd, tl ]
102{-# NOINLINE nil #-}
103{-# NOINLINE nil #-}
103nil :: Pattern
104nil :: Pattern
105− Ctor listAlts nilName []
106+ Ctor list nilName []
109−-- ALTS
110+-- BUILT-IN UNIONS
112−{-# NOINLINE unitAlts #-}
113+{-# NOINLINE unit #-}
113−unitAlts :: Can.CtorAlts
114+unit :: Can.Union
115− Can.toAlts [ Can.CtorAlt unitName 0 ]
118+ Can.Ctor unitName Index.first 0 []
120+ Can.Union [] [ ctor ] 1 Can.Normal
118−{-# NOINLINE pairAlts #-}
123+{-# NOINLINE pair #-}
119−pairAlts :: Can.CtorAlts
124+pair :: Can.Union
121− Can.toAlts [ Can.CtorAlt pairName 2 ]
126+ let
128+ Can.Ctor pairName Index.first 2 [Can.TVar "a", Can.TVar "b"]
130+ Can.Union ["a","b"] [ ctor ] 1 Can.Normal
124−{-# NOINLINE tripleAlts #-}
133+{-# NOINLINE triple #-}
125−tripleAlts :: Can.CtorAlts
134+triple :: Can.Union
126−tripleAlts =
135+triple =
127− Can.toAlts [ Can.CtorAlt tripleName 3 ]
136+ let
138+ Can.Ctor pairName Index.first 3 [Can.TVar "a", Can.TVar "b", Can.TVar "c"]
140+ Can.Union ["a","b","c"] [ ctor ] 1 Can.Normal
130−{-# NOINLINE listAlts #-}
143+{-# NOINLINE list #-}
131−listAlts :: Can.CtorAlts
144+list :: Can.Union
133− Can.toAlts [ Can.CtorAlt consName 2, Can.CtorAlt nilName 0 ]
146+ let
148+ Can.Ctor nilName Index.first 0 []
151+ Can.Ctor consName Index.second 2
153+ , Can.TType ModuleName.list N.list [Can.TVar "a"]
156+ Can.Union ["a"] [ nilCtor, consCtor ] 2 Can.Normal
136{-# NOINLINE unitName #-}
159{-# NOINLINE unitName #-}
@@ -247,6 +270,9 @@ checkExpr (A.At region expression) errors =
247 Can.VarForeign _ _ _ ->
270 Can.VarForeign _ _ _ ->
273+ Can.VarCtor _ _ _ _ _ ->
250 Can.VarDebug _ _ _ ->
276 Can.VarDebug _ _ _ ->
@@ -403,7 +429,7 @@ isExhaustive matrix n =
403 <$> isExhaustive (Maybe.mapMaybe specializeRowByAnything matrix) (n - 1)
429 <$> isExhaustive (Maybe.mapMaybe specializeRowByAnything matrix) (n - 1)
406− let alts@(Can.CtorAlts numAlts altList) = snd (Map.findMin ctors) in
432+ let alts@(Can.Union _ altList numAlts _) = snd (Map.findMin ctors) in
407 if numSeen < numAlts then
433 if numSeen < numAlts then
409 <$> Maybe.mapMaybe (isMissing alts ctors) altList
435 <$> Maybe.mapMaybe (isMissing alts ctors) altList
@@ -411,7 +437,7 @@ isExhaustive matrix n =
414− isAltExhaustive (Can.CtorAlt name arity) =
440+ isAltExhaustive (Can.Ctor name _ arity _) =
415 recoverCtor alts name arity <$>
441 recoverCtor alts name arity <$>
416 isExhaustive
442 isExhaustive
417 (Maybe.mapMaybe (specializeRowByCtor name arity) matrix)
443 (Maybe.mapMaybe (specializeRowByCtor name arity) matrix)
@@ -420,21 +446,21 @@ isExhaustive matrix n =
420 concatMap isAltExhaustive altList
446 concatMap isAltExhaustive altList
423−isMissing :: Can.CtorAlts -> Map.Map N.Name a -> Can.CtorAlt -> Maybe Pattern
449+isMissing :: Can.Union -> Map.Map N.Name a -> Can.Ctor -> Maybe Pattern
424−isMissing alts ctors (Can.CtorAlt name arity) =
450+isMissing union ctors (Can.Ctor name _ arity _) =
425 if Map.member name ctors then
451 if Map.member name ctors then
428− Just (Ctor alts name (replicate arity Anything))
454+ Just (Ctor union name (replicate arity Anything))
431−recoverCtor :: Can.CtorAlts -> N.Name -> Int -> [Pattern] -> [Pattern]
457+recoverCtor :: Can.Union -> N.Name -> Int -> [Pattern] -> [Pattern]
432−recoverCtor alts name arity patterns =
458+recoverCtor union name arity patterns =
434 (args, rest) =
460 (args, rest) =
435 splitAt arity patterns
461 splitAt arity patterns
437− Ctor alts name args : rest
463+ Ctor union name args : rest
@@ -499,9 +525,9 @@ isUseful matrix vector =
499 -- of those. But what if some of those Ctors have subpatterns
525 -- of those. But what if some of those Ctors have subpatterns
500 -- that make them less general? If so, this actually is useful!
526 -- that make them less general? If so, this actually is useful!
502− isUsefulAlt (Can.CtorAlt ctor arity) =
528+ isUsefulAlt (Can.Ctor name _ arity _) =
504− (Maybe.mapMaybe (specializeRowByCtor ctor arity) matrix)
530+ (Maybe.mapMaybe (specializeRowByCtor name arity) matrix)
505 (replicate arity Anything ++ patterns)
531 (replicate arity Anything ++ patterns)
507 any isUsefulAlt alts
533 any isUsefulAlt alts
@@ -579,7 +605,7 @@ specializeRowByAnything row =
581data Complete
607data Complete
582− = Yes [Can.CtorAlt]
608+ = Yes [Can.Ctor]
@@ -592,7 +618,7 @@ isComplete matrix =
592 if numSeen == 0 then
618 if numSeen == 0 then
595− let (Can.CtorAlts numAlts alts) = snd (Map.findMin ctors) in
621+ let (Can.Union _ alts numAlts _) = snd (Map.findMin ctors) in
596 if numSeen == numAlts then Yes alts else No
622 if numSeen == numAlts then Yes alts else No
@@ -600,16 +626,16 @@ isComplete matrix =
600-- COLLECT CTORS
626-- COLLECT CTORS
603−collectCtors :: [[Pattern]] -> Map.Map N.Name Can.CtorAlts
629+collectCtors :: [[Pattern]] -> Map.Map N.Name Can.Union
604collectCtors matrix =
630collectCtors matrix =
605 List.foldl' collectCtorsHelp Map.empty matrix
631 List.foldl' collectCtorsHelp Map.empty matrix
608−collectCtorsHelp :: Map.Map N.Name Can.CtorAlts -> [Pattern] -> Map.Map N.Name Can.CtorAlts
634+collectCtorsHelp :: Map.Map N.Name Can.Union -> [Pattern] -> Map.Map N.Name Can.Union
609collectCtorsHelp ctors row =
635collectCtorsHelp ctors row =
610 case row of
636 case row of
611− Ctor alts name _ : _ ->
637+ Ctor union name _ : _ ->
612− Map.insert name alts ctors
638+ Map.insert name union ctors