Generic Numeric Code¶
Problem¶
You want a block cipher component that works with different unsigned integer widths. Each block stores one value. You need to check whether it has an odd number of set bits and to XOR it with another block of the same width. You start with Unsigned as the constraint.
class Block[T: Unsigned]
var data: T
new create(data': T) =>
data = data'
fun parity(): Bool =>
(data.popcount() % 2) == 1
fun ref apply_xor(o: Block[T]) =>
data = o.data xor data
The compiler rejects both popcount and xor. Unsigned is the union of the built-in unsigned integer types, so T could itself be a union such as (U8 | U16). On a U8, popcount() returns a U8. On a U16, it returns a U16. The compiler has no single compatible signature for that call on the union. For xor, the operand type matters too. Two blocks with that union as their type argument could hold a U8 and a U16, and a U8 takes another U8 as its XOR operand.
Solution¶
Use the same type parameter for the stored value and for the operations on that value.
class Block[T: UnsignedInteger[T]]
UnsignedInteger[T] is a trait. With T as its argument, popcount() returns a T, and XOR takes a T and returns a T. The trait is declared val, so you can omit the capability in this constraint.
Parity also needs the constants 2 and 1. With only the trait as a constraint, the compiler cannot give a bare integer literal the type T. In T.from[U8](2), the argument type gives the literal the concrete type U8. The conversion returns the T that the remainder operation accepts.
class Block[T: UnsignedInteger[T]]
var data: T
new create(data': T) =>
data = data'
fun parity(): Bool =>
(data.popcount() % T.from[U8](2)) == T.from[U8](1)
fun ref apply_xor(o: Block[T]) =>
data = o.data xor data
actor Main
new create(env: Env) =>
let a = Block[U32](0b1011)
let b = Block[U32](0b1100)
env.out.print("a parity (odd number of 1s): " + a.parity().string())
env.out.print("b parity (even number of 1s): " + b.parity().string())
a.apply_xor(b)
env.out.print(a.data.string())
a.parity() returns true because 0b1011 has three set bits. b.parity() returns false because 0b1100 has two. After a.apply_xor(b), the program prints a.data as 7 because 0b1011 xor 0b1100 is 0b0111.
Discussion¶
If you want to accept only Pony’s built-in unsigned integer types, add Unsigned to the trait constraint. That restriction also gives the compiler enough information to type the bare literals in parity. The rest of the class stays the same.
class Block[T: (Unsigned & UnsignedInteger[T])]
// ...
fun parity(): Bool =>
(data.popcount() % 2) == 1
For arithmetic shared by signed and unsigned integers, use Integer[T]. Add Signed to restrict the type argument to built-in signed integers, or Int to accept built-in integers of either sign. Check the return type of operations specific to signed or unsigned integers. For example, a signed integer’s popcount() returns its unsigned counterpart, so the unsigned block’s parity calculation does not carry over directly.
Two standard library methods illustrate when the union matters. String.read_int initializes its accumulator with var value: A = 0 and passes A to _read_int_base, which also requires a built-in integer. Format.int declares [A: (Int & Integer[A])], but it creates zero with x.from[USize](0). With [A: Integer[A] val] as the constraint, the same body compiles and formats I32(-42) and U64(42).
For an example of using a type parameter to pass integer values without boxing them, see Avoid Boxing with Parameterization.