0

I've found primitives for 64-bit floating point and 32-bit signed integer operations. I've not found primitives for 64-bit unsigned integer operations. Without those, what is the correct way to deal with 64-bit uint arithmetic on Agda?

MaiaVictor
  • 51,090
  • 44
  • 144
  • 286

1 Answers1

1

It seems to be present on 2.5.4. (Exactly one version above the one I was using.)

Added support for built-in 64-bit machine words.

These are defined in Agda.Builtin.Word and come with two primitive operations to convert to and from natural numbers.

Word64 : Set
primWord64ToNat   : Word64 → Nat
primWord64FromNat : Nat → Word64

Converting to a natural number is the trivial embedding, and converting from a natural number gives you the remainder modulo 2^64. The proofs of these theorems are not primitive, but can be defined in a library using primTrustMe.

Basic arithmetic operations can be defined on Word64 by converting to natural numbers, peforming the corresponding operation, and then converting back. The compiler will optimise these to use 64-bit arithmetic. For instance,

addWord : Word64 → Word64 → Word64
addWord a b = primWord64FromNat (primWord64ToNat a + primWord64ToNat b)

subWord : Word64 → Word64 → Word64
subWord a b = primWord64FromNat (primWord64ToNat a + 18446744073709551616 - primWord64ToNat b)

These compiles (in the GHC backend) to addition and subtraction on Data.Word.Word64

Community
  • 1
  • 1
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286