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
| Effect | What it allows | Functions that need it |
|---|---|---|
print | Showing text | print |
input | Reading what the user types | input |
env | Reading program arguments and environment variables | args, env |
read | Reading files and folders | read, exists, list_dir |
write | Writing files and folders | write, append, delete_file, make_dir |
network | Network access | fetch, post |
time | Reading the clock, waiting | now, sleep |
random | Random numbers | random, 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:
- Calling counts. Using a function that needs
readmeans you needread. - Mentioning counts. Passing
readaround as a value, without even calling it, still requires you to declare it. So an effect can't be smuggled through a callback. - Top-level code is pure. Statements outside functions can't use effects. Do that work inside
main. - Methods are pure.
.upper(),.map(...)and the rest never touch the outside world.
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.