nmap.v 3.33 KB
Newer Older
1
(* Copyright (c) 2012-2019, Coq-std++ developers. *)
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]. *)
5 6
From stdpp Require Import pmap mapset.
From stdpp Require Export prelude fin_maps.
7
Set Default Proof Using "Type".
Robbert Krebbers's avatar
Robbert Krebbers committed
8 9 10

Local Open Scope N_scope.

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

16
Instance Nmap_eq_dec `{EqDecision A} : EqDecision (Nmap A).
17
Proof.
18
 refine (λ t1 t2,
19
  match t1, t2 with
20
  | NMap x t1, NMap y t2 => cast_if_and (decide (x = y)) (decide (t1 = t2))
21
  end); abstract congruence.
22
Defined.
23
Instance Nempty {A} : Empty (Nmap A) := NMap None .
24
Global Opaque Nempty.
25
Instance Nlookup {A} : Lookup N A (Nmap A) := λ i t,
Robbert Krebbers's avatar
Robbert Krebbers committed
26 27 28 29
  match i with
  | N0 => Nmap_0 t
  | Npos p => Nmap_pos t !! p
  end.
30
Instance Npartial_alter {A} : PartialAlter N A (Nmap A) := λ f i t,
Robbert Krebbers's avatar
Robbert Krebbers committed
31
  match i, t with
32 33
  | 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
34
  end.
35
Instance Nto_list {A} : FinMapToList N A (Nmap A) := λ t,
Robbert Krebbers's avatar
Robbert Krebbers committed
36
  match t with
37
  | NMap o t =>
38
     from_option (λ x, [(0,x)]) [] o ++ (prod_map Npos id <$> map_to_list t)
Robbert Krebbers's avatar
Robbert Krebbers committed
39
  end.
40 41
Instance Nomap: OMap Nmap := λ A B f t,
  match t with NMap o t => NMap (o = f) (omap f t) end.
42
Instance Nmerge: Merge Nmap := λ A B C f t1 t2,
Robbert Krebbers's avatar
Robbert Krebbers committed
43
  match t1, t2 with
44
  | NMap o1 t1, NMap o2 t2 => NMap (f o1 o2) (merge f t1 t2)
Robbert Krebbers's avatar
Robbert Krebbers committed
45
  end.
46
Instance Nfmap: FMap Nmap := λ A B f t,
47
  match t with NMap o t => NMap (f <$> o) (f <$> t) end.
Robbert Krebbers's avatar
Robbert Krebbers committed
48

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

82 83
(** * Finite sets *)
(** We construct sets of [N]s satisfying extensional equality. *)
84
Notation Nset := (mapset Nmap).
85
Instance Nmap_dom {A} : Dom (Nmap A) Nset := mapset_dom.
86
Instance: FinMapDom N Nmap Nset := mapset_dom_spec.