Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a lambda function inside an if statement does not work #15

Open
Feliix42 opened this issue Nov 9, 2018 · 3 comments
Open

Using a lambda function inside an if statement does not work #15

Feliix42 opened this issue Nov 9, 2018 · 3 comments

Comments

@Feliix42
Copy link
Member

Feliix42 commented Nov 9, 2018

When trying to compile the following snippet

ns some_ns;

use sf calculations (generate_value, generate_data, calculate, check_if_to_run);

fn main() -> i32 {
    let x = generate_value();
    let y = generate_data();
    let z = check_if_to_run();

    let f = fn(d) { calculate(d, x) };

    if (z) {
        f(y)
    } else {
        y
    }
}

ohuac (v0.2.0) yields the following error:

ohuac: FatalError {fatalErrorMessage = "Undefined Binding: DFVar Binding \"y_0\"
defined vars: fromList [(Binding \"__2\",Target {operator = FnId 7, index = 1}),
(Binding \"z_0\",Target {operator = FnId 2, index = -1}),
(Binding \"a\",Target {operator = FnId 5, index = -1}),
(Binding \"x_0\",Target {operator = FnId 1, index = -1}),
(Binding \"b\",Target {operator = FnId 10, index = -1}),
(Binding \"x_0_0\",Target {operator = FnId 8, index = 0}),
(Binding \"c\",Target {operator = FnId 6, index = -1}),
(Binding \"d_0\",Target {operator = FnId 4, index = -1}),
(Binding \"y_0_0\",Target {operator = FnId 9, index = 0}),
(Binding \"__1\",Target {operator = FnId 7, index = 0})]"}

When writing y instead of f(y), the algorithm compiles.

I guess this is a bug?

@JustusAdam
Copy link
Member

Funky. Would it surprise you to hear, that the lambda is actually not the problem?
It seems y gets inlined, but only into the first if branch, not the second one.

This is the code it actually produces

let x_0 = calculations/generate_value ()
in let z_0 = calculations/check_if_to_run ()
   in let b =
          ohua.lang/if z_0
            (λ __1 ->
            let d_0 = calculations/generate_data () in let a = calculations/calculate d_0 x_0 in a)
            (λ __2 -> let c = ohua.lang/id y_0 in c)
      in b

@JustusAdam
Copy link
Member

This is because inlining an expression also removes its original binding site. I will have to do some investigating into how that can be changed to actually work correctly. I had a similar issue recently actually so I think this is a bit bigger than just this bug

@JustusAdam
Copy link
Member

BTW: if you insert the line let y2 = y and then return y2 from the else branch it also compiles correctly 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants