Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
b5bdd5b
Basic adt typing and fix subst pol
NeilKleistGao May 20, 2025
e8d47a0
Support type parameters
NeilKleistGao May 21, 2025
37d6f63
Distinguish generic ctors from normal ones
NeilKleistGao May 22, 2025
921a079
WIP: Add pattern matching
NeilKleistGao May 22, 2025
75c2afd
Add docs
NeilKleistGao May 23, 2025
5850017
Merge branch 'hkmc2' into adt💬
LPTK May 27, 2025
929a49a
Deal with merge conflicts
chengluyu May 27, 2025
056e138
WIP from meeting
LPTK May 28, 2025
0c534f1
Add proper Unit type
LPTK May 28, 2025
a36c6d2
Elaborate example a bit
LPTK May 28, 2025
973afbc
Reject non-variable pattern args for now
NeilKleistGao May 28, 2025
8b259cf
WIP: Fix
NeilKleistGao May 28, 2025
37a60a3
WIP: Rerun test
NeilKleistGao May 29, 2025
0194f4d
Use mutable set for cache and have more test cases
NeilKleistGao May 29, 2025
7b77eef
Add more spaces for Paper example
NeilKleistGao May 29, 2025
8999fec
Add example test file
LPTK May 30, 2025
8febb63
Changes from meeting
LPTK May 30, 2025
36560d2
Fix missing cases
NeilKleistGao May 30, 2025
27038a7
W
LPTK May 30, 2025
94bf051
W
LPTK May 30, 2025
d0915ba
Merge remote-tracking branch 'NeilKleistGao/adt💬' into adt💬
LPTK May 30, 2025
096f282
Add pmsort
NeilKleistGao May 30, 2025
84f4712
Merge
NeilKleistGao May 30, 2025
b6ddcaf
Add a clean version
NeilKleistGao May 30, 2025
d318440
Remove difftests things for now
NeilKleistGao May 30, 2025
2783f5f
Add more examples
NeilKleistGao May 30, 2025
9f48b1e
Fix problems in ExamplesInResponse.mls
LPTK Jun 2, 2025
15e5fcb
Add equivalent sig
LPTK Jun 2, 2025
4fbb6ea
Port some examples from flix
NeilKleistGao Jun 6, 2025
8be7290
Port GUI example from flix
NeilKleistGao Jun 9, 2025
1a3b41d
Add extension examples
NeilKleistGao Jun 9, 2025
9ada6c8
Categorize examples and add more documentations
NeilKleistGao Jun 10, 2025
4e8a8a1
Add else type check
NeilKleistGao Jun 16, 2025
717a864
Merge
NeilKleistGao Jun 17, 2025
2788340
WIP: Add decl-site variance
NeilKleistGao Jun 17, 2025
c0198de
Add test for pat-mat
NeilKleistGao Jun 19, 2025
3c1abef
Changes from meeting + some older changes (start making the constrain…
LPTK Jun 24, 2025
60e27b3
Merge from hkmc2
NeilKleistGao Jun 25, 2025
cd6b77c
Rename
NeilKleistGao Jun 25, 2025
a5f3219
Make region targ covariant
NeilKleistGao Jul 3, 2025
afc5e74
Merge branch 'hkmc2' of github.com:hkust-taco/mlscript into adt💬
NeilKleistGao Jul 22, 2025
32a98c0
Add case study examples
NeilKleistGao Jul 23, 2025
3cd0c3a
WIP: Add codegen for constraint solver (without tests) and minor fix
NeilKleistGao Jul 24, 2025
9cb9f26
Some fix
NeilKleistGao Jul 25, 2025
4c6c366
Update web demo code
NeilKleistGao Jul 30, 2025
240a555
Add missing updates
NeilKleistGao Aug 20, 2025
522f9c1
Minor
NeilKleistGao Aug 21, 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
2 changes: 1 addition & 1 deletion hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx):

case Quoted(body) => quote(body)(k)

// * BbML-specific cases: t.Cls#field and mutable operations
// * InvalML-specific cases: t.Cls#field and mutable operations
case sp @ SelProj(prefix, _, proj) =>
setupSelection(prefix, proj, sp.sym)(k)
case Region(reg, body) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package hkmc2
package bbml
package invalml

import scala.collection.mutable

Expand All @@ -9,12 +9,11 @@ import mlscript.utils.*, shorthands.*
import utils.*
import utils.Scope

// * TODO use mutabnle cache instead for correct asymptotic complexity
type Cache = Set[(Type, Type)]
type Cache = mutable.HashSet[(Type, Type)]
type ExtrudeCache = mutable.HashMap[(Uid[InfVar], Bool), InfVar]

case class CCtx(cache: Cache, parents: Ls[(Type, Type)], origin: Term, exp: Opt[GeneralType])(using Scope):
def err(using Raise) =
def err(using Raise, InvalCtx) =
raise(ErrorReport(
msg"Type error in ${origin.describe}${exp match
case S(ty) => msg" with expected type ${ty.show}"
Expand All @@ -25,23 +24,23 @@ case class CCtx(cache: Cache, parents: Ls[(Type, Type)], origin: Term, exp: Opt[
)
))
def nest(sub: (Type, Type)): CCtx =
copy(cache = cache + sub, parents = parents match
copy(cache = cache += sub, parents = parents match
case `sub` :: _ => parents
case _ => sub :: parents
)
object CCtx:
inline def init(origin: Term, exp: Opt[GeneralType])(using Scope) = CCtx(Set.empty, Nil, origin, exp)
inline def init(origin: Term, exp: Opt[GeneralType])(using Scope) = CCtx(mutable.HashSet.empty, Nil, origin, exp)
def cctx(using CCtx): CCtx = summon

class ConstraintSolver(infVarState: InfVarUid.State, elState: Elaborator.State, tl: TraceLogger):
import tl.{trace, log}

import hkmc2.bbml.NormalForm.*
import hkmc2.invalml.NormalForm.*

private def freshXVar(lvl: Int, sym: Symbol, hint: Str): InfVar =
InfVar(lvl, infVarState.nextUid, new VarState(), false)(InstSymbol(sym)(using elState), hint)

def extrude(ty: Type)(using lvl: Int, pol: Bool, cache: ExtrudeCache, bbctx: BbCtx, cctx: CCtx, tl: TL): Type =
def extrude(ty: Type)(using lvl: Int, pol: Bool, cache: ExtrudeCache, invalctx: InvalCtx, cctx: CCtx, tl: TL): Type =
trace[Type](s"Extruding[${printPol(pol)}] ${ty.showDbg}", r => s"~> ${r.showDbg}"):
if ty.lvl <= lvl then ty else ty.toBasic/*TODO improve extrude directly*/ match
case ClassLikeType(sym, targs) =>
Expand Down Expand Up @@ -79,7 +78,7 @@ class ConstraintSolver(infVarState: InfVarUid.State, elState: Elaborator.State,
case NegType(ty) => Type.mkNegType(extrude(ty)(using lvl, !pol))
case Top | Bot => ty

private def constrainConj(conj: Conj)(using BbCtx, CCtx, TL): Unit = trace(s"Constraining ${conj.showDbg}"):
private def constrainConj(conj: Conj)(using InvalCtx, CCtx, TL): Unit = trace(s"Constraining ${conj.showDbg}"):
conj match
case Conj(i, u, (v, pol) :: tail) =>
var rest = Conj(i, u, tail)
Expand All @@ -89,13 +88,13 @@ class ConstraintSolver(infVarState: InfVarUid.State, elState: Elaborator.State,
if pol then
val nc = Type.mkNegType(bd).toDnf // always cache the normal form to avoid unexpected cache misses
log(s"New bound: ${v.showDbg} <: ${nc.showDbg}")
cctx.nest(v -> nc) givenIn:
cctx.nest(v.toDnf -> nc) givenIn:
v.state.upperBounds ::= nc
v.state.lowerBounds.foreach(lb => constrainImpl(lb, nc))
else
val c = bd.toDnf // always cache the normal form to avoid unexpected cache misses
log(s"New bound: ${v.showDbg} :> ${c.showDbg}")
cctx.nest(c -> v) givenIn:
cctx.nest(c -> v.toDnf) givenIn:
v.state.lowerBounds ::= c
v.state.upperBounds.foreach(ub => constrainImpl(c, ub))
case Conj(i, u, Nil) => (conj.i, conj.u) match
Expand All @@ -122,10 +121,10 @@ class ConstraintSolver(infVarState: InfVarUid.State, elState: Elaborator.State,
// raise(ErrorReport(msg"Cannot solve ${conj.i.toString()} <: ${conj.u.toString()}" -> N :: Nil))
cctx.err

private def constrainDNF(disj: Disj)(using BbCtx, CCtx, TL): Unit =
private def constrainDNF(disj: Disj)(using InvalCtx, CCtx, TL): Unit =
disj.conjs.foreach(constrainConj(_))

private def constrainArgs(lhs: TypeArg, rhs: TypeArg)(using BbCtx, CCtx, TL): Unit =
private def constrainArgs(lhs: TypeArg, rhs: TypeArg)(using InvalCtx, CCtx, TL): Unit =
constrainImpl(rhs.negPart, lhs.negPart)
constrainImpl(lhs.posPart, rhs.posPart)

Expand All @@ -137,13 +136,13 @@ class ConstraintSolver(infVarState: InfVarUid.State, elState: Elaborator.State,
case NegType(ty) => NegType(inlineSkolemBounds(ty, !pol))
case _: ClassLikeType | _: FunType | _: InfVar | Top | Bot => ty

private def constrainImpl(lhs: Type, rhs: Type)(using BbCtx, CCtx, TL): Unit =
private def constrainImpl(lhs: Type, rhs: Type)(using InvalCtx, CCtx, TL): Unit =
val p = lhs.toDnf -> rhs.toDnf
if cctx.cache(p) then log(s"Cached!")
else trace(s"CONSTRAINT ${lhs.showDbg} <: ${rhs.showDbg}"):
cctx.nest(p) givenIn:
val ty = dnf(inlineSkolemBounds(lhs & rhs.!, true)(using Set.empty))
constrainDNF(ty)
def constrain(lhs: Type, rhs: Type)(using BbCtx, CCtx, TL): Unit =
def constrain(lhs: Type, rhs: Type)(using InvalCtx, CCtx, TL): Unit =
constrainImpl(lhs, rhs)

Loading