Skip to content

Commit 094193d

Browse files
committed
Monomorphize effects
1 parent 3a65080 commit 094193d

File tree

1 file changed

+73
-8
lines changed
  • effekt/shared/src/main/scala/effekt/core

1 file changed

+73
-8
lines changed

effekt/shared/src/main/scala/effekt/core/Mono.scala

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ object Mono extends Phase[CoreTransformed, CoreTransformed] {
3939
var monoContext = MonoContext(solution, monoNames)
4040
val monoDecls = declarations flatMap (monomorphize(_)(using monoContext))
4141
val monoDefs = monomorphize(definitions)(using monoContext)
42-
// monoDecls.foreach(decl => println(util.show(decl)))
43-
// monoDefs.foreach(defn => println(util.show(defn)))
42+
// println(util.show(monoDecls))
43+
// println()
44+
// println(util.show(monoDefs))
4445
val newModuleDecl = ModuleDecl(path, includes, monoDecls, externs, monoDefs, exports)
4546
return Some(CoreTransformed(source, tree, mod, newModuleDecl))
4647
}
@@ -99,10 +100,24 @@ def findConstraints(declaration: Declaration)(using ctx: MonoFindContext): Const
99100
List.empty
100101

101102
def findConstraints(block: Block)(using ctx: MonoFindContext): Constraints = block match
102-
case BlockVar(id, annotatedTpe, annotatedCapt) => ???
103-
case BlockLit(tparams, cparams, vparams, bparams, body) => ???
103+
case BlockVar(id, annotatedTpe, annotatedCapt) => findConstraints(annotatedTpe)
104+
case BlockLit(tparams, cparams, vparams, bparams, body) => findConstraints(body)
104105
case Unbox(pure) => ???
105-
case New(impl) => ???
106+
case New(impl) => findConstraints(impl)
107+
108+
def findConstraints(blockType: BlockType)(using ctx: MonoFindContext): Constraints = blockType match
109+
case BlockType.Interface(name, targs) => List(Constraint(targs.map(findId).toVector, name))
110+
case o => println(o); ???
111+
112+
def findConstraints(impl: Implementation)(using ctx: MonoFindContext): Constraints = impl match
113+
case Implementation(interface, operations) =>
114+
findConstraints(interface) ++
115+
(operations flatMap findConstraints)
116+
117+
def findConstraints(operation: Operation)(using ctx: MonoFindContext): Constraints = operation match
118+
case Operation(name, tparams, cparams, vparams, bparams, body) =>
119+
tparams.zipWithIndex.foreach(ctx.extendTypingContext(_, _, name))
120+
findConstraints(body)
106121

107122
def findConstraints(constructor: Constructor)(using ctx: MonoFindContext): Constraints = constructor match
108123
case Constructor(id, List()) => List.empty
@@ -114,11 +129,22 @@ def findConstraints(stmt: Stmt)(using ctx: MonoFindContext): Constraints = stmt
114129
case Return(expr) => findConstraints(expr)
115130
case Val(id, annotatedTpe, binding, body) => findConstraints(binding) ++ findConstraints(body)
116131
case App(callee: BlockVar, targs, vargs, bargs) => List(Constraint(targs.map(findId).toVector, callee.id))
132+
case Invoke(callee: BlockVar, method, methodTpe, targs, vargs, bargs) => List(Constraint(targs.map(findId).toVector, callee.id))
133+
case Reset(body) => findConstraints(body)
117134
case If(cond, thn, els) => findConstraints(cond) ++ findConstraints(thn) ++ findConstraints(els)
135+
case Def(id, block, body) => findConstraints(block) ++ findConstraints(body)
136+
case Shift(prompt, body) => findConstraints(prompt) ++ findConstraints(body)
137+
case Match(scrutinee, clauses, default) => clauses.map(_._2).flatMap(findConstraints) ++ findConstraints(default)
138+
case Resume(k, body) => findConstraints(k) ++ findConstraints(body)
118139
case o => println(o); ???
119140

141+
def findConstraints(opt: Option[Stmt])(using ctx: MonoFindContext): Constraints = opt match
142+
case None => List.empty
143+
case Some(stmt) => findConstraints(stmt)
144+
120145
def findConstraints(expr: Expr)(using ctx: MonoFindContext): Constraints = expr match
121146
case DirectApp(b, List(), vargs, bargs) => List.empty
147+
case PureApp(b, List(), vargs) => List.empty
122148
case ValueVar(id, annotatedType) => List.empty
123149
case Literal(value, annotatedType) => List.empty
124150
case Make(data, tag, targs, vargs) => List(Constraint(data.targs.map(findId).toVector, data.name))
@@ -207,8 +233,29 @@ def monomorphize(decl: Declaration)(using ctx: MonoContext): List[Declaration] =
207233
)
208234

