Skip to content

Commit

Permalink
Drop annotated answer since it is already in the prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
b-studios committed Jan 22, 2025
1 parent ae9b43d commit 54e1a91
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
2 changes: 1 addition & 1 deletion effekt/shared/src/main/scala/effekt/core/Recursive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion effekt/shared/src/main/scala/effekt/core/Transformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions effekt/shared/src/main/scala/effekt/core/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions effekt/shared/src/main/scala/effekt/core/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ => ???
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions effekt/shared/src/main/scala/effekt/core/vm/VM.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions effekt/shared/src/main/scala/effekt/cps/Transformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
5 changes: 3 additions & 2 deletions effekt/shared/src/main/scala/effekt/machine/Transformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 54e1a91

Please sign in to comment.