|
| 1 | +// s2 E1 EFFECT ROWS (vic 2026-11-09, vic/c/rt-effects-design.md RULED): the protocols each def needs, a Datalog program |
| 2 | +// over the source bodies, as s2/pure.srs. a request P.op(...) names the protocol; a constructor is a request on Region; |
| 3 | +// a {} binder of a protocol declares it. a call in handle scope S passes on what its callee needs, except what S handles. |
| 4 | +// needs(F, P) :- req(F, P) | decl(F, P) | calls(F, G, S), needs(G, P), not hd(S, P) |
| 5 | +// row(F, P) :- needs(F, P), not decl(F, P) |
| 6 | +// req(F, P) is written only when no handle of P is in scope; scope 0 handles nothing. each row(F, P) is one implicit |
| 7 | +// {eff.P: P} binder of F (Ty.fn); main has none, so a request main does not handle is error[unhandled-effect]. |
| 8 | + |
| 9 | +enum Eff.Rel: |
| 10 | + case Eff.RReq |
| 11 | + case Eff.RDecl |
| 12 | + case Eff.RCalls |
| 13 | + case Eff.RHd |
| 14 | + case Eff.RNeeds |
| 15 | + case Eff.RRow |
| 16 | + |
| 17 | +def Eff.relSym {r: Region} (rel: Eff.Rel): Sym.Sym = rel match |
| 18 | + case Eff.RReq => Sym.of("req") |
| 19 | + case Eff.RDecl => Sym.of("decl") |
| 20 | + case Eff.RCalls => Sym.of("calls") |
| 21 | + case Eff.RHd => Sym.of("hd") |
| 22 | + case Eff.RNeeds => Sym.of("needs") |
| 23 | + case Eff.RRow => Sym.of("row") |
| 24 | + |
| 25 | +// ── requests: P.op names op of protocol P (one symbol: the last `.` splits it) ── |
| 26 | +enum Eff.Req: |
| 27 | + case Eff.Req(p: Sym.Sym, op: Sym.Sym) |
| 28 | + |
| 29 | +def Eff.reqOf {r: Region} (ps: Coll.List[Ast.Proto], f: Sym.Sym): Coll.Option[Eff.Req] = |
| 30 | + val s = Sym.str(f) |
| 31 | + Eff.lastDot(s, rt_str_len(s) - 1) match |
| 32 | + case Coll.None => Coll.None |
| 33 | + case Coll.Some(i) => |
| 34 | + val p = Sym.of(rt_str_slice(s, 0, i)) |
| 35 | + val op = Sym.of(rt_str_slice(s, i + 1, rt_str_len(s))) |
| 36 | + Ty.protoOp(ps, p, op) match |
| 37 | + case Coll.Some(_) => Coll.Some(Eff.Req(p, op)) |
| 38 | + case Coll.None => Coll.None |
| 39 | + |
| 40 | +// 46 = `.` |
| 41 | +def Eff.lastDot {r: Region} (s: Str, i: I64): Coll.Option[I64] = |
| 42 | + if i < 0 then Coll.None else if rt_str_at(s, i) == 46 then Coll.Some(i) else Eff.lastDot(s, i - 1) |
| 43 | + |
| 44 | +// the symbol `a.b` (srs has no string concatenation: the printer writes it) |
| 45 | +def Eff.dotted {r: Region} (a: Str, b: Sym.Sym): Sym.Sym = |
| 46 | + rt_capture_begin() |
| 47 | + rt_print(a) |
| 48 | + rt_print(".") |
| 49 | + rt_print(Sym.str(b)) |
| 50 | + Sym.of(rt_capture_end()) |
| 51 | + |
| 52 | +// ── rows: per def, the implicit capabilities, each a protocol and its binder name eff.P ── |
| 53 | +enum Eff.Cap: |
| 54 | + case Eff.Cap(p: Sym.Sym, x: Sym.Sym) |
| 55 | + |
| 56 | +enum Eff.Rows: |
| 57 | + case Eff.Rows(m: Coll.SMap[Coll.List[Eff.Cap]]) |
| 58 | + |
| 59 | +enum Eff.Set: |
| 60 | + case Eff.Got(rows: Eff.Rows) |
| 61 | + case Eff.RulesBad(rel: Sym.Sym) |
| 62 | + |
| 63 | +def Eff.none {r: Region} (): Eff.Rows = Eff.Rows(Coll.sEmpty()) |
| 64 | + |
| 65 | +def Eff.row {r: Region} (rs: Eff.Rows, f: Sym.Sym): Coll.List[Eff.Cap] = |
| 66 | + val Eff.Rows(m) = rs |
| 67 | + Coll.sLookup(m, f) match |
| 68 | + case Coll.Some(cs) => cs |
| 69 | + case Coll.None => Coll.Nil |
| 70 | + |
| 71 | +def Eff.has {r: Region} (rs: Eff.Rows, f: Sym.Sym, p: Sym.Sym): Bool = Eff.capIn(Eff.row(rs, f), p) |
| 72 | + |
| 73 | +def Eff.capIn(cs: Coll.List[Eff.Cap], p: Sym.Sym): Bool = cs match |
| 74 | + case Coll.Nil => false |
| 75 | + case Coll.Cons(c, rest) => |
| 76 | + val Eff.Cap(q, _) = c |
| 77 | + if Sym.eq(q, p) then true else Eff.capIn(rest, p) |
| 78 | + |
| 79 | +def Eff.rows {r: Region} (ds: Coll.List[Ast.Decl]): Eff.Set = |
| 80 | + val cx = Eff.cxOf(ds, Eff.Cx(Coll.Nil, Coll.sEmpty(), Coll.sEmpty(), Coll.sEmpty(), Eff.syms())) |
| 81 | + D.solve(Eff.rules(), Eff.decls(cx, ds, Eff.W(1, Coll.Nil, Coll.Nil, Coll.sEmpty()))) match |
| 82 | + case D.Solved(m) => Eff.Got(Eff.Rows(Eff.group(Eff.capNames(cx), D.tuples(m, Eff.relSym(Eff.RRow)), Coll.sEmpty()))) |
| 83 | + case D.Unsafe(rel) => Eff.RulesBad(rel) |
| 84 | + case D.NegCycle(rel) => Eff.RulesBad(rel) |
| 85 | + |
| 86 | +// row tuples (F, P) by F; main has no row (vic: main must have the empty row) |
| 87 | +def Eff.group {r: Region} (names: Coll.SMap[Sym.Sym], ts: Coll.List[Coll.List[D.Val]], m: Coll.SMap[Coll.List[Eff.Cap]]): Coll.SMap[Coll.List[Eff.Cap]] = ts match |
| 88 | + case Coll.Nil => m |
| 89 | + case Coll.Cons(t, rest) => t match |
| 90 | + case Coll.Cons(fv, more) => more match |
| 91 | + case Coll.Cons(pv, _) => fv match |
| 92 | + case D.VSym(f) => pv match |
| 93 | + case D.VSym(p) => |
| 94 | + if Sym.eq(f, Sym.of("main")) then Eff.group(names, rest, m) |
| 95 | + else |
| 96 | + val cs: Coll.List[Eff.Cap] = Coll.sLookup(m, f) match |
| 97 | + case Coll.Some(xs) => xs |
| 98 | + case Coll.None => Coll.Nil |
| 99 | + val x: Sym.Sym = Coll.sLookup(names, p) match |
| 100 | + case Coll.Some(n) => n |
| 101 | + case Coll.None => p |
| 102 | + Eff.group(names, rest, Coll.sInsert(m, f, Coll.Cons(Eff.Cap(p, x), cs))) |
| 103 | + case D.VInt(_) => Eff.group(names, rest, m) |
| 104 | + case D.VInt(_) => Eff.group(names, rest, m) |
| 105 | + case Coll.Nil => Eff.group(names, rest, m) |
| 106 | + case Coll.Nil => Eff.group(names, rest, m) |
| 107 | + |
| 108 | +// eff.P for each protocol P |
| 109 | +def Eff.capNames {r: Region} (cx: Eff.Cx): Coll.SMap[Sym.Sym] = |
| 110 | + val Eff.Cx(ps, _, _, _, _) = cx |
| 111 | + Eff.namesOf(ps, Coll.sEmpty()) |
| 112 | + |
| 113 | +def Eff.namesOf {r: Region} (ps: Coll.List[Ast.Proto], m: Coll.SMap[Sym.Sym]): Coll.SMap[Sym.Sym] = ps match |
| 114 | + case Coll.Nil => m |
| 115 | + case Coll.Cons(p, rest) => |
| 116 | + val Ast.Proto(_, n, _, _, _, _, _, _) = p |
| 117 | + Eff.namesOf(rest, Coll.sInsert(m, n, Eff.dotted("eff", n))) |
| 118 | + |
| 119 | +// ── rules ── |
| 120 | +def Eff.atom {r: Region} (rel: Eff.Rel, xs: Coll.List[I64]): D.Atom = D.Atom(Eff.relSym(rel), Pure.vars(xs)) |
| 121 | + |
| 122 | +def Eff.rules {r: Region} (): Coll.List[D.Rule] = |
| 123 | + val n1 = D.Rule(Eff.atom(Eff.RNeeds, Pure.two(0, 1)), Coll.Cons(D.Pos(Eff.atom(Eff.RReq, Pure.two(0, 1))), Coll.Nil)) |
| 124 | + val n2 = D.Rule(Eff.atom(Eff.RNeeds, Pure.two(0, 1)), Coll.Cons(D.Pos(Eff.atom(Eff.RDecl, Pure.two(0, 1))), Coll.Nil)) |
| 125 | + // needs(0, 1) :- calls(0, 2, 3), needs(2, 1), not hd(3, 1) |
| 126 | + val callsA = D.Pos(D.Atom(Eff.relSym(Eff.RCalls), Pure.vars(Coll.Cons(0, Pure.two(2, 3))))) |
| 127 | + val n3 = D.Rule(Eff.atom(Eff.RNeeds, Pure.two(0, 1)), Coll.Cons(callsA, Coll.Cons(D.Pos(Eff.atom(Eff.RNeeds, Pure.two(2, 1))), Coll.Cons(D.Neg(Eff.atom(Eff.RHd, Pure.two(3, 1))), Coll.Nil)))) |
| 128 | + val w1 = D.Rule(Eff.atom(Eff.RRow, Pure.two(0, 1)), Coll.Cons(D.Pos(Eff.atom(Eff.RNeeds, Pure.two(0, 1))), Coll.Cons(D.Neg(Eff.atom(Eff.RDecl, Pure.two(0, 1))), Coll.Nil))) |
| 129 | + Coll.Cons(n1, Coll.Cons(n2, Coll.Cons(n3, Coll.Cons(w1, Coll.Nil)))) |
| 130 | + |
| 131 | +// ── facts ── |
| 132 | +// a callee: a def with a body or a {} binder of a protocol (it may need one), or an extern with none (it never does: |
| 133 | +// no fact) |
| 134 | +enum Eff.Callee: |
| 135 | + case Eff.Runs |
| 136 | + case Eff.Leaf |
| 137 | + |
| 138 | +// the relation and Region symbols, made once per program (not per fact) |
| 139 | +enum Eff.Syms: |
| 140 | + case Eff.Syms(req: Sym.Sym, decl: Sym.Sym, calls: Sym.Sym, hd: Sym.Sym, region: Sym.Sym) |
| 141 | + |
| 142 | +// the protocols, handler -> protocol, the callees, the cases with fields (a call of one allocates), the symbols |
| 143 | +enum Eff.Cx: |
| 144 | + case Eff.Cx(ps: Coll.List[Ast.Proto], hp: Coll.SMap[Sym.Sym], fns: Coll.SMap[Eff.Callee], ctors: Coll.SMap[Bool], ss: Eff.Syms) |
| 145 | + |
| 146 | +def Eff.syms {r: Region} (): Eff.Syms = Eff.Syms(Eff.relSym(Eff.RReq), Eff.relSym(Eff.RDecl), Eff.relSym(Eff.RCalls), Eff.relSym(Eff.RHd), Sym.of("Region")) |
| 147 | + |
| 148 | +def Eff.cxOf {r: Region} (ds: Coll.List[Ast.Decl], cx: Eff.Cx): Eff.Cx = ds match |
| 149 | + case Coll.Nil => cx |
| 150 | + case Coll.Cons(d, rest) => |
| 151 | + val Eff.Cx(ps, hp, fns, ctors, ss) = cx |
| 152 | + d match |
| 153 | + case Ast.DProto(p) => Eff.cxOf(rest, Eff.Cx(Coll.Cons(p, ps), hp, fns, ctors, ss)) |
| 154 | + case Ast.DHandler(h) => |
| 155 | + val Ast.Handler(_, n, p, _) = h |
| 156 | + Eff.cxOf(rest, Eff.Cx(ps, Coll.sInsert(hp, n, p), fns, ctors, ss)) |
| 157 | + case Ast.DFn(f) => |
| 158 | + val Ast.Fn(_, n, _, _, _, _, _) = f |
| 159 | + Eff.cxOf(rest, Eff.Cx(ps, hp, Coll.sInsert(fns, n, Eff.calleeOf(f)), ctors, ss)) |
| 160 | + case Ast.DEnum(e) => |
| 161 | + val Ast.Enum(_, _, _, cases) = e |
| 162 | + Eff.cxOf(rest, Eff.Cx(ps, hp, fns, Eff.ctorsOf(cases, ctors), ss)) |
| 163 | + case _ => Eff.cxOf(rest, cx) |
| 164 | + |
| 165 | +// an extern with no {} binder is a leaf (rt_print, rt_str_eq, ...) |
| 166 | +def Eff.calleeOf {r: Region} (f: Ast.Fn): Eff.Callee = |
| 167 | + val Ast.Fn(_, _, gs, _, _, _, body) = f |
| 168 | + body match |
| 169 | + case Ast.Extern => Ty.evidence(gs, Coll.Nil) match |
| 170 | + case Coll.Nil => Eff.Leaf |
| 171 | + case Coll.Cons(_, _) => Eff.Runs |
| 172 | + case _ => Eff.Runs |
| 173 | + |
| 174 | +def Eff.ctorsOf {r: Region} (cs: Coll.List[Ast.Case], m: Coll.SMap[Bool]): Coll.SMap[Bool] = cs match |
| 175 | + case Coll.Nil => m |
| 176 | + case Coll.Cons(c, rest) => |
| 177 | + val Ast.Case(_, n, fields) = c |
| 178 | + fields match |
| 179 | + case Coll.Nil => Eff.ctorsOf(rest, m) |
| 180 | + case Coll.Cons(_, _) => Eff.ctorsOf(rest, Coll.sInsert(m, n, true)) |
| 181 | + |
| 182 | +// the walk: the next handle scope id, the facts; in the current def, the protocols already requested and the |
| 183 | +// (callee, scope) pairs already called (each fact once) |
| 184 | +enum Eff.W: |
| 185 | + case Eff.W(next: I64, facts: Coll.List[D.Fact], reqs: Coll.List[Sym.Sym], calls: Coll.SMap[Coll.List[I64]]) |
| 186 | + |
| 187 | +// where a walk is: the def, its handle scope id and the protocols handled there, its locals |
| 188 | +enum Eff.At: |
| 189 | + case Eff.At(f: Sym.Sym, s: I64, hd: Coll.List[Sym.Sym], ls: Coll.List[Sym.Sym]) |
| 190 | + |
| 191 | +def Eff.add {r: Region} (w: Eff.W, rel: Sym.Sym, vs: Coll.List[D.Val]): Eff.W = |
| 192 | + val Eff.W(n, fs, rq, cs) = w |
| 193 | + Eff.W(n, Coll.Cons(D.Fact(rel, vs), fs), rq, cs) |
| 194 | + |
| 195 | +def Eff.v2 {r: Region} (a: D.Val, b: D.Val): Coll.List[D.Val] = Coll.Cons(a, Coll.Cons(b, Coll.Nil)) |
| 196 | + |
| 197 | +def Eff.isIn {r: Region} (m: Coll.SMap[Bool], x: Sym.Sym): Bool = Coll.sLookup(m, x) match |
| 198 | + case Coll.Some(_) => true |
| 199 | + case Coll.None => false |
| 200 | + |
| 201 | +def Eff.decls {r: Region} (cx: Eff.Cx, ds: Coll.List[Ast.Decl], w: Eff.W): Coll.List[D.Fact] = ds match |
| 202 | + case Coll.Nil => |
| 203 | + val Eff.W(_, fs, _, _) = w |
| 204 | + fs |
| 205 | + case Coll.Cons(d, rest) => d match |
| 206 | + case Ast.DFn(f) => |
| 207 | + val Eff.W(n, fs, _, _) = w |
| 208 | + Eff.decls(cx, rest, Eff.fn(cx, f, Eff.W(n, fs, Coll.Nil, Coll.sEmpty()))) |
| 209 | + case _ => Eff.decls(cx, rest, w) |
| 210 | + |
| 211 | +// decl(F, P) for each {} binder of a protocol; the body of a run def (a spec def is pure: it has no row) |
| 212 | +def Eff.fn {r: Region} (cx: Eff.Cx, f: Ast.Fn, w: Eff.W): Eff.W = |
| 213 | + val Ast.Fn(_, n, gs, ps, _, _, body) = f |
| 214 | + val ev = Ty.evidence(gs, Coll.Nil) |
| 215 | + val w1 = Eff.declsOf(cx, n, ev, w) |
| 216 | + body match |
| 217 | + case Ast.Def(_, e) => Eff.expr(cx, Eff.At(n, 0, Coll.Nil, Ty.names(ps, Ty.names(ev, Coll.Nil))), e, w1) |
| 218 | + case _ => w1 |
| 219 | + |
| 220 | +def Eff.declsOf {r: Region} (cx: Eff.Cx, f: Sym.Sym, bs: Coll.List[Ast.Binder], w: Eff.W): Eff.W = bs match |
| 221 | + case Coll.Nil => w |
| 222 | + case Coll.Cons(b, rest) => |
| 223 | + val Ast.Binder(_, _, t) = b |
| 224 | + val Ast.Ty(_, tn, _, _, _) = t |
| 225 | + val Eff.Cx(ps, _, _, _, ss) = cx |
| 226 | + val Eff.Syms(_, decl, _, _, _) = ss |
| 227 | + if Ty.isProto(ps, tn) then Eff.declsOf(cx, f, rest, Eff.add(w, decl, Eff.v2(D.VSym(f), D.VSym(tn)))) else Eff.declsOf(cx, f, rest, w) |
| 228 | + |
| 229 | +def Eff.req {r: Region} (cx: Eff.Cx, at: Eff.At, p: Sym.Sym, w: Eff.W): Eff.W = |
| 230 | + val Eff.At(f, _, hd, _) = at |
| 231 | + val Eff.W(n, fs, rq, cs) = w |
| 232 | + if Ty.memberSym(p, hd) || Ty.memberSym(p, rq) then w |
| 233 | + else |
| 234 | + val Eff.Cx(_, _, _, _, ss) = cx |
| 235 | + val Eff.Syms(req, _, _, _, _) = ss |
| 236 | + Eff.W(n, Coll.Cons(D.Fact(req, Eff.v2(D.VSym(f), D.VSym(p))), fs), Coll.Cons(p, rq), cs) |
| 237 | + |
| 238 | +// calls(F, G, S): a leaf, F itself, or a pair already called adds nothing |
| 239 | +def Eff.call {r: Region} (cx: Eff.Cx, at: Eff.At, g: Sym.Sym, w: Eff.W): Eff.W = |
| 240 | + val Eff.At(f, s, _, _) = at |
| 241 | + val Eff.Cx(_, _, fns, _, ss) = cx |
| 242 | + val Eff.W(n, fs, rq, cs) = w |
| 243 | + val seen: Coll.List[I64] = Coll.sLookup(cs, g) match |
| 244 | + case Coll.Some(xs) => xs |
| 245 | + case Coll.None => Coll.Nil |
| 246 | + val leaf: Bool = Coll.sLookup(fns, g) match |
| 247 | + case Coll.Some(k) => k match |
| 248 | + case Eff.Leaf => true |
| 249 | + case Eff.Runs => false |
| 250 | + case Coll.None => true |
| 251 | + if leaf || Sym.eq(f, g) || Eff.memberI(s, seen) then w |
| 252 | + else |
| 253 | + val Eff.Syms(_, _, calls, _, _) = ss |
| 254 | + Eff.W(n, Coll.Cons(D.Fact(calls, Coll.Cons(D.VSym(f), Eff.v2(D.VSym(g), D.VInt(s)))), fs), rq, Coll.sInsert(cs, g, Coll.Cons(s, seen))) |
| 255 | + |
| 256 | +def Eff.memberI(x: I64, xs: Coll.List[I64]): Bool = xs match |
| 257 | + case Coll.Nil => false |
| 258 | + case Coll.Cons(h, t) => if h == x then true else Eff.memberI(x, t) |
| 259 | + |
| 260 | +def Eff.isFn {r: Region} (m: Coll.SMap[Eff.Callee], x: Sym.Sym): Bool = Coll.sLookup(m, x) match |
| 261 | + case Coll.Some(_) => true |
| 262 | + case Coll.None => false |
| 263 | + |
| 264 | +def Eff.reqTo {r: Region} (cx: Eff.Cx, at: Eff.At, q: Coll.Option[Eff.Req], w: Eff.W): Eff.W = q match |
| 265 | + case Coll.Some(rq) => |
| 266 | + val Eff.Req(p, _) = rq |
| 267 | + Eff.req(cx, at, p, w) |
| 268 | + case Coll.None => w |
| 269 | + |
| 270 | +def Eff.local {r: Region} (at: Eff.At, xs: Coll.List[Sym.Sym]): Eff.At = |
| 271 | + val Eff.At(f, s, hd, ls) = at |
| 272 | + Eff.At(f, s, hd, Coll.revOnto(xs, ls)) |
| 273 | + |
| 274 | +def Eff.expr {r: Region} (cx: Eff.Cx, at: Eff.At, e: Ast.Expr, w: Eff.W): Eff.W = e match |
| 275 | + case Ast.Name(_, x) => |
| 276 | + val Eff.At(_, _, _, ls) = at |
| 277 | + if Ty.memberSym(x, ls) then w else Eff.call(cx, at, x, w) |
| 278 | + case Ast.Call(_, g, _, _, xs) => |
| 279 | + val w1 = Eff.exprs(cx, at, xs, w) |
| 280 | + val Eff.Cx(ps, _, fns, ctors, ss) = cx |
| 281 | + val Eff.Syms(_, _, _, _, region) = ss |
| 282 | + if Eff.isFn(fns, g) then Eff.call(cx, at, g, w1) |
| 283 | + else if Eff.isIn(ctors, g) then Eff.req(cx, at, region, w1) |
| 284 | + else Eff.reqTo(cx, at, Eff.reqOf(ps, g), w1) |
| 285 | + case Ast.Method(_, _, _, xs) => Eff.exprs(cx, at, xs, w) |
| 286 | + case Ast.Bin(_, _, l, x) => Eff.expr(cx, at, x, Eff.expr(cx, at, l, w)) |
| 287 | + case Ast.Un(_, _, x) => Eff.expr(cx, at, x, w) |
| 288 | + case Ast.If(_, c, t, x) => Eff.expr(cx, at, x, Eff.expr(cx, at, t, Eff.expr(cx, at, c, w))) |
| 289 | + case Ast.Match(_, s, arms) => Eff.arms(cx, at, arms, Eff.expr(cx, at, s, w)) |
| 290 | + case Ast.Block(_, ss, last) => Eff.stmts(cx, at, ss, last, w) |
| 291 | + case Ast.While(_, c, b) => Eff.expr(cx, at, b, Eff.expr(cx, at, c, w)) |
| 292 | + case Ast.Assign(_, _, x) => Eff.expr(cx, at, x, w) |
| 293 | + case Ast.Quant(_, _, x, _, b) => Eff.expr(cx, Eff.local(at, Coll.Cons(x, Coll.Nil)), b, w) |
| 294 | + case _ => w |
| 295 | + |
| 296 | +def Eff.exprs {r: Region} (cx: Eff.Cx, at: Eff.At, xs: Coll.List[Ast.Expr], w: Eff.W): Eff.W = xs match |
| 297 | + case Coll.Nil => w |
| 298 | + case Coll.Cons(x, rest) => Eff.exprs(cx, at, rest, Eff.expr(cx, at, x, w)) |
| 299 | + |
| 300 | +def Eff.arms {r: Region} (cx: Eff.Cx, at: Eff.At, as: Coll.List[Ast.Arm], w: Eff.W): Eff.W = as match |
| 301 | + case Coll.Nil => w |
| 302 | + case Coll.Cons(a, rest) => |
| 303 | + val Ast.Arm(_, p, b) = a |
| 304 | + val at2: Eff.At = p match |
| 305 | + case Ast.PCase(_, xs) => Eff.local(at, xs) |
| 306 | + case Ast.PAny => at |
| 307 | + Eff.arms(cx, at, rest, Eff.expr(cx, at2, b, w)) |
| 308 | + |
| 309 | +// a handle opens a new scope for the rest of the block: hd(S, P) for every protocol handled there |
| 310 | +def Eff.stmts {r: Region} (cx: Eff.Cx, at: Eff.At, ss: Coll.List[Ast.Stmt], last: Ast.Expr, w: Eff.W): Eff.W = ss match |
| 311 | + case Coll.Nil => Eff.expr(cx, at, last, w) |
| 312 | + case Coll.Cons(s, rest) => s match |
| 313 | + case Ast.Val(_, x, _, e) => Eff.stmts(cx, Eff.local(at, Coll.Cons(x, Coll.Nil)), rest, last, Eff.expr(cx, at, e, w)) |
| 314 | + case Ast.Var(_, x, _, e) => Eff.stmts(cx, Eff.local(at, Coll.Cons(x, Coll.Nil)), rest, last, Eff.expr(cx, at, e, w)) |
| 315 | + case Ast.Handle(_, x, h, xs) => |
| 316 | + val Eff.W(n, fs, rq, cs) = Eff.exprs(cx, at, xs, w) |
| 317 | + val Eff.Cx(_, hp, _, _, ss) = cx |
| 318 | + val Eff.Syms(_, _, _, hdRel, _) = ss |
| 319 | + val Eff.At(f, _, hd, ls) = at |
| 320 | + val hd2: Coll.List[Sym.Sym] = Coll.sLookup(hp, h) match |
| 321 | + case Coll.Some(p) => Coll.Cons(p, hd) |
| 322 | + case Coll.None => hd |
| 323 | + Eff.stmts(cx, Eff.At(f, n, hd2, Coll.Cons(x, ls)), rest, last, Eff.hds(hdRel, n, hd2, Eff.W(n + 1, fs, rq, cs))) |
| 324 | + case Ast.Do(e) => Eff.stmts(cx, at, rest, last, Eff.expr(cx, at, e, w)) |
| 325 | + case Ast.Check(_, _, e) => Eff.stmts(cx, at, rest, last, Eff.expr(cx, at, e, w)) |
| 326 | + |
| 327 | +def Eff.hds {r: Region} (rel: Sym.Sym, s: I64, ps: Coll.List[Sym.Sym], w: Eff.W): Eff.W = ps match |
| 328 | + case Coll.Nil => w |
| 329 | + case Coll.Cons(p, rest) => Eff.hds(rel, s, rest, Eff.add(w, rel, Eff.v2(D.VInt(s), D.VSym(p)))) |
0 commit comments