Skip to content

Move All Resolution to the Resolution Stage #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
452f81a
Move all resolution to the resolution stage
FlandiaYingman Apr 16, 2025
424b104
Funny test cases
FlandiaYingman Apr 16, 2025
c8f9a0c
Changes from meeting
LPTK Apr 16, 2025
b3e2e89
Improved Symbol Resolution
FlandiaYingman Apr 22, 2025
d3e10df
Update hkmc2/shared/src/test/mlscript/codegen/FunnyOpen.mls
FlandiaYingman Apr 22, 2025
20f8401
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala
FlandiaYingman Apr 22, 2025
5b01df0
Meaningless Underscore
FlandiaYingman Apr 22, 2025
f1a13ca
Revert HkScratch.mls
FlandiaYingman Apr 22, 2025
b77b920
Fix funny BBML problem
FlandiaYingman Apr 23, 2025
1640526
Fix funny changes
FlandiaYingman Apr 23, 2025
4e731a1
Funny restParam
FlandiaYingman Apr 23, 2025
133c5b2
Good job
FlandiaYingman Apr 23, 2025
2c33835
Fix whitespace
FlandiaYingman Apr 23, 2025
c3c5e8f
TyApp implict resolution
FlandiaYingman Apr 23, 2025
3522036
Funny Fix
FlandiaYingman Apr 23, 2025
3c41858
Fix ref resolution
FlandiaYingman Apr 23, 2025
1c7e9c2
Merge branch 'hkmc2' into move-implicit-resolution
FlandiaYingman Apr 23, 2025
cfa6077
Funny test diff
FlandiaYingman Apr 23, 2025
a83d71c
Merge branch 'hkmc2' into move-implicit-resolution
FlandiaYingman Apr 25, 2025
13d0dc7
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Resolver.scala
FlandiaYingman Apr 25, 2025
51e907c
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Resolver.scala
FlandiaYingman Apr 25, 2025
74fadce
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala
FlandiaYingman Apr 25, 2025
99726ff
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Resolver.scala
FlandiaYingman Apr 25, 2025
96d9a17
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Resolver.scala
FlandiaYingman Apr 25, 2025
2c2f7c0
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Resolver.scala
FlandiaYingman Apr 25, 2025
778228f
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Resolver.scala
FlandiaYingman Apr 25, 2025
b2cb124
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Resolver.scala
FlandiaYingman Apr 25, 2025
ce4e1f3
Fix comments
FlandiaYingman Apr 25, 2025
6307b89
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Resolver.scala
FlandiaYingman Apr 26, 2025
c3eb4d1
wrap line
FlandiaYingman Apr 26, 2025
8c04952
FIx symbol
FlandiaYingman Apr 26, 2025
413ac95
Fix lowering symbol
FlandiaYingman Apr 26, 2025
8fcd996
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Resolver.scala
LPTK Apr 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx):
final def term(t: st, inStmtPos: Bool = false)(k: Result => Block)(using Subst): Block =
tl.log(s"Lowering.term ${t.showDbg.truncate(100, "[...]")}${
if inStmtPos then " (in stmt)" else ""}${
t.symbol.fold("")(" – symbol " + _)}")
t.resolvedSymbol.fold("")(" – symbol " + _)}")

