@@ -39,8 +39,9 @@ object Mono extends Phase[CoreTransformed, CoreTransformed] {
39
39
var monoContext = MonoContext (solution, monoNames)
40
40
val monoDecls = declarations flatMap (monomorphize(_)(using monoContext))
41
41
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))
44
45
val newModuleDecl = ModuleDecl (path, includes, monoDecls, externs, monoDefs, exports)
45
46
return Some (CoreTransformed (source, tree, mod, newModuleDecl))
46
47
}
@@ -99,10 +100,24 @@ def findConstraints(declaration: Declaration)(using ctx: MonoFindContext): Const
99
100
List .empty
100
101
101
102
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)
104
105
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)
106
121
107
122
def findConstraints (constructor : Constructor )(using ctx : MonoFindContext ): Constraints = constructor match
108
123
case Constructor (id, List ()) => List .empty
@@ -114,11 +129,22 @@ def findConstraints(stmt: Stmt)(using ctx: MonoFindContext): Constraints = stmt
114
129
case Return (expr) => findConstraints(expr)
115
130
case Val (id, annotatedTpe, binding, body) => findConstraints(binding) ++ findConstraints(body)
116
131
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)
117
134
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)
118
139
case o => println(o); ???
119
140
141
+ def findConstraints (opt : Option [Stmt ])(using ctx : MonoFindContext ): Constraints = opt match
142
+ case None => List .empty
143
+ case Some (stmt) => findConstraints(stmt)
144
+
120
145
def findConstraints (expr : Expr )(using ctx : MonoFindContext ): Constraints = expr match
121
146
case DirectApp (b, List (), vargs, bargs) => List .empty
147
+ case PureApp (b, List (), vargs) => List .empty
122
148
case ValueVar (id, annotatedType) => List .empty
123
149
case Literal (value, annotatedType) => List .empty
124
150
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] =
207
233
)
208
234
209
235
def monomorphize (block : Block )(using ctx : MonoContext ): Block = block match
236
+ case b : BlockLit => monomorphize(b)
210
237
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)
212
259
213
260
def monomorphize (field : Field )(using ctx : MonoContext ): Field = field match
214
261
case Field (id, tpe) => Field (id, monomorphize(tpe))
@@ -219,7 +266,7 @@ def monomorphize(blockVar: BlockVar, replacementId: FunctionId)(using ctx: MonoC
219
266
case BlockVar (id, BlockType .Function (tparams, cparams, vparams, bparams, result), annotatedCapt) =>
220
267
val monoAnnotatedTpe = BlockType .Function (List .empty, cparams, vparams map monomorphize, bparams map monomorphize, monomorphize(result))
221
268
BlockVar (replacementId, monoAnnotatedTpe, annotatedCapt)
222
- case o => ???
269
+ case o => println(o); ???
223
270
224
271
def monomorphize (stmt : Stmt )(using ctx : MonoContext ): Stmt = stmt match
225
272
case Return (expr) => Return (monomorphize(expr))
@@ -230,8 +277,22 @@ def monomorphize(stmt: Stmt)(using ctx: MonoContext): Stmt = stmt match
230
277
App (monomorphize(callee, replacementData.name), List .empty, vargs map monomorphize, bargs map monomorphize)
231
278
case Let (id, annotatedTpe, binding, body) => Let (id, monomorphize(annotatedTpe), monomorphize(binding), monomorphize(body))
232
279
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))
233
290
case o => println(o); ???
234
291
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
+
235
296
def monomorphize (expr : Expr )(using ctx : MonoContext ): Expr = expr match
236
297
case DirectApp (b, targs, vargs, bargs) =>
237
298
val replacementData = replacementDataFromTargs(b.id, targs)
@@ -259,7 +320,7 @@ def monomorphize(blockParam: BlockParam)(using ctx: MonoContext): BlockParam = b
259
320
def monomorphize (blockType : BlockType )(using ctx : MonoContext ): BlockType = blockType match
260
321
case BlockType .Function (tparams, cparams, vparams, bparams, result) =>
261
322
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)
263
324
264
325
def monomorphize (valueType : ValueType )(using ctx : MonoContext ): ValueType = valueType match
265
326
case ValueType .Var (name) => ValueType .Var (ctx.replacementTparams(name).tpe)
@@ -280,6 +341,10 @@ def freshMonoName(baseId: Id, tpes: Vector[TypeArg.Base]): Id =
280
341
281
342
def replacementDataFromTargs (id : FunctionId , targs : List [ValueType ])(using ctx : MonoContext ): ValueType .Data =
282
343
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)
283
348
var baseTypes : List [TypeArg .Base ] = List .empty
284
349
targs.foreach({
285
350
case ValueType .Data (name, targs) => baseTypes :+= TypeArg .Base (name)
0 commit comments