209235
def monomorphize(block: Block)(using ctx: MonoContext): Block = block match
236+
case b: BlockLit => monomorphize(b)
210237
case b: BlockVar => monomorphize(b)
211-
case o => println(o); ???
238+
case New(impl) => New(monomorphize(impl))
239+
case o => println(o); ???
240+
241+
def monomorphize(impl: Implementation)(using ctx: MonoContext): Implementation = impl match
242+
case Implementation(interface, operations) => Implementation(monomorphize(interface), operations.map(monomorphize))
243+
244+
def monomorphize(interface: BlockType.Interface)(using ctx: MonoContext): BlockType.Interface = interface match
245+
case BlockType.Interface(name, targs) =>
246+
val replacementData = replacementDataFromTargs(name, targs)
247+
BlockType.Interface(replacementData.name, replacementData.targs)
248+
249+
def monomorphize(operation: Operation)(using ctx: MonoContext): Operation = operation match
250+
case Operation(name, tparams, cparams, vparams, bparams, body) =>
251+
Operation(name, List.empty, cparams, vparams map monomorphize, bparams map monomorphize, monomorphize(body))
252+
253+
def monomorphize(block: BlockLit)(using ctx: MonoContext): BlockLit = block match
254+
case BlockLit(tparams, cparams, vparams, bparams, body) =>
255+
BlockLit(List.empty, cparams, vparams map monomorphize, bparams map monomorphize, monomorphize(body))
256+
257+
def monomorphize(block: BlockVar)(using ctx: MonoContext): BlockVar = block match
258+
case BlockVar(id, annotatedTpe, annotatedCapt) => BlockVar(id, monomorphize(annotatedTpe), annotatedCapt)
212259

213260
def monomorphize(field: Field)(using ctx: MonoContext): Field = field match
214261
case Field(id, tpe) => Field(id, monomorphize(tpe))
@@ -219,7 +266,7 @@ def monomorphize(blockVar: BlockVar, replacementId: FunctionId)(using ctx: MonoC
219266
case BlockVar(id, BlockType.Function(tparams, cparams, vparams, bparams, result), annotatedCapt) =>
220267
val monoAnnotatedTpe = BlockType.Function(List.empty, cparams, vparams map monomorphize, bparams map monomorphize, monomorphize(result))
221268
BlockVar(replacementId, monoAnnotatedTpe, annotatedCapt)
222-
case o => ???
269+
case o => println(o); ???
223270

224271
def monomorphize(stmt: Stmt)(using ctx: MonoContext): Stmt = stmt match
225272
case Return(expr) => Return(monomorphize(expr))
@@ -230,8 +277,22 @@ def monomorphize(stmt: Stmt)(using ctx: MonoContext): Stmt = stmt match
230277
App(monomorphize(callee, replacementData.name), List.empty, vargs map monomorphize, bargs map monomorphize)
231278
case Let(id, annotatedTpe, binding, body) => Let(id, monomorphize(annotatedTpe), monomorphize(binding), monomorphize(body))
232279
case If(cond, thn, els) => If(monomorphize(cond), monomorphize(thn), monomorphize(els))
280+
case Invoke(BlockVar(id, annotatedTpe, annotatedCapt), method, methodTpe, targs, vargs, bargs) =>
281+
Invoke(BlockVar(id, monomorphize(annotatedTpe), annotatedCapt), method, methodTpe, List.empty, vargs map monomorphize, bargs map monomorphize)
282+
// TODO: Monomorphizing here throws an error complaining about a missing implementation
283+
// Not sure what is missing, altough it does works like this
284+
case Reset(body) => Reset(body)
285+
case Def(id, block, body) => Def(id, monomorphize(block), monomorphize(body))
286+
case Shift(prompt, body) => Shift(monomorphize(prompt), monomorphize(body))
287+
case Match(scrutinee, clauses, default) =>
288+
val monoClauses = clauses.map((id, blockLit) => (id, monomorphize(blockLit)))
289+
Match(monomorphize(scrutinee), monoClauses, monomorphize(default))
233290
case o => println(o); ???
234291

292+
def monomorphize(opt: Option[Stmt])(using ctx: MonoContext): Option[Stmt] = opt match
293+
case None => None
294+
case Some(stmt) => Some(monomorphize(stmt))
295+
235296
def monomorphize(expr: Expr)(using ctx: MonoContext): Expr = expr match
236297
case DirectApp(b, targs, vargs, bargs) =>
237298
val replacementData = replacementDataFromTargs(b.id, targs)
@@ -259,7 +320,7 @@ def monomorphize(blockParam: BlockParam)(using ctx: MonoContext): BlockParam = b
259320
def monomorphize(blockType: BlockType)(using ctx: MonoContext): BlockType = blockType match
260321
case BlockType.Function(tparams, cparams, vparams, bparams, result) =>
261322
BlockType.Function(List.empty, cparams, vparams map monomorphize, bparams map monomorphize, monomorphize(result))
262-
case o => println(o); ???
323+
case b: BlockType.Interface => monomorphize(b)
263324

264325
def monomorphize(valueType: ValueType)(using ctx: MonoContext): ValueType = valueType match
265326
case ValueType.Var(name) => ValueType.Var(ctx.replacementTparams(name).tpe)
@@ -280,6 +341,10 @@ def freshMonoName(baseId: Id, tpes: Vector[TypeArg.Base]): Id =
280341

281342
def replacementDataFromTargs(id: FunctionId, targs: List[ValueType])(using ctx: MonoContext): ValueType.Data =
282343
if (targs.isEmpty) return ValueType.Data(id, targs)
344+
// TODO: Incredibly hacky, resume did not seem to appear when finding constraints
345+
// it does show up while monomorphizing which caused an error
346+
// this seems to work for now
347+
if (id.name.name == "Resume") return ValueType.Data(id, targs)
283348
var baseTypes: List[TypeArg.Base] = List.empty
284349
targs.foreach({
285350
case ValueType.Data(name, targs) => baseTypes :+= TypeArg.Base(name)

0 commit comments

Comments
 (0)