@@ -4,6 +4,8 @@ Require Import ExtLib.Data.POption.
44Require Import ExtLib.Core.RelDec.
55Require Import ExtLib.Tactics.Consider.
66
7+ Require Import Coq.Bool.Bool.
8+
79Set Universe Polymorphism.
810
911Section plist.
@@ -44,6 +46,17 @@ Section plist.
4446 | pcons b m => b = a \/ pIn a m
4547 end .
4648
49+ Polymorphic Inductive pNoDup : plist -> Prop :=
50+ pNoDup_nil : pNoDup pnil
51+ | pNoDup_cons : forall (x : T) (l : plist),
52+ ~ pIn x l -> pNoDup l -> pNoDup (pcons x l).
53+
54+ Polymorphic Fixpoint inb {RelDecA : RelDec (@eq T)} (x : T) (lst : plist) :=
55+ match lst with
56+ | pnil => false
57+ | pcons l ls => if x ?[ eq ] l then true else inb x ls
58+ end .
59+
4760 Polymorphic Fixpoint anyb (p : T -> bool) (ls : plist) : bool :=
4861 match ls with
4962 | pnil => false
@@ -56,6 +69,12 @@ Section plist.
5669 | pcons l ls0 => if p l then anyb p ls0 else false
5770 end .
5871
72+ Polymorphic Fixpoint nodup {RelDecA : RelDec (@eq T)} (lst : plist) :=
73+ match lst with
74+ | pnil => true
75+ | pcons l ls => andb (negb (inb l ls)) (nodup ls)
76+ end .
77+
5978 Polymorphic Fixpoint nth_error (ls : plist) (n : nat) : poption T :=
6079 match n , ls with
6180 | 0 , pcons l _ => pSome l
@@ -86,12 +105,57 @@ End plist.
86105Arguments pnil {_}.
87106Arguments pcons {_} _ _.
88107Arguments pIn {_} _ _.
108+ Arguments pNoDup {_} _.
89109Arguments anyb {_} _ _.
90110Arguments allb {_} _ _.
111+ Arguments inb {_ _} _ _.
112+ Arguments nodup {_ _} _.
91113Arguments fold_left {_ _} _ _ _.
92114Arguments fold_right {_ _} _ _ _.
93115Arguments nth_error {_} _ _.
94116
117+
118+ Section plistOk.
119+ Context {A : Type}.
120+ Context {RelDecA : RelDec (@eq A)}.
121+ Context {RelDecA_Correct : RelDec_Correct RelDecA}.
122+
123+ Lemma inb_sound (x : A) (lst : plist A) (H : inb x lst = true) : pIn x lst.
124+ Proof .
125+ induction lst; simpl in *; try congruence.
126+ consider (x ?[ eq ] t); intros; subst.
127+ + left; reflexivity.
128+ + right; apply IHlst; assumption.
129+ Qed .
130+
131+ Lemma inb_complete (x : A) (lst : plist A) (H : pIn x lst) : inb x lst = true.
132+ Proof .
133+ induction lst; simpl in *; try intuition congruence.
134+ consider (x ?[ eq ] t); intros; destruct H as [H | H]; try congruence.
135+ apply IHlst; assumption.
136+ Qed .
137+
138+ Lemma nodup_sound (lst : plist A) (H : nodup lst = true) : pNoDup lst.
139+ Proof .
140+ induction lst.
141+ + constructor.
142+ + simpl in *. rewrite andb_true_iff in H; destruct H as [H1 H2].
143+ rewrite negb_true_iff in H1. constructor.
144+ * intro H. apply inb_complete in H. intuition congruence.
145+ * apply IHlst; assumption.
146+ Qed .
147+
148+ Lemma nodup_complete (lst : plist A) (H : pNoDup lst) : nodup lst = true.
149+ Proof .
150+ induction lst.
151+ + constructor.
152+ + simpl in *. rewrite andb_true_iff. inversion H; subst; split; clear H.
153+ * apply eq_true_not_negb. intros H; apply H2. apply inb_sound; assumption.
154+ * apply IHlst; assumption.
155+ Qed .
156+
157+ End plistOk.
158+
95159Section pmap.
96160 Polymorphic Universe i j.
97161 Context {T : Type@{i}}.
0 commit comments