Chapter 6 - Sum and Sigma Types


Require Import Base.

6.1 Boolean Sums and Certifying Test


(* We must not override the Base definitions of this stuff,
   because otherwise Coq will complain when we use it!

Inductive sumbool (X Y : Prop) : Type :=
| left : X -> sumbool X Y
| right : Y -> sumbool X Y.


Arguments left {X} {Y} _.
Arguments right {X} {Y} _.


Notation "{ X } + { Y }" := (sumbool X Y).
*)


Definition min (x y : nat) : nat :=
  if le_dec x y then x else y.

Compute min 7 3.

Set Printing All.
Print min.
Unset Printing All.

Goal x y, (x y min x y = x) (y x min x y = y).
Proof.
  intros x y. split ; intros A.
  - unfold min. destruct (le_dec x y) as [B|B].
    + reflexivity.
    + omega.
  - unfold min. destruct (le_dec x y) as [B|B].
    + omega.
    + reflexivity.
Qed.

Goal x y, (x y min x y = x) (y x min x y = y).
Proof.
    intros x y. split ; intros A ; unfold min ; destruct (le_dec x y) ; omega.
Qed.

Check le_lt_dec.
Check le_ge_dec.
Check le_gt_dec.
Check lt_eq_lt_dec.

Set Printing All.
Check {True} + {False} + {False}.
Unset Printing All.

(* We must not override the Base definitions of this stuff,
   because otherwise Coq will complain when we use it!

Inductive sumor (X : Type) (Y : Prop) : Type :=
| inleft : X -> sumor X Y 
| inright : Y -> sumor X Y.

Notation "X + { Y }" := (sumor X Y). *)


(* Exercise 6.1.1 *)
Goal X Y : Prop, {X} + {Y} X Y.
Abort.

(* Exercise 6.1.2 *)
Goal x y, if le_dec x y then x y else ¬ x y.
Abort.

Goal x y, if le_dec x y then x y else x > y.
Abort.

6.2 - Inhabitation and Decidability

Inductive inhabited (X : Type) : Prop :=
| inhabits : X inhabited X.

Goal X : Prop, inhabited X X.
Proof.
  split.
  - intros [A] ; exact A.
  - intros A. constructor. exact A.
Qed.

(* We must not override the Base definition of dec,
   because otherwise Coq will complain when we use it.

Definition dec (X : Prop) : Type := {X} + {~ X}.
*)


Check le_dec.

Definition dec2bool (X : Prop) (d : dec X) : bool :=
  if d then true else false.

Compute (dec2bool (le_dec 2 3)).

Definition True_dec : dec True := left I.

Definition False_dec : dec False := right (fun AA).

(* Exercise 6.2.1 *)
Goal X : Type, X inhabited X.
Abort.

(* Exercise 6.2.2 *)
Goal X Y: Prop, X Y inhabited ({X} + {Y}).
Abort.

(* Exercise 6.2.3 *)
Goal X: Prop, dec X X ¬ X.
Abort.

6.3 Writing Certifying Test


Definition impl_dec (X Y : Prop) : dec X dec Y dec (X Y).
  intros A [B|B].
  - left. auto.
  - destruct A as [A|A].
    + right. auto.
    + left. tauto.
Defined.

Check impl_dec (le_dec 3 2) False_dec.

Compute (dec2bool (impl_dec (le_dec 3 2) False_dec)).

Definition nat_eq_dec (x y : nat) : dec (x=y).
  revert y. induction x ; simpl ; intros [|y].
  - left. auto.
  - right. auto.
  - right. auto.
  - destruct (IHx y).
    + left. congruence.
    + right. congruence.
Defined.

Compute dec2bool (nat_eq_dec 3 3).

Goal x y : nat, dec (x=y).
Proof. unfold dec. decide equality. Qed.

Definition le_dec (x y : nat) : dec (x y).
  destruct (leb x y) eqn:A.
  - left. apply leb_iff. exact A.
  - right. intros B. apply leb_iff in B. congruence.
Defined.

Definition dec_prop_iff (X Y : Prop) : (X Y) dec X dec Y.
  intros A [B|B].
  - left. tauto.
  - right. tauto.
Defined.

(* Exercise 6.3.1 *)
Goal X : Prop, inhabited X dec X.
Abort.

Goal X : Prop, dec X dec (inhabited X).
Abort.

Goal X : Prop, dec (inhabited X) dec X.
Abort.

(* Exercise 6.3.2 *)
Definition and_dec (X Y : Prop) : dec X dec Y dec (X Y).
Abort.

Definition or_dec (X Y : Prop) : dec X dec Y dec (X Y).
Abort.

(* Exercise 6.3.3 *)
Goal x y: nat, {x < y} + {x = y} + {y < x}.
Abort.

(* Exercise 6.3.4- Do it twice: Once with "decide equality", once without. *)
Goal x y: bool, {x = y} + {x y}.
Abort.

(* Exercise 6.3.5 *)
(* In Coq 8.5 the names are Nat.eqb for nat_eqb and Nat.eqb_eq for nat_eqb_agrees. *)
Print Nat.eqb.
Check Nat.eqb_eq.

(* Definition nat_eqb' (x y: nat) : bool :=
 (* ... *)


Lemma nat_eqb_agrees (x y: nat) :
  x = y <-> nat_eqb' x y = true.
Abort. *)


Definition nat_eq_dec' (x y: nat):
  dec (x = y).
Abort.

(* Exercise 6.3.6 *)

Lemma leb_iff x y : leb x y = true x y.
Abort.

Definition le_dec' (x y: nat):
  dec (x y).
Abort.

