From 54e1a91d0fce7673b95c6e35db0bdc5f367035a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Brachtha=CC=88user?= Date: Wed, 22 Jan 2025 15:54:45 +0100 Subject: [PATCH] Drop annotated answer since it is already in the prompt --- .../src/main/scala/effekt/core/PolymorphismBoxing.scala | 5 +++-- .../shared/src/main/scala/effekt/core/PrettyPrinter.scala | 2 +- effekt/shared/src/main/scala/effekt/core/Recursive.scala | 2 +- .../shared/src/main/scala/effekt/core/Transformer.scala | 2 +- effekt/shared/src/main/scala/effekt/core/Tree.scala | 8 ++++---- effekt/shared/src/main/scala/effekt/core/Type.scala | 8 ++++++-- .../scala/effekt/core/optimizer/BindSubexpressions.scala | 2 +- .../src/main/scala/effekt/core/optimizer/Deadcode.scala | 4 ++-- .../src/main/scala/effekt/core/optimizer/Normalizer.scala | 2 +- .../src/main/scala/effekt/core/optimizer/Reachable.scala | 2 +- .../effekt/core/optimizer/RemoveTailResumptions.scala | 4 ++-- .../scala/effekt/core/optimizer/StaticArguments.scala | 4 ++-- effekt/shared/src/main/scala/effekt/core/vm/VM.scala | 4 ++-- effekt/shared/src/main/scala/effekt/cps/Transformer.scala | 4 ++-- .../main/scala/effekt/generator/chez/Transformer.scala | 2 +- .../src/main/scala/effekt/machine/Transformer.scala | 5 +++-- 16 files changed, 33 insertions(+), 27 deletions(-) diff --git a/effekt/shared/src/main/scala/effekt/core/PolymorphismBoxing.scala b/effekt/shared/src/main/scala/effekt/core/PolymorphismBoxing.scala index f464a92e7..1a4804924 100644 --- a/effekt/shared/src/main/scala/effekt/core/PolymorphismBoxing.scala +++ b/effekt/shared/src/main/scala/effekt/core/PolymorphismBoxing.scala @@ -298,8 +298,9 @@ object PolymorphismBoxing extends Phase[CoreTransformed, CoreTransformed] { Stmt.Alloc(id, transform(init), region, transform(body)) case Stmt.Var(id, init, cap, body) => Stmt.Var(id, transform(init), cap, transform(body)) - case Stmt.Reset(answer, BlockLit(tps, cps, vps, bps, body)) => - Stmt.Reset(answer, BlockLit(tps, cps, vps, bps, coerce(transform(body), answer))) + case Stmt.Reset(BlockLit(tps, cps, vps, prompt :: Nil, body)) => + Stmt.Reset(BlockLit(tps, cps, vps, prompt :: Nil, coerce(transform(body), stmt.tpe))) + case Stmt.Reset(body) => ??? case Stmt.Shift(prompt, body) => Stmt.Shift(prompt, transform(body)) case Stmt.Resume(k, body) => diff --git a/effekt/shared/src/main/scala/effekt/core/PrettyPrinter.scala b/effekt/shared/src/main/scala/effekt/core/PrettyPrinter.scala index 2140d4759..8af77e6ea 100644 --- a/effekt/shared/src/main/scala/effekt/core/PrettyPrinter.scala +++ b/effekt/shared/src/main/scala/effekt/core/PrettyPrinter.scala @@ -197,7 +197,7 @@ object PrettyPrinter extends ParenPrettyPrinter { case If(cond, thn, els) => "if" <+> parens(toDoc(cond)) <+> block(toDocStmts(thn)) <+> "else" <+> block(toDocStmts(els)) - case Reset(answer, body) => + case Reset(body) => "reset" <+> toDoc(body) case Shift(prompt, body) => diff --git a/effekt/shared/src/main/scala/effekt/core/Recursive.scala b/effekt/shared/src/main/scala/effekt/core/Recursive.scala index 0cd8312cd..db0440581 100644 --- a/effekt/shared/src/main/scala/effekt/core/Recursive.scala +++ b/effekt/shared/src/main/scala/effekt/core/Recursive.scala @@ -76,7 +76,7 @@ class Recursive( process(body) case Stmt.Get(id, capt, tpe) => () case Stmt.Put(id, tpe, value) => process(value) - case Stmt.Reset(answer, body) => process(body) + case Stmt.Reset(body) => process(body) case Stmt.Shift(prompt, body) => process(prompt); process(body) case Stmt.Resume(k, body) => process(k); process(body) case Stmt.Region(body) => process(body) diff --git a/effekt/shared/src/main/scala/effekt/core/Transformer.scala b/effekt/shared/src/main/scala/effekt/core/Transformer.scala index 22544c465..54c95ceed 100644 --- a/effekt/shared/src/main/scala/effekt/core/Transformer.scala +++ b/effekt/shared/src/main/scala/effekt/core/Transformer.scala @@ -459,7 +459,7 @@ object Transformer extends Phase[Typechecked, CoreTransformed] { val body: BlockLit = BlockLit(Nil, List(promptCapt), Nil, List(promptParam), Binding(transformedHandlers, transform(prog))) - Context.bind(Reset(answerType, body)) + Context.bind(Reset(body)) case r @ source.Region(name, body) => val region = r.symbol diff --git a/effekt/shared/src/main/scala/effekt/core/Tree.scala b/effekt/shared/src/main/scala/effekt/core/Tree.scala index 91279a046..491afb6cc 100644 --- a/effekt/shared/src/main/scala/effekt/core/Tree.scala +++ b/effekt/shared/src/main/scala/effekt/core/Tree.scala @@ -280,7 +280,7 @@ enum Stmt extends Tree { // binds a fresh prompt as [[id]] in [[body]] and delimits the scope of captured continuations // Reset({ [cap]{p: Prompt[answer] at cap} => stmt: answer}): answer - case Reset(answer: ValueType, body: BlockLit) + case Reset(body: Block.BlockLit) // captures the continuation up to the given prompt // Invariant, it always has the shape: @@ -620,7 +620,7 @@ object Variables { case Stmt.Put(id, annotatedCapt, value) => Variables.block(id, core.Type.TState(value.tpe), annotatedCapt) ++ free(value) - case Stmt.Reset(answer, body) => free(body) + case Stmt.Reset(body) => free(body) case Stmt.Shift(prompt, body) => free(prompt) ++ free(body) case Stmt.Resume(k, body) => free(k) ++ free(body) case Stmt.Hole() => Variables.empty @@ -720,8 +720,8 @@ object substitutions { Put(substituteAsVar(id), substitute(capt), substitute(value)) // We annotate the answer type here since it needs to be the union of body.tpe and all shifts - case Reset(answer, body) => - Reset(substitute(answer), substitute(body)) + case Reset(body) => + Reset(substitute(body)) case Shift(prompt, body) => val after = substitute(body) diff --git a/effekt/shared/src/main/scala/effekt/core/Type.scala b/effekt/shared/src/main/scala/effekt/core/Type.scala index 5222f1264..7a99d8539 100644 --- a/effekt/shared/src/main/scala/effekt/core/Type.scala +++ b/effekt/shared/src/main/scala/effekt/core/Type.scala @@ -208,7 +208,11 @@ object Type { case Stmt.Var(id, init, cap, body) => body.tpe case Stmt.Get(id, capt, tpe) => tpe case Stmt.Put(id, capt, value) => TUnit - case Stmt.Reset(answer, body) => answer + case Stmt.Reset(BlockLit(_, _, _, prompt :: Nil, body)) => prompt.tpe match { + case TPrompt(tpe) => tpe + case _ => ??? + } + case Stmt.Reset(body) => ??? case Stmt.Shift(prompt, body) => body.bparams match { case core.BlockParam(id, BlockType.Interface(ResumeSymbol, List(result, answer)), captures) :: Nil => result case _ => ??? @@ -235,7 +239,7 @@ object Type { case Stmt.Var(id, init, cap, body) => body.capt -- Set(cap) case Stmt.Get(id, capt, tpe) => capt case Stmt.Put(id, capt, value) => capt - case Stmt.Reset(answer, body) => body.capt + case Stmt.Reset(body) => body.capt case Stmt.Shift(prompt, body) => prompt.capt ++ body.capt case Stmt.Resume(k, body) => k.capt ++ body.capt case Stmt.Region(body) => body.capt diff --git a/effekt/shared/src/main/scala/effekt/core/optimizer/BindSubexpressions.scala b/effekt/shared/src/main/scala/effekt/core/optimizer/BindSubexpressions.scala index db2fd4c82..6d14f51cc 100644 --- a/effekt/shared/src/main/scala/effekt/core/optimizer/BindSubexpressions.scala +++ b/effekt/shared/src/main/scala/effekt/core/optimizer/BindSubexpressions.scala @@ -83,7 +83,7 @@ object BindSubexpressions { // Congruences case Stmt.Region(body) => Stmt.Region(transform(body)) case Stmt.Val(id, tpe, binding, body) => Stmt.Val(id, transform(tpe), transform(binding), transform(body)) - case Stmt.Reset(answer, body) => Stmt.Reset(answer, transform(body)) + case Stmt.Reset(body) => Stmt.Reset(transform(body)) case Stmt.Shift(prompt, body) => Stmt.Shift(transform(prompt), transform(body)) case Stmt.Resume(k, body) => Stmt.Resume(transform(k), transform(body)) case Stmt.Hole() => Stmt.Hole() diff --git a/effekt/shared/src/main/scala/effekt/core/optimizer/Deadcode.scala b/effekt/shared/src/main/scala/effekt/core/optimizer/Deadcode.scala index fd6889dc4..776c2c011 100644 --- a/effekt/shared/src/main/scala/effekt/core/optimizer/Deadcode.scala +++ b/effekt/shared/src/main/scala/effekt/core/optimizer/Deadcode.scala @@ -13,10 +13,10 @@ class Deadcode(reachable: Map[Id, Usage]) extends core.Tree.Rewrite { case Stmt.Def(id, block, body) if unused(id) => rewrite(body) case Stmt.Let(id, tpe, binding, body) if binding.capt.isEmpty && unused(id) => rewrite(body) - case Stmt.Reset(answer, body) => + case Stmt.Reset(body) => rewrite(body) match { case BlockLit(tparams, cparams, vparams, List(prompt), body) if unused(prompt.id) => body - case b => Stmt.Reset(answer, b) + case b => Stmt.Reset(b) } case Stmt.Match(sc, clauses, default) => diff --git a/effekt/shared/src/main/scala/effekt/core/optimizer/Normalizer.scala b/effekt/shared/src/main/scala/effekt/core/optimizer/Normalizer.scala index ea664a698..bcbe2ca66 100644 --- a/effekt/shared/src/main/scala/effekt/core/optimizer/Normalizer.scala +++ b/effekt/shared/src/main/scala/effekt/core/optimizer/Normalizer.scala @@ -286,7 +286,7 @@ object Normalizer { normal => // "Congruences" // ------------- - case Stmt.Reset(answer, body) => Stmt.Reset(answer, normalize(body)) + case Stmt.Reset(body) => Stmt.Reset(normalize(body)) case Stmt.Shift(prompt, body) => Shift(prompt, normalize(body)) case Stmt.Return(expr) => Return(normalize(expr)) case Stmt.Alloc(id, init, region, body) => Alloc(id, normalize(init), region, normalize(body)) diff --git a/effekt/shared/src/main/scala/effekt/core/optimizer/Reachable.scala b/effekt/shared/src/main/scala/effekt/core/optimizer/Reachable.scala index 51aaf2380..fb5c753f8 100644 --- a/effekt/shared/src/main/scala/effekt/core/optimizer/Reachable.scala +++ b/effekt/shared/src/main/scala/effekt/core/optimizer/Reachable.scala @@ -94,7 +94,7 @@ class Reachable( process(body) case Stmt.Get(id, capt, tpe) => process(id) case Stmt.Put(id, tpe, value) => process(id); process(value) - case Stmt.Reset(answer, body) => process(body) + case Stmt.Reset(body) => process(body) case Stmt.Shift(prompt, body) => process(prompt); process(body) case Stmt.Resume(k, body) => process(k); process(body) case Stmt.Region(body) => process(body) diff --git a/effekt/shared/src/main/scala/effekt/core/optimizer/RemoveTailResumptions.scala b/effekt/shared/src/main/scala/effekt/core/optimizer/RemoveTailResumptions.scala index 7316eb268..c290ff9bd 100644 --- a/effekt/shared/src/main/scala/effekt/core/optimizer/RemoveTailResumptions.scala +++ b/effekt/shared/src/main/scala/effekt/core/optimizer/RemoveTailResumptions.scala @@ -37,7 +37,7 @@ object RemoveTailResumptions { case Stmt.Var(id, init, capture, body) => tailResumptive(k, body) && !freeInExpr(init) case Stmt.Get(id, annotatedCapt, annotatedTpe) => false case Stmt.Put(id, annotatedCapt, value) => false - case Stmt.Reset(answer, BlockLit(tparams, cparams, vparams, bparams, body)) => tailResumptive(k, body) // is this correct? + case Stmt.Reset(BlockLit(tparams, cparams, vparams, bparams, body)) => tailResumptive(k, body) // is this correct? case Stmt.Shift(prompt, body) => stmt.tpe == Type.TBottom case Stmt.Resume(k2, body) => k2.id == k // what if k is free in body? case Stmt.Hole() => true @@ -55,7 +55,7 @@ object RemoveTailResumptions { Stmt.Region(removeTailResumption(k, body)) case Stmt.Alloc(id, init, region, body) => Stmt.Alloc(id, init, region, removeTailResumption(k, body)) case Stmt.Var(id, init, capture, body) => Stmt.Var(id, init, capture, removeTailResumption(k, body)) - case Stmt.Reset(answer, body) => Stmt.Reset(answer, removeTailResumption(k, body)) + case Stmt.Reset(body) => Stmt.Reset(removeTailResumption(k, body)) case Stmt.Resume(k2, body) if k2.id == k => body case Stmt.Resume(k, body) => stmt diff --git a/effekt/shared/src/main/scala/effekt/core/optimizer/StaticArguments.scala b/effekt/shared/src/main/scala/effekt/core/optimizer/StaticArguments.scala index a3c445e4e..9e3e0ce06 100644 --- a/effekt/shared/src/main/scala/effekt/core/optimizer/StaticArguments.scala +++ b/effekt/shared/src/main/scala/effekt/core/optimizer/StaticArguments.scala @@ -142,9 +142,9 @@ object StaticArguments { case Stmt.Invoke(b, method, methodTpe, targs, vargs, bargs) => Stmt.Invoke(rewrite(b), method, methodTpe, targs, vargs.map(rewrite), bargs.map(rewrite)) - case Stmt.Reset(answer, body) => + case Stmt.Reset(body) => rewrite(body) match { - case b => Stmt.Reset(answer, b) + case b => Stmt.Reset(b) } // congruences diff --git a/effekt/shared/src/main/scala/effekt/core/vm/VM.scala b/effekt/shared/src/main/scala/effekt/core/vm/VM.scala index d8c971e0f..98bfbd9ec 100644 --- a/effekt/shared/src/main/scala/effekt/core/vm/VM.scala +++ b/effekt/shared/src/main/scala/effekt/core/vm/VM.scala @@ -401,13 +401,13 @@ class Interpreter(instrumentation: Instrumentation, runtime: Runtime) { returnWith(Value.Literal(()), env, updated, heap) - case Stmt.Reset(answer, BlockLit(_, _, _, List(prompt), body)) => + case Stmt.Reset(BlockLit(_, _, _, List(prompt), body)) => val freshPrompt = freshAddress() instrumentation.reset() State.Step(body, env.bind(prompt.id, Computation.Prompt(freshPrompt)), Stack.Segment(Nil, freshPrompt, stack), heap) - case Stmt.Reset(answer, b) => ??? + case Stmt.Reset(b) => ??? case Stmt.Shift(prompt, BlockLit(tparams, cparams, vparams, List(resume), body)) => instrumentation.shift() diff --git a/effekt/shared/src/main/scala/effekt/cps/Transformer.scala b/effekt/shared/src/main/scala/effekt/cps/Transformer.scala index 45a956819..76cea12b8 100644 --- a/effekt/shared/src/main/scala/effekt/cps/Transformer.scala +++ b/effekt/shared/src/main/scala/effekt/cps/Transformer.scala @@ -105,13 +105,13 @@ object Transformer { default.map(transform(_, ks, k))) } - case core.Stmt.Reset(answer, core.Block.BlockLit(_, _, _, prompt :: Nil, body)) => + case core.Stmt.Reset(core.Block.BlockLit(_, _, _, prompt :: Nil, body)) => val ks2 = Id("ks") val k2 = Id("k") Reset(Block.BlockLit(Nil, List(prompt.id), ks2, k2, transform(body, ks2, Continuation.Dynamic(k2))), MetaCont(ks), k.reify) - case core.Stmt.Reset(answer, body) => sys error "Shouldn't happen" + case core.Stmt.Reset(body) => sys error "Shouldn't happen" // Only unidirectional, yet // core.Block.BlockLit(tparams, cparams, vparams, List(resume), body) diff --git a/effekt/shared/src/main/scala/effekt/generator/chez/Transformer.scala b/effekt/shared/src/main/scala/effekt/generator/chez/Transformer.scala index acf1efe17..bbf30582b 100644 --- a/effekt/shared/src/main/scala/effekt/generator/chez/Transformer.scala +++ b/effekt/shared/src/main/scala/effekt/generator/chez/Transformer.scala @@ -107,7 +107,7 @@ trait Transformer { case Alloc(id, init, region, body) => chez.Let(List(Binding(nameDef(id), chez.Builtin("fresh", chez.Variable(nameRef(region)), toChez(init)))), toChez(body)) - case Reset(answer, body) => chez.Reset(toChez(body)) + case Reset(body) => chez.Reset(toChez(body)) case Shift(p, body) => chez.Shift(nameRef(p.id), toChez(body)) diff --git a/effekt/shared/src/main/scala/effekt/machine/Transformer.scala b/effekt/shared/src/main/scala/effekt/machine/Transformer.scala index 33b7cec40..092f927df 100644 --- a/effekt/shared/src/main/scala/effekt/machine/Transformer.scala +++ b/effekt/shared/src/main/scala/effekt/machine/Transformer.scala @@ -222,10 +222,11 @@ object Transformer { Switch(value, transformedClauses, transformedDefault) } - case core.Reset(answer, core.BlockLit(Nil, cparams, Nil, List(prompt), body)) => + case core.Reset(core.BlockLit(Nil, cparams, Nil, List(prompt), body)) => noteParameters(List(prompt)) - val variable = Variable(freshName("returned"), transform(answer)) + val answerType = stmt.tpe + val variable = Variable(freshName("returned"), transform(answerType)) val returnClause = Clause(List(variable), Return(List(variable))) Reset(Variable(transform(prompt.id), Type.Prompt()), returnClause, transform(body))