Skip to content

Literal byte arrays - #1375

Merged
eponier merged 5 commits into
mainfrom
expr-in-assert
Feb 3, 2026
Merged

Literal byte arrays#1375
eponier merged 5 commits into
mainfrom
expr-in-assert

Conversation

@vbgl

@vbgl vbgl commented Jan 30, 2026

Copy link
Copy Markdown
Member

Description

This adds literal (byte) arrays to the language of expressions. In source code they can be written using string syntax (e.g., "data") or array syntax (e.g., { x, y + z }). Both syntax were already accepted for top-level definitions of global variables.

Fixes #510

This PR is marked as draft as it needs a bit of polishing (see checklist below).

Future work (not for this PR): generalize the type of cells (currently limited to u8).

Checklist

  • Add a changelog entry in changes if the PR is a user-visible change
  • Add one or several tests to compiler/tests if it makes sense, especially if it is a bug fix
  • Update the documentation if needed

@vbgl
vbgl force-pushed the expr-in-assert branch 3 times, most recently from 0bc4d11 to 03a02c4 Compare January 30, 2026 14:05
@vbgl

vbgl commented Jan 30, 2026

Copy link
Copy Markdown
Member Author

The first commit is unrelated: documentation of expressions still made use of the legacy syntax for memory accesses.

Then we have:

  1. the uincl relation for arrays can be decided
  2. addition of the new constructor for expressions (syntax & semantics) and fix the compiler accordingly
  3. extend the remove-global pass to handle global variables whose value is an array
  4. change the concrete syntax (a few things become simpler as an array is now a usual expression), add tests and documentation

@vbgl
vbgl marked this pull request as ready for review January 30, 2026 14:56
@vbgl

vbgl commented Jan 30, 2026

Copy link
Copy Markdown
Member Author

I’ve doubts about the rev in the semantics: which operator does cancel it? Either curry or WArray.fill.

@eponier

eponier commented Jan 30, 2026

Copy link
Copy Markdown
Contributor

Out of curiosity, is there a specific usecase you have in mind?

@vbgl

vbgl commented Jan 30, 2026

Copy link
Copy Markdown
Member Author
  1. PR Safety assert2 #1170 uses them.
  2. “scoped globals” (the one declared within an inline function) are currently restricted to word

@eponier

eponier commented Jan 30, 2026

Copy link
Copy Markdown
Contributor

What should happen in these cases? reg u8[3] r = { 1, 2, 3 }; and stack u8[3] s = { 1, 2, 3 }? For now, we get an error in array expansion for the first one, and an assert false for the second one.

@vbgl

vbgl commented Jan 30, 2026

Copy link
Copy Markdown
Member Author

For now, I think it is fine for the compiler to reject these programs. I’ve just changed the assertion failure in a better error message.

Comment thread proofs/compiler/constant_prop.v Outdated
| Ok r =>
match op return sem_t (eval_atype (type_of_opN op).2) -> _ with
| Opack ws _ => fun w => Papp1 (Oword_of_int ws) (Pconst (wunsigned w))
| Oarray len => fun _ => Pbool false

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.

To be sure I understand, this is dead code and would typically be an assert false if it were possible?

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, this is dead code. I’m not sure there is a more elegant way to write the same code (here, there are two nested pattern-matching twice on the same value).

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.

Actually, Rocq is able to elaborate a bit. So you could write

Definition s_opN (op:opN) (es:pexprs) : pexpr :=
  match op, app_sopn _ (sem_opN_typed op) es with
  | Opack ws _, Ok w => Papp1 (Oword_of_int ws) (Pconst (wunsigned w))
  | Ocombine_flags _, Ok b => Pbool b
  | _, _ => PappN op es
  end.

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.

(and this has no impact on the proof it seems)

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.

Thanks. I’ll apply your suggestion.

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.

Done.

eponier
eponier previously approved these changes Feb 2, 2026

@eponier eponier left a comment

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.

I made suggestions for the doc. Also I'm surprised that global_in_global_def.jazz now passes. Shouldn't it complain that we're going u8 -> u16 conversion at some point?

Apart from that, this seems good to go.

Comment thread docs/source/language/syntax/expressions.md
Comment thread docs/source/language/syntax/expressions.md Outdated
@eponier

eponier commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

I’ve doubts about the rev in the semantics: which operator does cancel it? Either curry or WArray.fill.

It looks like curry is indeed reverting its arguments.

Example curry2 A B (f : seq (sem_t A) -> B) a b :
  curry 2 f a b = f [:: b; a].
Proof. by reflexivity. Qed.

@vbgl

vbgl commented Feb 2, 2026

Copy link
Copy Markdown
Member Author

Right. curry is fishy. It is only used (so far) in the semantics of packs, so that’s fine.

vbgl and others added 2 commits February 2, 2026 17:11
Co-authored-by: Jean-Christophe Léchenet <jean-christophe.lechenet@inria.fr>
@vbgl
vbgl force-pushed the expr-in-assert branch 2 times, most recently from 9ed76e8 to a15a3e2 Compare February 2, 2026 16:27
@vbgl

vbgl commented Feb 2, 2026

Copy link
Copy Markdown
Member Author

I made suggestions for the doc. Also I'm surprised that global_in_global_def.jazz now passes. Shouldn't it complain that we're going u8 -> u16 conversion at some point?

You’re perfectly right. Thanks. I’ve added the missing check in pretyping and now the test case fails again, as expected.

eponier
eponier previously approved these changes Feb 3, 2026

@eponier eponier left a comment

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.

This is good, and I don't want to delay it further. I made one suggestion about the definition of s_opN. Just tell me if you want to integrate it. In any case, I'll merge the PR.

@eponier eponier left a comment

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.

Let's merge!

@eponier
eponier merged commit 92d8f69 into main Feb 3, 2026
1 check passed
@eponier
eponier deleted the expr-in-assert branch February 3, 2026 12:34
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.

Add constant string literals

2 participants