ceu-lang / ceu

The Programming Language Céu
http://www.ceu-lang.org/
Other
218 stars 23 forks source link

Example 15 from "Try Ceu" page isn't correct #135

Open danbst opened 5 years ago

danbst commented 5 years ago

I've modified code (copying here original one)

var int ret = 0;
par/or do
    await async (ret) do
        // calculates fat of 10
        var int fat = 1;
        var int i = 10;
        loop do
            if i == 0 then
                break;
            end
            fat = fat * i;
            i = i - 1;
        end
        ret = fat;
    end;
with
    await 1s;  // kill the async if it takes long
    ret = 0;
end
escape ret;

and set var int i = 1;. I expected factorial of 1 to be computed faster than 1s, but surprisingly result was 0.

How can this be explained?

fsantanna commented 5 years ago

Hi @danbst,

The behavior is correct. Unfortunately, the example/explanation is mostly incomplete. To get the expected behavior, you need to split the emit 1s, into multiple emit 100ms.

Since the examples are running on the server in "simulation mode", all input is artificial and has no relation with the real world. If your program says emit 1s, Céu will just assume that happened and simulate that 1s elapsed, so it will not have time to advance the async trail. When you split the simulation into multiple steps of 100ms, each time Céu will also advance the async trail.

(As an alternative, you could also use an async/thread trail to get real parallelism, but it's not supported in the online tutorial.)

Let me know if you have further question.

Regards, Francisco

danbst commented 5 years ago

Oh, ok. Then I propose to change that example, so there are 10 emit 1s; in "input", and await 10s in code. So, reducing factorial number would actually work.

fsantanna commented 5 years ago

Sure! Thanks for the feedback!