CategoricalData / hydra

Transformations transformed
Apache License 2.0
64 stars 9 forks source link

Flatten nested 'let' terms for transformation into Java #110

Closed joshsh closed 9 months ago

joshsh commented 9 months ago

When we map 'let' terms into Java, we turn the individual bindings into local variables, taking care to get the order of variable definitions right, and wrapping them in a container for delayed evaluation in the case of recursive variables.

However, nested 'let' terms are currently unsupported. For example:

let a = 1
    b = let x = 10
            y = 20
            in x + y
    in a + b

This is a perfectly reasonable term in Haskell, but in order to map it into Java, we will need to flatten it first, e.g.

let a = 1
    b = b_x + b_y
    b_x = 10
    b_y = 20
    in a + b

Now we have siblings which reference each other, but this is supported so long as there are no cycles. This flattened structure can be mapped directly into Java.

wisnesky commented 9 months ago

That this transformation is semantics preserving follows from the associativity law for monads! https://wiki.haskell.org/Monad_laws