Repository navigation
Conversation
strub
force-pushed
the
feat/nested-patterns
branch
from
December 1, 2026 14:32
468a503 to
eff376f
Compare
strub
commented
Dec 1, 2026
The patterns of `match` and of `if (e is p)` can now be nested, in
formulas, expressions and statements. A pattern is a wildcard, a
variable, a constructor applied to patterns, a tuple of patterns, a
record of patterns (`{| f = p; ... |}`, where the fields that are not
listed match anything), a list literal (`[p1; ...; pn]`) or `(p as x)`,
which also binds the matched value:
match db.[i] with
| Some ({| status = IWait m _ |} as s) => { ... }
| _ => { }
end;
match (a, b) with
| (Some x, Some y) => x + y
| (Some x, None) => x
| (None, _) => 0
end
Infix constructors follow the precedence and the associativity of the
corresponding operators, as in formulas: `x :: y :: _` is a pattern.
The core language is unchanged. The patterns are typed and compiled, in
EcTyping, to nested flat matches (one branch per constructor), so the
tactics see what was written by hand before. The variables bound at a
tuple or record position are replaced, in the branch, by the
corresponding projections. The matched term does not have to be of a
datatype anymore: it can be a tuple or a record.
The branches are tried in order. A branch that cannot be reached is an
error, as is a non-exhaustive matching, which now reports the missing
patterns:
this pattern matching is non-exhaustive, Some (Accepted _ _),
Some Rejected are missing
Changes of behaviour:
- A name in argument position is a constructor when it resolves to a
constructor of the expected type, and a binder otherwise. It was
always a binder: `Some None` matched any `Some _`, binding a variable
named `None`. A name at the root of a pattern is still a constructor.
- `true` and `false` are rejected in patterns. `bool` is not a
datatype, so they were binders too: `Some true` matched any `Some _`.
- The expected type is used to select a constructor or a record field
whose name is shared by several types.
- `if (e is p) { ... }`, without `else`, is accepted when `p` cannot
fail.
Limitations:
- In a statement, a variable must occur below a constructor: it is then
bound by a flat match. Otherwise, it would stand for a part of the
matched expression, whose value may change during the execution of
the branch. `match (a, b) with | (x, Some y) => ...` is rejected for
`x`.
- The operators defined by cases (`with x = p`) and the exceptional
postconditions only accept flat patterns.
- Goals and `print` show the compiled matches.
Tests in tests/nested-patterns.ec.
strub
force-pushed
the
feat/nested-patterns
branch
from
December 1, 2026 16:09
eff376f to
64111a9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The patterns of
matchand ofif (e is p)can now be nested, informulas, expressions and statements. A pattern is a wildcard, a
variable, a constructor applied to patterns, a tuple of patterns, a
record of patterns (
{| f = p; ... |}, where the fields that are notlisted match anything), a list literal (
[p1; ...; pn]) or(p as x),which also binds the matched value:
Infix constructors follow the precedence and the associativity of the
corresponding operators, as in formulas:
x :: y :: _is a pattern.The core language is unchanged. The patterns are typed and compiled, in
EcTyping, to nested flat matches (one branch per constructor), so the
tactics see what was written by hand before. The variables bound at a
tuple or record position are replaced, in the branch, by the
corresponding projections. The matched term does not have to be of a
datatype anymore: it can be a tuple or a record.
The branches are tried in order. A branch that cannot be reached is an
error, as is a non-exhaustive matching, which now reports the missing
patterns:
Changes of behaviour:
A name in argument position is a constructor when it resolves to a
constructor of the expected type, and a binder otherwise. It was
always a binder:
Some Nonematched anySome _, binding a variablenamed
None. A name at the root of a pattern is still a constructor.trueandfalseare rejected in patterns.boolis not adatatype, so they were binders too:
Some truematched anySome _.The expected type is used to select a constructor or a record field
whose name is shared by several types.
if (e is p) { ... }, withoutelse, is accepted whenpcannotfail.
Limitations:
In a statement, a variable must occur below a constructor: it is then
bound by a flat match. Otherwise, it would stand for a part of the
matched expression, whose value may change during the execution of
the branch.
match (a, b) with | (x, Some y) => ...is rejected forx.The operators defined by cases (
with x = p) and the exceptionalpostconditions only accept flat patterns.
Goals and
printshow the compiled matches.Tests in tests/nested-patterns.ec.