Stream: beginners

Topic: Writing a platform


view this post on Zulip Silver (May 17 2024 at 20:38):

Hi! New to Roc, was inspired to learn it by some of the SYCL talks :)
I come from Zig land, so I'm interested in jumping right in to writing a platform to understand the low level details! Is there a reference anywhere for the functions I need to provide to roc, and how to call the entrypoint?
I've had a look at some example code but I'd like to know where the "source of truth" is for all the function names etc.
Not averse to reading the source, if that's the answer ^-^

view this post on Zulip Zeljko Nesic (May 18 2024 at 00:23):

@Silver https://github.com/roc-lang/roc/tree/main/examples/platform-switching/zig-platform :)

view this post on Zulip Luke Boswell (May 18 2024 at 05:45):

Also this conversation may be helpful https://roc.zulipchat.com/#narrow/stream/302903-Writing-a-platform/topic/How.20does.20.22roc__mainForHost_1_exposed_generic.22.20work.3F/near/435340464

view this post on Zulip Luke Boswell (May 18 2024 at 05:47):

I've been down the rabbit hole a bit lately on platform dev. When I get a spare moment, I'd like to try and write up something to help others with this, so any questions you have are really helpful.

view this post on Zulip Silver (May 18 2024 at 07:11):

@Zeljko Nesic Awesome! That's a great reference, thanks

view this post on Zulip Silver (May 18 2024 at 07:13):

I'm struggling a bit with understanding all the roc syntax involved - there's provides which, in a platform file, seems to export functions using C names? but I'm not entirely sure how those names are computed, since there's a number and sometimes a _generic or _caller or something involved

view this post on Zulip Silver (May 18 2024 at 07:16):

there's also hosted files which seem to handle interfacing with functions exposed by the host implementation, but I'm not clear on how those interact with purity.
There doesn't seem to be much documentation on Roc's effect system so I'm not really sure how that works - there's Effect, but also Task, which wraps Effect? Though ofc both of those are defined by the platform so I guess it's just convention?

view this post on Zulip Luke Boswell (May 18 2024 at 07:31):

A lot of this is not documented because it is a WIP and there are some changes planned I think. Zulip is probably the best way to find this information out.

view this post on Zulip Luke Boswell (May 18 2024 at 07:31):

I'm currently doing some touristing in Milan before I get on a plane back to Aus, but happy to help where I can.

view this post on Zulip Luke Boswell (May 18 2024 at 07:33):

https://github.com/roc-lang/roc/issues/6382

view this post on Zulip Luke Boswell (May 18 2024 at 07:34):

For example this is how we want to change the interface between roc apps and the host.

view this post on Zulip Luke Boswell (May 18 2024 at 07:34):

Actually, there is a lot described in GH issues, but it's a bit scattered.

view this post on Zulip Silver (May 18 2024 at 08:39):

Thanks, that's good to know :)

view this post on Zulip Silver (May 18 2024 at 08:58):

I've had a look at the compiler source code and I think I understand the generated function names a little better now, but I'm still not 100% clear on the difference between roc__thing_0_exposed and roc__thing_0_exposed_generic - why does the platform-switching example use the _generic variant, and when would you use the non-generic version?

view this post on Zulip Luke Boswell (May 18 2024 at 09:11):

Im not sure, I think it might have been testing two different APIs or a WIP thing.

view this post on Zulip Silver (May 18 2024 at 09:20):

Alright :)

view this post on Zulip Silver (May 18 2024 at 09:21):

How do I run roc glue? I'm not sure what arg to give it for GLUE_SPEC and it's not giving terribly helpful errors when I try to guess haha

view this post on Zulip Luke Boswell (May 18 2024 at 09:30):

roc glue crates/glue/src/RustGlue.roc glue-stuff/ path/to/platform/main.roc

view this post on Zulip Luke Boswell (May 18 2024 at 09:31):

The first argument is the path to a glue spec, the second is the dir path to generate files into, the last is the platform you want to generate for. I suggest starting with a super basic platform.

view this post on Zulip Luke Boswell (May 18 2024 at 09:32):

Also this might help
https://roc.zulipchat.com/#narrow/stream/316715-contributing/topic/Glue.20generate.20Types.20in.20JSON/near/434573060

view this post on Zulip Luke Boswell (May 18 2024 at 09:33):

I find it useful to encode the types from the compiler into JSON and then I can pretty format in my editor and easily see what the data is.

view this post on Zulip Silver (May 18 2024 at 09:34):

hmm okay, I guess I need a clone of the compiler source code for that then? since the glue specs don't seem to be distributed with the nightlies

view this post on Zulip Silver (May 18 2024 at 09:34):

That's sort of annoying since I was hoping to integrate it into my Zig build process

view this post on Zulip Luke Boswell (May 18 2024 at 09:35):

Well no, you can use a package for the glue

view this post on Zulip Luke Boswell (May 18 2024 at 09:36):

