BagPermInsertion Sort With Bags
From Stdlib Require Import Strings.String. (* for manual grading *)
From Stdlib Require Import Setoid Morphisms.
From VFA Require Import Perm.
From VFA Require Import Sort.
From Stdlib Require Import Setoid Morphisms.
From VFA Require Import Perm.
From VFA Require Import Sort.
To keep this chapter more self-contained,
we restate the critical definitions from Lists.
Definition bag := list nat.
Fixpoint count (v:nat) (s:bag) : nat :=
match s with
| nil ⇒ 0
| h :: t ⇒
(if h =? v then 1 else 0) + count v t
end.
Fixpoint count (v:nat) (s:bag) : nat :=
match s with
| nil ⇒ 0
| h :: t ⇒
(if h =? v then 1 else 0) + count v t
end.
We will say two bags are equivalent if they have the same number
of copies of every possible element.
(* It is easy to prove bag_eqv is an equivalence relation. *)
Lemma bag_eqv_refl : ∀ b, bag_eqv b b.
Proof.
(* 在这里填写 *) Admitted.
Lemma bag_eqv_sym: ∀ b1 b2, bag_eqv b1 b2 → bag_eqv b2 b1.
Proof.
(* 在这里填写 *) Admitted.
Lemma bag_eqv_trans: ∀ b1 b2 b3, bag_eqv b1 b2 → bag_eqv b2 b3 → bag_eqv b1 b3.
Proof.
(* 在这里填写 *) Admitted.
Lemma bag_eqv_refl : ∀ b, bag_eqv b b.
Proof.
(* 在这里填写 *) Admitted.
Lemma bag_eqv_sym: ∀ b1 b2, bag_eqv b1 b2 → bag_eqv b2 b1.
Proof.
(* 在这里填写 *) Admitted.
Lemma bag_eqv_trans: ∀ b1 b2 b3, bag_eqv b1 b2 → bag_eqv b2 b3 → bag_eqv b1 b3.
Proof.
(* 在这里填写 *) Admitted.
The following little lemma is handy in a couple of places.
Lemma bag_eqv_cons : ∀ x b1 b2, bag_eqv b1 b2 → bag_eqv (x::b1) (x::b2).
Proof.
(* 在这里填写 *) Admitted.
☐
Proof.
(* 在这里填写 *) Admitted.
☐
Correctness
Definition is_a_sorting_algorithm' (f: list nat → list nat) :=
∀ al, bag_eqv al (f al) ∧ sorted (f al).
∀ al, bag_eqv al (f al) ∧ sorted (f al).
练习: 3 stars,标准 (insert_bag)
First, prove the auxiliary lemma insert_bag, which will be useful for proving sort_bag below. Your proof will be by induction.
Theorem insertion_sort_correct:
is_a_sorting_algorithm' sort.
Proof.
split. apply sort_bag. apply sort_sorted.
Qed.
is_a_sorting_algorithm' sort.
Proof.
split. apply sort_bag. apply sort_sorted.
Qed.
练习: 1 star,标准 (permutations_vs_multiset)
Compare your proofs of insert_perm, sort_perm with your proofs of insert_bag, sort_bag. Which proofs are simpler?- easier with permutations,
- easier with multisets
- about the same.
- permutations or
- multisets
(* 不要修改下面这行: *)
Definition manual_grade_for_permutations_vs_multiset : option (nat×string) := None.
☐
Definition manual_grade_for_permutations_vs_multiset : option (nat×string) := None.
☐
Permutations and Multisets
练习: 3 stars,标准 (perm_bag)
The forward direction is straighforward, by induction on the evidence for Permutation:练习: 2 stars,高级 (bag_nil_inv)
Lemma bag_cons_inv : ∀ l x n,
S n = count x l →
∃ l1 l2,
l = l1 ++ x :: l2
∧ count x (l1 ++ l2) = n.
Proof.
(* 在这里填写 *) Admitted.
☐
S n = count x l →
∃ l1 l2,
l = l1 ++ x :: l2
∧ count x (l1 ++ l2) = n.
Proof.
(* 在这里填写 *) Admitted.
☐
Lemma count_insert_other : ∀ l1 l2 x y,
y ≠ x → count y (l1 ++ x :: l2) = count y (l1 ++ l2).
Proof.
(* 在这里填写 *) Admitted.
☐
y ≠ x → count y (l1 ++ x :: l2) = count y (l1 ++ l2).
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem bag_eqv_iff_perm:
∀ al bl, bag_eqv al bl ↔ Permutation al bl.
Proof.
intros. split. apply bag_perm. apply perm_bag.
Qed.
∀ al bl, bag_eqv al bl ↔ Permutation al bl.
Proof.
intros. split. apply bag_perm. apply perm_bag.
Qed.
Therefore, it doesn't matter whether you prove your sorting
algorithm using the Permutations method or the multiset method.
Corollary sort_specifications_equivalent:
∀ sort, is_a_sorting_algorithm sort ↔ is_a_sorting_algorithm' sort.
Proof.
unfold is_a_sorting_algorithm, is_a_sorting_algorithm'.
split; intros;
destruct (H al); split; auto;
apply bag_eqv_iff_perm; auto.
Qed.
∀ sort, is_a_sorting_algorithm sort ↔ is_a_sorting_algorithm' sort.
Proof.
unfold is_a_sorting_algorithm, is_a_sorting_algorithm'.
split; intros;
destruct (H al); split; auto;
apply bag_eqv_iff_perm; auto.
Qed.
(* 2026-11-26 15:59 *)
