Skip to content

feat(typing): nested patterns in match - #1251

Open
strub wants to merge 1 commit into
mainfrom
feat/nested-patterns
Open

strub wants to merge 1 commit into
mainfrom
feat/nested-patterns

Conversation

@strub

@strub strub commented Dec 1, 2026

Copy link
Copy Markdown
Member

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
strub force-pushed the feat/nested-patterns branch from 468a503 to eff376f Compare December 1, 2026 14:32
Comment thread src/ecTyping.ml Resolved
Comment thread src/ecTyping.ml Resolved
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
strub force-pushed the feat/nested-patterns branch from eff376f to 64111a9 Compare December 1, 2026 16:09
@strub strub self-assigned this Dec 1, 2026
@strub strub added the yolo-pr Don't bother reviewing, I will merge label Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

yolo-pr Don't bother reviewing, I will merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant