on master help sign in
Make sure pattern match destructures come out in the right order

The only important change is in Opt.Case, but the changes about destructuring lists and tuples in order do not hurt anything.

commit64f3f0a8f4baf42cc2ac6c424ca8a943cf0c8716 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 30 Jan 2018, 16:36 (9 years ago) parent1b73f5f
1 file changed+18−13
src/Optimize/Expression.hs+18−13
src/Optimize/Expression.hs+18−13
@@ -129,7 +129,7 @@ optimize cycle (A.At region expression) =
129129 Can.Case expr branches ->
130130 let
131131 optimizeBranch root (Can.CaseBranch pattern branch) =
132− do destructors <- destructHelp (Opt.Root root) pattern []
132+ do destructors <- destructCase root pattern
133133 obranch <- optimize cycle branch
134134 pure (pattern, foldr Opt.Destruct obranch destructors)
135135 in
@@ -209,6 +209,11 @@ destructArgs args =
209209 return (argNames, concat destructorLists)
210210
211211
212+destructCase :: N.Name -> Can.Pattern -> Names.Tracker [Opt.Destructor]
213+destructCase rootName pattern =
214+ reverse <$> destructHelp (Opt.Root rootName) pattern []
215+
216+
212217destruct :: Can.Pattern -> Names.Tracker (N.Name, [Opt.Destructor])
213218destruct pattern@(A.At _ ptrn) =
214219 case ptrn of
@@ -254,16 +259,16 @@ destructHelp path (A.At region pattern) revDs =
254259 Can.PTuple a b (Just c) ->
255260 case path of
256261 Opt.Root _ ->
257− destructHelp (Opt.Index Index.first path) a =<<
262+ destructHelp (Opt.Index Index.third path) c =<<
258263 destructHelp (Opt.Index Index.second path) b =<<
259− destructHelp (Opt.Index Index.third path) c revDs
264+ destructHelp (Opt.Index Index.first path) a revDs
260265
261266 _ ->
262267 do name <- Names.generate
263268 let newRoot = Opt.Root name
264− destructHelp (Opt.Index Index.first newRoot) a =<<
269+ destructHelp (Opt.Index Index.third newRoot) c =<<
265270 destructHelp (Opt.Index Index.second newRoot) b =<<
266− destructHelp (Opt.Index Index.third newRoot) c (Opt.Destructor name path : revDs)
271+ destructHelp (Opt.Index Index.first newRoot) a (Opt.Destructor name path : revDs)
267272
268273 Can.PList [] ->
269274 pure revDs
@@ -285,11 +290,11 @@ destructHelp path (A.At region pattern) revDs =
285290
286291 Can.PCtor _ _ (Can.Union _ _ _ opts) _ _ args ->
287292 case args of
288− [Can.PatternCtorArg i _ arg] ->
293+ [Can.PatternCtorArg _ _ arg] ->
289294 case opts of
290− Can.Normal -> destructHelp (Opt.Index i path) arg revDs
295+ Can.Normal -> destructHelp (Opt.Index Index.first path) arg revDs
291296 Can.Unbox -> destructHelp (Opt.Unbox path) arg revDs
292− Can.Enum -> destructHelp (Opt.Index i path) arg revDs
297+ Can.Enum -> destructHelp (Opt.Index Index.first path) arg revDs
293298
294299 _ ->
295300 case path of
@@ -305,14 +310,14 @@ destructTwo :: Opt.Path -> Can.Pattern -> Can.Pattern -> [Opt.Destructor] -> Nam
305310destructTwo path a b revDs =
306311 case path of
307312 Opt.Root _ ->
308− destructHelp (Opt.Index Index.first path) a =<<
309− destructHelp (Opt.Index Index.second path) b revDs
313+ destructHelp (Opt.Index Index.second path) b =<<
314+ destructHelp (Opt.Index Index.first path) a revDs
310315
311316 _ ->
312317 do name <- Names.generate
313318 let newRoot = Opt.Root name
314− destructHelp (Opt.Index Index.first newRoot) a =<<
315− destructHelp (Opt.Index Index.second newRoot) b (Opt.Destructor name path : revDs)
319+ destructHelp (Opt.Index Index.second newRoot) b =<<
320+ destructHelp (Opt.Index Index.first newRoot) a (Opt.Destructor name path : revDs)
316321
317322
318323destructCtorArg :: Opt.Path -> [Opt.Destructor] -> Can.PatternCtorArg -> Names.Tracker [Opt.Destructor]
@@ -391,7 +396,7 @@ optimizeTail cycle rootName argNames locExpr@(A.At _ expression) =
391396 Can.Case expr branches ->
392397 let
393398 optimizeBranch root (Can.CaseBranch pattern branch) =
394− do destructors <- destructHelp (Opt.Root root) pattern []
399+ do destructors <- destructCase root pattern
395400 obranch <- optimizeTail cycle rootName argNames branch
396401 pure (pattern, foldr Opt.Destruct obranch destructors)
397402 in