Skip to content

Commit 672efd6

Browse files
committed
Remove Predef declaration from Prelude
It had nothing to do there in the first place.
1 parent e50cb7e commit 672efd6

File tree

12 files changed

+58
-50
lines changed

12 files changed

+58
-50
lines changed

hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ object Elaborator:
103103
val Object = assumeBuiltinCls("Object")
104104
val untyped = assumeBuiltinTpe("untyped")
105105
// println(s"Builtins: $Int, $Num, $Str, $untyped")
106-
val Predef = assumeBuiltinMod("Predef")
107106
object source:
108107
private val module = assumeBuiltinMod("source")
109108
private def assumeObject(nme: Str): BlockMemberSymbol =
@@ -152,8 +151,14 @@ object Elaborator:
152151
given State = this
153152
val globalThisSymbol = TopLevelSymbol("globalThis")
154153
val runtimeSymbol = TempSymbol(N, "runtime")
155-
val effectSigSymbol = ClassSymbol(Tree.TypeDef(syntax.Cls, Tree.Error(), N, N), Tree.Ident("EffectSig"))
156-
val returnClsSymbol = ClassSymbol(Tree.TypeDef(syntax.Cls, Tree.Error(), N, N), Tree.Ident("Return"))
154+
val effectSigSymbol = ClassSymbol(TypeDef(syntax.Cls, Dummy, N, N), Ident("EffectSig"))
155+
val returnClsSymbol = ClassSymbol(TypeDef(syntax.Cls, Dummy, N, N), Ident("Return"))
156+
val matchResultClsSymbol =
157+
val id = new Ident("MatchResult")
158+
ClassSymbol(TypeDef(syntax.Cls, App(id, Tup(Ident("captures") :: Nil)), N, N), id)
159+
val matchFailureClsSymbol =
160+
val id = new Ident("MatchFailure")
161+
ClassSymbol(TypeDef(syntax.Cls, App(id, Tup(Ident("errors") :: Nil)), N, N), id)
157162
val builtinOpsMap =
158163
val baseBuiltins = builtins.map: op =>
159164
op -> BuiltinSymbol(op,

hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ enum Term extends Statement:
7474
case sel: SelProj => sel.sym
7575
case _ => N
7676

77-
def sel(id: Tree.Ident, sym: Opt[FieldSymbol]) =
77+
def sel(id: Tree.Ident, sym: Opt[FieldSymbol]): Sel =
7878
Sel(this, id)(sym)
79-
def selNoSym(nme: Str, synth: Bool = false) =
79+
def selNoSym(nme: Str, synth: Bool = false): Sel | SynthSel =
8080
val id = new Tree.Ident(nme)
8181
if synth
8282
then SynthSel(this, id)(N)

hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/DesugaringBase.scala

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package semantics
33
package ucs
44

55
import mlscript.utils.*, shorthands.*
6-
import syntax.Tree.*, Elaborator.{Ctxl, ctx}
6+
import syntax.Tree.*, Elaborator.{Ctxl, ctx}, Elaborator.State
77

88
/** Contains some helpers that makes UCS desugaring easier. */
9-
trait DesugaringBase(using state: Elaborator.State):
9+
trait DesugaringBase(using state: State):
1010
val elaborator: Elaborator
1111

1212
import elaborator.tl.*, state.globalThisSymbol
@@ -22,30 +22,24 @@ trait DesugaringBase(using state: Elaborator.State):
2222
protected final def app(l: Term, r: Term, label: Str): Term.App = app(l, r, FlowSymbol(label))
2323
protected final def app(l: Term, r: Term, s: FlowSymbol): Term.App = Term.App(l, r)(App(Empty(), Empty()), s)
2424

25-
/** Get the class symbol defined in the `Predef` module. */
26-
protected def resolvePredefMember(name: Str): Ctxl[(Term.SynthSel, ClassSymbol)] =
27-
val predefSymbol = ctx.builtins.Predef
28-
val innerSel = sel(globalThisSymbol.ref(), "Predef", predefSymbol)
29-
val memberSymbol = predefSymbol.tree.definedSymbols.get(name).flatMap(_.asCls).getOrElse:
30-
lastWords(s"Cannot resolve `$name` in `Predef`.")
31-
(sel(innerSel, name, memberSymbol), memberSymbol)
32-
3325
/** Make a term looks like `globalThis.Predef.MatchResult` with its symbol. */
34-
protected lazy val matchResultClass: Ctxl[(Term.SynthSel, ClassSymbol)] = resolvePredefMember("MatchResult")
26+
protected lazy val matchResultClass: Ctxl[(Term.Sel | Term.SynthSel, ClassSymbol)] =
27+
(State.runtimeSymbol.ref().selNoSym("MatchResult", synth=true), State.matchResultClsSymbol)
3528

3629
/** Make a pattern looks like `globalThis.Predef.MatchResult.class`. */
3730
protected def matchResultPattern(parameters: Opt[List[BlockLocalSymbol]]): Ctxl[Pattern.ClassLike] =
3831
val (classRef, classSym) = matchResultClass
39-
val classSel = Term.SynthSel(matchResultClass._1, Ident("class"))(S(classSym))
32+
val classSel = Term.SynthSel(classRef, Ident("class"))(S(classSym))
4033
Pattern.ClassLike(classSym, classSel, parameters, false)(Empty())
4134

4235
/** Make a term looks like `globalThis.Predef.MatchFailure` with its symbol. */
43-
protected lazy val matchFailureClass: Ctxl[(Term.SynthSel, ClassSymbol)] = resolvePredefMember("MatchFailure")
36+
protected lazy val matchFailureClass: Ctxl[(Term.Sel | Term.SynthSel, ClassSymbol)] =
37+
(State.runtimeSymbol.ref().selNoSym("MatchFailure", synth=true), State.matchFailureClsSymbol)
4438

4539
/** Make a pattern looks like `globalThis.Predef.MatchFailure.class`. */
4640
protected def matchFailurePattern(parameters: Opt[List[BlockLocalSymbol]]): Ctxl[Pattern.ClassLike] =
4741
val (classRef, classSym) = matchResultClass
48-
val classSel = Term.SynthSel(matchResultClass._1, Ident("class"))(S(classSym))
42+
val classSel = Term.SynthSel(classRef, Ident("class"))(S(classSym))
4943
Pattern.ClassLike(classSym, classSel, parameters, false)(Empty())
5044

5145
/** Create a term that selects a method in the `Predef` module. */

hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ enum Tree extends AutoLocated:
124124
case Annotated(annotation, target) => annotation :: target :: Nil
125125
case MemberProj(cls, name) => cls :: Nil
126126
case Keywrd(kw) => Nil
127+
case Dummy => Nil
127128

128129
def describe: Str = this match
129130
case Empty() => "empty"
@@ -169,7 +170,7 @@ enum Tree extends AutoLocated:
169170
case Open(_) => "open"
170171
case MemberProj(_, _) => "member projection"
171172
case Keywrd(kw) => s"'${kw.name}' keyword"
172-
case Unt() => "unit"
173+
case Dummy => "‹dummy›"
173174

174175
def deparenthesized: Tree = this match
175176
case Bra(BracketKind.Round, inner) => inner.deparenthesized

hkmc2/shared/src/test/mlscript-compile/Predef.mjs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,6 @@ Predef1 = class Predef {
55
static {
66
this.assert = globalThis.console.assert;
77
this.foldl = Predef.fold;
8-
this.MatchResult = function MatchResult(captures1) {
9-
return new MatchResult.class(captures1);
10-
};
11-
this.MatchResult.class = class MatchResult {
12-
constructor(captures) {
13-
this.captures = captures;
14-
}
15-
toString() { return "MatchResult(" + globalThis.Predef.render(this.captures) + ")"; }
16-
};
17-
this.MatchFailure = function MatchFailure(errors1) {
18-
return new MatchFailure.class(errors1);
19-
};
20-
this.MatchFailure.class = class MatchFailure {
21-
constructor(errors) {
22-
this.errors = errors;
23-
}
24-
toString() { return "MatchFailure(" + globalThis.Predef.render(this.errors) + ")"; }
25-
};
268
this.TraceLogger = class TraceLogger {
279
static {
2810
this.enabled = false;

hkmc2/shared/src/test/mlscript-compile/Predef.mls

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ fun stringGet(string, i) = string.at(i)
133133
fun stringDrop(string, n) = string.slice(n)
134134

135135

136-
class MatchResult(captures)
137-
class MatchFailure(errors)
138-
139136
fun unreachable = throw Error("unreachable")
140137

141138
fun checkArgs(functionName, expected, isUB, got) =

hkmc2/shared/src/test/mlscript-compile/Runtime.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ Runtime1 = class Runtime {
1010
};
1111
this.Unit = new Unit$class;
1212
this.Unit.class = Unit$class;
13+
this.MatchResult = function MatchResult(captures1) {
14+
return new MatchResult.class(captures1);
15+
};
16+
this.MatchResult.class = class MatchResult {
17+
constructor(captures) {
18+
this.captures = captures;
19+
}
20+
toString() { return "MatchResult(" + globalThis.Predef.render(this.captures) + ")"; }
21+
};
22+
this.MatchFailure = function MatchFailure(errors1) {
23+
return new MatchFailure.class(errors1);
24+
};
25+
this.MatchFailure.class = class MatchFailure {
26+
constructor(errors) {
27+
this.errors = errors;
28+
}
29+
toString() { return "MatchFailure(" + globalThis.Predef.render(this.errors) + ")"; }
30+
};
1331
this.FunctionContFrame = function FunctionContFrame(next1) {
1432
return new FunctionContFrame.class(next1);
1533
};

hkmc2/shared/src/test/mlscript-compile/Runtime.mls

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ fun deboundMethod(mtdName, clsName) =
2020
"[debinding error] Method '" + mtdName + "' of class '" + clsName + "' was accessed without being called."
2121

2222

23+
// For `pattern` definitions
24+
class MatchResult(captures)
25+
class MatchFailure(errors)
26+
27+
2328
// Private definitions for algebraic effects
2429

2530
abstract class FunctionContFrame(next) with

hkmc2/shared/src/test/mlscript/decls/Prelude.mls

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,11 @@ declare val Infinity
4242
declare fun parseInt(str: Str, radix: Int): Int
4343
declare fun parseFloat(str: Str): Num
4444

45-
declare module Predef with
46-
class Test with
47-
val x = 0
48-
class MatchResult(captures)
49-
class MatchFailure(errors)
50-
5145

5246
declare module source with
5347
object
5448
line
5549
name
5650
file
51+
52+

hkmc2/shared/src/test/mlscript/rp/MatchResult.mls

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
:js
22

3+
import "../../mlscript-compile/Runtime.mls"
4+
5+
open Runtime { MatchResult, MatchFailure }
6+
7+
38
MatchResult(0)
49
//│ = MatchResult(0)
510

@@ -32,7 +37,7 @@ fun foo(x) = x is Cross
3237
//│ foo = function foo(x2) {
3338
//│ let matchResult;
3439
//│ matchResult = runtime.safeCall(Cross1.unapply(x2));
35-
//│ if (matchResult instanceof globalThis.Predef.MatchResult.class) {
40+
//│ if (matchResult instanceof runtime.MatchResult.class) {
3641
//│ return true
3742
//│ } else {
3843
//│ return false

0 commit comments

Comments
 (0)