Skip to content

Middlend Fixes#216

Open
DCupello1 wants to merge 25 commits into
mainfrom
middlend-fixes
Open

Middlend Fixes#216
DCupello1 wants to merge 25 commits into
mainfrom
middlend-fixes

Conversation

@DCupello1
Copy link
Copy Markdown

@DCupello1 DCupello1 commented Feb 10, 2026

This pull request is for some middlend fixes/changes that were done in the Rocq backend (#207) and are now being backported.

Fixes include:

  • Exposing wf relations, uncase removal projection functions, type families via hints in order to produce a better output in rocq.
  • In undep, made List IterE's with no iteration into a ListE in order for validation to work.
  • ImproveIds now actually tracks atoms in order to disambiguate them from variables.
  • New functionality in free (inter func)
  • New functionality in iter (now has visit arg func)
  • New functionality in walk (now has collect path func)
  • Small fix in sub which was missing a traversal in the expression of the coercion function.
  • After IL change, improveids did not have the correct transformation for mixops. This has now been fixed, along with changes to transforming ids with '#' which was added with the new fresh variables generation library.
  • Improveids: Added small fix for LetPrs to work.
  • Typefamilyremoval: Small fix to generated types to not clash with the base tf type.
  • Sidecondtitions: Only preserves the iteration of new inferred sideconditions whenever it has a ListN
  • Undep: Now has new ways to place wfness premises: put whenever we encounter a term or variable that needs it (ALL), only place whenever the term or variable is not in the conclusion or function body (MINIMAL), or no placement at all (NONE)
    • With the new change, this required providing a new proof obligation: for any function that constructs a term that has a correspoding wf relation, if its arguments are well formed, then the function call must also be well-formed
  • Typefamilyremoval: Small fix to inferral of the call expression, which was sometimes not returning the correct substituted result.
  • Sideconditions: removed an error message for relations that have fixed parameters. Was not necessary as it is should be valid.
  • Improveids: made it so the ids for hintdefs are also modified, which should have been the case in the beginning. This was only an issue if I wanted to leverage the hint system in the rendering process.

@DCupello1
Copy link
Copy Markdown
Author

Rebased this to account for new IL changes. Still leaves improve ids not entirely fixed, will be done in a separate pull request.

@DCupello1
Copy link
Copy Markdown
Author

@nomeata could you look into this PR please? Since the IL refactor is done, these PRs can finally go in.

This one in particular is just really small changes so it shouldn't take long I hope.

@nomeata
Copy link
Copy Markdown

nomeata commented Feb 24, 2026

Hmm, I've paged out spectec stuff a bit these days, so I'm not eager and probably not able to give a good review. Maybe someone else whose currently playing with this could?

@DCupello1
Copy link
Copy Markdown
Author

Ah no worries then!

@DCupello1
Copy link
Copy Markdown
Author

@rossberg can you take a look on this one?

Copy link
Copy Markdown
Collaborator

@rossberg rossberg left a comment

Choose a reason for hiding this comment

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

Reviewed with a somewhat superficial understanding. :)

Comment thread spectec/src/il/walk.ml Outdated
{ p with it = match p.it with
| ExpP (id, typ) -> ExpP (t.transform_var_id id, transform_typ t typ)
| TypP id -> TypP (t.transform_typ_id id)
| TypP id -> TypP (t.transform_var_id id)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hm, this is odd. The id is a type identifier.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hmm, aren't type parameters considered type variables? Maybe I should have a separate transform function for type variables, but I don't think theres a way to distinguish between type variables and type identifiers in arguments.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Well, no distinction is made between type names and type variables, it didn't seem necessary so far. The "sort" merely indicates the namespace, i.e., which component of the context binds it. I've exclusively used "var" for expression variables.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hmm, I tend to want to seperate them as they generally have different naming conventions. I'll keep it as t.transform_typ_id in order for it to make more sense.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Made the change, now improveids makes even more of a disctinction between user defined types and relations, and type constructors, so that i.e. the type variable X does not clash with the type constructor X (from shape) :)

Copy link
Copy Markdown
Author

@DCupello1 DCupello1 May 26, 2026

Choose a reason for hiding this comment

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

Sorry, meant to say a term constructor. Either way, it clashes when it gets to Rocq, especially when you use X when pattern matching, it gets confused with the type variable X and the term constructor X. For example, in the function relaxed2:

def $relaxed2(relaxed2, syntax X, X, X) : X       hint(show $relaxed(%1)#`[%3,%4])
def $relaxed2(i, syntax X, X_1, X_2) = (X_1 X_2)[i]  -- if $ND
def $relaxed2(i, syntax X, X_1, X_2) = (X_1 X_2)[0]  -- otherwise

would get converted as follows:

Definition fun_relaxed2 (v_relaxed2 : relaxed2) (v_X : eqType) (v_X_0 : v_X) (v_X_1 : v_X) {_ : Inhabited v_X} : v_X :=
	match v_relaxed2, v_X, v_X_0, v_X_1 return v_X with
		| i, X, X_1, X_2 => (if (ND ) then ([::X_1; X_2][| (i :> nat) |]) else ([::X_1; X_2][| 0 |]))
	end.

Copy link
Copy Markdown
Author

@DCupello1 DCupello1 May 26, 2026

Choose a reason for hiding this comment

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

Where shape is defined as:

Inductive shape : Type :=
	| X (v_lanetype : lanetype) (v_dim : dim) : shape.

Pretty sure rocq would complain that the X in the pattern must have two arguments or something like that.
(This might be simply a rocq-specific clash though)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah, okay, so Is the improveid meant to remove all potential name clashes between different name spaces? It would be worth documenting that (and the renaming strategy) in the MLI.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ah you are right! I should document that on the MLI. Yes, the idea is to remove potential name clashes between name spaces (an obvious one being functions ids and type ids being able to have the same name, etc.). I'll document it soon!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done!

Comment on lines +459 to +460
(* NOTE: for let since we only have expressions we cannot modify the env ever. *)
let _, new_quant_map = extend_quant_set env quant_map quants in
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe make that an assertion?

Comment thread spectec/src/middlend/uncaseremoval.mli Outdated
*)

val transform : Il.Ast.script -> Il.Ast.script
val uncase_proj_hint_id : string No newline at end of file
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: newline

Comment thread spectec/src/middlend/undep.ml Outdated
Comment on lines +34 to +36
| ALL (* Places wf premises whenever it encounters a term/variable that needs well-formedness check*)
| MINIMAL (* Places only wf premises in terms in relations and functions that do not appear in the conclusion *)
| NONE (* Does not place any wf premises in relations/functions *)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: OCaml constructor name conventions are capitalisation but not all-caps.

Comment thread spectec/src/middlend/undep.ml Outdated
(* flag that deactivates adding wellformedness predicates to relations *)
let deactivate_wfness = false
(* State that indicates what the placement algorithm should do *)
let wf_state = MINIMAL
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should this perhaps be a (exported) ref, so that it's actually configurable?

@DCupello1
Copy link
Copy Markdown
Author

DCupello1 commented May 26, 2026

Have done all of the changes, they all seem reasonable! Will update tests when all seems good.

Comment thread spectec/src/middlend/undep.mli Outdated
Comment on lines +44 to +46
| All (* Places wf premises whenever it encounters a term/variable that needs well-formedness check*)
| Minimal (* Places only wf premises in terms in relations and functions that do not appear in the conclusion *)
| Wfnone (* Does not place any wf premises in relations/functions *)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: Perhaps prefix the constructors consistently?

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