https://gitlab.com/nomadic-labs/mi-cho-coq
Revision d116674d67eca4b4b15d7c08efd878a8c8aa864b authored by kristinas on 11 May 2021, 09:41:59 UTC, committed by kristinas on 11 May 2021, 09:41:59 UTC
1 parent f70493e
Raw File
Tip revision: d116674d67eca4b4b15d7c08efd878a8c8aa864b authored by kristinas on 11 May 2021, 09:41:59 UTC
Added README
Tip revision: d116674
utils.v
(* Open Source License *)
(* Copyright (c) 2021 Nomadic Labs. <contact@nomadic-labs.com> *)

(* Permission is hereby granted, free of charge, to any person obtaining a *)
(* copy of this software and associated documentation files (the "Software"), *)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)
(* and/or sell copies of the Software, and to permit persons to whom the *)
(* Software is furnished to do so, subject to the following conditions: *)

(* The above copyright notice and this permission notice shall be included *)
(* in all copies or substantial portions of the Software. *)

(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
(* DEALINGS IN THE SOFTWARE. *)

Require Import String.
Require Import NArith.
Require Import ZArith.
Require Import Bool.
Require Import List.
Require Import Sorted.
Require Import syntax.
Require Import semantics.
Require Import macros.
Require Import comparable.
Require Import error.

Open Scope string_scope.

Notation cset_empty t
  := (set.empty (comparable_data t) (compare t)).

Notation cset_set t
  := (set.set (comparable_data t) (compare t)).

Notation cset_mem
  := (set.mem (comparable_data _) (compare _)).

Notation cset_insert t
  := (set.insert (comparable_data t) (compare t) (compare_eq_iff t) (lt_trans t) (gt_trans t)).

Notation cset_extensionality t
  := (set.extensionality (comparable_data t) (compare t) (compare_eq_iff t) (lt_trans t) (gt_trans t)).

Notation cset_not_in_sorted
  := (set.not_in_sorted (comparable_data _) (compare _) (compare_eq_iff _)).

Notation cset_list_in_sorted_correct t
  := (set.list_in_sorted_correct (comparable_data t) (compare t) (compare_eq_iff t) (lt_trans t)).

Notation cmap_empty t
  := (map.empty (comparable_data t) _ (compare t)).

Notation cmap_get t
  := (map.get (comparable_data t) _ (compare t)).

Notation cmap_get_def t k v0 m
  := match cmap_get t k m with
       | Some v => v
       | None => v0
     end.

Notation cmap_update t k v
  := (map.update (comparable_data t) _ (compare t) (compare_eq_iff t) (lt_trans t) (gt_trans t) k v).

Notation compare_equal x y
  := (comparison_to_int (compare _ x y) =? 0)%Z.

Lemma Is_true_iff_eq_true b : b = true <-> b.
Proof.
  destruct b; cbn.
  all: split; auto; discriminate.
Qed.

Section Sets.

  Variable A : Set.
  Variable compare : A -> A -> comparison.

  Let lt (a b : A) : Prop := compare a b = Lt.
  Let gt (a b : A) : Prop := compare a b = Gt.

  Hypothesis compare_eq_iff : forall a b : A, compare a b = Eq <-> a = b.
  Hypothesis lt_trans : Relations_1.Transitive lt.
  Hypothesis gt_trans : Relations_1.Transitive gt.

  Definition filter_StronglySorted l (R : A -> A -> Prop) (P : A -> Datatypes.bool) :
    StronglySorted R l -> StronglySorted R (filter P l).
  Proof.
    intro Hl.
    induction Hl as [ | a l Hl IHl H]; cbn.
    - apply SSorted_nil.
    - destruct (P a); auto.
      apply SSorted_cons; auto.
      clear Hl IHl.
      induction H as [ | b l Hb Hl IHl]; cbn.
      + auto.
      + destruct (P b); auto.
  Qed.

  Definition set_subset (s : set.set A compare) (pred : A -> Datatypes.bool)
    : set.set A compare.
  Proof.
    destruct s as [l Hl].
    split with (filter pred l).
    apply filter_StronglySorted; auto.
  Defined.

 Lemma set_mem_subset s P a :
    set.mem A compare a (set_subset s P) <->
    set.mem A compare a s /\ P a.
  Proof.
    destruct s as [l Hl]; cbn.
    rewrite !set.list_in_sorted_correct; auto.
    2: apply filter_StronglySorted; auto.
    rewrite filter_In, Is_true_iff_eq_true.
    split; auto.
  Qed.

  Lemma set_mem_insert_eq a s :
    set.mem A compare a (set.insert A compare compare_eq_iff lt_trans gt_trans a s) = true.
  Proof.
    destruct s as [l Hl]; cbn.
    induction Hl as [ | b l _ IHl _]; cbn.
    - rewrite (set.compare_refl A compare compare_eq_iff a); auto.
    - destruct compare eqn:H_a_b; cbn.
      all: rewrite H_a_b; auto.
      rewrite (set.compare_refl A compare compare_eq_iff a); auto.
  Qed.

  Lemma set_mem_insert_neq a b s :
    a <> b ->
    set.mem A compare a (set.insert A compare compare_eq_iff lt_trans gt_trans b s) =
    set.mem A compare a s.
  Proof.
    intro H.
    destruct s as [l Hl]; cbn.
    induction Hl as [ | c l _ IHl _]; cbn.
    - destruct compare eqn:H_a_b; auto.
      rewrite compare_eq_iff in H_a_b.
      intuition.
    - destruct compare eqn:H_b_c; cbn.
      + auto.
      + destruct compare eqn:H_a_b at 1.
        * rewrite compare_eq_iff in H_a_b.
          intuition.
        * rewrite (lt_trans _ _ _ H_a_b H_b_c); auto.
        * destruct compare at 1 2; auto.
      + destruct compare at 1 2; auto.
  Qed.

End Sets.

Notation cset_subset
  := (set_subset (comparable_data _) (compare _)).

Notation cset_mem_subset t
  := (set_mem_subset (comparable_data t) (compare t) (compare_eq_iff t) (lt_trans t)).

Notation cset_mem_insert_eq t
  := (set_mem_insert_eq (comparable_data t) (compare t) (compare_eq_iff t) (lt_trans t) (gt_trans t)).

Notation cset_mem_insert_neq t
  := (set_mem_insert_neq (comparable_data t) (compare t) (compare_eq_iff t) (lt_trans t) (gt_trans t)).

Section Maps.

  Variable A : Set.
  Variable B : Set.
  Variable compare : A -> A -> comparison.

  Let lt (a b : A) : Prop := compare a b = Lt.
  Let gt (a b : A) : Prop := compare a b = Gt.

  Hypothesis compare_eq_iff : forall a b, compare a b = Eq <-> a = b.
  Hypothesis lt_trans : Relations_1.Transitive lt.
  Hypothesis gt_trans : Relations_1.Transitive gt.

  Lemma map_get_update_eq k m v
    : map.get A B compare k (map.update A B compare compare_eq_iff lt_trans gt_trans k v m) = v.
  Proof.
    destruct v as [v | ]; cbn.
    - apply map.get_insert.
    - apply map.remove_present_same.
  Qed.

  Lemma map_get_update_neq k k' v' m
    : k <> k' ->
      map.get A B compare k (map.update A B compare compare_eq_iff lt_trans gt_trans k' v' m) =
      map.get A B compare k m.
  Proof.
    intro Hk.
    destruct v' as [v' | ]; cbn.
    - apply map.get_insert_other; auto.
    - destruct (map.get _ _ _ _ m) as [v | ] eqn:eq.
      + apply map.remove_present_untouched; auto.
      + apply map.remove_absent_other; auto.
  Qed.

  Lemma map_mem_update_eq_true k m v
    : map.mem A B compare k (map.update A B compare compare_eq_iff lt_trans gt_trans k (Some v) m) = true.
  Proof.
    rewrite map.get_mem_true.
    split with v.
    rewrite map.map_updateeq; auto.
  Qed.

  Lemma map_mem_update_eq_false k m
    : map.mem A B compare k (map.update A B compare compare_eq_iff lt_trans gt_trans k None m) = false.
  Proof.
    rewrite map.get_mem_false.
    rewrite map_get_update_eq; auto.
  Qed.

  Lemma map_mem_update_neq k k' v' m
    : k <> k' ->
      map.mem A B compare k (map.update A B compare compare_eq_iff lt_trans gt_trans k' v' m) =
      map.mem A B compare k m.
  Proof.
    intro Hk.
    destruct map.mem eqn:eq.
    all: symmetry.
    all: rewrite ?map.get_mem_true, ?map.get_mem_false.
    all: rewrite ?map.get_mem_true, ?map.get_mem_false in eq.
    all: rewrite map_get_update_neq in eq; auto.
  Qed.

  Lemma map_update_update_eq k v v' m
    : map.update A B compare compare_eq_iff lt_trans gt_trans k v (map.update A B compare compare_eq_iff lt_trans gt_trans k v' m) =
      map.update A B compare compare_eq_iff lt_trans gt_trans k v m.
  Proof.
    apply map.extensionality; auto.
    intro k'.
    destruct (map.eq_dec A compare compare_eq_iff k k') as [Hk | Hk].
    1: subst; rewrite !map_get_update_eq; auto.
    all: rewrite !map_get_update_neq; auto.
  Qed.

  Lemma map_update_update_neq k k' v v' m
    : k <> k' ->
      map.update A B compare compare_eq_iff lt_trans gt_trans k' v' (map.update A B compare compare_eq_iff lt_trans gt_trans k v m) =
      map.update A B compare compare_eq_iff lt_trans gt_trans k v (map.update A B compare compare_eq_iff lt_trans gt_trans k' v' m).
  Proof.
    intro H_k_k'.
    apply map.extensionality; auto.
    intro k''.
    destruct (map.eq_dec A compare compare_eq_iff k' k'') as [H_k'_k'' | H_k'_k''].
    all: destruct (map.eq_dec A compare compare_eq_iff k k'') as [H_k_k'' | H_k_k''].
    all: subst.
    1: intuition.
    3: rewrite !map_get_update_neq; auto.
    all: rewrite map_get_update_eq; auto.
    all: rewrite map_get_update_neq; auto.
    all: rewrite map_get_update_eq; auto.
  Qed.

End Maps.

Lemma comparison_le_gt_Z x y
  : (x <=? y)%Z = false <-> (x >? y)%Z = true.
Proof.
  unfold Z.leb, Z.gtb; destruct (x ?= y)%Z.
  all: intuition.
Qed.

Lemma comparison_lt_ge_Z x y
  : (x <? y)%Z = false <-> (x >=? y)%Z = true.
Proof.
  unfold Z.ltb, Z.geb; destruct (x ?= y)%Z.
  all: intuition.
Qed.

Lemma comparison_ge_lt_Z x y
  : (x >=? y)%Z = false <-> (x <? y)%Z = true.
Proof.
  unfold Z.geb, Z.ltb; destruct (x ?= y)%Z.
  all: intuition.
Qed.

Lemma comparison_gt_le_Z x y
  : (x >? y)%Z = false <-> (x <=? y)%Z = true.
Proof.
  unfold Z.gtb, Z.leb; destruct (x ?= y)%Z.
  all: intuition.
Qed.

Lemma comparison_to_int_leq_N x y
  : (comparison_to_int (compare nat x y) <=? 0)%Z = true <-> (x <= y)%N.
Proof.
  unfold N.le; cbn; destruct (x ?= y)%N; simpl.
  all: split; auto; discriminate.
Qed.

Lemma comparison_to_int_geq_N x y
  : (comparison_to_int (compare nat x y) >=? 0)%Z = true <-> (x >= y)%N.
Proof.
  unfold N.ge; cbn; destruct (x ?= y)%N; simpl.
  all: split; auto; discriminate.
Qed.

Lemma comparison_to_int_lt_N x y
  : (comparison_to_int (compare nat x y) <? 0)%Z = true <-> (x < y)%N.
Proof.
  unfold N.lt; cbn; destruct (x ?= y)%N; simpl.
  all: split; auto; discriminate.
Qed.

Lemma comparison_to_int_gt_N x y
  : (comparison_to_int (compare nat x y) >? 0)%Z = true <-> (x > y)%N.
Proof.
  unfold N.gt; cbn; destruct (x ?= y)%N; simpl.
  all: split; auto; discriminate.
Qed.

Lemma comparison_to_int_leq_Z x y
  : (comparison_to_int (compare int x y) <=? 0)%Z = true <-> (x <= y)%Z.
Proof.
  unfold Z.le; cbn; destruct (x ?= y)%Z; simpl.
  all: split; auto; discriminate.
Qed.

Lemma comparison_to_int_geq_Z x y
  : (comparison_to_int (compare int x y) >=? 0)%Z = true <-> (x >= y)%Z.
Proof.
  unfold Z.ge; cbn; destruct (x ?= y)%Z; simpl.
  all: split; auto; discriminate.
Qed.

Lemma comparison_to_int_lt_Z x y
  : (comparison_to_int (compare int x y) <? 0)%Z = true <-> (x < y)%Z.
Proof.
  unfold Z.lt; cbn; destruct (x ?= y)%Z; simpl.
  all: split; auto; discriminate.
Qed.

Lemma comparison_to_int_gt_Z x y
  : (comparison_to_int (compare int x y) >? 0)%Z = true <-> (x > y)%Z.
Proof.
  unfold Z.gt; cbn; destruct (x ?= y)%Z; simpl.
  all: split; auto; discriminate.
Qed.

Lemma eqb_neq {t} (x y : comparable_data t)
  : compare_equal x y = false <-> x <> y.
Proof.
  rewrite<- compare_eq_iff.
  destruct (compare t x y).
  all: intuition; discriminate.
Qed.
back to top