on master help sign in
Generate fewer destructors
6114d0eEvan Czaplicki
src/Optimize/Expression.hs+29−14
@@ -251,11 +251,18 @@ destructHelp path (A.At region pattern) revDs =
251 destructTwo path a b revDs
251 destructTwo path a b revDs
252
252
253 Can.PTuple a b (Just c) ->
253 Can.PTuple a b (Just c) ->
254+ case path of
255+ Opt.Root _ ->
256+ destructHelp (Opt.Index Index.first path) a =<<
257+ destructHelp (Opt.Index Index.second path) b =<<
258+ destructHelp (Opt.Index Index.third path) c revDs
259+
260+ _ ->
254− do name <- Names.generate
261+ do name <- Names.generate
255− let newRoot = Opt.Root name
262+ let newRoot = Opt.Root name
256− destructHelp (Opt.Index Index.first newRoot) a =<<
263+ destructHelp (Opt.Index Index.first newRoot) a =<<
257− destructHelp (Opt.Index Index.second newRoot) b =<<
264+ destructHelp (Opt.Index Index.second newRoot) b =<<
258− destructHelp (Opt.Index Index.third newRoot) c (Opt.Destructor name path : revDs)
265+ destructHelp (Opt.Index Index.third newRoot) c (Opt.Destructor name path : revDs)
259
266
260 Can.PList [] ->
267 Can.PList [] ->
261 pure revDs
268 pure revDs
@@ -276,19 +283,27 @@ destructHelp path (A.At region pattern) revDs =
276 pure revDs
283 pure revDs
277
284
278 Can.PCtor _ _ _ _ _ args ->
285 Can.PCtor _ _ _ _ _ args ->
286+ case path of
287+ Opt.Root _ ->
288+ foldM (destructCtorArg path) revDs args
289+
290+ _ ->
279− do name <- Names.generate
291+ do name <- Names.generate
280− foldM
281− (destructCtorArg (Opt.Root name))
292+ foldM (destructCtorArg (Opt.Root name)) (Opt.Destructor name path : revDs) args
282− (Opt.Destructor name path : revDs)
283− args
284
293
285
294
286destructTwo :: Opt.Path -> Can.Pattern -> Can.Pattern -> [Opt.Destructor] -> Names.Tracker [Opt.Destructor]
295destructTwo :: Opt.Path -> Can.Pattern -> Can.Pattern -> [Opt.Destructor] -> Names.Tracker [Opt.Destructor]
287destructTwo path a b revDs =
296destructTwo path a b revDs =
297+ case path of
298+ Opt.Root _ ->
299+ destructHelp (Opt.Index Index.first path) a =<<
300+ destructHelp (Opt.Index Index.second path) b revDs
301+
302+ _ ->
288− do name <- Names.generate
303+ do name <- Names.generate
289− let newRoot = Opt.Root name
304+ let newRoot = Opt.Root name
290− destructHelp (Opt.Index Index.first newRoot) a =<<
305+ destructHelp (Opt.Index Index.first newRoot) a =<<
291− destructHelp (Opt.Index Index.second newRoot) b (Opt.Destructor name path : revDs)
306+ destructHelp (Opt.Index Index.second newRoot) b (Opt.Destructor name path : revDs)
292
307
293
308
294destructCtorArg :: Opt.Path -> [Opt.Destructor] -> Can.PatternCtorArg -> Names.Tracker [Opt.Destructor]
309destructCtorArg :: Opt.Path -> [Opt.Destructor] -> Can.PatternCtorArg -> Names.Tracker [Opt.Destructor]