From 18499fc9b45f52b58d3d3747f23d43421571f3d7 Mon Sep 17 00:00:00 2001 From: Harry Li Date: Wed, 30 Oct 2024 12:14:06 +0800 Subject: [PATCH] Generalize `Apps` Previsouly `Apps` unapplies the tree of form `App(App(App(id: Ident, p1), p2), p3)` to `Apps(id: Ident, p1 :: p2 :: p3 :: Nil)`. Now it unapplies this as well but also `App(App(App(tree: Tree, p1), p2), p3)` to `Apps(tree: Tree, p1 :: p2 :: p3 :: Nil)`. This is especially useful for type parameters as we want to unwarp as many of layers of `App` as we can, then unwrap the `TyApp`. --- .../shared/src/main/scala/hkmc2/semantics/Elaborator.scala | 7 ++----- hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala | 5 ++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala index 2eb99012e..3238fa835 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala @@ -384,7 +384,7 @@ extends Importer: (ctx, acc) newCtx.givenIn: go(sts, newAcc) - case (hd @ LetLike(`let`, Apps(id, tups), rhso, N)) :: sts if id.name.headOption.exists(_.isLower) => + case (hd @ LetLike(`let`, Apps(id: Ident, tups), rhso, N)) :: sts if id.name.headOption.exists(_.isLower) => val sym = fieldOrVarSym(LetBind, id) log(s"Processing `let` statement $id (${sym}) ${ctx.outer}") @@ -428,12 +428,9 @@ extends Importer: case S(t) => typeParams(t) case N => (N, ctx) // Add parameters to context - val (ps, newCtx) = td.params match - case S(ts) => // Go through all parameter lists - ts.foldLeft((Ls[Param](), newCtx1)): + val (ps, newCtx) = td.paramLists.foldLeft((Ls[Param](), newCtx1)): case ((ps, ctx), t) => params(t)(using ctx).mapFirst(ps ++ _) .mapFirst(some) - case N => (N, newCtx1) val b = rhs.map(term(_)(using newCtx)) val r = FlowSymbol(s"‹result of ${sym}›", nextUid) val tdf = TermDefinition(k, sym, ps, diff --git a/hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala b/hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala index 8c721ee52..555131789 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala @@ -157,10 +157,9 @@ object PlainTup: def apply(fields: Tree*): Tree = Tup(fields.toList) object Apps: - def unapply(t: Tree): Opt[(Ident, Ls[Tup])] = t match + def unapply(t: Tree): Opt[(Tree, Ls[Tup])] = t match case App(Apps(id, args), arg: Tup) => S(id, args :+ arg) - case id: Ident => S(id -> Nil) - case _ => N + case t => S(t, Nil) sealed abstract class OuterKind(val desc: Str)