nmap.v 2.93 KB
Newer Older
1
(* Copyright (c) 2012-2013, Robbert Krebbers. *)
2 3 4
(* This file is distributed under the terms of the BSD license. *)
(** This files extends the implementation of finite over [positive] to finite
maps whose keys range over Coq's data type of binary naturals [N]. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
5 6 7 8 9
Require Import pmap.
Require Export prelude fin_maps.

Local Open Scope N_scope.

10
Record Nmap A := NMap { Nmap_0 : option A; Nmap_pos : Pmap A }.
Robbert Krebbers's avatar
Robbert Krebbers committed
11 12
Arguments Nmap_0 {_} _.
Arguments Nmap_pos {_} _.
13
Arguments NMap {_} _ _.
Robbert Krebbers's avatar
Robbert Krebbers committed
14

15 16 17 18 19 20 21 22 23
Instance Nmap_eq_dec `{ x y : A, Decision (x = y)} (t1 t2 : Nmap A) :
  Decision (t1 = t2).
Proof.
 refine
  match t1, t2 with
  | NMap x t1, NMap y t2 =>
    cast_if_and (decide (x = y)) (decide (t1 = t2))
  end; abstract congruence.
Defined.
24

25
Instance Nempty {A} : Empty (Nmap A) := NMap None .
Robbert Krebbers's avatar
Robbert Krebbers committed
26
Instance Nlookup {A} : Lookup N A (Nmap A) := λ i t,
Robbert Krebbers's avatar
Robbert Krebbers committed
27 28 29 30
  match i with
  | N0 => Nmap_0 t
  | Npos p => Nmap_pos t !! p
  end.
Robbert Krebbers's avatar
Robbert Krebbers committed
31
Instance Npartial_alter {A} : PartialAlter N A (Nmap A) := λ f i t,
Robbert Krebbers's avatar
Robbert Krebbers committed
32
  match i, t with
33 34
  | N0, NMap o t => NMap (f o) t
  | Npos p, NMap o t => NMap o (partial_alter f p t)
Robbert Krebbers's avatar
Robbert Krebbers committed
35
  end.
Robbert Krebbers's avatar
Robbert Krebbers committed
36
Instance Nto_list {A} : FinMapToList N A (Nmap A) := λ t,
Robbert Krebbers's avatar
Robbert Krebbers committed
37
  match t with
Robbert Krebbers's avatar
Robbert Krebbers committed
38
  | NMap o t => option_case (λ x, [(0,x)]) [] o ++
39
     (fst_map Npos <$> map_to_list t)
Robbert Krebbers's avatar
Robbert Krebbers committed
40
  end.
41
Instance Nmerge: Merge Nmap := λ A B C f t1 t2,
Robbert Krebbers's avatar
Robbert Krebbers committed
42
  match t1, t2 with
43
  | NMap o1 t1, NMap o2 t2 => NMap (f o1 o2) (merge f t1 t2)
Robbert Krebbers's avatar
Robbert Krebbers committed
44
  end.
45
Instance Nfmap: FMap Nmap := λ A B f t,
Robbert Krebbers's avatar
Robbert Krebbers committed
46
  match t with
47
  | NMap o t => NMap (fmap f o) (fmap f t)
Robbert Krebbers's avatar
Robbert Krebbers committed
48 49
  end.

50
Instance: FinMap N Nmap.
Robbert Krebbers's avatar
Robbert Krebbers committed
51 52
Proof.
  split.
53
  * intros ? [??] [??] H. f_equal.
54
    + apply (H 0).
55
    + apply map_eq. intros i. apply (H (Npos i)).
56 57 58 59 60 61 62
  * by intros ? [|?].
  * intros ? f [? t] [|i]; simpl.
    + done.
    + apply lookup_partial_alter.
  * intros ? f [? t] [|i] [|j]; simpl; try intuition congruence.
    intros. apply lookup_partial_alter_ne. congruence.
  * intros ??? [??] []; simpl. done. apply lookup_fmap.
63
  * intros ? [[x|] t]; unfold map_to_list; simpl.
Robbert Krebbers's avatar
Robbert Krebbers committed
64 65
    + constructor.
      - rewrite elem_of_list_fmap. by intros [[??] [??]].
66 67 68
      - rewrite (NoDup_fmap _). apply map_to_list_nodup.
    + rewrite (NoDup_fmap _). apply map_to_list_nodup.
  * intros ? t i x. unfold map_to_list. split.
Robbert Krebbers's avatar
Robbert Krebbers committed
69 70 71
    + destruct t as [[y|] t]; simpl.
      - rewrite elem_of_cons, elem_of_list_fmap.
        intros [? | [[??] [??]]]; simplify_equality; simpl; [done |].
72
        by apply elem_of_map_to_list.
Robbert Krebbers's avatar
Robbert Krebbers committed
73 74
      - rewrite elem_of_list_fmap.
        intros [[??] [??]]; simplify_equality; simpl.
75
        by apply elem_of_map_to_list.
Robbert Krebbers's avatar
Robbert Krebbers committed
76 77 78
    + destruct t as [[y|] t]; simpl.
      - rewrite elem_of_cons, elem_of_list_fmap.
        destruct i as [|i]; simpl; [intuition congruence |].
79
        intros. right. exists (i, x). by rewrite elem_of_map_to_list.
Robbert Krebbers's avatar
Robbert Krebbers committed
80 81
      - rewrite elem_of_list_fmap.
        destruct i as [|i]; simpl; [done |].
82 83
        intros. exists (i, x). by rewrite elem_of_map_to_list.
  * intros ??? f ? [o1 t1] [o2 t2] [|?]; simpl.
84
    + done.
85
    + apply (lookup_merge f t1 t2).
Robbert Krebbers's avatar
Robbert Krebbers committed
86
Qed.