Skip to content

Conversation

chengluyu
Copy link
Member

No description provided.

# Conflicts:
#	hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala
#	hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala
#	hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala
#	hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala
#	hkmc2/shared/src/main/scala/hkmc2/semantics/Resolver.scala
#	hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala
#	hkmc2/shared/src/main/scala/hkmc2/semantics/ups/NaiveCompiler.scala
#	hkmc2/shared/src/test/mlscript-compile/Runtime.mjs
#	hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls
#	hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls
#	hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls
#	hkmc2/shared/src/test/mlscript/handlers/NonLocalReturns.mls
#	hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls
#	hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls
#	hkmc2/shared/src/test/mlscript/ups/examples/Record.mls
# Conflicts:
#	hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala
#	hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala
#	hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Desugarer.scala
#	hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala
#	hkmc2/shared/src/test/mlscript/backlog/UCS.mls
#	hkmc2/shared/src/test/mlscript/codegen/Do.mls
#	hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls
#	hkmc2/shared/src/test/mlscript/syntax/WeirdBrackets.mls
#	hkmc2/shared/src/test/mlscript/ucs/hygiene/HygienicBindings.mls
#	hkmc2/shared/src/test/mlscript/ucs/hygiene/PatVars.mls
#	hkmc2/shared/src/test/mlscript/ucs/patterns/AliasPattern.mls
#	hkmc2/shared/src/test/mlscript/ucs/syntax/NestedOpSplits.mls
#	hkmc2/shared/src/test/mlscript/ups/Future.mls
#	hkmc2/shared/src/test/mlscript/ups/examples/Record.mls
#	hkmc2/shared/src/test/mlscript/ups/parametric/EtaConversion.mls

def duplicate: Split =
val copy = this match
(this match
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keeo the val?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  def duplicate: Split =
    (this match
      case Cons(head, tail) => Cons(head, tail.duplicate)
      case Let(name, term, tail) => Let(name, term, tail.duplicate)
      case Else(default) => Else(default)
      case End => End).setDuplicated

It's the return value.

:e
fun trimStart(str) =
if str is (@compile ((" " | "\t") ~ rest)) then trimStart(rest) else str
//│ ╔══[ERROR] Unrecognized pattern (unit).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this unit from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is because the pattern was parsed as part of the annotation, and nothing followed it until the closing parenthesis. As a result, the annotation was not applied to anything. Therefore, the parenthesis is treated as an empty one, which means it becomes unit.

:pe
:e
:w
// Note: `compile ((" " | "\t") ~ rest)` is parsed as an annotation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the then parsed as part of the annotation? 🤯

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if str is @compile ((" " | "\t") ~ rest) then trimStart(rest) else str

The pattern is parsed as part of the annotation. The parser wants to parse a term next, but found then.

# Conflicts:
#	hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala
#	hkmc2/shared/src/test/mlscript-compile/Predef.mjs
#	hkmc2/shared/src/test/mlscript-compile/Runtime.mjs
#	hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls
#	hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls
#	hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls
@chengluyu
Copy link
Member Author

chengluyu commented Sep 30, 2025

I just finished the final major refactor of this PR. The latest commit successfully declutters the Normalization class.

For historical reasons, Normalization not only checked the number of pattern parameters and reported errors, but also performed the efficient and backtracking-free pattern compilation. This made the code look very bloated.

Now, we check the number of arguments in the pattern and call efficient pattern compilation in the process of expanding splits with nested patterns into splits with flattened patterns (handled by SplitCompiler). Since SplitCompiler is also responsible for the naïve compilation of patterns, this refactoring not only reduces duplicate code but also makes the error messages about patterns in UPS and UCS more consistent.

Later today I will do the final minor wrap-up for this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants