-
Notifications
You must be signed in to change notification settings - Fork 721
if syntax
Following https://github.com/rocq-prover/rocq/wiki/Rocq-Call-2026-03-17 it was agreed that the current if <term : inductive_with_two_constructos> then term else term syntactic sugar for match should be deprecated (except for (things coercible to) bool). Potential replacements are gathered here. Please list offers below, with perceived pros and cons, and historical use if any. Please try to keep the "positive" and "negative" offers in the same order when there is a correspondance
-
No replacement, just deprecate
if <term : inductive_with_two_constructors>in favor of existingmatch -
if <term> is <pattern> then <term> else <term>- already used in MathComp ecosystem
- one of the syntax considered for the C++ standard (c.f., https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2392r3.pdf , with context sensitive keyword
is) - pros:
- short
- reads like a sentence
-
<term>and<pattern>in same order asmatch <term> with <pattern> - extensible to multiple matches by repeating " is " with some separator
- cons:
- requires making
isa keyword (but CI https://github.com/rocq-prover/rocq/pull/21609 seems to indicate no dramatic effect, except for UniMath that is usingisextensively as a variable name) - even when
isis a keyword, it may not be clear to readers that it is a binding keyword - even when
isis known to be a binding keyword, it is not clear from syntax which side has the binders ("is" reads like a symmetric relation) - directly confusable for a pointer-equality test a la Python (but pointer equality is not exposed in Rocq)
- directly confusable for equality comparison
- pattern (with bindings) deferred until after (which may be large)
- see also Rust discussion ultimately rejecting this option
- requires making
-
if <term> .is. <pattern> then <term> else <term>(or other variants likeif <term> \is <pattern>...)- used in Ada
- pros:
-
<term>and<pattern>in same order asmatch <term> with <pattern> - extensible to multiple matches by repeating " .is. " with some separator
-
- cons:
- longer
- breaks reading
- requires making
.is.a keyword (but maybe absolutely nobody uses it yet?) - pattern (with bindings) deferred until after (which may be large)
-
if let <pattern> := <term> then <term> else <term>- used in Rust and Lean
- pros:
- no new keyword
- bindings before terms and cases
- Was chosen for Rust after a 373-answer survey found it twice as liked and three times less disliked than
is - extensible to multiple matches by repeating "let := " with some separator ("&&" in Rust, chosen over ",")
- cons:
- let currently indicates an irrefutable pattern, not a switch
- longer (starts with two keywords)
-
<term>andpattern>in reversed order with respect to the unsugared match construct - common prefix with eg
if let x := tt in true then ... else ...(which is currently accepted)
-
if <pattern> := <term> then <term> else <term>- pros:
- short
- no new keyword
- starts with just one keyword
- bindings before terms and cases
- cons:
- "" being a pattern is not obvious, especially extending to multiple matches with a simple separator
-
<term>andpattern>in reversed order with respect to the unsugared match construct - the grammar may be unexpected to those not familiar with it
- grammar conflicts with boolean
if <term>
- pros:
-
match <term> with <pattern> then <term> else <term>- pros:
- no new keyword
- straightforwardly extensible to multiple matches following current conventions (comma-separated terms followed by comma-separated patterns)
- cons:
- longer keywords
- bindings deferred until after potentially large term
- pros:
-
if <term> match <pattern> then <term> else <term>- one of the syntax considered for the C++ standard (the other one uses context-sensitive keywords
isandas) - pros:
- no new keyword
- potentially extensible to multiple matches by repeating " match "
- cons
- Rocq already allows
matchin argument position (contrarily to OCaml where it is a syntax error), so this is slightly ambiguous - longer
- pattern with bindings deferred until after term (which may be large)
- may feel a bit strange to reuse the
matchkeyword with the role of thewithkeyword in match-with construct
- Rocq already allows
- one of the syntax considered for the C++ standard (the other one uses context-sensitive keywords
-
if <term> isn't <pattern> then <term> else <term>- already used in MathComp ecosystem
- pros:
- short
- reads like a sentence
-
<term>and<pattern>in same order asmatch <term> with <pattern>
- cons:
- bindings in second branch may not be obvious
- bindings deferred until after term (which may be large)
- requires making
isn'ta keyword (though CI seems to indicate nobody uses it) -
isn'twould be the only keyword with'in it and may not be recognized as one (especially by people not used to'in identifiers)
-
unless <term> is <pattern> then <term> else <term>- pros:
- "the pattern matching clause is still framed positively, so it restores, in my mind, the idea that some new variables are bound."
- reads like a sentence
-
<term>and<pattern>in same order asmatch <term> with <pattern>
- cons:
- requires making
unlessa keyword - not clear from reading which branch has bindings
- bindings deferred until after term (which may be large)
- requires making
- pros:
-
let <pattern> := <term> else <term> in <term>- similar to Rust
- pros:
- indentation would be nice when used in sequence or with a large second branch
- bindings come before potentially large term
- when
inis the end of a line, the "error-handling" part betweenelseandinis easy to skip
- cons:
- let currently indicates an irrefutable pattern, not a switch
-
if <term> is <pattern> else <term> then <term>- pros:
- short
- symmetric to
if <term> is <pattern> then <term> else <term> - avoids potentially surprising
'-containing keyword compared toisn't
- cons:
- bindings in second branch may not be obvious
- same other cons as
if <term> is <pattern> then <term> else <term>
- pros:
-
let <pattern> := term | <term_else> in <term>- Similar to Lean (
;vsin) - Used in Bedrock2 ecosystem, though not pervasive
- pros:
- short
- bindings are obvious and first
- no new keyword
- when
inis the end of a line, the "error-handling" part between|andinis easy to skip
- cons:
-
lethas been for single-case matches only so far, this proposal would change it to do multi-case matches
-
- Similar to Lean (
To the extent possible under law, the contributors of the Rocq wiki have waived all copyright and related or neighboring rights to their contributions.
By contributing to the Rocq wiki, you agree that you hold the copyright and you agree to license your contribution under the CC0 license or you agree that you have permission to distribute your contribution under the CC0 license.