Lvc.Infra.OptionMap

Require Import List Option Get.

Section ParametricOptionMapIndex.
  Variables X Y : Type.
  Hypothesis f : natXoption Y : Type.

  Fixpoint omapi_impl (n:nat) (L:list X) : option (list Y) :=
    match L with
      | x::L
        mdo v <- f n x;
          mdo vl <- omapi_impl (S n) L;
          Some (v::vl)
      | _Some nil
    end.

  Definition omapi := omapi_impl 0.

End ParametricOptionMapIndex.


Section ParametricOptionMap.
  Variables X Y : Type.
  Hypothesis f : Xoption Y : Type.

  Fixpoint omap (L:list X) : option (list Y) :=
    match L with
      | x::L
        mdo v <- f x;
          mdo vl <- omap L;
          Some (v::vl)
      | _Some nil
    end.

  Lemma omap_inversion (L:list X) (:list Y)
  : omap L = Some
    → n x, get L n x{ y : Y | get n y f x = Some y }.

End ParametricOptionMap.


Lemma omap_agree X Y (f g: Xoption Y) L
: ( n x, get L n xf x = g x)
  → omap f L = omap g L.

Lemma omap_agree_2 X Y (f: Xoption Y) (g: option Y) L
: ( n x y, get L n xget n yf x = g y)
  → length L = length
  → omap f L = omap g .

Lemma omap_length X Y (f: Xoption Y) L
: omap f L = Some
  → length L = length .

Lemma omap_app X Y (f:Xoption Y) L
: omap f (L ++ ) =
  mdo v <- omap f L;
  mdo <- omap f ;
  Some (v ++ ).