Last active
July 21, 2021 05:59
-
-
Save siraben/a790918a3b4556e7faf7ae83474dd29a to your computer and use it in GitHub Desktop.
Proof that addition under modulo n is associative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From Coq.Arith Require Import PeanoNat. | |
Import Nat. | |
Require Import Lia. | |
Definition add_mod n i j := | |
match i + j ?= n with | |
| Lt => i + j | |
| _ => i + j - n | |
end. | |
Ltac solve_compare := | |
repeat match goal with | |
| [ |- context [ match ?x ?= ?y with _ => _ end ] ] => | |
destruct (compare_spec x y); simpl; auto; try lia | |
end. | |
Theorem add_mod_assoc n : 0 < n -> | |
forall a b c, a < n -> b < n -> c < n -> add_mod n (add_mod n a b) c | |
= add_mod n a (add_mod n b c). | |
Proof. intros; unfold add_mod; solve_compare. Qed. | |
Theorem add_mod_inverse n : 0 < n -> | |
forall i, i < n -> add_mod n i (n - i) = 0. | |
Proof. intros; unfold add_mod; solve_compare. Qed. | |
Theorem add_mod_id n : 0 < n -> | |
forall i, i < n -> add_mod n i 0 = i. | |
Proof. intros; unfold add_mod; solve_compare. Qed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment