Zulip Chat Archive

Stream: Is there code for X?

Topic: int.mod_by_nat


Bolton Bailey (Sep 23 2022 at 22:56):

I was a bit surprised to see the type signature of docs#int.nat_mod is Z -> Z -> N. Do we have an int/nat modulus operation of the form Z -> N -> N?

Junyan Xu (Sep 23 2022 at 23:03):

Maybe just do

import data.zmod.basic
variables (a : ) (b : )
#check (a : zmod b).val

If b = 0 it would give you the absolute value of a.

Adam Topaz (Sep 23 2022 at 23:06):

what would you expect (-3) % 0 to be?

Adam Topaz (Sep 23 2022 at 23:07):

python gives me a division by zero error, but lean is happy to say that (-3) % 0 is -3 (which is the only reasonable answer IMO)

Bolton Bailey (Sep 23 2022 at 23:49):

I would expect (-3) % 0 to be 0 probably, but I wouldn't really care. What would you expect (-3).nat_mod 0 to be?

Adam Topaz (Sep 23 2022 at 23:51):

I don't think nat_mod should exist in the first place :rofl:

Bolton Bailey (Sep 23 2022 at 23:53):

Well, it's used a few times in mathlib. As far as I can tell each time the second argument is a nat cast to an int.

Bolton Bailey (Sep 23 2022 at 23:54):

Seems perfectly normal to me, I just want to extend % on naturals to also work when the first argument is an integer.

Adam Topaz (Sep 23 2022 at 23:55):

But % does work on ints

Bolton Bailey (Sep 23 2022 at 23:57):

I don't want to see a bunch of casts when my second argument is a nat, but fair enough.

Bolton Bailey (Sep 23 2022 at 23:58):

Actually wait no, it's important to me that the return type is a nat too.

Adam Topaz (Sep 24 2022 at 00:01):

So you can compose with docs#int.nat_abs

Adam Topaz (Sep 24 2022 at 00:01):

Or with docs#int.to_nat

Adam Topaz (Sep 24 2022 at 00:02):

Depending on the behavior you want for negative integers

Adam Topaz (Sep 24 2022 at 00:02):

docs#int.has_mod (since I'm on mobile)

Bolton Bailey (Sep 24 2022 at 00:03):

This is exactly what docs#int.nat_mod does

Adam Topaz (Sep 24 2022 at 00:07):

Aha okay. Well hopefully one little cast in the second variable isn't that bad?

Bolton Bailey (Sep 24 2022 at 00:11):

I guess not. I agree that nat_mod shouldn't exist, but I think if only one of nat_mod and mod_by_nat were to exist, it should be the latter.

Bolton Bailey (Sep 24 2022 at 00:15):

The point is that the % of int calls nat_abs on its second argument anyway, so better to write x.mod_by_nat |y| less often than x.nat_mod (y : \Z) more often. But also nat_mod is in core, so I can't get rid of the old way.

Bolton Bailey (Sep 24 2022 at 00:16):

docs#int.mod


Last updated: Dec 20 2023 at 11:08 UTC