-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for unsigned 8-bit integer type Byte (#2918)
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: * #2865
- Loading branch information
1 parent
d3f57a6
commit e2fe830
Showing
49 changed files
with
405 additions
and
7 deletions.
There are no files selected for viewing
Submodule juvix-stdlib
updated
5 files
+28 −0 | Stdlib/Data/Byte.juvix | |
+22 −0 | Stdlib/Data/Byte/Base.juvix | |
+1 −0 | Stdlib/Prelude.juvix | |
+1 −1 | test/Package.juvix | |
+4 −4 | test/juvix.lock.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Juvix.Compiler.Builtins.Byte where | ||
|
||
import Juvix.Compiler.Builtins.Effect | ||
import Juvix.Compiler.Internal.Extra | ||
import Juvix.Prelude | ||
|
||
registerByte :: (Member Builtins r) => AxiomDef -> Sem r () | ||
registerByte d = do | ||
unless (isSmallUniverse' (d ^. axiomType)) (error "Byte should be in the small universe") | ||
registerBuiltin BuiltinByte (d ^. axiomName) | ||
|
||
registerByteEq :: (Member Builtins r) => AxiomDef -> Sem r () | ||
registerByteEq f = do | ||
byte_ <- getBuiltinName (getLoc f) BuiltinByte | ||
bool_ <- getBuiltinName (getLoc f) BuiltinBool | ||
unless (f ^. axiomType === (byte_ --> byte_ --> bool_)) (error "Byte equality has the wrong type signature") | ||
registerBuiltin BuiltinByteEq (f ^. axiomName) | ||
|
||
registerByteFromNat :: (Member Builtins r) => AxiomDef -> Sem r () | ||
registerByteFromNat d = do | ||
let l = getLoc d | ||
byte_ <- getBuiltinName l BuiltinByte | ||
nat <- getBuiltinName l BuiltinNat | ||
unless (d ^. axiomType === (nat --> byte_)) (error "byte-from-nat has the wrong type signature") | ||
registerBuiltin BuiltinByteFromNat (d ^. axiomName) | ||
|
||
registerByteToNat :: (Member Builtins r) => AxiomDef -> Sem r () | ||
registerByteToNat f = do | ||
byte_ <- getBuiltinName (getLoc f) BuiltinByte | ||
nat_ <- getBuiltinName (getLoc f) BuiltinNat | ||
unless (f ^. axiomType === (byte_ --> nat_)) (error "byte-to-nat has the wrong type signature") | ||
registerBuiltin BuiltinByteToNat (f ^. axiomName) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.