on master help sign in
Track an index for each constructor

This will make it easier to generate integer identifiers when generating production code (but still generate names in debug mode)

commit77f542d293d8b875ffb867008a990749994cc704 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 5 Dec 2017, 14:16 (9 years ago) parent2c1b2e8
7 files changed+23−14
src/AST/Canonical.hs+2 src/Canonicalize/Environment.hs+2 src/Canonicalize/Environment/Foreign.hs+4−3 src/Canonicalize/Environment/Local.hs+9−5 src/Canonicalize/Pattern.hs+2−2 src/Nitpick/PatternMatches.hs+3−3 src/Type/Constrain/Pattern.hs+1−1
src/AST/Canonical.hs+2
@@ -153,10 +153,12 @@ data Pattern_
153153 , _p_vars :: [N.Name]
154154 , _p_alts :: CtorAlts
155155 , _p_name :: N.Name
156+ , _p_index :: Index.ZeroBased
156157 , _p_args :: [PatternCtorArg]
157158 }
158159 -- CACHE _p_home, _p_type, and _p_vars for type inference
159160 -- CACHE _p_alts for exhaustiveness checker
161+ -- CACHE _p_id for production code gen
160162
161163
162164data PatternCtorArg =
src/Canonicalize/Environment.hs+2
@@ -25,6 +25,7 @@ import qualified AST.Canonical as Can
2525import qualified AST.Module.Name as ModuleName
2626import qualified Canonicalize.Result as Result
2727import qualified Data.Bag as Bag
28+import qualified Data.Index as Index
2829import qualified Data.OneOrMore as OneOrMore
2930import qualified Elm.Name as N
3031import qualified Reporting.Error.Canonicalize as Error
@@ -100,6 +101,7 @@ data Pattern =
100101 , _p_type :: N.Name
101102 , _p_vars :: [N.Name]
102103 , _p_alts :: Can.CtorAlts
104+ , _p_index :: Index.ZeroBased
103105 , _p_args :: [Can.Type]
104106 }
105107
src/Canonicalize/Environment/Foreign.hs+4−3
@@ -15,6 +15,7 @@ import qualified AST.Module.Name as ModuleName
1515import qualified Canonicalize.Environment as Env
1616import qualified Canonicalize.Result as Result
1717import qualified Data.Bag as Bag
18+import qualified Data.Index as Index
1819import qualified Data.OneOrMore as OneOrMore
1920import qualified Elm.Interface as I
2021import qualified Elm.Name as N
@@ -179,10 +180,10 @@ toPatterns home (tipe, Can.Union vars ctors) =
179180 alts =
180181 Can.ctorsToAlts ctors
181182
182− fromCtor (name, args) =
183− (name, Env.Pattern home tipe vars alts args)
183+ fromCtor index (name, args) =
184+ (name, Env.Pattern home tipe vars alts index args)
184185 in
185− map fromCtor ctors
186+ Index.indexedMap fromCtor ctors
186187
187188
188189
src/Canonicalize/Environment/Local.hs+9−5
@@ -21,6 +21,7 @@ import qualified Canonicalize.Environment.Dups as Dups
2121import qualified Canonicalize.Result as Result
2222import qualified Canonicalize.Type as Type
2323import qualified Data.Bag as Bag
24+import qualified Data.Index as Index
2425import qualified Elm.Name as N
2526import qualified Reporting.Annotation as A
2627import qualified Reporting.Error.Canonicalize as Error
@@ -86,10 +87,10 @@ addPatterns unions (Env.Env home vars types patterns binops) =
8687 Map.fromList $ concatMap toPatterns (Map.toList unions)
8788
8889 toPatterns (tipe, Can.Union tvars ctors) =
89− map (ctorToPattern tipe tvars (Can.ctorsToAlts ctors)) ctors
90+ Index.indexedMap (ctorToPattern tipe tvars (Can.ctorsToAlts ctors)) ctors
9091
91− ctorToPattern tipe tvars alts (name, args) =
92− ( name, Env.Pattern home tipe tvars alts args )
92+ ctorToPattern tipe tvars alts index (name, args) =
93+ ( name, Env.Pattern home tipe tvars alts index args )
9394 in
9495 let
9596 newPatterns =
@@ -378,8 +379,11 @@ addFreeVars (A.At region tipe) freeVars =
378379 Src.TType _ _ _ args ->
379380 foldr addFreeVars freeVars args
380381
381− Src.TRecord fields ext ->
382− foldr addFreeVars (maybe id addFreeVars ext freeVars) (map snd fields)
382+ Src.TRecord fields Nothing ->
383+ foldr addFreeVars freeVars (map snd fields)
384+
385+ Src.TRecord fields (Just ext) ->
386+ foldr addFreeVars (addFreeVars ext freeVars) (map snd fields)
383387
384388 Src.TUnit ->
385389 freeVars
src/Canonicalize/Pattern.hs+2−2
@@ -90,11 +90,11 @@ canonicalize env (A.At region pattern) =
9090 Can.PatternCtorArg index tipe
9191 <$> canonicalize env ptrn
9292 in
93− do (Env.Pattern home tipe vars alts args) <- Env.findPattern nameRegion env maybePrefix name
93+ do (Env.Pattern home tipe vars alts index args) <- Env.findPattern nameRegion env maybePrefix name
9494 verifiedList <- Index.indexedZipWithA toCanonicalArg patterns args
9595 case verifiedList of
9696 Index.LengthMatch cargs ->
97− Result.ok (Can.PCtor home tipe vars alts name cargs)
97+ Result.ok (Can.PCtor home tipe vars alts name index cargs)
9898
9999 Index.LengthMismatch actualLength expectedLength ->
100100 Result.throw (error "TODO" region actualLength expectedLength)
src/Nitpick/PatternMatches.hs+3−3
@@ -70,7 +70,7 @@ simplify (A.At _ pattern) =
7070 Can.PTuple a b (Just c) ->
7171 Ctor tripleAlts tripleName [ simplify a, simplify b, simplify c ]
7272
73− Can.PCtor _ _ _ alts name args ->
73+ Can.PCtor _ _ _ alts name _ args ->
7474 Ctor alts name $
7575 map (\(Can.PatternCtorArg _ _ arg) -> simplify arg) args
7676
@@ -607,8 +607,8 @@ collectCtors matrix =
607607collectCtorsHelp :: Map.Map N.Name Can.CtorAlts -> [Pattern] -> Map.Map N.Name Can.CtorAlts
608608collectCtorsHelp ctors row =
609609 case row of
610− Ctor info name _ : _ ->
611− Map.insert name info ctors
610+ Ctor alts name _ : _ ->
611+ Map.insert name alts ctors
612612
613613 _ ->
614614 ctors
src/Type/Constrain/Pattern.hs+1−1
@@ -61,7 +61,7 @@ add (A.At region pattern) expectation state =
6161 Can.PTuple a b maybeC ->
6262 addTuple region a b maybeC expectation state
6363
64− Can.PCtor home typeName typeVars _ ctorName args ->
64+ Can.PCtor home typeName typeVars _ ctorName _ args ->
6565 addCtor region home typeName typeVars ctorName args expectation state
6666
6767 Can.PList patterns ->