Stream: ideas

Topic: simulating tasks


view this post on Zulip Ayaz Hafiz (Jun 05 2023 at 00:29):

Question on simulation, couldn't the roc effect interpreter just track a state?

It could do this, I guess the main cost there is that then you must create an ad hoc state for each interpreter and simulation you want to run

view this post on Zulip Ayaz Hafiz (Jun 05 2023 at 00:30):

another alternative is to provide effect handlers instead of an effect interpreter, and each effect handler takes the same “state” parameter. but then the language needs some machinery to make sure the state parameter is consistent - Im not sure if this idea works.

view this post on Zulip Notification Bot (Jun 05 2023 at 01:01):

A message was moved here from #ideas > Stored ability by Richard Feldman.

view this post on Zulip Richard Feldman (Jun 05 2023 at 01:02):

I'm interested in both of these ideas! What would they look like? :thinking:

view this post on Zulip Richard Feldman (Jun 05 2023 at 01:03):

like let's say Task is a builtin, and the way you specify what entries get put into the state machine the same way effects get specified today (you provide a type signature and the compiler takes care of generating it)

view this post on Zulip Richard Feldman (Jun 05 2023 at 01:04):

let's also assume we can have a language feature for creating simulated tasks (e.g. by specifying an effect interpreter)

view this post on Zulip Richard Feldman (Jun 05 2023 at 01:04):

what would those language features look like? how would they interact with map2? etc.

view this post on Zulip Ayaz Hafiz (Jun 05 2023 at 01:07):

Ill follow up here tomorrow

view this post on Zulip Ayaz Hafiz (Jun 05 2023 at 01:09):

let's also assume we can have a language feature for creating simulated tasks (e.g. by specifying an effect interpreter)

I will note right now that these are two different things though, right. Creating a task to simulate by way of an effect interpreter is separate from defining an effect interpreter. thy are two halves of the whole!

view this post on Zulip Richard Feldman (Jun 05 2023 at 01:28):

yeah definitely

view this post on Zulip Richard Feldman (Jun 05 2023 at 01:29):

like in the #ideas > Task as builtin proposal, in Roc code you specify types for the task operations you want to exist, and then the compiler generates a state machine from that, and then the host interprets that state machine

view this post on Zulip Richard Feldman (Jun 05 2023 at 01:29):

so potentially, there could be a way to specify that state machine interpreter in pure Roc code instead

view this post on Zulip Ayaz Hafiz (Jun 06 2023 at 03:07):

the basic premise of the handler idea is you provide only the handlers you need and the language plugs everything in for you for the purposes of effect interpretation. I'm eliding some details that are important here, but suppose you have effects of the form

httpGet : Request -> Task Response ...
tcpWrite : Bytes -> Task {} ...
tcpRead : Nat -> Task Bytes ...
# so on

suppose you now construct a Task whose evaluation you'd like to simulate (in this model, the simulation is a Task directly, composed inductively of the effects defined above). Let's say we have our effect-to-be-evaluated in a variable programEffect. There are two ways we could imagine evaluating programEffect in Roc code directly:

evaluate programEffect {
  handleTcpRead : \state, {} ->
    bytes = if state.count == 1 then [1,2, 3] else [1,2,3,4]
    ({state & count = state.count + 1}, Ok bytes)
}

this would evaluate programEffect in "the natural way", handling tcpRead as provided and crashing on httpGet/tcpWrite effects. It would handle map2 in the only way map2 can be handled.

I'm intentionally glossing over the syntax and whether this would need to be builtin to the language or provided by a library, but I think it requires at least some language support (as any effect interpreter would, namely because of map2). I hope this example elucidates my original message in the thread.

view this post on Zulip Richard Feldman (Jun 07 2023 at 03:19):

it does, thanks! :thumbs_up:


Last updated: Jun 16 2026 at 16:19 UTC