two's complement integers

64 views
Skip to first unread message

Michael Shulman

unread,
Mar 4, 2021, 3:43:43 PM3/4/21
to homotopyt...@googlegroups.com
Has anyone considered the following HIT definition of the integers?

data ℤ : Type where
zero : ℤ -- 0
negOne : ℤ -- -1
dbl : ℤ → ℤ -- n ↦ 2n
sucDbl : ℤ → ℤ -- n ↦ 2n+1
dblZero : dbl zero ≡ zero -- 2·0 = 0
sucDblNegOne : sucDbl negOne ≡ negOne -- 2·(-1)+1 = -1

The idea is that it's an arbitrary-precision version of little-endian
two's-complement, with sucDbl and dbl representing 1 and 0
respectively, and negOne and zero representing the highest-order sign
bit 1 and 0 respectively. Thus for instance

sucDbl (dbl (sucDbl (sucDbl zero))) = 01101 = 13
dbl (sucDbl (dbl (dbl negOne))) = 10010 = -14

The two path-constructors enable arbitrary expansion of the precision, e.g.

01101 = 001101 = 0001101 = ...
10010 = 110010 = 1110010 = ...

This seems like a fairly nice definition: it should already be a set
without any truncation, it's binary rather than unary, and the
arithmetic operations can be defined in the usual digit-by-digit way
without splitting into cases by sign. Mathematically speaking, it
represents integers by their images in the 2-adic integers, with zero
and negOne representing infinite tails of 0s and 1s respectively. (An
arbitrary 2-adic integer, of course, is just a stream of bits.)

Best,
Mike

Martin Escardo

unread,
Mar 4, 2021, 4:11:43 PM3/4/21
to Michael Shulman, homotopyt...@googlegroups.com
Nice. I haven't seen this. But I have considered two similar things:

(1) The natural numbers defined by

0
2x+1
2x+2

This is not a HIT - just a normal inductive types.

(2) A HIT for the dyadics numbers D in the interval [0,1] with constructors

data 𝔹 : Type₀ where
L R : 𝔹 -- 0 and 1
l r : 𝔹 → 𝔹 -- x ↦ x/2 and x ↦ (x+1)/2
eqL : L ≡ l L -- 0 = 0/2
eqM : l R ≡ r L -- 1/2 = (0+1)/2
eqR : R ≡ r R -- 1 = (1+1)/2

Then, like your HIT, this is automatically a set without the need of
truncation. But I didn't find an simple proof of this. Together with
Alex Rice, we proved this by showing it to be equivalent to another type
which is easily seen to be a aset:

data 𝔻 : Type₀ where
middle : 𝔻
left : 𝔻 → 𝔻
right : 𝔻 → 𝔻

data 𝔹' : Type₀ where
L' : 𝔹'
R' : 𝔹'
η : 𝔻 → 𝔹'

l' : 𝔹' → 𝔹'
l' L' = L'
l' R' = η middle
l' (η x) = η (left x)

r' : 𝔹' → 𝔹'
r' L' = η middle
r' R' = R'
r' (η x) = η (right x)

eqL' : L' ≡ l' L'
eqM' : l' R' ≡ r' L'
eqR' : R' ≡ r' R'

eqL' = refl
eqM' = refl
eqR' = refl

Then the types 𝔹 and 𝔹' are equivalent, and clearly 𝔹' is a set
because it has decidable equality.

Moreover, the "binary systems" (𝔹,L,R,l,r,eqL,eqM,eqR) and
(𝔹',L',R',l',r',eqL',eqM',eqR') are isomorphic.

(And we implemented in this in Cubical Agda.)

I wonder if your definition of the integers with the HIT is isomorphic
to an inductively defined version without a HIT as above, and this is
how you proved it to be a set.

Martin

Michael Shulman

unread,
Mar 4, 2021, 5:05:37 PM3/4/21
to Martin Escardo, homotopyt...@googlegroups.com
On Thu, Mar 4, 2021 at 1:11 PM Martin Escardo <escardo...@gmail.com> wrote:
> I wonder if your definition of the integers with the HIT is isomorphic
> to an inductively defined version without a HIT as above, and this is
> how you proved it to be a set.

No, I just did an encode-decode. I formalized most of it, up to some
obviously true lemmas about squares and cubes.

I thought for a little bit about whether there is an equivalent
"lower" inductive version, but nothing immediately occurred to me. Do
you have a suggestion?

Martin Escardo

unread,
Mar 4, 2021, 5:42:33 PM3/4/21
to Michael Shulman, Martin Escardo, homotopyt...@googlegroups.com


