Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FromNatural trait in package-base #2926

Merged
merged 6 commits into from
Aug 1, 2024
Merged

Add FromNatural trait in package-base #2926

merged 6 commits into from
Aug 1, 2024

Conversation

paulcadman
Copy link
Collaborator

@paulcadman paulcadman commented Jul 29, 2024

This PR adds FromNatural to package-base. The change is backwards compatible for existing Juvix code so we don't need to make a new version of package-base. The new trait is unused, it will be integrated in subsequent PRs.

FromNatural trait

The FromNatural trait has the following definition.

trait
type FromNatural A :=
  mkFromNatural {
    builtin from-nat
    fromNat : Nat -> A
};

Natural trait changes

The Natural trait is changed to remove its fromNat field and add a new instance field for FromNatural A.

juvix-stdlib changes

FromNatural instances are added for Int and Field in the standard library.

Rationale

The FromNatural trait will be used for the Bytes type.

We want the following properties for Byte:

  1. Values of the Bytes type should be assignable from a non-negative numeric literal.
  2. We don't want to implement + and * for Bytes.

Currently, in order for a type to have property 1. it must have an instance of Natural so property 2. can't be satisfied.

To solve this we split the from-nat builtin from the Natural trait into a new trait FromNatural.

@paulcadman paulcadman added this to the 0.6.5 milestone Jul 29, 2024
@paulcadman paulcadman self-assigned this Jul 29, 2024
@paulcadman paulcadman marked this pull request as ready for review July 29, 2024 19:00
@paulcadman paulcadman marked this pull request as draft July 30, 2024 06:51
@paulcadman paulcadman marked this pull request as ready for review July 30, 2024 07:36
@paulcadman
Copy link
Collaborator Author

Agreed with @janmasrovira to try to use instance fields instead of an instance coercion from Natural to FromNat.

@paulcadman paulcadman marked this pull request as draft July 30, 2024 16:18
@paulcadman paulcadman marked this pull request as ready for review July 31, 2024 07:51
@paulcadman paulcadman marked this pull request as draft July 31, 2024 09:52
@paulcadman paulcadman marked this pull request as ready for review July 31, 2024 13:17
The FromNatural trait will be used for the Bytes type.

We want the following properties for Byte:

1. Values of the Bytes type to be assignable from a non-negative numeric
literal.
2. We don't want to implement + and * for it.

Currently, in order for a type to have property 1. it must have an
instance of `Natural` so property 2. can't be satisfied.

To solve this we split the `from-nat` builtin from the `Natural` trait
into a new trait `FromNatural`.

A coercion instance is defined between Natural and FromNatural so you
don't need to write instances for both traits.

For this PR the new `FromNatural` trait is unused. I will make the
changes to juvix-stdlib to use the new V2 modules when I incorporate
compiler changes for the Bytes type.

This change is backwards compatible for existing Juvix code so we don't
need to make a new Juvix.Builtin.V2 path in package-base.
@paulcadman paulcadman merged commit d859a03 into main Aug 1, 2024
4 checks passed
@paulcadman paulcadman deleted the FromNatural-trait branch August 1, 2024 07:26
janmasrovira pushed a commit to anoma/juvix-stdlib that referenced this pull request Aug 5, 2024
Adds `FromNatural` instances for `Int` and `Field`.

Used by:

* anoma/juvix#2926

A temporary version of juvix-quickcheck is used that works with the
Natural/FromNatural changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants