Standard Library¶
A collection of extensions form the “standard library” for HUGR, and are defined in this repository.
Prelude¶
The prelude extension is assumed to be valid and available in all contexts, and so must be supported by all third-party tooling.
Types¶
usize: a positive integer size type.
string: a string type.
array<N, T>: a known-size (N) array of type T.
qubit: a linear (non-copyable) qubit type.
error: an error type which operations use as a variant of sum to indicate when an error may occur. See Arithmetic Extensions for some examples.
Operations¶
Name |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
- |
Append the string to the program’s output stream[1] (atomically). |
|
|
|
Create an array from all the inputs. |
|
|
… |
Immediately end execution and pass contents of error to context. Inputs following the |
Logic Extension¶
The Logic Extension provides a boolean type and basic logical operations.
The boolean type bool is defined to be Sum(#(),#()), with the convention that the
first option in the sum represents “false” and the second represents “true”.
The following operations are defined:
Name |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
|
logical “not” |
|
|
|
N-ary logical “and” (N >= 0) |
|
|
|
N-ary logical “or” (N >= 0) |
Note that an and<0> operation produces the constant value “true” and an
or<0> operation produces the constant value “false”.
Arithmetic Extensions¶
Types and operations for integer and
floating-point operations are provided by a collection of extensions under the
namespace arithmetic.
We largely adopt (a subset of) the definitions of WebAssembly 2.0, including the names of the operations. Where WebAssembly specifies a “partial” operation (i.e. when the result is not defined on certain inputs), we use a Sum type to hold the result.
A few additional operations not included in WebAssembly are also specified, and there are some other small differences (highlighted below).
arithmetic.int.types¶
The int<N> type is parametrized by its width N, which is a positive
integer.
The possible values of N are 2^i for i in the range [0,6].
The int<N> type represents an arbitrary bit string of length N.
Semantics are defined by the operations. There are three possible
interpretations of a value:
as a bit string \((a_{N-1}, a_{N-2}, \ldots, a_0)\) where \(a_i \in \\{0,1\\}\);
as an unsigned integer \(\sum_{i \lt N} 2^i a_i\);
as a signed integer \(\sum_{i \lt N-1} 2^i a_i - 2^{N-1} a_{N-1}\).
An asterix ( * ) in the tables below indicates that the definition either differs from or is not part of the WebAssembly specification.
arithmetic.int¶
This extension defines operations on the integer types.
Casts:
Name |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
|
widen an unsigned integer to a wider one with the same value (where M <= N) |
|
|
|
widen a signed integer to a wider one with the same value (where M <= N) |
|
|
|
narrow an unsigned integer to a narrower one with the same value if possible, and an error otherwise (where M >= N) |
|
|
|
narrow a signed integer to a narrower one with the same value if possible, and an error otherwise (where M >= N) |
|
|
|
convert to |
|
|
|
convert from |
Comparisons:
Name |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
|
equality test |
|
|
|
inequality test |
|
|
|
“less than” as unsigned integers |
|
|
|
“less than” as signed integers |
|
|
|
“greater than” as unsigned integers |
|
|
|
“greater than” as signed integers |
|
|
|
“less than or equal” as unsigned integers |
|
|
|
“less than or equal” as signed integers |
|
|
|
“greater than or equal” as unsigned integers |
|
|
|
“greater than or equal” as signed integers |
Other operations:
Name |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
|
maximum of unsigned integers |
|
|
|
maximum of signed integers |
|
|
|
minimum of unsigned integers |
|
|
|
minimum of signed integers |
|
|
|
addition modulo 2^N (signed and unsigned versions are the same op) |
|
|
|
subtraction modulo 2^N (signed and unsigned versions are the same op) |
|
|
|
negation modulo 2^N (signed and unsigned versions are the same op) |
|
|
|
multiplication modulo 2^N (signed and unsigned versions are the same op) |
|
|
|
given unsigned integers 0 <= n < 2^N, 0 <= m < 2^N, generates unsigned q, r where q*m+r=n, 0<=r<m (m=0 is an error) |
|
|
|
given unsigned integers 0 <= n < 2^N, 0 <= m < 2^N, generates unsigned q, r where q*m+r=n, 0<=r<m (m=0 will call panic) |
|
|
|
given signed integer -2^{N-1} <= n < 2^{N-1} and unsigned 0 <= m < 2^N, generates signed q and unsigned r where q*m+r=n, 0<=r<m (m=0 is an error) |
|
|
|
given signed integer -2^{N-1} <= n < 2^{N-1} and unsigned 0 <= m < 2^N, generates signed q and unsigned r where q*m+r=n, 0<=r<m (m=0 will call panic) |
|
|
|
as |
|
|
|
as |
|
|
|
as |
|
|
|
as |
|
|
|
as |
|
|
|
as |
|
|
|
as |
|
|
|
as |
|
|
|
convert signed to unsigned by taking absolute value |
|
|
|
bitwise AND |
|
|
|
bitwise OR |
|
|
|
bitwise XOR |
|
|
|
bitwise NOT |
|
|
|
shift first input left by k bits where k is unsigned interpretation of second input (leftmost bits dropped, rightmost bits set to zero) |
|
|
|
shift first input right by k bits where k is unsigned interpretation of second input (rightmost bits dropped, leftmost bits set to zero) |
|
|
|
rotate first input left by k bits where k is unsigned interpretation of second input (leftmost bits replace rightmost bits) |
|
|
|
rotate first input right by k bits where k is unsigned interpretation of second input (rightmost bits replace leftmost bits) |
|
|
|
decimal string representation of unsigned integer |
|
|
|
decimal string representation of signed integer |
arithmetic.float.types¶
The float64 type represents IEEE 754-2019 floating-point data of 64
bits.
Non-finite float64 values (i.e. NaN and ±infinity) are not allowed in Const
nodes.
arithmetic.float¶
Floating-point operations are defined as follows. All operations below follow WebAssembly except where stated.
Name |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
|
equality test (as WASM but with 0 and 1 interpreted as |
|
|
|
inequality test (as WASM but with 0 and 1 interpreted as |
|
|
|
“less than” (as WASM but with 0 and 1 interpreted as |
|
|
|
“greater than” (as WASM but with 0 and 1 interpreted as |
|
|
|
“less than or equal” (as WASM but with 0 and 1 interpreted as |
|
|
|
“greater than or equal” (as WASM but with 0 and 1 interpreted as |
|
|
|
maximum |
|
|
|
minimum |
|
|
|
addition |
|
|
|
subtraction |
|
|
|
negation |
|
|
|
absolute value |
|
|
|
multiplication |
|
|
|
division |
|
|
|
floor |
|
|
|
ceiling |
|
|
|
string representation[2] |
arithmetic.conversions¶
Conversions between integers and floats:
Name |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
|
float to unsigned int, rounding towards zero. Returns an error when the float is non-finite. |
|
|
|
float to signed int, rounding towards zero. Returns an error when the float is non-finite. |
|
|
|
unsigned int to float |
|
|
|
signed int to float |
|
|
|
reinterpret an int64 as a float64 based on its bytes, with the same endianness. |
|
|
|
reinterpret an float64 as an int based on its bytes, with the same endianness. |
Collections Extensions¶
There are multiple extensions defining types, values and operations to work with collections of data:
collections.array: The standard linear and fixed-length array type, parametrized by length and element type.collections.borrow_arr: A linear and fixed-length array type that provides additional unsafe operations for borrowing elements from the array, parametrized by length and element type.collections.static_array: An array type for modeling globally available constant arrays of copyable values, parametrized only by element type.collections.list: A variable-length list type, parametrized by element type.
collections.array¶
This extension provides the array type and value with the following operations:
Operation |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
|
Make a new array, given distinct inputs equal to its length. |
|
|
|
Copy an element out of the array (copyable elements only). Return none if the index is out of bounds. |
|
|
|
Exchange an element of the array with an external value. Tagged for failure/success if index is out of bounds respectively. |
|
|
|
Exchange the elements at two indices within the array. Tagged for failure/success if index is out of bounds respectively. |
|
|
|
Pop an element from the left of the array. |
|
|
|
Pop an element from the right of the array. |
|
|
|
Discard an empty array. |
|
|
|
Discard an array with copyable elements. |
|
|
|
Clone an array with copyable elements. |
|
|
|
Unpack an array into its individual elements. |
|
|
|
Create a new array whose elements are initialised by calling the given function |
|
|
|
A combination of map and foldl. Apply a function to each element of the array with an accumulator that is passed through from start to finish. Return the resulting array and the final state of the accumulator. |
collections.borrow_arr¶
This extension contains the borrow_array type and value. It has all the operations that the array extension does (see previous section), with additional unsafe operations to deal with borrowing elements. Borrowing means taking elements out of an array while relying on the underlying implementation to keep track of which elements have already been taken out.
Operation |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
|
Borrow an element from the array at the given index. The element already being borrowed should result in a panic. |
|
|
|
Return an element to the array at the given index. There already being an element at this index should result in a panic. |
|
|
|
Discard an array where all elements have been borrowed. Should panic if there are still elements in the array. |
|
|
|
Create a new borrow array where all elements are borrowed. |
|
|
|
Check if the element at the given index is borrowed. |
There are also conversion operations to convert borrow arrays to and from standard arrays:
Operation |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
|
Turn an array into a borrow array. |
|
|
|
Turn a borrow array into an array. |
collections.static_array¶
This extension contains the static_array type and value for modelling constant statically sized arrays. The only way of obtaining a value of type static_array is by creating a StaticArrayValue. There are only two operations provided:
Operation |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
|
Get the element at the given index. Return none if the index is out of bounds. |
|
|
|
Gets the length of the array. |
collections.list¶
This extension contains the list type and value. Lists are dynamically sized, with all elements having the same type.
Operation |
Inputs |
Outputs |
Meaning |
|---|---|---|---|
|
|
|
Pop from the end of a list. Return the new list and the popped value (or none if the list was empty). |
|
|
|
Push to the end of a list. Return the new list. |
|
|
|
Look up an element in a list by index. |
|
|
|
Replace the element at the given index, and return the old value. If the index is out of bounds, return the input value as an error. |
|
|
|
Insert an element at the given index. Elements at higher indices are shifted one position to the right. Return an error with the element if the index is out of bounds. |
|
|
|
Get the length of a list. |