Skip to content

Add a compiler pass for automatic generation of Generic instances for algebraic datatypes#466

Merged
rodrigogribeiro merged 5 commits into
mainfrom
generic-instance-automatic
Jun 19, 2026
Merged

Add a compiler pass for automatic generation of Generic instances for algebraic datatypes#466
rodrigogribeiro merged 5 commits into
mainfrom
generic-instance-automatic

Conversation

@rodrigogribeiro

Copy link
Copy Markdown
Collaborator
  • Add a compiler pass for generating Generic instance for datatypes.
  • Add a pragma to disable Generic instance generation for datatypes.

@rodrigogribeiro rodrigogribeiro marked this pull request as draft June 15, 2026 00:25
@rodrigogribeiro rodrigogribeiro force-pushed the generic-instance-automatic branch from 8a45423 to 527cdd2 Compare June 18, 2026 10:43
@rodrigogribeiro rodrigogribeiro marked this pull request as ready for review June 18, 2026 12:21
@rodrigogribeiro rodrigogribeiro requested a review from mbenke June 18, 2026 12:21
Comment on lines +12 to 25
// Only requirement: Generic instance using the primitive sum type.
// rep = sum((), uint256): inl(()) = None, inr(v) = Some(v)
instance Option(uint256) : Generic(sum((), uint256)) {
function from(x : Option(uint256)) -> sum((), uint256) {
match x {
| Option.None => return Sum.Inl(());
| Option.Some(v) => return Sum.Inr(v);
| Option.None => return inl(());
| Option.Some(v) => return inr(v);
}
}
function to(r : Sum((), uint256)) -> Option(uint256) {
function to(r : sum((), uint256)) -> Option(uint256) {
match r {
| Sum.Inl(_) => return Option.None;
| Sum.Inr(v) => return Option.Some(v);
| inl(_) => return Option.None;
| inr(v) => return Option.Some(v);
}

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.

This instance should be derived, right? And similarly in generic_product?
Or perhaps there should be two sets of tests: one for explicit instance, and one for derived?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed and added tests for the pragma that disable Generic instance generation.

pragma no-coverage-condition ;
pragma no-bounded-variable-condition ;

infixl 70 (^^) => pow;

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.

I don't think the operators have been merged yet:

test/examples/cases/user-op-lambda.solc
<input>:6:1:
  |
6 | infixl 70 (^^) => pow;
  | ^
unexpected 'i'
expecting "class", "constructor", "contract", "data", "default", "export", "fallback", "forall", "function", "import", "instance", "payable", "pragma", "public", "type", or end of input

@mbenke mbenke left a comment

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.

Nice work!

@rodrigogribeiro rodrigogribeiro merged commit 5758e18 into main Jun 19, 2026
4 checks passed
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