goldfirere / th-desugar

Desugars Template Haskell abstract syntax to a simpler format without changing semantics

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Desugared lambda expressions with @-patterns are needlessly complicated

RyanGlScott opened this issue · comments

If you desugar the expression \x@y -> f x y with th-desugar, you'll get this:

λ> putStrLn $([e| \x@y -> f x y |] >>= dsExp >>= stringE . ppShow)
DLamE
  [ arg_6989586621679031072_6989586621679031073 ]
  (DCaseE
     (DVarE arg_6989586621679031072_6989586621679031073)
     [ DMatch
         (DVarP y_6989586621679031071)
         (DLetE
            [ DValD (DVarP x_6989586621679031070) (DVarE y_6989586621679031071)
            ]
            (DAppE
               (DAppE (DVarE f) (DVarE x_6989586621679031070))
               (DVarE y_6989586621679031071)))
     ])

Cleaned up a little, that's:

\arg -> case arg of { y -> let x = y in f x y }

Yuck. Why does th-desugar needlessly introduce that arg variable? We could just as well do this:

\y -> let x = y in f x y

I originally suspected that the way that th-desugar's code is architected would make it difficult to achieve this, but upon a closer look, this is actually quite feasible. In fact, the resulting code is considerably cleaner than the original code. Patch incoming.