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 support for unsigned 8-bit integer type Byte #2918

Merged
merged 21 commits into from
Aug 2, 2024
Merged

Conversation

paulcadman
Copy link
Collaborator

@paulcadman paulcadman commented Jul 26, 2024

This PR adds Byte as a builtin with builtin functions for equality, byte-from-nat and byte-to-nat. The standard library is updated to include this definition with instances for FromNatural, Show and Eq traits.

The FromNatural trait means that you can assign Byte values using non-negative numeric literals.

You can use byte literals in jvc files by adding the u8 suffix to a numeric value. For example, 1u8 represents a byte literal.

Arithmetic is not supported as the intention is for this type to be used to construct ByteArrays of data where isn't not appropriate to modify using arithmetic operations. We may add a separate UInt8 type in the future which supports arithmetic.

The Byte is supported in the native, rust and Anoma backend. Byte is not supported in the Cairo backend because byte-from-nat cannot be defined.

The primitive builtin ops for Byte are called OpUInt8ToInt and OpUInt8FromInt, named because these ops work on integers and in future we may reuse these for a separate unsigned 8-bit integer type that supports arithmetic.

Part of:

@paulcadman paulcadman added core Related to JuvixCore anoma labels Jul 26, 2024
@paulcadman paulcadman added this to the 0.6.5 milestone Jul 26, 2024
@paulcadman paulcadman self-assigned this Jul 26, 2024
@paulcadman paulcadman force-pushed the support-uint8 branch 5 times, most recently from 43f4fd5 to 5bba06b Compare July 31, 2024 15:32
@paulcadman paulcadman changed the title Add support for unsigned 8-bit integer type UInt8 Add support for unsigned 8-bit integer type Byte Jul 31, 2024
@paulcadman paulcadman marked this pull request as ready for review August 1, 2024 16:53
@paulcadman paulcadman requested review from lukaszcz and janmasrovira and removed request for lukaszcz August 1, 2024 16:53
let !v = eval' env node
in nodeFromInteger
. toInteger
. fromMaybe (evalError "expected field element" v)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Expected an uint8?

LitInteger Integer
| -- | `LitNatural` represents a literal of type `Nat`
| -- | `LitNatural` represents a literal with trait `Natural`
LitNatural Integer
Copy link
Collaborator

Choose a reason for hiding this comment

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

So what happens with numeric literals that are bytes? What are they converted to during type checking?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The way that literals work in type checking is here:

https://github.com/anoma/juvix/blob/main/src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/TypeChecking/CheckerNew.hs#L1111-L1116

For a non-negative literal n the typechecker runs type inference on the literal wrapped in fromNat from the FromNatural trait:

fromNat {_} {{_}} `n`

The Byte type is then obtained via instance resolution.

This is why I updated the documentation for LitInteger and LitNatural to refer to traits rather than to types.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, OK, they have an instance of Natural?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The trait name in the comment should be FromNatural then.


n1 : Byte := fromNat 1;

n2 : Byte := fromNat 0xff;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need fromNat here? Isn't it supposed to be inserted automatically?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not needed - just below these are examples without fromNat. I wanted to cover using fromNat explicitly in the test.

@paulcadman paulcadman merged commit e2fe830 into main Aug 2, 2024
4 checks passed
@paulcadman paulcadman deleted the support-uint8 branch August 2, 2024 06:43
paulcadman added a commit to anoma/juvix-stdlib that referenced this pull request Aug 5, 2024
> ⚠️ Depends on:
> * #117
> * anoma/juvix#2918

Adds support for the builtin Byte type introduced in:
* anoma/juvix#2918
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