nmap.v 2.83 KB
Newer Older
1 2 3 4
(* Copyright (c) 2012, Robbert Krebbers. *)
(* 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
Instance Pmap_dec `{ x y : A, Decision (x = y)} :
16
   x y : Nmap A, Decision (x = y).
Robbert Krebbers's avatar
Robbert Krebbers committed
17
Proof. solve_decision. Defined.
18

19
Instance Nempty {A} : Empty (Nmap A) := NMap None .
Robbert Krebbers's avatar
Robbert Krebbers committed
20
Instance Nlookup {A} : Lookup N A (Nmap A) := λ i t,
Robbert Krebbers's avatar
Robbert Krebbers committed
21 22 23 24
  match i with
  | N0 => Nmap_0 t
  | Npos p => Nmap_pos t !! p
  end.
Robbert Krebbers's avatar
Robbert Krebbers committed
25
Instance Npartial_alter {A} : PartialAlter N A (Nmap A) := λ f i t,
Robbert Krebbers's avatar
Robbert Krebbers committed
26
  match i, t with
27 28
  | 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
29
  end.
Robbert Krebbers's avatar
Robbert Krebbers committed
30
Instance Nto_list {A} : FinMapToList N A (Nmap A) := λ t,
Robbert Krebbers's avatar
Robbert Krebbers committed
31
  match t with
Robbert Krebbers's avatar
Robbert Krebbers committed
32 33
  | NMap o t => option_case (λ x, [(0,x)]) [] o ++
     (fst_map Npos <$> finmap_to_list t)
Robbert Krebbers's avatar
Robbert Krebbers committed
34
  end.
Robbert Krebbers's avatar
Robbert Krebbers committed
35
Instance Nmerge {A} : Merge A (Nmap A) := λ f t1 t2,
Robbert Krebbers's avatar
Robbert Krebbers committed
36
  match t1, t2 with
37
  | NMap o1 t1, NMap o2 t2 => NMap (f o1 o2) (merge f t1 t2)
Robbert Krebbers's avatar
Robbert Krebbers committed
38
  end.
39
Instance Nfmap: FMap Nmap := λ A B f t,
Robbert Krebbers's avatar
Robbert Krebbers committed
40
  match t with
41
  | NMap o t => NMap (fmap f o) (fmap f t)
Robbert Krebbers's avatar
Robbert Krebbers committed
42 43
  end.

44
Instance: FinMap N Nmap.
Robbert Krebbers's avatar
Robbert Krebbers committed
45 46
Proof.
  split.
47
  * intros ? [??] [??] H. f_equal.
48 49 50 51 52 53 54 55 56
    + apply (H 0).
    + apply finmap_eq. intros i. apply (H (Npos i)).
  * 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.
Robbert Krebbers's avatar
Robbert Krebbers committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  * intros ? [[x|] t]; unfold finmap_to_list; simpl.
    + constructor.
      - rewrite elem_of_list_fmap. by intros [[??] [??]].
      - rewrite (NoDup_fmap _). apply finmap_to_list_nodup.
    + rewrite (NoDup_fmap _). apply finmap_to_list_nodup.
  * intros ? t i x. unfold finmap_to_list. split.
    + destruct t as [[y|] t]; simpl.
      - rewrite elem_of_cons, elem_of_list_fmap.
        intros [? | [[??] [??]]]; simplify_equality; simpl; [done |].
        by apply elem_of_finmap_to_list.
      - rewrite elem_of_list_fmap.
        intros [[??] [??]]; simplify_equality; simpl.
        by apply elem_of_finmap_to_list.
    + destruct t as [[y|] t]; simpl.
      - rewrite elem_of_cons, elem_of_list_fmap.
        destruct i as [|i]; simpl; [intuition congruence |].
        intros. right. exists (i, x). by rewrite elem_of_finmap_to_list.
      - rewrite elem_of_list_fmap.
        destruct i as [|i]; simpl; [done |].
        intros. exists (i, x). by rewrite elem_of_finmap_to_list.
77 78
  * intros ? f ? [o1 t1] [o2 t2] [|?]; simpl.
    + done.
79
    + apply (merge_spec f t1 t2).
Robbert Krebbers's avatar
Robbert Krebbers committed
80
Qed.