-
Notifications
You must be signed in to change notification settings - Fork 29
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
New UCS desugarer and rudimentary PreTyper
#194
Conversation
- Rename functions from `visitX` to `traverseX`. - Add Boolean condition test cases. - Use dollar symbol in generated variables. - Add `TupleCase` which currently is commented.
This commit was cherry-picked from a future commit which changed many files.
- Very minor but important: should use post-processed term as the desugared term. - Add incomplete AVL tree test cases which reveals many problems. - Cache operator split partial terms for efficiency. - `Traceable` now supports filtering debugging by topics. - Skip speicalization Boolean condition scrutinees.
and clean up some code...
We used to generate let bindings which destruct class parameters during normalization. This works fine but it exposes some inflexibility. For example, in the previous commit, I added a case where free variables from a absorbed case branch are shadowed. If we moving let bindings generation to desugaring stage, the problem can be solved easily. This change also paves the road for duplicated branch lifting.
Normalization stage is greatly simplified from now!
also support traversing let bindings
This is a refactor and not test changes were found.
I'm fixing not being able to report some unreachable cases and conflicting cases. I'm handling the situation we discussed before: some duplicated branches can also trigger these checks. The solution I take is to mark the duplicated syntax tree nodes, so we will not trigger warnings if these nodes are detected. |
That's a good enough short-term solution, but after this PR is merged we need to think of a more principled and correct way of handling this! |
This solution is more limited than I thought. I expected it to be able to detect all the duplicated cases. Because a split might be concatenated as a fallback in the specialization for different scrutinees, a Boolean flag is not enough. I tried to turn this boolean flag into a map, but I need to modify the map in many places. I haven't been able to make it work well until now, and the code will also become very messy. I feel it can figure out most of the duplicated cases, but it can't detect some of the covered cases, such as the one below.
I think a neater approach is to check on the desugared (rather than normalized) syntax tree. Although we can know which branches are removed during specialization, we don't know if they are removed due to duplication and which branches they overlap with because they need non-local information. Lastly, I've also improved the warning message for different duplicated cases at the same time. There're three types of duplication.
|
shared/src/test/diff/pretyper/ucs/coverage/ConflictedPatterns.mls
Outdated
Show resolved
Hide resolved
Makes sense. This can go in a future PR! |
BTW was this fixed or at least reproduced in the tests? |
Co-authored-by: Lionel Parreaux <lionel.parreaux@gmail.com>
Yes, this was fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
TL;DR
This PR adds new UCS desugarer and a rudimentary implementation of
PreTyper
.Features
Desugarer
The new UCS desugarer replaces the old desugarer. The facilities old desugarer were also removed. The new desugarer has following facilities.
mlscript.ucs.syntax
.mlscript.ucs.stages
. The stages are the following.Transformation
: transforming parsed AST to source abstract syntax.Desugaring
: generating let bindings and expand patterns.Normalization
: removing backtracking and convert core abstract syntax to case branches.PostProcessing
: merging case branches with the same scrutinee.CoverageChecking
: detecting unreachable cases and duplicated cases.Context
,Scrutinee
andPattern
.If
term is associated with a uniqueContext
. AContext
keeps track of all scrutinee and is in charge of allocating temporary variable names.Scrutinee
represents a term that is being matched in UCS expressions. It keeps the tally of all patterns the scrutinee has been matched against as well as occurrences and sub-scrutinees of each pattern.We support two kinds of new patterns in this PR.
PreTyper
PreTyper
traverses the AST and associate each variable with symbol it refers to.Symbol
s. Each symbol class maintains certain information useful to desugarer.Scope
maintains an association from variables to symbols.DiffTests
:ducs
controls the newDesugarer
right now. It supports sub-flags.:ducs:[stage]
where[stage]
is one oftransform
,desugar
,normalize
,postprocess
,coverage
will display the debug messages from the specified stage.:ducs:[stage].result
shows the pretty-printed outcome of the specified stage.:ducs:desugared
shows the desugared MLscript term.:ducs:A,B,C
shows debug messages for allA
,B
, andC
.:ShowPreTyperErrors
emits warnings and errors reported byPreTyper
.Others
Traceable
abstracts debugging methods used inTyper
, includingtrace
,println
, etc.freeVars
forLocated
. The original implementation is broken. But my implementation is not perfect either._ then ...
andelse
inIfOpsApp
.Num
,Int
,Bool
, andStr
.StrLit
.foldLeft
andforeach
toCaseBranchesImpl
.Future Works
This PR does not implement all features, but it lays the foundation for some of the following features.
CaseOf
doesn't support tuples right now. However, it paves the road for complicated tuple patterns even tuple splices.Besides, we can still improve location reporting in many places.