import std/io import std/process.{type proc} import std/ref import std/result effect log { fn log :: msg: String -> () } effect ask { fn ask :: -> Int } effect gen { ctl yield :: x: Int -> () } effect fail { final ctl fail :: reason: String -> Never } effect state { fn get :: -> s fn put :: x: s -> () } // Performing an operation adds its effect. fn greet :: name { log ("hi " <> name) } //~ TYPE greet: fn(String) -> () / fn counter :: { put (get () + 1) } //~ TYPE counter: fn() -> () / > // Higher-order functions are effect-polymorphic. fn twice :: f { f () f () } //~ TYPE twice: fn(fn() -> a / ) -> a / // A handler removes its effect from the body's row. A callback called // inside the handler receives it, so its type must be declared (ADR 0010). fn quiet :: action: fn() -> a / -> a / { handle { action () } with { log m -> () } } //~ TYPE quiet: fn(fn() -> a / ) -> a / fn quiet_inferred :: action { handle { action () //~ ERROR E0703 `log` handler be used outside its scope } with { log m -> () } } // `fn` clauses produce the operation's result. fn answer :: { handle { ask () + ask () } with { ask -> 21 } } //~ TYPE answer: fn() -> Int // `ctl` clauses produce the handler's result and may resume. fn collect :: action: fn() -> a / -> List / { handle { action () } with { yield x -> [x, ..resume ()], return _ -> [] } } //~ TYPE collect: fn(fn() -> a / ) -> List / // `final ctl` never resumes: the handler's result replaces the body's. fn attempt :: action: fn() -> a / -> Option / { handle { Some(action ()) } with { fail _ -> None } } //~ TYPE attempt: fn(fn() -> a / ) -> Option / // Annotated signatures bound what a body may perform. pub fn total :: -> () { log "no" //~ ERROR E0502 performs `log` which is not handled here } pub fn declared :: -> () / { log "yes" } // A declared effect the body never performs draws a warning (the row is // the contract a caller pays for; issue #203, option A). pub fn dead_member :: -> () / //~ WARN W0501 { log "yes" } // A lambda's own effects are its scope's, not the fn's; the call to it // performs them, so this row is honest (no warning). pub fn via_lambda :: -> () / { let f = fn { log "in a lambda" } f () } // A callee's row is included in the caller's: a callback with fewer // effects than its caller is fine, because operations go to the handlers // visible where they are written, never to the caller's (ADR 0010). pub fn same_row :: f: fn() -> () / -> () / { f () log "after" } pub fn fewer_effects :: f: fn() -> () / -> () / { f () log "after" } // A total function can be passed where any row is expected (opening). fn pure_callback :: -> () / { same_row fn { () } } pub fn rigid_row :: f: fn() -> () / -> () / { f () log "extra" //~ ERROR E0502 performs `log` which is not handled here } // Calls `f` once (a stand-in for `result.try`'s callback: anything taking // a function). fn calls :: f: fn() -> () / -> () / { f () } // In a callback (a lambda), a concrete effect and then the action, whose // row's tail is rigid: the callback's flexible tail becomes the callee's // extras over the rigid one (was E0502; the postgres package's // `savepoint` is this shape). pub fn use_callback :: action: fn() -> () / -> () / { calls fn { log "first" action () } } //~ TYPE use_callback: fn(fn() -> () / ) -> () / // The same with the concrete effect after the rigid-tail call: the // order no longer matters — the rigid tail binds at settle, when the // callback's row has finished growing (issue #312). pub fn use_callback_late :: action: fn() -> () / -> () / { calls fn { action () log "after" } } //~ TYPE use_callback_late: fn(fn() -> () / ) -> () / // A use-continuation may perform more of the row after passing a // row-polymorphic parameter (issue #312): the rigid tail binds at // settle, not at the call. fn read312 :: env: fn(String) -> Option / -> Result / { match env "b" { Some(_) -> Ok 1 //~ ERROR E0216 , None -> Error "missing" //~ ERROR E0216 } } fn twice312 :: env: fn(String) -> Option / -> Result / { use a <- result.try (Ok 1) //~ ERROR E0216 use b <- result.try (read312 env) let _ = process.subject () Ok(a + b) } //~ TYPE twice312: fn(fn(String) -> Option / ) -> Result / // Built-in effects come from the standard library. pub fn main :: -> () / { io.println "hello" } pub fn counter_ref :: -> Int { region { let r = ref.new 0 ref.set r (ref.get r + 1) ref.get r } } pub fn state_total :: -> () { counter () //~ ERROR E0502 performs `state` } // Handler shape errors. fn missing_op :: { handle { //~ ERROR E0504 does not handle operation `put` counter () } with { get -> 0 } } fn mixed :: { handle { greet "x" } with { log m -> () , ask -> 1 //~ ERROR E0501 this clause is for `ask`, not `log` } } fn wrong_clause_type :: { handle { ask () } with { ask -> "not an int" //~ ERROR E0202 expected `Int`, found `String` } } fn no_resume_in_fn_clause :: { handle { log "x" } with { log m -> resume () //~ ERROR E0101 only available in `ctl` handler clauses } } // Lambdas carry their own effects. pub fn lambda_effect :: -> () { let f = fn { log "x" } //~^ ERROR E0502 performs `log` which is not handled here f () } // Lexical scoping (ADR 0010): an operation goes to the nearest handler // where it is written; its type arguments do not pick another one. // Here `get()` reaches the inner handler, so that handler's state is an // Int and its String clause is wrong. fn nearest_handler :: { handle { handle { let n: Int = get () n } with { get -> "inner" //~ ERROR E0202 expected `Int`, found `String` , put x -> () } } with { get -> 0, put x -> () } } // A closure cannot outlive the handler it uses. fn escaping_closure :: { let f = handle { //~ ERROR E0703 `log` handler be used outside its scope fn { log "x" } } with { log m -> () } f } // A lambda written inside a handler uses that handler, so it cannot be // passed where a callback that receives `log` from its caller is expected. fn stored_lambda :: -> () / { handle { let f = fn { log "x" } quiet f //~ ERROR E0202 receives the `log` handler from its caller } with { log m -> io.println m } } // Written directly as the argument, it receives `quiet`'s handler. fn direct_lambda :: -> () { quiet fn { log "x" } } // Effect rows must contain effects. pub fn bad_row :: x: Int -> Int / //~ ERROR E0505 `Int` is not an effect { x } pub fn row_var_first :: f: fn() -> () / -> () //~^ ERROR E0505 must come last { () } // Trait methods declare effects; impls may not add any. trait Greeter { fn greet_with :: x: a -> String } impl Greeter { fn greet_with :: x { log "side effect" //~ ERROR E0502 performs `log` which is not handled here show x } }