(* Exercise 6.3.7 *)
Definition listdec (X: Type) (eqX: x y: X, dec (x = y)):
   xs ys: list X, dec (xs = ys).
Abort.

(* Exercise 6.3.8 *)
Definition bool2dec (X : Prop) (b : bool) : (X b = true) dec X.
Abort.

(* Exercise 6.3.9 *)
Definition cas (X Y Z : Type) : (X × Y Z) X Y Z.
  intros f x y. exact (f (x,y)).
Defined.

Definition car (X Y Z : Type) : (X Y Z) X × Y Z.
  intros f [x y]. exact (f x y).
Defined.

Definition add : nat nat nat.
  fix f 1. intros x y. destruct x as [|x'].
  - exact y.
  - exact (S (f x' y)).
Defined.

6.5 Decidable Predicates


Definition decidable (X : Type) (p : X Prop) : Type := x, dec (p x).

Definition XM := (X : Prop), X ¬X.

Goal decidable (fun X : PropX ¬X) XM.
Proof. intros A X. destruct (A X) as [B|B] ; tauto. Qed.

(* Exercise 6.5.1 *)
Goal (X : Type) (p : X Prop) (f : X bool), ( x, p x f x = true) decidable p.
Abort.

6.6 Sigma Types

(* This overrides already defined names (from the Base library)
   which we shouldn't do, because if we do, we have two different things
   with the same name.

Inductive sig (X : Type) (p : X -> Prop) : Type :=
  exist : forall x : X, p x -> sig p.

 
Notation "{ x  |  p }" := (sig (fun x => p)).
Notation "{ x : X  |  p }" := (sig (fun x : X => p)).
*)


Definition double (x : nat) : { y | y = 2×x}.
   (2×x). reflexivity.
Defined.

Compute let (y,_) := double 4 in y.

Definition div2_cert (n : nat) : {k | n = 2×k} + {k | n = 2×k + 1}.
  induction n.
  - left. 0. reflexivity.
  - destruct IHn as [[k A]|[k A]].
    + right. k. omega.
    + left. (S k). omega.
Defined.

(* This overrides already defined names (from the Base library)
   which we shouldn't do, because if we do, we have two different things
   with the same name, which confuses Coq
Inductive sum (X Y : Type) :=
| inl : X -> sum X Y
| inr : Y -> sum X Y.

 
Notation "x + y" := (sum x y) : type_scope.
*)


Definition mod2 x := if div2_cert x then 0 else 1.

Definition div2 x := match div2_cert x with
                        | inl (exist _ k _) ⇒ k
                        | inr (exist _ k _) ⇒ k
                      end.

Goal x, x = 2 × div2 x + mod2 x.
Proof.
  intros x. unfold div2, mod2.
  destruct (div2_cert x) as [[k A]|[k A]] ; omega.
Qed.

(* Exercise 6.6.1 *)
Goal x, mod2 x 1.
Abort.

(* Exercise 6.6.2 *)
Lemma Sigma_Skolem (X Y : Type) (p : X Y Prop) :
 ( x, {y | p x y}) { f | x, p x (f x) }.
Abort.

(* Exercise 6.6.3 *)
Goal X (p : X Prop), {x | p x} x, p x.
Abort.

(* Exercise 6.6.4 *)
Goal X: Type, (p: X Prop), ( x, p x) inhabited {x | p x}.
Abort.

(* Exercise 6.6.5 *)
Goal (X : Type) (p : X Prop), decidable p {f : X bool | x, p x f x = true}.
Abort.

(* Exercise 6.6.6 *)
Definition div3_cert (n : nat) : {k | n = 3×k} + {k | n = 3×k + 1} + {k | n = 3×k + 2}.
Abort.

6.7 Strong Truth Value Semantics


Definition b2P (x : bool) : Prop := if x then True else False.

Definition STVS : Type := X : Prop, {X=True} + {X=False}.

Definition TVS := X : Prop, X=True X=False.

Goal STVS TVS.
Proof. intros stvs X. destruct (stvs X) ; subst X ; auto. Qed.

Section STVS.
Variable stvs : STVS.
Definition P2b (X : Prop) : bool := if stvs X then true else false.

Lemma P2bTrue : P2b True = true.
Proof.
    unfold P2b. destruct (stvs True) as [A|A].
    + reflexivity.
    + exfalso. rewrite <- A. exact I.
Qed.

Lemma P2bFalse : P2b False = false.
Proof.
  unfold P2b. destruct (stvs False) as [A|A].
  + exfalso. rewrite A. exact I.
  + reflexivity.
Qed.

Goal x : bool, P2b (b2P x) = x.
Proof. intros [|] ; simpl. exact P2bTrue. exact P2bFalse. Qed.

Goal X : Prop, b2P (P2b X) = X.
Proof.
  intros X. destruct (stvs X) ; subst X.
  - rewrite P2bTrue. reflexivity.
  - rewrite P2bFalse. reflexivity.
Qed.

End STVS.

Print P2b.

(* Exercise 6.7.1 *)
Goal x y: bool, b2P x = b2P y x = y.
Abort.

(* Exercise 6.7.2 *)
Definition surjective (X Y: Type) (f: X Y):=
   y:Y, x: X, f x = y.

Goal TVS surjective b2P.
Abort.

(* Exercise 6.7.3 *)
Goal A : STVS, X Y : Prop, P2b A X = P2b A Y X = Y.
Abort.

(* Exercise 6.7.4 *)
Goal STVS X : Prop, dec X.
Abort.

(* Exercise 6.7.5 *)
Goal STVS (X : Type) (p : X Prop), decidable p.
Abort.