// Row evidence (ADR 0066), the typing rules. A row variable in a row // argument of an effect is an evidence variable: it may stand for // handlers a caller supplies, which any other row variable refuses // (E0202). An arrow whose row ends in one is bundled. import std/io import std/list effect ask { fn ask :: -> Int } effect tell { fn tell :: line: String -> () } effect later { fn later :: render: fn() -> Int / -> Int } fn with_ask :: n: Int action: fn() -> a / -> a / { handle { action () } with { ask -> n } } fn with_tell :: action: fn() -> a / -> a / { handle { action () } with { tell line -> io.println "told: {line}" } } // The handler runs `render` in place, with the evidence `inline`'s // caller gave for `e`. fn inline :: action: fn() -> a / | e> -> a / { handle { action () } with { later render -> render () } } //~ TYPE inline: fn(fn() -> a / | e>) -> a / // The handler re-installs `ask` around `render`; a lambda calls the // bundled `render` and builds its evidence there. fn rehandled :: action: fn() -> a / > | e> -> a / { handle { action () } with { later render -> with_ask 100 fn { render () } } } //~ TYPE rehandled: fn(fn() -> a / > | e>) -> a / // The lambda receives `ask` from whoever handles `later`. fn page :: -> Int / > | r2> { later fn { ask () + 1 } } //~ TYPE page: fn() -> Int / > | r2> fn chatty :: -> Int / >, tell | r2> { tell "before" later fn { tell "inside" ask () } } //~ TYPE chatty: fn() -> Int / >, tell | r2> pub fn main :: -> () / { let a = with_ask 41 fn { inline fn { page () } } io.println "{a}" let b = with_tell fn { with_ask 7 fn { inline fn { chatty () } } } io.println "{b}" let d = with_ask 1 fn { rehandled fn { later fn { ask () } } } io.println "{d}" } fn apply :: action: fn() -> a / -> a / { action () } // Where the caller's row ends in a plain variable, an evidence row closes // there: it prints as the row it is. fn run_closed :: f { apply fn { inline f } } //~ TYPE run_closed: fn(fn() -> a / >>) -> a // An evidence variable is not dropped from an inferred row. fn open_inline :: { inline fn { page () } } //~ TYPE open_inline: fn() -> Int / // An inferred effect argument is named as a row. fn later_one :: { later fn { 1 } } //~ TYPE later_one: fn() -> Int / > // A closed row written as an effect's argument is an evidence row. fn closed_argument :: -> Int / >> { later fn { ask () } } //~ TYPE closed_argument: fn() -> Int / >> // A bundled closure is not passed where an unbundled arrow is declared: // they receive their handlers differently. fn run_plain :: f: fn() -> Int / -> Int / { f () } fn bundled_where_closed :: action: fn() -> a / > | e> -> a / { handle { action () } with { later render -> run_plain render //~ ERROR E0202 bundled } } fn bundled_where_open :: action: fn() -> a / > | e> -> a / { handle { action () } with { later render -> with_ask 100 render //~ ERROR E0202 bundled } } // A lambda that captured a handler is not one that receives it. fn captured :: -> Int / > | r2> { with_ask 1 fn { let f = fn { ask () } later f //~ ERROR E0202 } } // Where a function is passed its handlers one by one, a bundled one is // not supported yet (ADR 0066): a lambda that calls it is. fn sidebar :: -> Int / { ask () } fn named_part :: -> Int / > | r2> { later sidebar //~ ERROR E0202 one bundled as row evidence, the other one by one } effect stage { fn stage :: f: fn(Int, Int) -> Int / -> Int } fn partial_stage :: action: fn() -> a / | e> -> a / { handle { action () } with { stage f -> (f 1) 2 //~ ERROR E0202 partially applied } } fn inline_value :: -> () { let f = inline //~ ERROR E0202 a function name used as a value () } // A function with a closed row is called the plain way from a bundled // lambda: its row closes there (issue #362). fn twice :: n: Int -> Int { n * 2 } fn shout :: line: String -> () / { tell line } fn closed_callees :: -> Int / > | r2> { later fn { shout (show (list.length [1, 2, 3])) twice (ask ()) } } //~ TYPE closed_callees: fn() -> Int / > | r2>