unison/unison-src/transcripts/fix3265.md
Dan Doel bc7d5386ef Some tweaks and an improved test case
- visit_ was producing problematic results, because it visits every
  subexpression. This means that for:

    f x y z

  it visits all of:

    f x y z, f x y, f x, f

  But, if we take prefixes for all of those, we'll always end up
  eliminating only one variable, even if more are redundant.
- Instead of the above, use `visit` with a recursive procedure. This
  requires some other modifications, because we need to manually recurse
  into arguments of applied functions that aren't variables.
- Added a test case for the above trickiness
- Prefix should have been a newtype
2022-11-02 11:21:36 -04:00

1.1 KiB

.> builtins.merge

Tests cases that produced bad decompilation output previously. There are three cases that need to be 'fixed up.'

  1. lambda expressions with free variables need to be beta reduced
  2. let defined functions need to have arguments removed and occurrences rewritten.
  3. let-rec defined functions need to have arguments removed, but it is a more complicated process.
> Any (w x -> let
    f0 y = match y with
       0 -> x
       n -> 1 + f1 (drop y 1)
    f1 y = match y with
       0 -> w + x
       n -> 1 + f0 (drop y 1)
    f2 x = f2 x
    f3 y = 1 + y + f2 x
    g h = h 1 + x
    g (z -> x + f0 z))

Also check for some possible corner cases.

f should not have its x argument eliminated, because it doesn't always occur with x as the first argument, but if we aren't careful, we might do that, because we find the first occurrence of f, and discard its arguments, where f also occurs.

> Any (x -> let
    f x y = match y with
      0 -> 0
      _ -> f x (f y (drop y 1))

    f x 20)