nevalang / neva

🌊 Dataflow programming language with static types and implicit parallelism. Compiles to native code and Go
https://nevalang.org
MIT License
91 stars 7 forks source link

Closure (Implement "visibility scope" via hierarchy of deferred connections) #627

Open emil14 opened 4 months ago

emil14 commented 4 months ago

In control flow languages we have concept of scope

a = foo()
b = bar()
baz(a)

The reason for that is that in a control-flow language you describe program as a sequence of things that happens one after another in a single thread. So when we print(a,b) we sure both a and b exist and foo and bar(a) were computed. However, that's not the case for dataflow language

foo -> print
bar -> del

We send message from foo to println while knowing nothing about whether bar already sent or not. There is a solution for that problem - locks.

foo -> lock:data
bar -> lock:sig
lock:data -> println

However, our program just got harder to understand. And the more complex control-flow we need, the more locks and related connections we gonna have.

Scope Proposal

foo -> (
    bar -> (foo -> baz)
)

Right now this program considered invalid (see #501 for more). The idea is to change that by making compiler handle this in a specific way

bar -> lock1:data
foo -> [lock1:sig, lock2:data]
bar -> lock2:sig
lock2:data -> baz
emil14 commented 4 months ago

This could be a (part of the) solution for https://github.com/nevalang/neva/issues/473

emil14 commented 2 months ago

Can this solve this fizzbuzz version's problem?

flow FizzBuzz(data int) (res string | int) {
    nodes { h1 Helper, h2 Helper, h3 Helper }

    :data -> h1:num
    15 -> h1:den
    h1:then -> ('FizzBuzz' -> :res)

    h1:else -> (:data -> h2:num)
    3 -> h2:den
    h2:then -> ('Fizz' -> :res)

    h2:else -> (:data -> h3:num)
    5 -> h3:den
    h3:then -> ('Buzz' -> :res)

    h3:else -> (:data -> :res)
}
emil14 commented 2 months ago

Question is do we really need deferred connections here

emil14 commented 2 months ago

Could it be solved with FanOut?