on master help sign in
Update Nitpick from using Can.CtorAlts to using Can.Union

The necessary metadata just got centralized into an already existing thing.

commit7149cfe3e235e47da72ed3c61ee454911e2baaff authorEvan Czaplicki <evancz@users.noreply.github.com> dateWed 10 Jan 2018, 02:13 (9 years ago) parentd78e502
1 file changed+68−42
src/Nitpick/PatternMatches.hs+68−42
src/Nitpick/PatternMatches.hs+68−42
@@ -23,6 +23,8 @@ import qualified Data.Maybe as Maybe
2323import qualified Data.Text as Text
2424
2525import qualified AST.Canonical as Can
26+import qualified AST.Module.Name as ModuleName
27+import qualified Data.Index as Index
2628import qualified Elm.Name as N
2729import qualified Reporting.Annotation as A
2830import qualified Reporting.Region as R
@@ -35,8 +37,7 @@ import qualified Reporting.Region as R
3537data Pattern
3638 = Anything
3739 | Literal Literal
38− | Ctor Can.CtorAlts N.Name [Pattern]
39− deriving (Eq)
40+ | Ctor Can.Union N.Name [Pattern]
4041
4142
4243data Literal
@@ -63,16 +64,16 @@ simplify (A.At _ pattern) =
6364 Anything
6465
6566 Can.PUnit ->
66− Ctor unitAlts unitName []
67+ Ctor unit unitName []
6768
6869 Can.PTuple a b Nothing ->
69− Ctor pairAlts pairName [ simplify a, simplify b ]
70+ Ctor pair pairName [ simplify a, simplify b ]
7071
7172 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 ]
7374
74− Can.PCtor _ _ _ alts name _ args ->
75− Ctor alts name $
75+ Can.PCtor _ _ union name _ args ->
76+ Ctor union name $
7677 map (\(Can.PatternCtorArg _ _ arg) -> simplify arg) args
7778
7879 Can.PList entries ->
@@ -96,41 +97,63 @@ simplify (A.At _ pattern) =
9697
9798cons :: Can.Pattern -> Pattern -> Pattern
9899cons hd tl =
99− Ctor listAlts consName [ simplify hd, tl ]
100+ Ctor list consName [ simplify hd, tl ]
100101
101102
102103{-# NOINLINE nil #-}
103104nil :: Pattern
104105nil =
105− Ctor listAlts nilName []
106+ Ctor list nilName []
106107
107108
108109
109−-- ALTS
110+-- BUILT-IN UNIONS
110111
111112
112−{-# NOINLINE unitAlts #-}
113−unitAlts :: Can.CtorAlts
114−unitAlts =
115− Can.toAlts [ Can.CtorAlt unitName 0 ]
113+{-# NOINLINE unit #-}
114+unit :: Can.Union
115+unit =
116+ let
117+ ctor =
118+ Can.Ctor unitName Index.first 0 []
119+ in
120+ Can.Union [] [ ctor ] 1 Can.Normal
116121
117122
118−{-# NOINLINE pairAlts #-}
119−pairAlts :: Can.CtorAlts
120−pairAlts =
121− Can.toAlts [ Can.CtorAlt pairName 2 ]
123+{-# NOINLINE pair #-}
124+pair :: Can.Union
125+pair =
126+ let
127+ ctor =
128+ Can.Ctor pairName Index.first 2 [Can.TVar "a", Can.TVar "b"]
129+ in
130+ Can.Union ["a","b"] [ ctor ] 1 Can.Normal
122131
123132
124−{-# NOINLINE tripleAlts #-}
125−tripleAlts :: Can.CtorAlts
126−tripleAlts =
127− Can.toAlts [ Can.CtorAlt tripleName 3 ]
133+{-# NOINLINE triple #-}
134+triple :: Can.Union
135+triple =
136+ let
137+ ctor =
138+ Can.Ctor pairName Index.first 3 [Can.TVar "a", Can.TVar "b", Can.TVar "c"]
139+ in
140+ Can.Union ["a","b","c"] [ ctor ] 1 Can.Normal
128141
129142
130−{-# NOINLINE listAlts #-}
131−listAlts :: Can.CtorAlts
132−listAlts =
133− Can.toAlts [ Can.CtorAlt consName 2, Can.CtorAlt nilName 0 ]
143+{-# NOINLINE list #-}
144+list :: Can.Union
145+list =
146+ let
147+ nilCtor =
148+ Can.Ctor nilName Index.first 0 []
149+
150+ consCtor =
151+ Can.Ctor consName Index.second 2
152+ [ Can.TVar "a"
153+ , Can.TType ModuleName.list N.list [Can.TVar "a"]
154+ ]
155+ in
156+ Can.Union ["a"] [ nilCtor, consCtor ] 2 Can.Normal
134157
135158
136159{-# NOINLINE unitName #-}
@@ -247,6 +270,9 @@ checkExpr (A.At region expression) errors =
247270 Can.VarForeign _ _ _ ->
248271 errors
249272
273+ Can.VarCtor _ _ _ _ _ ->
274+ errors
275+
250276 Can.VarDebug _ _ _ ->
251277 errors
252278
@@ -403,7 +429,7 @@ isExhaustive matrix n =
403429 <$> isExhaustive (Maybe.mapMaybe specializeRowByAnything matrix) (n - 1)
404430
405431 else
406− let alts@(Can.CtorAlts numAlts altList) = snd (Map.findMin ctors) in
432+ let alts@(Can.Union _ altList numAlts _) = snd (Map.findMin ctors) in
407433 if numSeen < numAlts then
408434 (:)
409435 <$> Maybe.mapMaybe (isMissing alts ctors) altList
@@ -411,7 +437,7 @@ isExhaustive matrix n =
411437
412438 else
413439 let
414− isAltExhaustive (Can.CtorAlt name arity) =
440+ isAltExhaustive (Can.Ctor name _ arity _) =
415441 recoverCtor alts name arity <$>
416442 isExhaustive
417443 (Maybe.mapMaybe (specializeRowByCtor name arity) matrix)
@@ -420,21 +446,21 @@ isExhaustive matrix n =
420446 concatMap isAltExhaustive altList
421447
422448
423−isMissing :: Can.CtorAlts -> Map.Map N.Name a -> Can.CtorAlt -> Maybe Pattern
424−isMissing alts ctors (Can.CtorAlt name arity) =
449+isMissing :: Can.Union -> Map.Map N.Name a -> Can.Ctor -> Maybe Pattern
450+isMissing union ctors (Can.Ctor name _ arity _) =
425451 if Map.member name ctors then
426452 Nothing
427453 else
428− Just (Ctor alts name (replicate arity Anything))
454+ Just (Ctor union name (replicate arity Anything))
429455
430456
431−recoverCtor :: Can.CtorAlts -> N.Name -> Int -> [Pattern] -> [Pattern]
432−recoverCtor alts name arity patterns =
457+recoverCtor :: Can.Union -> N.Name -> Int -> [Pattern] -> [Pattern]
458+recoverCtor union name arity patterns =
433459 let
434460 (args, rest) =
435461 splitAt arity patterns
436462 in
437− Ctor alts name args : rest
463+ Ctor union name args : rest
438464
439465
440466
@@ -499,9 +525,9 @@ isUseful matrix vector =
499525 -- of those. But what if some of those Ctors have subpatterns
500526 -- that make them less general? If so, this actually is useful!
501527 let
502− isUsefulAlt (Can.CtorAlt ctor arity) =
528+ isUsefulAlt (Can.Ctor name _ arity _) =
503529 isUseful
504− (Maybe.mapMaybe (specializeRowByCtor ctor arity) matrix)
530+ (Maybe.mapMaybe (specializeRowByCtor name arity) matrix)
505531 (replicate arity Anything ++ patterns)
506532 in
507533 any isUsefulAlt alts
@@ -579,7 +605,7 @@ specializeRowByAnything row =
579605
580606
581607data Complete
582− = Yes [Can.CtorAlt]
608+ = Yes [Can.Ctor]
583609 | No
584610
585611
@@ -592,7 +618,7 @@ isComplete matrix =
592618 if numSeen == 0 then
593619 No
594620 else
595− let (Can.CtorAlts numAlts alts) = snd (Map.findMin ctors) in
621+ let (Can.Union _ alts numAlts _) = snd (Map.findMin ctors) in
596622 if numSeen == numAlts then Yes alts else No
597623
598624
@@ -600,16 +626,16 @@ isComplete matrix =
600626-- COLLECT CTORS
601627
602628
603−collectCtors :: [[Pattern]] -> Map.Map N.Name Can.CtorAlts
629+collectCtors :: [[Pattern]] -> Map.Map N.Name Can.Union
604630collectCtors matrix =
605631 List.foldl' collectCtorsHelp Map.empty matrix
606632
607633
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
609635collectCtorsHelp ctors row =
610636 case row of
611− Ctor alts name _ : _ ->
612− Map.insert name alts ctors
637+ Ctor union name _ : _ ->
638+ Map.insert name union ctors
613639
614640 _ ->
615641 ctors