aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/Maps.v
blob: 7a45259eec2ea64a70b40b30bd4f95bba7911009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
From vericert Require Import Vericertlib.

From compcert Require Export Maps.
From compcert Require Import Errors.
Import PTree.

Set Implicit Arguments.

Local Open Scope error_monad_scope.

(** Instance of traverse for [PTree] and [Errors]. This should maybe be generalised
    in the future. *)

Module PTree.

Fixpoint xtraverse (A B : Type) (f : positive -> A -> res B) (m : PTree.t A) (i : positive)
         {struct m} : res (PTree.t B) :=
  match m with
  | Leaf => OK Leaf
  | Node l o r =>
    let newo :=
        match o with
        | None => OK None
        | Some x =>
          match f (prev i) x with
          | Error err => Error err
          | OK val => OK (Some val)
          end
        end in
    match newo with
    | OK no =>
      do nl <- xtraverse f l (xO i);
      do nr <- xtraverse f r (xI i);
      OK (Node nl no nr)
    | Error msg => Error msg
    end
  end.

Definition traverse (A B : Type) (f : positive -> A -> res B) m := xtraverse f m xH.

Definition traverse1 (A B : Type) (f : A -> res B) := traverse (fun _ => f).

Definition filter (A: Type) (pred: PTree.elt -> A -> bool) (m: t A) : t A :=
  PTree.map (fun _ a => snd a) (PTree.filter1 (fun a => pred (fst a) (snd a)) (PTree.map (fun i x => (i, x)) m)).

Theorem filter_spec: forall (A: Type) (pred: PTree.elt -> A -> bool) (m: PTree.t A) (i: PTree.elt) (x : A),
    (filter pred m) ! i =
    match m ! i with
    | None => None
    | Some x => if pred i x then Some x else None
    end.
Proof.
  intros.
  unfold filter.

  rewrite gmap.
  unfold option_map.

  rewrite gfilter1.

  rewrite gmap.
  unfold option_map.

  destruct (m ! i).
  - simpl.
    destruct (pred i a); simpl; reflexivity.
  - reflexivity.
Qed.

End PTree.