hydromatic / morel

Standard ML interpreter, with relational extensions, implemented in Java
Apache License 2.0
291 stars 15 forks source link

`FromBuilder` should remove intermediate renames #215

Open julianhyde opened 5 months ago

julianhyde commented 5 months ago

FromBuilder should remove intermediate renames. Consider

from i in [1, 2],
  j in [3, 4],
  k in [5, 6]
yield {a = j, b = i}
where a + b = 5

This can be converted to

from i in [1, 2],
  j in [3, 4],
  k in [5, 6]
where j + i = 5
yield {a = j, b = i}

The yield {a = j, b = i} is removed; all occurrences of a after the yield are replaced with i, and all occurrences of b are replaced with j.

A final yield is added to ensure that the from expression still has type {a:int, b:int} list.

The variable k is dead after the yield, and must not be used. (Do we need to add a new subclass of Core.FromStep to kill variables?)

julianhyde commented 5 months ago

Another optimization:

from d in dept join loc in [d.loc] yield loc

should become

from d in dept join yield d.loc

The singleton join loc in [d.loc] should be seen as a project, yield {d, d.loc}, and therefore it can be merged with the following yield loc.