Skip to content

Instantly share code, notes, and snippets.

@siraben
Last active July 21, 2021 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siraben/a790918a3b4556e7faf7ae83474dd29a to your computer and use it in GitHub Desktop.
Save siraben/a790918a3b4556e7faf7ae83474dd29a to your computer and use it in GitHub Desktop.
Proof that addition under modulo n is associative
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