(* Copyright (C) 2005-2008 Sebastien Briais *)
(* http://lamp.epfl.ch/~sbriais/ *)
(* This library is free software; you can redistribute it and/or modify *)
(* it under the terms of the GNU Lesser General Public License as *)
(* published by the Free Software Foundation; either version 2.1 of the *)
(* License, or (at your option) any later version. *)
(* This library is distributed in the hope that it will be useful, but *)
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *)
(* Lesser General Public License for more details. *)
(* You should have received a copy of the GNU Lesser General Public *)
(* License along with this library; if not, write to the Free Software *)
(* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *)
(* 02110-1301 USA *)
Unset Standard Proposition Elimination Names.
(** d is a common divisor of a and b if d | a and d | b *)
Definition is_cd (d a b : nat) := (divides a d)/\(divides b d).
(** d is a greatest common divisor of a and b if it is a maximal common divisor *)
Definition is_gcd (d a b:nat) := (is_cd d a b)/\(forall (d':nat),(is_cd d' a b)->(divides d d')).
unfold is_gcd.
intros.
elim H;elim H0;intros.
apply divides_antisym;auto.
Qed.
unfold is_gcd.
intros.
elim H;intros.
split.
red;red in H0;tauto.
intros.
apply H1.
red;red in H2;tauto.
Qed.
unfold is_gcd.
intro.
split.
red;split;[apply zero_max_div | apply divides_refl].
unfold is_cd;tauto.
Qed.
unfold is_gcd.
intros.
split.
red;split;[apply divides_refl | apply one_min_div].
unfold is_cd;tauto.
Qed.
intros.
unfold is_gcd.
split;intro.
elim H0;intros.
split.
red in H1;red.
elim H1;intros.
split;try tauto.
apply divides_minus;trivial.
unfold is_cd;intros.
apply H2;red;elim H3;intros.
split;[tauto | rewrite (le_plus_minus a b H);apply divides_plus;trivial].
elim H0;unfold is_cd;intros.
split.
split;[tauto | elim H1;intros;rewrite (le_plus_minus a b H);apply divides_plus;trivial].
intros.
elim H3;intros;apply H2.
split;try (apply divides_minus);trivial.
Qed.
unfold is_gcd.
intros.
unfold is_cd.
split;try tauto.
split;apply divides_refl.
Qed.
unfold is_gcd;unfold is_cd;intros;tauto.
Qed.
unfold is_gcd;unfold is_cd;intros;tauto.
Qed.
(** we now show that gcd(a,b) exists for all a and b (we even have an algorithm) *)
Definition f (x:nat*nat) := (fst x)+(snd x).
Definition R (x y:nat*nat) := (f x)<(f y).
unfold R.
apply (well_founded_ltof (nat*nat) f).
Qed.
apply (induction_ltof2 (nat*nat) f (fun x:nat*nat => {d:nat | (is_gcd d (fst x) (snd x))})).
unfold ltof.
unfold f.
intros.
case (lt_eq_lt_dec (fst x) (snd x));intro.
case s;intro.
destruct (fst x).
exists (snd x);apply gcd_zero.
elim (H (S n,snd x-S n)).
simpl;intro d;intro.
exists d.
elim (gcd_minus d (S n) (snd x));try (auto with arith).
simpl.
omega.
rewrite e;exists (snd x);apply gcd_refl.
destruct (snd x).
exists (fst x);apply gcd_sym;apply gcd_zero.
elim (H (S n,fst x-S n)).
simpl;intro d;intro.
exists d.
apply gcd_sym.
elim (gcd_minus d (S n) (fst x));try (auto with arith).
simpl.
omega.
Qed.
intros.
elim (gcd_exists_prod (a,b)).
simpl;intro d;intro;exists d;trivial.
Qed.
(** take the first projection of the proof *)
Definition gcd (a b:nat) := let (d,_):=(gcd_exists a b) in d.
intros.
unfold gcd.
generalize (gcd_exists a b).
intro;elim s;intro d;trivial.
Qed.
(** a and b are relatively prime if gcd(a,b)=1 *)
Definition rel_prime (a b:nat) := (is_gcd 1 a b).

(** if a and b are relatively prime then so are b and a *)
Lemma rel_prime_sym : forall (a b:nat),(rel_prime a b)->(rel_prime b a).