Skip to content

`elem` applied to a known string should compile to a case expression.

Motivation

Writing foo x = x elem "abc" should compile to a case expression like


case x of
  'a' -> True
  'b' -> True
  'c' -> True
  _ -> False

Case expression are in general either O(log(n)) or O(1) whereas a traversal based elem is O(n) so this can be a big performance win.

Code of this sort is particularly common for parser/lexer like code. And ghcide at some point saw double digit % reductions in runtime when doing this transformation by hand.

Proposal

Ideally we would have a rule for this transformation which precedence over more general rules applying to elem.

However there is currently no easy way to achieve this.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information