vector.v 4.07 KB
Newer Older
Ralf Jung's avatar
Ralf Jung committed
1
From stdpp Require Export vector.
2 3
From iris.algebra Require Export ofe.
From iris.algebra Require Import list.
4
Set Default Proof Using "Type".
5 6 7 8 9 10 11 12

Section ofe.
  Context {A : ofeT}.

  Instance vec_equiv m : Equiv (vec A m) := equiv (A:=list A).
  Instance vec_dist m : Dist (vec A m) := dist (A:=list A).

  Definition vec_ofe_mixin m : OfeMixin (vec A m).
13
  Proof. by apply (iso_ofe_mixin vec_to_list). Qed.
14
  Canonical Structure vecO m : ofeT := OfeT (vec A m) (vec_ofe_mixin m).
15

16
  Global Instance list_cofe `{Cofe A} m : Cofe (vecO m).
17
  Proof.
18
    apply: (iso_cofe_subtype (λ l : list A, length l = m)
19
      (λ l, eq_rect _ (vec A) (list_to_vec l) m) vec_to_list)=> //.
Ralf Jung's avatar
Ralf Jung committed
20
    - intros v []. by rewrite /= vec_to_list_to_vec.
21
    - intros c. by rewrite (conv_compl 0 (chain_map _ c)) /= vec_to_list_length.
22 23
  Qed.

24
  Global Instance vnil_discrete : Discrete (@vnil A).
25
  Proof. intros v _. by inv_vec v. Qed.
26 27
  Global Instance vcons_discrete n x (v : vec A n) :
    Discrete x  Discrete v  Discrete (x ::: v).
28 29
  Proof.
    intros ?? v' ?. inv_vec v'=>x' v'. inversion_clear 1.
30
    constructor. by apply discrete. change (v  v'). by apply discrete.
31
  Qed.
32
  Global Instance vec_ofe_discrete m : OfeDiscrete A  OfeDiscrete (vecO m).
33 34 35
  Proof. intros ? v. induction v; apply _. Qed.
End ofe.

36
Arguments vecO : clear implicits.
37 38 39 40 41 42
Typeclasses Opaque vec_dist.

Section proper.
  Context {A : ofeT}.

  Global Instance vcons_ne n :
43
    Proper (dist n ==> forall_relation (λ x, dist n ==> dist n)) (@vcons A).
44 45
  Proof. by constructor. Qed.
  Global Instance vcons_proper :
46
    Proper (equiv ==> forall_relation (λ x, equiv ==> equiv)) (@vcons A).
47 48 49 50 51 52
  Proof. by constructor. Qed.

  Global Instance vlookup_ne n m :
    Proper (dist n ==> eq ==> dist n) (@Vector.nth A m).
  Proof.
    intros v. induction v as [|x m v IH]; intros v'; inv_vec v'.
Robbert Krebbers's avatar
Robbert Krebbers committed
53 54
    - intros _ x. inv_fin x.
    - intros x' v' EQ i ? <-. inversion_clear EQ. inv_fin i=> // i. by apply IH.
55 56 57 58
  Qed.
  Global Instance vlookup_proper m :
    Proper (equiv ==> eq ==> equiv) (@Vector.nth A m).
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
59
    intros v v' ? x x' ->. apply equiv_dist=> n. f_equiv. by apply equiv_dist.
60 61
  Qed.

62
  Global Instance vec_to_list_ne m : NonExpansive (@vec_to_list A m).
Robbert Krebbers's avatar
Robbert Krebbers committed
63
  Proof. by intros v v'. Qed.
64
  Global Instance vec_to_list_proper m : Proper (() ==> ()) (@vec_to_list A m).
Robbert Krebbers's avatar
Robbert Krebbers committed
65
  Proof. by intros v v'. Qed.
66 67 68
End proper.

(** Functor *)
69
Definition vec_map {A B : ofeT} m (f : A  B) : vecO A m  vecO B m :=
70 71 72 73 74 75 76 77 78 79 80
  @vmap A B f m.
Lemma vec_map_ext_ne {A B : ofeT} m (f g : A  B) (v : vec A m) n :
  ( x, f x {n} g x)  vec_map m f v {n} vec_map m g v.
Proof.
  intros Hf. eapply (list_fmap_ext_ne f g v) in Hf.
  by rewrite -!vec_to_list_map in Hf.
Qed.
Instance vec_map_ne {A B : ofeT} m f n :
  Proper (dist n ==> dist n) f 
  Proper (dist n ==> dist n) (@vec_map A B m f).
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
81
  intros ? v v' H. eapply list_fmap_ne in H; last done.
82 83
  by rewrite -!vec_to_list_map in H.
Qed.
84 85 86 87
Definition vecO_map {A B : ofeT} m (f : A -n> B) : vecO A m -n> vecO B m :=
  OfeMor (vec_map m f).
Instance vecO_map_ne {A A'} m :
  NonExpansive (@vecO_map A A' m).
88
Proof. intros n f g ? v. by apply vec_map_ext_ne. Qed.
89

90 91 92
Program Definition vecOF (F : oFunctor) m : oFunctor := {|
  oFunctor_car A _ B _ := vecO (oFunctor_car F A B) m;
  oFunctor_map A1 _ A2 _ B1 _ B2 _ fg := vecO_map m (oFunctor_map F fg)
93 94
|}.
Next Obligation.
95
  intros F A1 ? A2 ? B1 ? B2 ? n m f g Hfg. by apply vecO_map_ne, oFunctor_map_ne.
96 97
Qed.
Next Obligation.
98
  intros F m A ? B ? l.
99 100
  change (vec_to_list (vec_map m (oFunctor_map F (cid, cid)) l)  l).
  rewrite vec_to_list_map. apply listOF.
101 102
Qed.
Next Obligation.
103
  intros F m A1 ? A2 ? A3 ? B1 ? B2 ? B3 ? f g f' g' l.
104 105
  change (vec_to_list (vec_map m (oFunctor_map F (f  g, g'  f')) l)
     vec_map m (oFunctor_map F (g, g')) (vec_map m (oFunctor_map F (f, f')) l)).
106
  rewrite !vec_to_list_map. by apply: (oFunctor_map_compose (listOF F) f g f' g').
107 108
Qed.

109 110
Instance vecOF_contractive F m :
  oFunctorContractive F  oFunctorContractive (vecOF F m).
111
Proof.
112
  by intros ?? A1 ? A2 ? B1 ? B2 ? n ???; apply vecO_map_ne; first apply oFunctor_map_contractive.
113
Qed.