Roc's standard library can't open a file. Eight years in, that's the feature
Yesterday I wrote a 40-line log counter, then built it for Linux and Windows from my Mac in about half a second each. The language never touched the file it reads. It can't. There is no file function anywhere in Roc's standard library.
That gap is the most interesting thing in Roc 0.1.0, the first numbered release of Richard Feldman's functional language, which sat at #4 on Hacker News yesterday with 427 points.
What eight years bought
Feldman started sketching Roc in 2018 as an Elm-style language for everything Elm didn't cover. The compiler was rewritten from scratch in Zig and finished this year.
The release post leads with speed: a pure-Roc DEFLATE library within a 2.6% median of C's libdeflate on an M1, and cold type-checks under a second on community codebases as big as 66,000 lines. The numbers are good. But speed is a claim you take on trust until you profile your own code. The design is something you can feel in ten minutes, so that's what I tried.
The GitHub release ships one roc executable per OS and CPU, with a SHA-256 in the official install script. On Apple Silicon it's a 93 MB download that unpacks to a 290 MB binary. Feldman said in the thread that nearly all of that is LLVM and LLD, and the compiler without them is about 5 MB of WebAssembly. No sudo, no PATH changes, I ran it from a scratch folder.
The app
Here is the whole program. It counts ERROR and WARN lines in a log file:
app [main!] {
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.28.1/7brgwR8YUqfeGrkdfF1V4GPPN7DnP7ftqub9KcrTEZtr.tar.zst",
roc: "0.1.0",
}
import cli.Stdout
import cli.Path
level_of = |line| {
if line.contains("ERROR") { Error }
else if line.contains("WARN") { Warn }
else { Info }
}
tally = |lines| {
var $errors = 0.U64
var $warns = 0.U64
for line in lines {
match level_of(line) {
Error => { $errors = $errors + 1 }
Warn => { $warns = $warns + 1 }
Info => {}
}
}
{ errors: $errors, warns: $warns }
}
expect tally(["a ERROR", "b WARN", "c INFO", "d ERROR"]) == { errors: 2, warns: 1 }
main! = |args| {
path = args.get(0) ? |_| MissingPath
bytes = Path.from_os_str(path).read_bytes!()?
text = Str.from_utf8(bytes)?
{ errors, warns } = tally(text.split_on("\n"))
Stdout.line!("${errors.to_str()} errors, ${warns.to_str()} warnings")
}
Yes, that's a for loop with reassignable variables in a functional language. The $ marks anything that can change, and it can only change inside the function that declared it.
What I saw, with the build summary and the file output trimmed:
$ roc tally.roc app.log
2 errors, 1 warnings
$ roc tally.roc nope.log
Program exited with error: PathErr(NotFound, Path.unix("nope.log"))
$ roc test tally.roc
All (1) tests passed in 76.8 ms.
$ roc build tally.roc --target=x64musl --output=out/tally-linux
$ file out/tally-linux
ELF 64-bit LSB pie executable, x86-64, static-pie linked
I wrote no error handling. Each ? passes its failure up, Roc collects the possible failures into one tag union, and the platform printed the one that happened. The Linux build is a 3.2 MB static binary, and the Windows one built just as fast. I only ran the Mac binary; I have no Linux or Windows box to test the others on.
The rule you hit on day one
Every function that does I/O must end in !, and only ! functions may call it. I broke that on purpose by passing an effectful check into keep_if. roc check refused (trimmed):
── ✗ type mismatch ────────────────
impure3.roc:13:6
The method keep_if has the type:
List(a), (a -> Bool) -> List(a)
But I need it to have the type:
List(a), (Str => Bool) -> b
Hint: This function is pure, but an effectful function is expected.
The thin arrow -> is a pure function and the fat arrow => is one with effects. You can tell from a signature whether a call can touch the outside world.
The flip side: pure code runs at compile time when it can. I wrote Ok(max_errors) = U64.from_str("3x") at the top level, and roc check failed before anything ran, saying the destructure missed Err(_) and that this was "discovered empirically during compile-time evaluation." A bad config constant becomes a build error.
The platform is the capability list
A commenter, ifh-hn, said it plainly in the HN thread: "I don't really get the application/platform thing." Here is my take after one evening.
That URL in the header is the platform, and it's the only place I/O can come from. Path and Stdout above are not Roc's; they're basic-cli's. Roc's own library is all pure functions and data structures. So the header is a complete list of what the program, and every package it pulls in, is able to do. Change the header and the list changes. Leave the platform line out and you get a built-in one whose whole API, as far as I can see in the source, is printing a string.
basic-cli exposes files, HTTP, SQLite, TCP sockets and subprocesses. A browser platform would leave out the filesystem, and a package that wants it would fail to build instead of failing in production.
Two more things live in that URL. The file name is a hash of the bundle, and Roc refuses a download that doesn't match it, so a hijacked server can't swap the code under you. And the platform decides where you can ship. basic-cli includes prebuilt hosts for five targets. When I asked for wasm32, the compiler listed those five and stopped.
Compare that with a typical npm install, where any package you import can read your home directory by default. In Roc, "what can this dependency do?" is answered by one line you already have open.
What you can build today
- CLIs and scripts on
basic-cli, like the one above. - Web servers: Roux and Roxum, both benchmarked in the release post against Rust's
axum. - Full-stack web apps with Joy, which its author says he's used for paid client work for two years.
- Games on
roc-ray, and notebooks in the browser with Roclet. - Advent of Code, which starts today. More than one HN commenter is weighing it for this year's puzzles.
Rough edges
My first run printed 2.0 errors. An untyped 0 defaults to Dec, a decimal, so a counter needs 0.U64. The tutorial explains this, but it will catch anyone.
Breaking the ! naming rule is only a warning. The program still ran, then exited with status 2.
The type error above is clear about pure versus effectful but drags in a string-literal constraint that has nothing to do with my mistake.
basic-cli is a 24 MB download that unpacks to 109 MB in the cache, so the first roc check took 5.1 seconds. After that, checks took about 25 ms.
And 0.1 isn't 1.0. grayrest noted in the thread that there were breaking changes most weeks for months, and concurrency primitives are on the 2027 list, not in this release.
Try it
curl -fLO https://github.com/roc-lang/roc/releases/download/0.1.0/roc-macos_apple_silicon-0.1.0.tar.gz
tar -xzf roc-macos_apple_silicon-0.1.0.tar.gz
./roc-macos_apple_silicon-0.1.0/roc version
Then paste the program above into tally.roc and point it at any log you have.
If one line at the top of every dependency told you every file, socket and process it could touch, which dependency in your current project would you check first?
Comments
No comments yet. Start the discussion.