Effects and permissions

This is the idea Cresset is built around. A pure function only computes: given the same inputs it returns the same output and touches nothing else. An effect is anything that reaches outside the program or is not repeatable: printing, reading files, writing files, the network, the clock, randomness.

In Cresset, a function must declare the effects it uses with needs, and the compiler checks it.

The effects

EffectWhat it allowsFunctions that need it
printShowing textprint
inputReading what the user typesinput
envReading program arguments and environment variablesargs, env
readReading files and foldersread, exists, list_dir
writeWriting files and folderswrite, append, delete_file, make_dir
networkNetwork accessfetch, post
timeReading the clock, waitingnow, sleep
randomRandom numbersrandom, randint, shuffle, choice

Declaring effects

fn load_scores(path) needs read:
    text = read(path)?
    return ok(text.lines().len())

fn main() needs print, read:
    print(load_scores("examples/scores.txt"))

load_scores says it needs read. main calls it, so main must declare read too. Effects flow upward: whoever uses an effectful function takes on its effects.

What the compiler refuses

A function that uses an effect it didn't declare is an error, found before any code runs:

fn left_pad(text, width):
    secret = read("C:/Users/secrets.txt")
    return text

fn main() needs print:
    print(left_pad("hi", 5))
error: 'left_pad' uses 'read', which needs the 'read' permission, but doesn't declare it
  --> examples/denied.crs:5
   |  secret = read("C:/Users/secrets.txt")
help: add the permission to the signature:  fn left_pad(text, width) needs read:

The rule is strict on purpose:

Granting permissions when you run

Declaring what a program wants is only half. You decide what it gets, each time you run it:

node cresset.js run program.crs --allow read,write

By default only print is allowed. If main needs more than you granted, Cresset refuses to start and says exactly which permissions it asked for. This is checked before anything runs.

Even after the checks pass, every effect still goes through a runtime guard that enforces the grant. The static check catches mistakes early; the runtime guard is a second wall behind it.

A limit worth knowing

Cresset tracks effects by name. If a function receives another function as a parameter and calls it, the compiler can't tell which one it will be. The rule that mentioning an effectful function requires declaring it covers this: whoever passes read in has to declare read. Inside a package the runtime cap catches the rest.

Next

Give a dependency only the permissions it needs: Packages.