Stream: platform development

Topic: platform effect function calling roc closure


view this post on Zulip John Murray (Dec 18 2023 at 14:16):

Is it possible for a platform effect to take in a roc closure as a callback?

I see this example has a todo to figure out how to call the closure pointer

Could the roc__mainForHost_0_caller work to call arbitrary closures or would that only work for main? Would manually boxing the closure help?

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 15:40):

Theoretically

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 15:41):

Though, I wouldn't expect it to work easily currently

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 15:41):

That said, it may not generate in an exposed manner unless the closure is exposed via the mainForHost.

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 15:42):

A lot of the changes to platforms that are coming are related to this. In the new form, the standard will be that all effects take a continuation.

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 15:42):

Anyway, what is your goal? That may give me a better idea of what could be done.

view this post on Zulip John Murray (Dec 18 2023 at 15:47):

The use case i have is to try to hook up roc code to this rust property based testing lib

it wants all the tests to be in a closure like

runner.run(<value generators> |<generated vals>| {
       // test logic that would call the closure
    }).unwrap();

So in an ideal world the effect func would be something like

fn roc_fx_run_pbt(val_gen: Foo, testFn: RocFn) {
let mut runner = TestRunner::default();
    runner.run(val_gen, |generated| {
        testFn(generated)
    }).unwrap();
}

view this post on Zulip John Murray (Dec 18 2023 at 15:49):

I think i could probably get away with having my testing platform take in a list of tests in the mainForHost and just repeatedly call main over and over with different generated values to run the tests multiple times

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 15:57):

I'm a bit confused, why would that be an effect? Isn't that the main goal of the platform. Why not just make main be the function you are property testing?

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 16:00):

I wonder if it would currently work to just have main return a list of closures

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 16:00):

Not actually sure.

view this post on Zulip John Murray (Dec 18 2023 at 16:03):

I was hoping to have one 1 roc app that would run many different pbt tests so it would be something like

main =
    _ <- pf.PBT <some type to specify generator> (\<gen vals> -> test1)
    _ <- pf.PBT <some type to specify diff generator> (\<gen vals2> -> test2)

so the list of closures approach would probably be fine. Ill mess around with that

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 16:10):

I would say, try it, and see what llvm ir we generate (mono ir might also be useful to look at).

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 16:10):

That is the only way to tell if this might work currently or not

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 16:10):

But I do think it is totally untested.

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 16:11):

My gut feeling is that it will generate something useless cause we won't actually expose the functions. So you will have a closure capture with no function to actually pass the data to.

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 16:13):

On the otherhand, I know that main returning a record of closures works, so maybe a list of closures could work too. Though it is also untested.

view this post on Zulip Richard Feldman (Dec 18 2023 at 17:58):

John Murray said:

Is it possible for a platform effect to take in a roc closure as a callback?

technically yes, but in practice the glue for it isn't there yet - @Folkert de Vries and @Brendan Hansknecht and I have been working on this for quite awhile

view this post on Zulip Richard Feldman (Dec 18 2023 at 17:58):

once we have "effect interpreters" working, this will work too :big_smile:

view this post on Zulip Richard Feldman (Dec 18 2023 at 17:59):

because that's all about passing Roc closures to the host

view this post on Zulip John Murray (Dec 18 2023 at 18:01):

Is there a desgin/plan doc for effect interpreters? I've heard that mentioned a few times so curious what it looks like

view this post on Zulip Brendan Hansknecht (Dec 18 2023 at 18:11):

Thought the doc would be linked here, but guess not... https://www.roc-lang.org/plans

We should add details here when we collect docs and such

view this post on Zulip Richard Feldman (Dec 18 2023 at 18:12):

oh yeah we should also remove "Inspect Inference" from that page since it exists now! :smiley:


Last updated: Jul 05 2025 at 12:14 UTC