(* This program 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 program 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 General Public License for more details.                       *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)


Set Implicit Arguments.
Unset Strict Implicit.
Require Export Parts.
(** Title "Union of two parts.".*)
Section Union1.
Variable E : Setoid.

Definition union : part_set E -> part_set E -> part_set E.
(* Goal: forall (_ : Carrier (part_set E)) (_ : Carrier (part_set E)), Carrier (part_set E) *)
intros A B.
(* Goal: Carrier (part_set E) *)
apply (Build_Predicate (Pred_fun:=fun x : E => in_part x A \/ in_part x B)).
(* Goal: @pred_compatible E (fun x : Carrier E => or (@in_part E x A) (@in_part E x B)) *)
red in |- *.
(* Goal: forall (x y : Carrier E) (_ : or (@in_part E x A) (@in_part E x B)) (_ : @Equal E y x), or (@in_part E y A) (@in_part E y B) *)
intros x y H' H'0; try assumption.
(* Goal: or (@in_part E y A) (@in_part E y B) *)
elim H'; [ intros H'1; try exact H'1; clear H' | intros H'1; clear H' ].
(* Goal: or (@in_part E x A) (@in_part E x B) *)
(* Goal: or (@in_part E x A) (@in_part E x B) *)
left; try assumption.
(* Goal: @in_part E y B *)
apply in_part_comp_l with x; auto with algebra.
(* Goal: or (@in_part E x A) (@in_part E x B) *)
right; try assumption.
(* Goal: @in_part E y B *)
apply in_part_comp_l with x; auto with algebra.
Defined.

Lemma included_union_l : forall A B : part_set E, included A (union A B).
(* Goal: forall (A B C : Carrier (part_set E)) (_ : @included E A C) (_ : @included E B C), @included E (union A B) C *)
unfold included in |- *; simpl in |- *; intuition.
Qed.

Lemma included_union_r : forall A B : part_set E, included B (union A B).
(* Goal: forall (A B C : Carrier (part_set E)) (_ : @included E A C) (_ : @included E B C), @included E (union A B) C *)
unfold included in |- *; simpl in |- *; intuition.
Qed.

Lemma in_part_union_l :
 forall (A B : part_set E) (x : E), in_part x A -> in_part x (union A B).
(* Goal: forall (A B : Carrier (part_set E)) (x : Carrier E) (_ : @in_part E x B), @in_part E x (union A B) *)
simpl in |- *; intuition.
Qed.

Lemma in_part_union_r :
 forall (A B : part_set E) (x : E), in_part x B -> in_part x (union A B).
(* Goal: forall (A B : Carrier (part_set E)) (x : Carrier E) (_ : @in_part E x B), @in_part E x (union A B) *)
simpl in |- *; intuition.
Qed.
Parameter
  in_part_union_or :
    forall (A B : part_set E) (x : E),
    in_part x A \/ in_part x B -> in_part x (union A B).

Lemma in_part_union :
 forall (A B : part_set E) (x : E),
 in_part x (union A B) -> in_part x A \/ in_part x B.
(* Goal: forall (A B : Carrier (part_set E)) (x : Carrier E) (_ : @in_part E x (union A B)), or (@in_part E x A) (@in_part E x B) *)
intros A B x; try assumption.
(* Goal: forall _ : @in_part E x (union A B), or (@in_part E x A) (@in_part E x B) *)
unfold union in |- *; intuition.
Qed.

Lemma union_not_in_l :
 forall (A B : part_set E) (x : E),
 in_part x (union A B) -> ~ in_part x A -> in_part x B.
(* Goal: forall (A B : Carrier (part_set E)) (x : Carrier E) (_ : @in_part E x B), @in_part E x (union A B) *)
unfold union in |- *; simpl in |- *; intuition.
Qed.

Lemma included2_union :
 forall A B C : part_set E,
 included A C -> included B C -> included (union A B) C.
(* Goal: forall (A B C : Carrier (part_set E)) (_ : @included E A C) (_ : @included E B C), @included E (union A B) C *)
unfold included in |- *; simpl in |- *; intuition.
Qed.

Lemma union_comp :
 forall A A' B B' : part_set E,
 Equal A A' -> Equal B B' -> Equal (union A B) (union A' B').