On 04/03/2021 22:05, Michael Shulman wrote:
For example, take ℤ' = ℕ + ℕ and define the functions you have in your
hit and prove the equations you have in your HIT.

Then using the HIT universal property you get a function from your ℤ to
this ℤ', and conversely you define a function from ℤ' to ℤ "manually".

This is what Alex Rice and I did to prove that our 𝔹 and 𝔹' are
equivalent. The cubes don't disappear completely, of course. But there
were much fewer cubes than when we tried to prove directly that 𝔹 is a
set, and we gave up because the code was getting too long and
unpleasant, with too many cases to check. The indirect version was much
shorter and pleasing.

Martin




Nicolai Kraus

unread,
Mar 4, 2021, 6:16:34 PM3/4/21
to HomotopyT...@googlegroups.com
On 04/03/2021 22:05, Michael Shulman wrote:
I thought for a little bit about whether there is an equivalent
"lower" inductive version, but nothing immediately occurred to me.  Do
you have a suggestion?

I'm not sure what the precise thing is that you're looking for because, without further specification, any standard definition of Z would qualify :-)
The HIT is neat, but wouldn't it in practice behave pretty similar to a standard representation via binary lists? E.g. something like Unit + Bool * List(Bool), where inl(*) is zero, the first Bool is the sign, and you add a 1 in front of the list in order to get a positive integer. What's the advantage of the HIT - maybe one can avoid case distinctions?
Nicolai

Michael Shulman

unread,
Mar 4, 2021, 9:27:17 PM3/4/21
to Nicolai Kraus, HomotopyT...@googlegroups.com
On Thu, Mar 4, 2021 at 3:16 PM Nicolai Kraus <nicola...@gmail.com> wrote:
> I'm not sure what the precise thing is that you're looking for because, without further specification, any standard definition of Z would qualify :-)

Yes, that seems to be what Martin suggested too with ℕ + ℕ. It seemed
to me as though the distance between ℕ + ℕ and my ℤ is greater than
the distance between his 𝔹 and 𝔹', but maybe not in any important
way.

> The HIT is neat, but wouldn't it in practice behave pretty similar to a standard representation via binary lists? E.g. something like Unit + Bool * List(Bool), where inl(*) is zero, the first Bool is the sign, and you add a 1 in front of the list in order to get a positive integer. What's the advantage of the HIT - maybe one can avoid case distinctions?

Is there a non-HIT binary representation that can be interpreted as
two's-complement (thereby avoiding case distinctions on sign)? I
haven't been able to figure out a way to do that with mere lists of
booleans.

Jason Gross

unread,
Mar 4, 2021, 10:02:33 PM3/4/21
to Michael Shulman, Nicolai Kraus, homotopyt...@googlegroups.com
Note that the Coq standard library Z is a binary representation of integers and Z.testbit gives access to the infinite twos-compliment representation.  Of course, it makes use of case-distinctions on sign to define it, but you go bit-by-bit; if the number is negative, you just invert the bit before returning it.  Is there something you're after by having the representation not encode sign bits separately?

--
You received this message because you are subscribed to the Google Groups "Homotopy Type Theory" group.
To unsubscribe from this group and stop receiving emails from it, send an email to HomotopyTypeThe...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/HomotopyTypeTheory/CAOvivQwtAMVnOG7D_A_s1EMAum4fv_x945MLB%2B01Y_pMdEKC2w%40mail.gmail.com.

Michael Shulman

unread,
Mar 4, 2021, 11:41:26 PM3/4/21
to Jason Gross, Nicolai Kraus, homotopyt...@googlegroups.com
I don't have a particular application in mind at the moment. It just
struck me how 2-adic integers have such simple coinductive definitions
of the arithmetic operations, without any case distinctions at all,
and I wondered whether there was a definition of plain integers with a
similar property. It seems annoying to have to constantly case-split
on a sign bit.

I guess a lower-inductive representation could take advantage of the
fact that the 2-adic representation of an integer other than 0 or -1
has a unique largest digit that differs from all larger digits, i.e.
it's either

...0000 (i.e. 0)
...1111 (i.e. -1)
...0001 + arbitrary bit string
...1110 + arbitrary bit string

This involves more case splits when defining arithmetic, but it would
probably be an easy set to show that the HIT one is equivalent to.
> To view this discussion on the web visit https://groups.google.com/d/msgid/HomotopyTypeTheory/CAKObCaqiyUcB%2BFrbvPmR-eS5ZMsb2CTVCtVbAYZeMUcOzCpmbA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages