Skip to content

Formalize integer division and CIntegers in the metatheory - #7864

Open
ana-pantilie wants to merge 16 commits into
masterfrom
ana/metatheory-cinteger
Open

Formalize integer division and CIntegers in the metatheory#7864
ana-pantilie wants to merge 16 commits into
masterfrom
ana/metatheory-cinteger

Conversation

@ana-pantilie

@ana-pantilie ana-pantilie commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

@ana-pantilie ana-pantilie changed the title Formalize CIntegers in the metatheory Formalize integer division and CIntegers in the metatheory Jul 23, 2026
@ana-pantilie
ana-pantilie marked this pull request as ready for review July 28, 2026 11:10
@ana-pantilie
ana-pantilie requested review from a team and kwxm July 28, 2026 11:13
@IntersectMBO IntersectMBO deleted a comment Jul 30, 2026

@kwxm kwxm 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.

Looks good, although we should talk about the conformance tests since they're quite big. I made some comments, but they're mostly just nitpicking.

Comment thread plutus-metatheory/src/Algorithmic/CEK.lagda.md Outdated
Comment thread plutus-metatheory/src/Algorithmic/CEK.lagda.md Outdated
Comment thread plutus-metatheory/src/Untyped/CEK.lagda.md
## Quotient and remainder

The `quot` and `rem` functions are based on the Haskell `quot` and `rem` functions, which perform truncated division.
This follows the implementation of Haskell's `integerQuotRem#`.

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.

For this to do the right thing it's important here that Agda's / and % behave identically to Haskell's quot and rem for positive arguments. Can we safely assume that this is true, or should we try to test it somehow? Maybe not: the proved properties later are helpful, and the conformance tests give us some extra assurance, and we're planning to convert more extensive Haskell property tests like these into conformance tests, which should make things a lot better.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There was actually a bug in the implementation which was uncovered when porting the Haskell property tests to Agda proofs. I also added a property test suite which checks that the Agda semantics of the division operators is equivalent to the Haskell semantics.

Comment thread plutus-metatheory/src/Builtin/Integer/Base.lagda.md Outdated
Comment thread plutus-metatheory/src/Builtin/Integer/Properties.lagda.md Outdated
Comment thread plutus-metatheory/src/Builtin/Integer/Properties.lagda.md

This type constitutes the denotational semantics of the Cardano `BuiltinInteger` type for all of the inputs to the `BuiltinInteger` builtin functions, except `equalsInteger` and `expModInteger`.

The inputs to `equalsInteger` are of the unrestricted `ℤ` type. The `expModInteger` function is not yet formalised and is left as future work.

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.

expModInteger is an interesting case. We could implement it in Agda easily enough (exponentiate using square-and-multiply and then use %), but that might be pretty slow. On the other hand, the Haskell version uses integerPowMod#, which I think ultimately uses this C code in GMP, and it'd be out of the question to formalise that in Agda (although we could try asking Claude just to see how it reacts ...). I suppose we'll have to try something not too complicated and see if it performs reasonably.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I opened an issue about this, thanks. https://github.com/IntersectMBO/plutus-private/issues/2381

@ana-pantilie
ana-pantilie marked this pull request as draft August 4, 2026 17:34
@ana-pantilie
ana-pantilie requested a review from kwxm August 12, 2026 16:03
@ana-pantilie
ana-pantilie marked this pull request as ready for review August 12, 2026 16:03
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.

2 participants