Skip to content
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

Type Classes #256

Open
wants to merge 5 commits into
base: hkmc2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 25 additions & 2 deletions hkmc2/jvm/src/test/scala/hkmc2/MLsDiffMaker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import mlscript.utils.*, shorthands.*
import utils.*

import hkmc2.semantics.Elaborator
import hkmc2.semantics.ImplicitResolver


abstract class MLsDiffMaker extends DiffMaker:
Expand Down Expand Up @@ -34,11 +35,14 @@ abstract class MLsDiffMaker extends DiffMaker:
val silent = NullaryCommand("silent")
val dbgElab = NullaryCommand("de")
val dbgParsing = NullaryCommand("dp")
val dbgResolving = NullaryCommand("dr")

val showParse = NullaryCommand("p")
val showParsedTree = DebugTreeCommand("pt")
val showElab = NullaryCommand("el")
val showElaboratedTree = DebugTreeCommand("elt")
val showResolve = NullaryCommand("r")
val showResolvedTree = DebugTreeCommand("rt")
val showLoweredTree = NullaryCommand("lot")
val ppLoweredTree = NullaryCommand("slot")
val showContext = NullaryCommand("ctx")
Expand All @@ -56,6 +60,7 @@ abstract class MLsDiffMaker extends DiffMaker:
override def dbg: Bool =
dbgParsing.isSet
|| dbgElab.isSet
|| dbgResolving.isSet
|| debug.isSet

val etl = new TraceLogger:
Expand All @@ -68,13 +73,21 @@ abstract class MLsDiffMaker extends DiffMaker:
// * Perhaps this should be the default behavior of TraceLogger.
if doTrace then super.trace(pre, post)(thunk)
else thunk

val rtl = new TraceLogger:
override def doTrace = dbgResolving.isSet
override def emitDbg(str: String): Unit = output(str)

var curCtx = Elaborator.State.init
var curICtx = ImplicitResolver.ICtx.empty

var prelude = Elaborator.Ctx.empty

override def run(): Unit =
if file =/= preludeFile then importFile(preludeFile, verbose = false)
// if file =/= preludeFile then
importFile(preludeFile, verbose = false)
curCtx = curCtx.nest(N)
prelude = curCtx
super.run()


Expand Down Expand Up @@ -173,7 +186,7 @@ abstract class MLsDiffMaker extends DiffMaker:
private var blockNum = 0

def processTrees(trees: Ls[syntax.Tree])(using Raise): Unit =
val elab = Elaborator(etl, file / os.up)
val elab = Elaborator(etl, file / os.up, prelude)
val blockSymbol =
semantics.TopLevelSymbol("block#"+blockNum)
blockNum += 1
Expand All @@ -187,6 +200,16 @@ abstract class MLsDiffMaker extends DiffMaker:
showElaboratedTree.get.foreach: post =>
output(s"Elaborated tree:")
output(e.showAsTree(using post))

val resolver = ImplicitResolver(rtl)
curICtx = resolver.resolveBlk(e)(using curICtx)

if showResolve.isSet then
output(s"Resolved: ${e.showDbg}")
showResolvedTree.get.foreach: post =>
output(s"Resolved tree:")
output(e.showAsTree(using post))

processTerm(e, inImport = false)


Expand Down
3 changes: 2 additions & 1 deletion hkmc2/shared/src/main/scala/hkmc2/MLsCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ class MLsCompiler(preludeFile: os.Path):
val (pblk, newCtx) = elab.importFrom(preludeParse.resultBlk)(using initState)

newCtx.nest(N).givenIn:
val elab = Elaborator(etl, wd, newCtx)

val (blk, newCtx) = elab.importFrom(mainParse.resultBlk)
val (blk, _) = elab.importFrom(mainParse.resultBlk)
val low = ltl.givenIn:
codegen.Lowering()
val jsb = codegen.js.JSBuilder()
Expand Down
6 changes: 3 additions & 3 deletions hkmc2/shared/src/main/scala/hkmc2/bbml/bbML.scala
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,13 @@ class BBTyper(using elState: Elaborator.State, tl: TL, scope: Scope):
effBuff += eff
ctx += sym -> rhsTy
goStats(stats)
case TermDefinition(_, Fun, sym, ps :: Nil, sig, Some(body), _, _, _) :: stats =>
case TermDefinition(_, Fun, sym, ps :: Nil, _, sig, Some(body), _, _, _) :: stats =>
typeFunDef(sym, Term.Lam(ps, body), sig, ctx)
goStats(stats)
case TermDefinition(_, Fun, sym, Nil, sig, Some(body), _, _, _) :: stats =>
case TermDefinition(_, Fun, sym, Nil, _, sig, Some(body), _, _, _) :: stats =>
typeFunDef(sym, body, sig, ctx) // * may be a case expressions
goStats(stats)
case TermDefinition(_, Fun, sym, _, S(sig), None, _, _, _) :: stats =>
case TermDefinition(_, Fun, sym, _, _, S(sig), None, _, _, _) :: stats =>
ctx += sym -> typeType(sig)
goStats(stats)
case (clsDef: ClassDef) :: stats =>
Expand Down
15 changes: 15 additions & 0 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ class Lowering(using TL, Raise, Elaborator.State):
case sem.Fld(flags, value, asc) =>
TODO("Other argument forms")
case spd: Spd => true -> spd.term
case ca: sem.CtxArg => ca.term match
case S(t) =>
false -> t
case N =>
// All contextual arguments should have been
// populated by implicit resolution before lowering.
// Fail silently.
false -> Term.Error
val l = new TempSymbol(S(t))
def rec(as: Ls[Bool -> st], asr: Ls[Arg]): Block = as match
case Nil => k(Call(fr, asr.reverse)(isMlsFun))
Expand All @@ -103,6 +111,7 @@ class Lowering(using TL, Raise, Elaborator.State):
subTerm(prefix): p =>
conclude(Select(p, nme)(sel.sym))
case _ => subTerm(f)(conclude)
case st.TyApp(lhs, _) => term(lhs)(k)
case st.Blk(Nil, res) => term(res)(k)
case st.Blk(Lit(Tree.UnitLit(true)) :: stats, res) =>
subTerm(st.Blk(stats, res))(k)
Expand Down Expand Up @@ -131,6 +140,12 @@ class Lowering(using TL, Raise, Elaborator.State):
val (paramLists, bodyBlock) = setupFunctionDef(td.params, bod, S(td.sym.nme))
Define(FunDefn(td.sym, paramLists, bodyBlock),
term(st.Blk(stats, res))(k))
case syntax.Ins =>
// Implciit instances are not parameterized for now.
assert(td.params.isEmpty)
subTerm(bod)(r =>
Define(ValDefn(td.owner, syntax.ImmutVal, td.sym, r),
term(st.Blk(stats, res))(k)))
// case cls: ClassDef =>
case cls: ClassLikeDef =>
reportAnnotations(cls, cls.annotations)
Expand Down
Loading
Loading