(* Goal: forall A B : Carrier (part_set E), @Equal (part_set E) (union A B) (union B A) *)
unfold union in |- *; simpl in |- *.
(* Goal: forall A B : Predicate E, @eq_part E (@Build_Predicate E (fun x : Carrier E => or (@in_part E x A) (@in_part E x B)) (fun (x y : Carrier E) (H' : or (@in_part E x A) (@in_part E x B)) (H'0 : @Equal E y x) => @or_ind (@in_part E x A) (@in_part E x B) (or (@in_part E y A) (@in_part E y B)) (fun H'1 : @in_part E x A => @or_introl (@in_part E y A) (@in_part E y B) (@in_part_comp_l E A x y H'1 H'0)) (fun H'1 : @in_part E x B => @or_intror (@in_part E y A) (@in_part E y B) (@in_part_comp_l E B x y H'1 H'0)) H')) (@Build_Predicate E (fun x : Carrier E => or (@in_part E x B) (@in_part E x A)) (fun (x y : Carrier E) (H' : or (@in_part E x B) (@in_part E x A)) (H'0 : @Equal E y x) => @or_ind (@in_part E x B) (@in_part E x A) (or (@in_part E y B) (@in_part E y A)) (fun H'1 : @in_part E x B => @or_introl (@in_part E y B) (@in_part E y A) (@in_part_comp_l E B x y H'1 H'0)) (fun H'1 : @in_part E x A => @or_intror (@in_part E y B) (@in_part E y A) (@in_part_comp_l E A x y H'1 H'0)) H')) *)
unfold eq_part in |- *; simpl in |- *.
(* Goal: forall (A A' B B' : Predicate E) (_ : forall x : Carrier E, and (forall _ : @in_part E x A, @in_part E x A') (forall _ : @in_part E x A', @in_part E x A)) (_ : forall x : Carrier E, and (forall _ : @in_part E x B, @in_part E x B') (forall _ : @in_part E x B', @in_part E x B)) (x : Carrier E), and (forall _ : or (@in_part E x A) (@in_part E x B), or (@in_part E x A') (@in_part E x B')) (forall _ : or (@in_part E x A') (@in_part E x B'), or (@in_part E x A) (@in_part E x B)) *)
intros A A' B B' H' H'0 x; split; [ intros H'1; try assumption | idtac ].
(* Goal: or (@in_part E x A) (@in_part E x B) *)
elim H'1; [ intros H'2; try exact H'2; clear H'1 | intros H'2; clear H'1 ].
(* Goal: or (@in_part E x A) (@in_part E x B) *)
(* Goal: or (@in_part E x A) (@in_part E x B) *)
left; try assumption.
elim (H' x); intros H'3 H'4; lapply H'3;
 [ intros H'5; try exact H'5; clear H'3 | clear H'3 ].
(* Goal: @in_part E x B' *)
auto with algebra.
(* Goal: or (@in_part E x A) (@in_part E x B) *)
right; try assumption.
elim (H'0 x); intros H'3 H'4; lapply H'3;
 [ intros H'5; try exact H'5; clear H'3 | clear H'3 ].
(* Goal: @in_part E x B' *)
auto with algebra.
(* Goal: forall _ : or (@in_part E x A') (@in_part E x B'), or (@in_part E x A) (@in_part E x B) *)
intros H'1; try assumption.
(* Goal: or (@in_part E x A) (@in_part E x B) *)
elim H'1; [ intros H'2; try exact H'2; clear H'1 | intros H'2; clear H'1 ].
(* Goal: or (@in_part E x A) (@in_part E x B) *)
(* Goal: or (@in_part E x A) (@in_part E x B) *)
left; try assumption.
elim (H' x); intros H'3 H'4; lapply H'4;
 [ intros H'5; try exact H'5; clear H'4 | clear H'4 ].
(* Goal: @in_part E x B' *)
auto with algebra.
(* Goal: or (@in_part E x A) (@in_part E x B) *)
right; try assumption.
elim (H'0 x); intros H'3 H'4; lapply H'4;
 [ intros H'5; try exact H'5; clear H'4 | clear H'4 ].
(* Goal: @in_part E x B' *)
auto with algebra.
Qed.

Lemma union_assoc :
 forall A B C : part_set E, Equal (union A (union B C)) (union (union A B) C).
(* Goal: forall A B : Carrier (part_set E), @Equal (part_set E) (union A B) (union B A) *)
unfold union in |- *; simpl in |- *.
(* Goal: forall A B : Predicate E, @eq_part E (@Build_Predicate E (fun x : Carrier E => or (@in_part E x A) (@in_part E x B)) (fun (x y : Carrier E) (H' : or (@in_part E x A) (@in_part E x B)) (H'0 : @Equal E y x) => @or_ind (@in_part E x A) (@in_part E x B) (or (@in_part E y A) (@in_part E y B)) (fun H'1 : @in_part E x A => @or_introl (@in_part E y A) (@in_part E y B) (@in_part_comp_l E A x y H'1 H'0)) (fun H'1 : @in_part E x B => @or_intror (@in_part E y A) (@in_part E y B) (@in_part_comp_l E B x y H'1 H'0)) H')) (@Build_Predicate E (fun x : Carrier E => or (@in_part E x B) (@in_part E x A)) (fun (x y : Carrier E) (H' : or (@in_part E x B) (@in_part E x A)) (H'0 : @Equal E y x) => @or_ind (@in_part E x B) (@in_part E x A) (or (@in_part E y B) (@in_part E y A)) (fun H'1 : @in_part E x B => @or_introl (@in_part E y B) (@in_part E y A) (@in_part_comp_l E B x y H'1 H'0)) (fun H'1 : @in_part E x A => @or_intror (@in_part E y B) (@in_part E y A) (@in_part_comp_l E A x y H'1 H'0)) H')) *)
unfold eq_part in |- *; simpl in |- *.
(* Goal: forall (A B C : Predicate E) (x : Carrier E), and (forall _ : or (@in_part E x A) (or (@in_part E x B) (@in_part E x C)), or (or (@in_part E x A) (@in_part E x B)) (@in_part E x C)) (forall _ : or (or (@in_part E x A) (@in_part E x B)) (@in_part E x C), or (@in_part E x A) (or (@in_part E x B) (@in_part E x C))) *)
intros A B C x; split; [ try assumption | idtac ].
(* Goal: forall _ : or (@in_part E x B) (@in_part E x A), or (@in_part E x A) (@in_part E x B) *)
intuition.
(* Goal: forall _ : or (@in_part E x B) (@in_part E x A), or (@in_part E x A) (@in_part E x B) *)
intuition.
Qed.

Lemma union_com : forall A B : part_set E, Equal (union A B) (union B A).
(* Goal: forall A B : Carrier (part_set E), @Equal (part_set E) (union A B) (union B A) *)
unfold union in |- *; simpl in |- *.
(* Goal: forall A B : Predicate E, @eq_part E (@Build_Predicate E (fun x : Carrier E => or (@in_part E x A) (@in_part E x B)) (fun (x y : Carrier E) (H' : or (@in_part E x A) (@in_part E x B)) (H'0 : @Equal E y x) => @or_ind (@in_part E x A) (@in_part E x B) (or (@in_part E y A) (@in_part E y B)) (fun H'1 : @in_part E x A => @or_introl (@in_part E y A) (@in_part E y B) (@in_part_comp_l E A x y H'1 H'0)) (fun H'1 : @in_part E x B => @or_intror (@in_part E y A) (@in_part E y B) (@in_part_comp_l E B x y H'1 H'0)) H')) (@Build_Predicate E (fun x : Carrier E => or (@in_part E x B) (@in_part E x A)) (fun (x y : Carrier E) (H' : or (@in_part E x B) (@in_part E x A)) (H'0 : @Equal E y x) => @or_ind (@in_part E x B) (@in_part E x A) (or (@in_part E y B) (@in_part E y A)) (fun H'1 : @in_part E x B => @or_introl (@in_part E y B) (@in_part E y A) (@in_part_comp_l E B x y H'1 H'0)) (fun H'1 : @in_part E x A => @or_intror (@in_part E y B) (@in_part E y A) (@in_part_comp_l E A x y H'1 H'0)) H')) *)
unfold eq_part in |- *; simpl in |- *.
(* Goal: forall (A B : Predicate E) (x : Carrier E), and (forall _ : or (@in_part E x A) (@in_part E x B), or (@in_part E x B) (@in_part E x A)) (forall _ : or (@in_part E x B) (@in_part E x A), or (@in_part E x A) (@in_part E x B)) *)
intros A B x; split; [ try assumption | idtac ].
(* Goal: forall _ : or (@in_part E x B) (@in_part E x A), or (@in_part E x A) (@in_part E x B) *)
intuition.
(* Goal: forall _ : or (@in_part E x B) (@in_part E x A), or (@in_part E x A) (@in_part E x B) *)
intuition.
Qed.
Parameter union_empty_l : forall A : part_set E, Equal (union (empty E) A) A.
Parameter union_empty_r : forall A : part_set E, Equal (union A (empty E)) A.
End Union1.
Hint Resolve included_union_l included_union_r in_part_union_l
  in_part_union_r included2_union union_comp union_assoc union_empty_l
  union_empty_r: algebra.
Hint Immediate union_com: algebra.