I have an example somewhere

view this post on Zulip Silver (May 18 2024 at 09:36):

Looks like the Zig glue is for an older version of Zig too, that's unfortunate

view this post on Zulip Silver (May 18 2024 at 09:36):

I'll probably just handroll it for now :)

view this post on Zulip Luke Boswell (May 18 2024 at 09:37):

Yeah thats probably best. If you are interested in helping with Zig glue please let me know.

view this post on Zulip Silver (May 18 2024 at 09:37):

I'd love to at some point, but I'll prolly focus on just learning the language first! ^-^

view this post on Zulip Luke Boswell (May 18 2024 at 09:38):

You should be able to use this...

app [makeGlue] {
    pf: platform "https://github.com/lukewilliamboswell/roc/releases/download/test/olBfrjtI-HycorWJMxdy7Dl2pcbbBoJy4mnSrDtRrlI.tar.br",
    json: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.7.0/xuaMzXRVG_SEhOFZucS3iBozlRdObWsfKaYZMHVE_q0.tar.br",
}

import pf.Types exposing [Types]
import pf.File exposing [File]
import json.Core exposing [json]

makeGlue : List Types -> Result (List File) Str
makeGlue = \typesByArch ->
    typesByArch
    |> List.map convertTypesToFile
    |> List.concat staticFiles
    |> Ok

staticFiles : List File
staticFiles = []

convertTypesToFile : Types -> File
convertTypesToFile = \types ->

    content =
        Encode.toBytes types json
        |> Str.fromUtf8
        |> Result.withDefault "INVALID UTF8 ENCODED FROM JSON"

    { name: "types.json", content }

view this post on Zulip Luke Boswell (May 18 2024 at 09:39):

Im just on my phone, but that's using a URL package of glue I have been using for testing things.

view this post on Zulip Silver (May 18 2024 at 09:39):

Ah interesting, thanks!

view this post on Zulip Luke Boswell (May 18 2024 at 09:40):

Because glue spec is an app that is just a plugin for the compiler there are only .roc source files in the package and no prebuilt binaries.

view this post on Zulip Luke Boswell (May 18 2024 at 09:45):

Also if anyone is interested you can make your own bundle for glue package using something like roc build --bundle .tar.gz crates/glue/platform/main.roc and upload that somewhere.

view this post on Zulip Silver (May 18 2024 at 10:56):

:)
image.png

view this post on Zulip Silver (May 18 2024 at 10:57):

Got effects working

view this post on Zulip Silver (May 18 2024 at 10:57):

Wasn't too bad once I figured out what everything does heheh

view this post on Zulip Silver (May 18 2024 at 11:44):

In the platform header, what's the first {} in requires {} { main : Whatever }? I've only ever seen it empty so I'm intrigued as to what might go in it :)

view this post on Zulip Luke Boswell (May 18 2024 at 11:47):

You can pass Types to the platform

view this post on Zulip Luke Boswell (May 18 2024 at 11:47):

Types that are defined in the app

view this post on Zulip Silver (May 18 2024 at 11:47):

Oh neat! So the first one is types, and the second one is defs?

view this post on Zulip Luke Boswell (May 18 2024 at 11:48):

https://github.com/lukewilliamboswell/roc-ray/blob/main/examples/pong.roc

view this post on Zulip Luke Boswell (May 18 2024 at 11:48):

Here's an example

view this post on Zulip Silver (May 18 2024 at 11:48):

Cool!

view this post on Zulip Silver (May 18 2024 at 11:50):

Is there any way to introspect those types from the host implementation? :eyes:

view this post on Zulip Silver (May 18 2024 at 11:51):

Or reflection in the platform would work too I guess, if I could generate data to pass to the host

view this post on Zulip Silver (May 18 2024 at 11:52):

I'm not yet sure how much type-level hackery Roc allows haha

view this post on Zulip Luke Boswell (May 18 2024 at 11:59):

No reflection. I don't think its possible for the host to know beforehand as it's compiled separately from the app.

view this post on Zulip Silver (May 18 2024 at 12:00):

aww that's a shame, oh well :)

view this post on Zulip Apurva Mishra (May 18 2024 at 14:48):

Silver said:

I'm not yet sure how much type-level hackery Roc allows haha

From my exploration, I don't think platform exposes much hacking for types. Though, I would really like to see it in future versions.

view this post on Zulip Silver (May 18 2024 at 15:13):

omg i only just realised I put this thread in #announcements hahahaha I'm so sorry, I meant to put this in #beginners

view this post on Zulip Silver (May 18 2024 at 15:13):

Is there a way to move it?

view this post on Zulip Luke Boswell (May 18 2024 at 17:18):

@Anton

view this post on Zulip Richard Feldman (May 18 2024 at 22:50):

moved!

view this post on Zulip Silver (May 19 2024 at 08:04):

Thanks! Sorry for the trouble haha


Last updated: Jul 06 2025 at 12:14 UTC