haskell-issues icon indicating copy to clipboard operation
haskell-issues copied to clipboard

"Eta-reduce" case matches

Open Gurkenglas opened this issue 8 years ago • 2 comments

There should be something that allows you to replace

case a of
  B c -> d c
  E _ -> f

with:

case a of
  B -> d
  E _ -> f

Gurkenglas avatar Apr 12 '16 21:04 Gurkenglas

Can you provide an example where the benefit of that notation would be more clear?

I think I'd find this very confusing as a reader. Particulary when the various case patterns then appear to have different types.

sjakobi avatar Dec 27 '16 02:12 sjakobi

evaluateMyLanguage :: Fix MyLanguage -> Int
evaluateMyLanguage = cata $ \case
  Literal i -> i
  Add x y -> x + y
  Mul x y -> x * y

vs.

evaluateMyLanguage :: Fix MyLanguage -> Int
evaluateMyLanguage = cata $ \case
  Literal -> id
  Add -> (+)
  Mul -> (*)

. It's also closer to each datatype's catamorphism (maybe for Maybe, bool for Bool, foldr for [], etc.)

Gurkenglas avatar Dec 27 '16 11:12 Gurkenglas