def warnStmt = if inStmtPos then
raise:
Expand Down Expand Up @@ -306,7 +306,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx):
End("error")
case st.TyApp(f, ts) => term(f)(k) // * Type arguments are erased
case st.App(f, arg) =>
val isMlsFun = f.symbol.fold(f.isInstanceOf[st.Lam]):
val isMlsFun = f.resolvedSymbol.fold(f.isInstanceOf[st.Lam]):
case _: sem.BuiltinSymbol => true
case sym: sem.BlockMemberSymbol =>
sym.trmImplTree.fold(sym.clsTree.isDefined)(_.k is syntax.Fun)
Expand All @@ -320,17 +320,17 @@ class Lowering()(using Config, TL, Raise, State, Ctx):
subTerm_nonTail(arg): ar =>
k(Call(fr, Arg(spread = true, ar) :: Nil)(isMlsFun, true).withLocOf(t))
f match
case t if t.symbol.isDefined && (t.symbol.get is ctx.builtins.js.try_catch) =>
case t if t.resolvedSymbol.isDefined && (t.resolvedSymbol.get is ctx.builtins.js.try_catch) =>
conclude(Value.Ref(State.runtimeSymbol).selN(Tree.Ident("try_catch")))
case t if t.symbol.isDefined && (t.symbol.get is ctx.builtins.debug.printStack) =>
case t if t.resolvedSymbol.isDefined && (t.resolvedSymbol.get is ctx.builtins.debug.printStack) =>
if !config.effectHandlers.exists(_.debug) then
raise(ErrorReport(
msg"Debugging functions are not enabled" ->
t.toLoc :: Nil,
source = Diagnostic.Source.Compilation))
return End("error")
conclude(Value.Ref(State.runtimeSymbol).selSN("raisePrintStackEffect").withLocOf(f))
case t if t.symbol.isDefined && (t.symbol.get is ctx.builtins.debug.getLocals) =>
case t if t.resolvedSymbol.isDefined && (t.resolvedSymbol.get is ctx.builtins.debug.getLocals) =>
if !config.effectHandlers.exists(_.debug) then
raise(ErrorReport(
msg"Debugging functions are not enabled" ->
Expand Down
10 changes: 9 additions & 1 deletion hkmc2/shared/src/main/scala/hkmc2/semantics/Resolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,16 @@ class Resolver(tl: TraceLogger)
case _ =>

t match
// If a reference was not resolved to take implicit arguments or
// return some module type, then its result symbol is the same as
// the symbol it refers to.
case t: Term.Ref if t.resSym.isEmpty =>
t.resSym = S(t.sym)
// If a type application was not resolved to take implicit
// arguments, then its result symbol is the same as the symbol its
// LHS.
case t: Term.TyApp if t.sym.isEmpty =>
t.sym = t.lhs.resolvedSymbol
case _ =>

def resolveArg(p: Param)(lhs: Term)(using ictx: ICtx): Elem =
Expand Down Expand Up @@ -766,7 +774,7 @@ object ModuleChecker:
t match
case Term.Blk(_, res) => evalsToModule(res)
case Term.IfLike(`if`, split) => split.results.exists(evalsToModule(_))
case t => t.symbol.exists(checkSym)
case t => t.resolvedSymbol.exists(checkSym)

extension [T](xs: Ls[Opt[T]])
def sequence: Opt[Ls[T]] =
Expand Down
29 changes: 22 additions & 7 deletions hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sealed trait ResolvableImpl:
case S(iargsLs) => iargsLs.foldLeft(t.withoutIArgs): (t, args) =>
Term.App(t, args)(Tree.DummyApp, N, FlowSymbol("implicit app")).noIArgs // N: todo

def defn: Opt[Definition] = t.symbol match
def defn: Opt[Definition] = t.resolvedSymbol match
case S(sym: MemberSymbol[?]) => sym.defn
case S(sym: BlockLocalSymbol) => sym.decl match
case S(td: Definition) => S(td)
Expand Down Expand Up @@ -84,7 +84,7 @@ enum Term extends Statement:
case Builtin(id: Tree.Ident, nme: Str)
case Ref(sym: Symbol)(val tree: Tree.Ident, val refNum: Int, var resSym: Opt[Symbol]) extends Term with ResolvableImpl
case App(lhs: Term, rhs: Term)(val tree: Tree.App, var sym: Opt[FieldSymbol], val resSym: FlowSymbol) extends Term with ResolvableImpl
case TyApp(lhs: Term, targs: Ls[Term])(var sym: Opt[FieldSymbol]) extends Term with ResolvableImpl
case TyApp(lhs: Term, targs: Ls[Term])(var sym: Opt[Symbol]) extends Term with ResolvableImpl
case Sel(prefix: Term, nme: Tree.Ident)(var sym: Opt[FieldSymbol]) extends Term with ResolvableImpl
case SynthSel(prefix: Term, nme: Tree.Ident)(var sym: Opt[FieldSymbol]) extends Term with ResolvableImpl
case DynSel(prefix: Term, fld: Term, arrayIdx: Bool)
Expand Down Expand Up @@ -115,15 +115,29 @@ enum Term extends Statement:
case Handle(lhs: LocalSymbol, rhs: Term, args: List[Term],
derivedClsSym: ClassSymbol, defs: Ls[HandlerTermDefinition], body: Term)

def symbol: Opt[Symbol] = this match
case ref: Ref if ref.resSym.nonEmpty => ref.resSym
/**
* The prelinminary symbol for the term that is resolved during
* elaboration.
*/
lazy val symbol: Opt[Symbol] = this match
case Ref(sym) => S(sym)
case sel: Sel => sel.sym
case sel: SynthSel => sel.sym
case sel: SelProj => sel.sym
case TyApp(lhs = lhs) => lhs.symbol
case _ => N

/**
* The symbol representing the evaluation result of the term. This
* symbol is resolved during the resolution stage.
*/
def resolvedSymbol: Opt[Symbol] = this match
case ref: Ref => ref.resSym
case sel: Sel => sel.sym
case sel: SynthSel => sel.sym
case sel: SelProj => sel.sym
case app: App => app.sym
case tyApp: TyApp if tyApp.sym.nonEmpty => tyApp.sym
case TyApp(prefix, _) => prefix.symbol
case tyApp: TyApp => tyApp.sym
case _ => N

def sel(id: Tree.Ident, sym: Opt[FieldSymbol]): Sel =
Expand Down Expand Up @@ -183,7 +197,8 @@ sealed trait Statement extends AutoLocated with ProductWithExtraInfo:

def extraInfo: Str = this match
case ref: Ref if ref.resSym.isEmpty => ""
case r: (Resolvable & Term | SelProj) => r.symbol.mkString
case r: Resolvable => r.resolvedSymbol.mkString
case r: SelProj => r.symbol.mkString
case _ => ""

def subStatements: Ls[Statement] = this match
Expand Down
2 changes: 1 addition & 1 deletion hkmc2/shared/src/main/scala/hkmc2/utils/utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ extension (t: Product)
t match
case t: Resolvable if t.iargsLs.forall(_.nonEmpty) =>
args = args ++ Iterator:
"iargs = " + aux(t.iargsLs)
"iargsLs = " + aux(t.iargsLs)
case _ =>
prefix + locally:
if inTailPos then ": \\\n" + args.mkString("\n")
Expand Down
6 changes: 3 additions & 3 deletions hkmc2/shared/src/test/mlscript/backlog/NamedArgs.mls
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ id(a: 0)
//│ lhs = SynthSel{member:id}:
//│ prefix = Ref:
//│ sym = member:Predef
//│ iargs = N
//│ iargsLs = N
//│ nme = Ident of "id"
//│ iargs = N
//│ iargsLs = N
//│ rhs = Tup of Ls of
//│ Fld:
//│ flags = ()
//│ term = Lit of StrLit of "a"
//│ asc = S of Lit of IntLit of 0
//│ iargs = N
//│ iargsLs = N
//│ = {a: 0}

id of a: 0
Expand Down
24 changes: 12 additions & 12 deletions hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ Foo(42).x
//│ prefix = App:
//│ lhs = Ref:
//│ sym = member:Foo
//│ iargs = N
//│ iargsLs = N
//│ rhs = Tup of Ls of
//│ Fld:
//│ flags = ()
//│ term = Lit of IntLit of 42
//│ asc = N
//│ iargs = N
//│ iargsLs = N
//│ nme = Ident of "x"
//│ iargs = N
//│ iargsLs = N
//│ = 42


Expand All @@ -39,9 +39,9 @@ foo.x
//│ res = Sel:
//│ prefix = Ref:
//│ sym = foo
//│ iargs = N
//│ iargsLs = N
//│ nme = Ident of "x"
//│ iargs = N
//│ iargsLs = N
//│ = 42


Expand All @@ -54,9 +54,9 @@ foo.y
//│ res = Sel:
//│ prefix = Ref:
//│ sym = foo
//│ iargs = N
//│ iargsLs = N
//│ nme = Ident of "y"
//│ iargs = N
//│ iargsLs = N
//│ = 42


Expand Down Expand Up @@ -84,26 +84,26 @@ case
//│ head = Branch:
//│ scrutinee = Ref:
//│ sym = caseScrut
//│ iargs = N
//│ iargsLs = N
//│ pattern = ClassLike:
//│ sym = class:Foo
//│ trm = SynthSel{class:Foo}:
//│ prefix = Ref:
//│ sym = member:Foo
//│ iargs = N
//│ iargsLs = N
//│ nme = Ident of "class"
//│ iargs = N
//│ iargsLs = N
//│ args = S of Ls of
//│ S of $param0
//│ refined = false
//│ continuation = Let:
//│ sym = a
//│ term = Ref:
//│ sym = $param0
//│ iargs = N
//│ iargsLs = N
//│ tail = Else of Ref:
//│ sym = a
//│ iargs = N
//│ iargsLs = N
//│ tail = End
//│ Lowered:
//│ Program:
Expand Down
8 changes: 4 additions & 4 deletions hkmc2/shared/src/test/mlscript/ctx/SymbolResolution.mls
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ R.bar
//│ res = Sel{module:M}:
//│ prefix = Ref{member:R} of member:R
//│ nme = Ident of "bar"
//│ iargs = S of Ls of
//│ iargsLs = S of Ls of
//│ Tup of Ls of
//│ Fld:
//│ flags = ()
Expand All @@ -175,7 +175,7 @@ foo
//│ stats = Nil
//│ res = Ref{module:M}:
//│ sym = member:foo
//│ iargs = S of Ls of
//│ iargsLs = S of Ls of
//│ Tup of Ls of
//│ Fld:
//│ flags = ()
Expand Down Expand Up @@ -204,7 +204,7 @@ R.bar[Int]
//│ nme = Ident of "bar"
//│ targs = Ls of
//│ Ref{member:Int} of member:Int
//│ iargs = S of Ls of
//│ iargsLs = S of Ls of
//│ Tup of Ls of
//│ Fld:
//│ flags = ()
Expand All @@ -231,7 +231,7 @@ foo[Int]
//│ lhs = Ref{member:foo} of member:foo
//│ targs = Ls of
//│ Ref{member:Int} of member:Int
//│ iargs = S of Ls of
//│ iargsLs = S of Ls of
//│ Tup of Ls of
//│ Fld:
//│ flags = ()
Expand Down
Loading
Loading