158 lines
3.2 KiB
Coq
158 lines
3.2 KiB
Coq
Require Import List.
|
|
Require Import Coq.Arith.Even.
|
|
Require Import Recdef.
|
|
Import ListNotations.
|
|
Require Import Relations.
|
|
|
|
(* ** Attempt 3 *)
|
|
Record bag := mkBag {
|
|
nblack: nat
|
|
; nwhite: nat
|
|
}.
|
|
|
|
Print relation.
|
|
(*
|
|
relation = fun A : Type => A -> A -> Prop
|
|
: Type -> Type
|
|
|
|
Arguments relation _%type_scope
|
|
*)
|
|
|
|
Inductive step : relation bag :=
|
|
| bb : forall b w,
|
|
step (mkBag (S (S b)) w) (mkBag b (S w))
|
|
| ww : forall b w,
|
|
step (mkBag b (S (S w))) (mkBag b (S w))
|
|
| bw : forall b w,
|
|
step (mkBag (S b) (S w)) (mkBag (S b) w).
|
|
|
|
Print clos_refl_trans.
|
|
(*
|
|
Inductive clos_refl_trans (A : Type) (R : relation A) (x : A) : A -> Prop :=
|
|
rt_step : forall y : A, R x y -> clos_refl_trans A R x y
|
|
| rt_refl : clos_refl_trans A R x x
|
|
| rt_trans : forall y z : A,
|
|
clos_refl_trans A R x y ->
|
|
clos_refl_trans A R y z -> clos_refl_trans A R x z
|
|
|
|
Arguments clos_refl_trans _%type_scope
|
|
Arguments rt_step _%type_scope
|
|
Arguments rt_refl _%type_scope
|
|
Arguments rt_trans _%type_scope
|
|
*)
|
|
|
|
Check step.
|
|
Check clos_refl_trans bag step.
|
|
|
|
Definition process : bag -> bag -> Prop := clos_refl_trans _ step.
|
|
|
|
Definition invariant (bin bout:bag) : Prop :=
|
|
Nat.odd (nblack bin) = Nat.odd (nblack bout).
|
|
|
|
Lemma step_invariant (bin bout:bag)
|
|
: step bin bout -> invariant bin bout.
|
|
Proof.
|
|
inversion 1; unfold invariant; simpl; trivial.
|
|
Qed.
|
|
|
|
(*
|
|
inversion 1.
|
|
- unfold invariant.
|
|
simpl.
|
|
trivial.
|
|
*)
|
|
|
|
Theorem process_invariant (bin bout:bag)
|
|
: process bin bout -> invariant bin bout.
|
|
Proof.
|
|
(*intros.
|
|
induction H.*)
|
|
induction 1.
|
|
- apply step_invariant.
|
|
exact H.
|
|
- reflexivity.
|
|
- unfold invariant.
|
|
transitivity (Nat.odd (nblack y)).
|
|
* transitivity (IH.
|
|
|
|
|
|
(* ** Attempt 2 *)
|
|
|
|
Notation black := false.
|
|
Notation white := true.
|
|
|
|
|
|
|
|
(*
|
|
Fixpoint proc (bag:list bool) : list bool :=
|
|
match bag with
|
|
| (a::b::ls) =>
|
|
match a, b with
|
|
| black, black => proc (white::ls)
|
|
| white, white => proc (white::ls)
|
|
| _, _ => proc (black::ls)
|
|
end
|
|
| _ => bag
|
|
end.
|
|
*)
|
|
|
|
Function proc_rec (bag:list bool) {measure length bag} : list bool :=
|
|
match bag with
|
|
| nil | [_] => bag
|
|
| _::_::ls => proc_rec (proc_rec bag)
|
|
(*| _ => bag*)
|
|
end.
|
|
|
|
Definition b := false.
|
|
Check b.
|
|
Compute match b with
|
|
| true => true
|
|
| false => true
|
|
end.
|
|
|
|
Fixpoint proc (bag:list bool) {struct bag} : list bool :=
|
|
match bag with
|
|
| (a::b::ls) =>
|
|
match a, b with
|
|
| false, false => proc (true::ls)
|
|
| true, true => proc (true::ls)
|
|
| _, _ => proc (false::ls)
|
|
end
|
|
| _ => bag
|
|
end.
|
|
|
|
Compute Nat.even 3.
|
|
Compute even 3.
|
|
|
|
Check filter.
|
|
(*
|
|
filter
|
|
: forall A : Type, (A -> bool) -> list A -> list A
|
|
*)
|
|
Check count_occ.
|
|
|
|
Definition booleqb (a b:bool):bool := negb (xorb a b).
|
|
Compute filter (fun x=>x) [].
|
|
|
|
Definition count (l:list bool) (val:bool) : nat :=
|
|
length (filter (fun elem => booleqb elem val) l).
|
|
Compute count [true; false; true] true.
|
|
|
|
Compute Nat.odd 1.
|
|
(*
|
|
Goal forall (p q : Prop), p -> p->q -> q.
|
|
Proof.
|
|
intros.
|
|
*)
|
|
|
|
Theorem proc_odd_b : forall bag:list bool,
|
|
(length bag) > 0 -> odd (count bag false) -> proc bag = [false].
|
|
Proof.
|
|
intros.
|
|
induction bag.
|
|
- simpl.
|
|
inversion H.
|
|
- induction a.
|
|
* intuition.
|
|
* apply IHbag in H.
|