Module Extra

Require Export Util.
Import Utf8 Coqlib.

Definition list_exists {A} (f: Abool) : list Abool :=
  fix list_exists m :=
    match m with
    | nil => false
    | a :: m' => if f a then true else list_exists m'
    end.

Lemma list_exists_forall {A} f m :
  list_exists f m = false
  ∀ a : A, In a mf a = false.
Proof.
  elim m; clear m.
  intros _ ? ().
  intros a m IH. simpl. destruct (f _) eqn: Ha.
  intros; eq_some_inv.
  intros H; specialize (IH H); clear H.
  intros a' [ -> | H ]; auto.
Qed.

Lemma list_norepet_cons {A} (a: A) (m: list A) :
  list_norepet (a :: m) →
  ¬ In a mlist_norepet m.
Proof.
  intros H.
  set (Δ (n: list A) :=
         match n with nil => True | a :: m => ¬ In a mlist_norepet m end).
  change (Δ (a :: m)).
  case H. exact I.
  intros hd tl. apply conj.
Qed.

Definition map_o {X Y} (f: Xoption Y) : list Xoption (list Y) :=
  fix map_o xs :=
    match xs with
    | nil => Some nil
    | x :: xs' => do y <- f x; do ys <- map_o xs'; Some (y :: ys)
    end.