Skip to content

[spectec] Dynamically check side conditions on type fields/cases#2158

Merged
rossberg merged 6 commits into
mainfrom
il.side
May 12, 2026
Merged

[spectec] Dynamically check side conditions on type fields/cases#2158
rossberg merged 6 commits into
mainfrom
il.side

Conversation

@rossberg
Copy link
Copy Markdown
Member

@rossberg rossberg commented May 6, 2026

SpecTec IL: Implement and formalise the semantics of dynamic checking of type side conditions, as discussed with @zilinc and @conrad-watt. Now, when e.g. a type

syntax digit = DIG nat  -- if nat < 10

is defined, every application of DIG will actually evaluate the premises associated with the constructor. If that fails, the application fails. IOW, constructors with side conditions are partial. The motivation is to actually check and preserve defined invariants. This is necessary to get the right failure behaviour for undefined constructor applications. To give a silly but simple example, a rule like

rule R:
  n ~~ m
  -- if DIG n = DIG m

will now properly be rejected for n, m ≥ 10, where previously it succeeded according to the operational semantics.

The implementation in the evaluator is simple. It does not affect the frontend much, since that only does simple evaluations for type-checking purposes. However, middlends/backends translating constructors (or records) should now respect this refined semantics if they don't already (@zilinc, @DCupello1, FYI).

The formalisation got a bit more complicated than I'd like, due to the need to express it via a reduction semantics. For this purpose, INJ and STR forms can now carry a temporary list of yet-to-check premises. Also, they now have a type annotation, since reduction has to know where to look for the premises initially.

@zilinc, @conrad-watt, @DCupello1, PTAL.

rule Step_exp/STR-ctx:
S |- STR ef* ~> STR ef*[[n] = ef'_n]
rule Step_exp/STR-ctx1:
S |- STR t ef* ~> STR t ef*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should the second t be t' in the conclusion?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, fixed!

rule Step_exp/STR:
S |- STR t (a `= e `- eps)* ~> STR t (a `= e `- $subst_quantprems($tupsubst(e, t'), `{q*} pr*))*
-- Expand_typ: S |- t ~~ STRUCT tf*
-- if (op `: t' `- `{q*} pr*) = tf*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't op be a?

(Other than this, everything looks fine to me!)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes it should. Fixed.

@rossberg
Copy link
Copy Markdown
Member Author

Thanks, it sounds like you're all okay with this refinement, so I gonna land.

@rossberg rossberg merged commit aed61c2 into main May 12, 2026
10 checks passed
@rossberg rossberg deleted the il.side branch May 12, 2026 15:53
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.

3 participants