Skip to content

Commit d597792

Browse files
committed
Fix constructor mono
1 parent 1339e67 commit d597792

File tree

1 file changed

+15
-11
lines changed
  • effekt/shared/src/main/scala/effekt/core

1 file changed

+15
-11
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,14 @@ def monomorphize(toplevel: Toplevel)(using ctx: MonoContext): List[Toplevel] = t
263263
List(Toplevel.Val(id, monomorphize(tpe), monomorphize(binding)))
264264

265265
def monomorphize(decl: Declaration)(using ctx: MonoContext): List[Declaration] = decl match
266-
case Data(id, List(), constructors) => List(decl)
266+
case Data(id, List(), constructors) =>
267+
List(Declaration.Data(id, List.empty, constructors.flatMap(monomorphize(_, 0))))
267268
case Data(id, tparams, constructors) =>
268269
val monoTypes = ctx.solution.getOrElse(id, Set.empty).toList
269270
monoTypes.map(baseTypes =>
270271
val replacementTparams = tparams.zip(baseTypes).toMap
271272
ctx.replacementTparams ++= replacementTparams
272-
val newConstructors = constructors map {
273-
case Constructor(id, tparams, fields) => Constructor(id, tparams, fields map monomorphize)
274-
}
275-
Declaration.Data(ctx.names(id, baseTypes), List.empty, newConstructors)
273+
Declaration.Data(ctx.names(id, baseTypes), List.empty, constructors.flatMap(monomorphize(_, tparams.size)))
276274
)
277275
case Interface(id, List(), properties) => List(decl)
278276
case Interface(id, tparams, properties) =>
@@ -283,6 +281,15 @@ def monomorphize(decl: Declaration)(using ctx: MonoContext): List[Declaration] =
283281
Declaration.Interface(ctx.names(id, baseTypes), List.empty, properties)
284282
)
285283

284+
def monomorphize(constructor: Constructor, tparamCount: Int)(using ctx: MonoContext): List[Constructor] = constructor match
285+
case Constructor(id, tparams, fields) =>
286+
val monoTypes = ctx.solution.getOrElse(id, Set.empty).toList
287+
monoTypes.map(baseTypes =>
288+
val replacementTparams = tparams.drop(tparamCount).zip(baseTypes).toMap
289+
ctx.replacementTparams ++= replacementTparams
290+
Constructor(ctx.names(id, baseTypes), List.empty, fields map monomorphize)
291+
)
292+
286293
def monomorphize(block: Block)(using ctx: MonoContext): Block = block match
287294
case b: BlockLit => monomorphize(b)
288295
case b: BlockVar => monomorphize(b)
@@ -408,19 +415,16 @@ def monomorphize(opt: Option[Stmt])(using ctx: MonoContext): Option[Stmt] = opt
408415
case Some(stmt) => Some(monomorphize(stmt))
409416

410417
def monomorphize(expr: Expr)(using ctx: MonoContext): Expr = expr match
411-
case DirectApp(b, targs, vargs, bargs) =>
412-
val replacementData = replacementDataFromTargs(b.id, targs)
413-
DirectApp(monomorphize(b, replacementData.name), List.empty, vargs map monomorphize, bargs map monomorphize)
414418
case Literal(value, annotatedType) =>
415419
Literal(value, monomorphize(annotatedType))
416420
case PureApp(b, targs, vargs) =>
417421
val replacementData = replacementDataFromTargs(b.id, targs)
418422
PureApp(monomorphize(b, replacementData.name), List.empty, vargs map monomorphize)
419423
case Make(data, tag, targs, vargs) =>
420-
Make(replacementDataFromTargs(data.name, data.targs), tag, List.empty, vargs map monomorphize)
424+
val replacementTag = ctx.names.getOrElse((tag, (targs map toTypeArg).toVector), tag)
425+
Make(replacementDataFromTargs(data.name, data.targs), replacementTag, List.empty, vargs map monomorphize)
421426
case Box(b, annotatedCapture) =>
422-
// TODO: Does this need other handling?
423-
Box(b, annotatedCapture)
427+
Box(monomorphize(b), annotatedCapture)
424428
case ValueVar(id, annotatedType) =>
425429
ValueVar(id, monomorphize(annotatedType))
426430

0 commit comments

Comments
 (0)