Skip to content

Reversible printing with sort printing and anonymous sort quality variables (combines #22155, #22283, #22310) - #22311

Draft
JasonGross wants to merge 4 commits into
rocq-prover:masterfrom
JasonGross:reversible-printing-full-stack
Draft

Reversible printing with sort printing and anonymous sort quality variables (combines #22155, #22283, #22310)#22311
JasonGross wants to merge 4 commits into
rocq-prover:masterfrom
JasonGross:reversible-printing-full-stack

Conversation

@JasonGross

@JasonGross JasonGross commented Jul 27, 2026

Copy link
Copy Markdown
Member

Written by Claude (Anthropic AI) at the request of and under the supervision of @JasonGross.

This PR stacks three printing features plus one integration commit so that reversible printing knows how to make sort qualities and sort quality variables explicit when they are what keeps a printed term from re-parsing.

It combines:

  • Add Set Printing Sorts option #22155Set Printing Sorts: like Set Printing Universes but shows only the sort structure. Plain sorts print as usual; hidden sort quality variables are shown with their universe levels anonymized as _ (e.g. Type@{s ; _}), and universe instances of polymorphic references are displayed when they carry sort qualities. (Note: distinct from the pre-existing Printing Sort Qualities flag, which they coexist with.)
  • Add Printing Reversible flags: check printed terms reparse to equal terms, escalating printing options when not #22283Printing Reversible … flags: after printing a term, the printed form is re-parsed and re-elaborated and compared back to the original; when it does not round-trip, printing options are escalated along a ladder until it does (or a reversible-printing warning is emitted).
  • Add Set Printing Sort Quality Variables Anonymously #22310Set Printing Sort Quality Variables Anonymously: sort quality variables that have no name print as _ (which re-parses to a fresh quality variable) instead of their raw α-names (which cannot be re-parsed at all). This PR tracks the amended Add Set Printing Sort Quality Variables Anonymously #22310, which also includes the Type@{_ ; u} sort-annotation grammar extension: _ is now accepted as an anonymous sort quality inside a sort (mirroring what universe instances already accepted). The ladder integration relies on this so that the sort in a printed type — not just an instance annotation — round-trips.
  • one new commit integrating sort printing and anonymous quality variables into the reversible-printing escalation ladder.

Escalation ladder

The reversible-printing ladder now tries, in order of increasing explicitness (each rung is standalone, not cumulative):

coercions → implicit arguments → sorts → sorts + anonymous quality variables → notations off → universes + anonymous quality variables → universes → universes without notations + anonymous quality variables → universes without notations → parentheses → raw (Printing All + universes + parentheses).

Rationale for trying anonymous quality variables before the non-anonymous form at each rung: an unnamed sort quality variable has no name-based syntax that re-parses — its raw α-name is unparsable — so printing it anonymously as _ is the only reversible form available. With the Type@{_ ; u} grammar extension above, this anonymous form re-parses both in an instance annotation (idT@{_ ; _}) and inside a sort in the type (Type@{_ ; _}), so a bare sort-polymorphic constant round-trips fully with no warning at the sorts + anonymous-quality-variables rung.

The immediately following rung retries with the flag off, because anonymizing a quality variable can itself be what breaks re-parsing (the _ re-parses to a fresh variable, which round-trips only when unification can match it); in that case the more explicit non-anonymous rung, or a later universes rung, is what succeeds. Raw printing stays faithful, with anonymous quality variables off.

Notes

🤖 Generated with Claude Code

https://claude.ai/code/session_01L9BGQT7XUuubV6C619DW4b

…erms, escalating printing options when not

Add five mutually exclusive flags — Printing Reversible Up To
Unification, Up To Conversion, Up To Conversion Modulo Universe
Unification, Up To Conversion Modulo Universes, and Up To Conversion
Modulo Sorts And Universes — under which the printer checks each
printed term by reparsing and re-elaborating it and comparing the
result against the original at the selected strictness. When the check
fails, printing options are escalated until the printed form becomes
reversible; if no combination suffices, a reversible-printing warning
is emitted.

Comparison modes:
- Up To Unification: the reparse must unify with the original.
- Up To Conversion: conversion, strict on universes.
- Up To Conversion Modulo Universe Unification: conversion threading
  universe unification through the evar map (fails when e.g. a fresh
  flexible level cannot be equated with an algebraic universe).
- Up To Conversion Modulo Universes: conversion ignoring universe
  levels and instance level components while requiring sort qualities
  to agree (SProp/Prop/Type distinct; quality variables compare
  structurally, so sort-polymorphic instances whose omission would
  change the elaborated quality are kept).
- Up To Conversion Modulo Sorts And Universes: conversion ignoring
  sorts and universes entirely (Reductionops.is_conv_nounivs), the
  laxest of the conversion-based checks.

Includes output tests, reference-manual documentation and a changelog
entry.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01L9BGQT7XUuubV6C619DW4b
@coqbot-app coqbot-app Bot added the needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. label Jul 27, 2026
@JasonGross JasonGross added kind: enhancement Enhancement to an existing user-facing feature, tactic, etc. needs: merge of dependency This PR depends on another PR being merged first. part: printer The printing mechanism of Coq. labels Jul 27, 2026
JasonGross and others added 3 commits July 27, 2026 04:25
Like Set Printing Universes but showing only the sort structure:
plain sorts (Type, SProp, Prop, Set) print as usual, sort variables
that would otherwise be hidden are displayed with their universe
levels replaced by _, e.g. Type@{s;_}, and universe instances of
polymorphic references are displayed when they carry sort qualities
(e.g. f@{q ; _}) and stay hidden otherwise.

Fixes rocq-prover#22153

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019ttctspSoVoquHLQtbPVZw
When the flag is on, sort quality variables that cannot be referred to
by name print as "_" (which parses back, denoting a fresh quality
variable) instead of their raw α-names (which cannot be parsed back).
Named quality variables are printed by name as usual.

Implemented as a new anonymous_qvars field of PrintingFlags.Extern
(forced off by raw printing), applied in the GQuality fallback of
Constrextern.extern_glob_quality and threaded through sorts, universe
instances and relevance marks; Constrextern.extern_sort and
Printer.pr_sort gain a corresponding argument.

Also extend the sort grammar to accept "_" as an anonymous quality in
sort annotations (Type@{_ ; u}): previously "_" was accepted as a
quality only in universe instances, so the anonymous form printed for
a sort could not be parsed back ("_" parsed as a universe level and
the ";" was rejected). Docgram regenerated accordingly.

Includes output tests, reference-manual documentation and a changelog
entry.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01L9BGQT7XUuubV6C619DW4b
…rsible-printing ladder

Extend the reversible-printing escalation ladder so that sort qualities
and sort quality variables can be made explicit when they are what keeps
a printed term from re-parsing.

Two rungs are inserted after implicit arguments: a sort rung (Detype.sorts)
that shows a sort-polymorphic instance at sort level, and a rung that
additionally prints sort quality variables anonymously (Extern.anonymous_qvars),
because an unnamed quality variable otherwise prints as a raw, unparsable
alpha-name whereas "_" re-parses to a fresh quality variable that
unification can match. The anon-qvars commit in this stack also adds the
"Type@{_ ; u}" sort-annotation grammar, so "_" now parses as a sort
quality inside a sort as well as in a universe instance; a bare
sort-polymorphic constant therefore round-trips fully (both its instance
and the sort in its type) with no warning under this rung.

At each universes rung the anonymous-quality-variable form is tried first
and then without it, since anonymizing a quality variable can itself be
what breaks re-parsing (the "_" re-parses to a fresh variable, which
round-trips only when unification can match it). Rungs remain ordered by
explicitness rather than being cumulative, and raw printing stays faithful
with anonymous quality variables off.

The ladder order is now: coercions, implicit arguments, sorts,
sorts + anonymous quality variables, notations off, universes + anonymous
quality variables, universes, universes without notations + anonymous
quality variables, universes without notations, parentheses, raw.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01L9BGQT7XUuubV6C619DW4b
@JasonGross
JasonGross force-pushed the reversible-printing-full-stack branch from 37170dd to 014a05e Compare July 27, 2026 04:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind: enhancement Enhancement to an existing user-facing feature, tactic, etc. needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. needs: merge of dependency This PR depends on another PR being merged first. part: printer The printing mechanism of Coq.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant