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

Combines #22155, #22283, and #22310 with an integration commit so reversible printing can expose sort qualities and sort quality variables when needed for round-tripping:

Escalation ladder

Each rung is standalone rather than 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).

Anonymous quality variables come first at applicable rungs because an unnamed variable has no reparsable name. With Type@{_ ; u}, _ works both in an instance annotation (idT@{_ ; _}) and in the type's sort (Type@{_ ; _}). The following non-anonymous rung remains necessary because _ creates a fresh variable and only round-trips when unification can match it. Raw printing keeps anonymous quality variables off.

This draft must not merge before #22155, #22283, and #22310; it will be rebased onto them after they merge.

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

🤖 Generated with Claude Code

Wordsmithed by Codex.

…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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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