Library QedkConcrete.stringmaps

Finite-map laws for concrete accounts and storage

The generated runtime represents maps by sorted association lists. We connect its actual operations to the standard library's proved list-map implementation. Sortedness is a representation invariant, not a premise about EVM behavior; map construction and updates preserve it.
From Stdlib Require Import List Bool ZArith Sorting.Sorted
  Structures.OrderedType FSets.FMapList.
Require Import RocqOfOCaml.RocqOfOCaml.
Set Default Goal Selector "!".

Module StringOrder <: OrderedType.
  Definition t := string.
  Definition eq := @eq string.
  Definition lt := Basics.String.Lt.t.
  Definition eq_refl := @eq_refl string.
  Definition eq_sym := @eq_sym string.
  Definition eq_trans := @eq_trans string.
  Definition lt_trans := Basics.String.Lt.transitivity.
  Lemma lt_not_eq x y : lt x y → ¬ eq x y.
  Proof. intros Hlt Heq. unfold eq in Heq. subst y. exact (@Basics.String.Lt.irreflexivity x Hlt). Qed.
  Definition compare x y : Compare lt eq x y.
  Proof.
    destruct (String.eqb x y) eqn:Heq.
    - apply EQ. apply String.eqb_eq. exact Heq.
    - destruct (Basics.String.ltb_spec x y) as [Hlt|Hlt].
      + apply LT. exact Hlt.
      + apply GT. apply (Bool.reflect_iff _ _ (Basics.String.ltb_spec y x)).
        apply Basics.String.ltb_or_eqb_or_gtb; [|exact Heq].
        destruct (Basics.String.ltb_spec x y); [contradiction|reflexivity].
  Defined.
  Definition eq_dec := String.string_dec.
End StringOrder.

Module Raw := FMapList.Raw StringOrder.
Definition stringOrder : OCamlMap.OrderedType (t := string) :=
  {| OCamlMap.OrderedType.compare := OCamlString.compare |}.

Local Abbreviation before := (OCamlMap.Make.key_before stringOrder).
Local Abbreviation equal := (OCamlMap.Make.key_equal stringOrder).

Lemma before_true x y : before x y = true ↔ StringOrder.lt x y.
Proof.
  unfold OCamlMap.Make.key_before, OCamlMap.Make.key_compare, stringOrder;
    cbn beta iota zeta delta [OCamlMap.OrderedType.compare].
  rewrite OCamlString.compare_matches_structural. unfold OCamlString.compare_structural. destruct (String.eqb x y) eqn:Heq.
  - apply String.eqb_eq in Heq. subst y. cbn. split; [discriminate|].
    intro Hlt. exfalso. exact (@Basics.String.Lt.irreflexivity x Hlt).
  - destruct (Basics.String.ltb_spec x y); cbn; intuition discriminate.
Qed.

Lemma equal_true x y : equal x y = true ↔ x = y.
Proof.
  unfold OCamlMap.Make.key_equal, OCamlMap.Make.key_compare, stringOrder;
    cbn beta iota zeta delta [OCamlMap.OrderedType.compare].
  rewrite OCamlString.compare_matches_structural. unfold OCamlString.compare_structural. destruct (String.eqb x y) eqn:Heq.
  - apply String.eqb_eq in Heq. cbn. intuition.
  - apply String.eqb_neq in Heq.
    destruct (Basics.String.ltb x y); cbn; intuition discriminate.
Qed.

Lemma compare_facts x y :
  match StringOrder.compare x y with
  | LT _ ⇒ before x y = true ∧ equal x y = false
  | EQ _ ⇒ before x y = false ∧ equal x y = true
  | GT _ ⇒ before x y = false ∧ equal x y = false
  end.
Proof.
  destruct (StringOrder.compare x y) as [Hlt|Heq|Hgt].
  - split; [apply before_true; exact Hlt|].
    apply Bool.not_true_is_false. intro H. apply equal_true in H.
    exact (StringOrder.lt_not_eq _ _ Hlt H).
  - change (x = y) in Heq. subst y. split; [|apply equal_true; reflexivity].
    apply Bool.not_true_is_false. intro H. apply before_true in H.
    exact (@Basics.String.Lt.irreflexivity x H).
  - split; apply Bool.not_true_is_false; intro H.
    + apply before_true in H. apply (@Basics.String.Lt.irreflexivity x).
      exact (@StringOrder.lt_trans _ _ _ H Hgt).
    + apply equal_true in H. subst y.
      exact (@Basics.String.Lt.irreflexivity x Hgt).
Qed.

Lemma find_as_raw {V : Set} (m : list (string × V)) key :
  OCamlMap.Make.find_opt stringOrder key m = Raw.find key m.
Proof.
  induction m as [|[k v] tail IH]; cbn [OCamlMap.Make.find_opt Raw.find]; [reflexivity|].
  pose proof (compare_facts key k) as Hfacts.
  destruct (StringOrder.compare key k); destruct Hfacts as [Hb He];
    rewrite Hb; try rewrite He; auto.
Qed.

Lemma add_as_raw {V : Set} (m : list (string × V)) key value :
  OCamlMap.Make.add stringOrder key value m = Raw.add key value m.
Proof.
  induction m as [|[k v] tail IH]; cbn [OCamlMap.Make.add Raw.add]; [reflexivity|].
  pose proof (compare_facts key k) as Hfacts.
  destruct (StringOrder.compare key k); destruct Hfacts as [Hb He];
    rewrite Hb; try rewrite He; try rewrite IH; reflexivity.
Qed.

Lemma remove_as_raw {V : Set} (m : list (string × V)) key :
  OCamlMap.Make.remove stringOrder key m = Raw.remove key m.
Proof.
  induction m as [|[k v] tail IH]; cbn [OCamlMap.Make.remove Raw.remove]; [reflexivity|].
  pose proof (compare_facts key k) as Hfacts.
  destruct (StringOrder.compare key k); destruct Hfacts as [Hb He];
    rewrite Hb; try rewrite He; try rewrite IH; reflexivity.
Qed.

Definition WellFormed {V : Set} (m : list (string × V)) : Prop :=
  Sorted (@Raw.PX.ltk V) m.

Lemma empty_wellformed {V : Set} : @WellFormed V nil.
Proof. constructor. Qed.

Lemma add_wellformed {V : Set} (m : list (string × V)) key value :
  WellFormed m → WellFormed (OCamlMap.Make.add stringOrder key value m).
Proof. rewrite add_as_raw. intro Hwf. exact (@Raw.add_sorted V m Hwf key value). Qed.

Lemma remove_wellformed {V : Set} (m : list (string × V)) key :
  WellFormed m → WellFormed (OCamlMap.Make.remove stringOrder key m).
Proof. rewrite remove_as_raw. intro Hwf. exact (@Raw.remove_sorted V m Hwf key). Qed.

Lemma find_add_same {V : Set} (m : list (string × V)) key value :
  WellFormed m →
  OCamlMap.Make.find_opt stringOrder key (OCamlMap.Make.add stringOrder key value m) = Some value.
Proof.
  intro Hwf. rewrite find_as_raw, add_as_raw.
  apply Raw.find_1; [exact (@Raw.add_sorted V m Hwf key value)|].
  apply Raw.add_1. reflexivity.
Qed.

Lemma find_add_other {V : Set} (m : list (string × V)) key query value :
  WellFormed m → key ≠ query →
  OCamlMap.Make.find_opt stringOrder query (OCamlMap.Make.add stringOrder key value m) =
    OCamlMap.Make.find_opt stringOrder query m.
Proof.
  intros Hwf Hneq. rewrite !find_as_raw, add_as_raw.
  destruct (Raw.find query m) as [old|] eqn:Hfind.
  - apply Raw.find_1; [exact (@Raw.add_sorted V m Hwf key value)|].
    apply Raw.add_2; [exact Hneq|]. apply Raw.find_2. exact Hfind.
  - destruct (Raw.find query (Raw.add key value m)) as [v|] eqn:Hnew; [|reflexivity].
    apply Raw.find_2 in Hnew.
    pose proof (@Raw.add_3 V m key query v value Hneq Hnew) as Hold.
    pose proof (@Raw.find_1 V m Hwf query v Hold). congruence.
Qed.

Lemma find_remove_same {V : Set} (m : list (string × V)) key :
  WellFormed m →
  OCamlMap.Make.find_opt stringOrder key (OCamlMap.Make.remove stringOrder key m) = None.
Proof.
  intro Hwf. rewrite find_as_raw, remove_as_raw.
  destruct (Raw.find key (Raw.remove key m)) as [v|] eqn:Hfind; [|reflexivity].
  exfalso. apply (@Raw.remove_1 V m Hwf key key eq_refl).
  ∃ v. apply Raw.find_2. exact Hfind.
Qed.

Lemma find_remove_other {V : Set} (m : list (string × V)) key query :
  WellFormed m → key ≠ query →
  OCamlMap.Make.find_opt stringOrder query (OCamlMap.Make.remove stringOrder key m) =
    OCamlMap.Make.find_opt stringOrder query m.
Proof.
  intros Hwf Hneq. rewrite !find_as_raw, remove_as_raw.
  destruct (Raw.find query m) as [old|] eqn:Hfind.
  - apply Raw.find_1; [exact (@Raw.remove_sorted V m Hwf key)|].
    apply Raw.remove_2; [exact Hwf|exact Hneq|]. apply Raw.find_2. exact Hfind.
  - destruct (Raw.find query (Raw.remove key m)) as [v|] eqn:Hnew; [|reflexivity].
    apply Raw.find_2 in Hnew.
    pose proof (@Raw.remove_3 V m Hwf key query v Hnew) as Hold.
    pose proof (@Raw.find_1 V m Hwf query v Hold). congruence.
Qed.

Lemma update_wellformed {V : Set} (m : list (string × V)) key f :
  WellFormed m → WellFormed (OCamlMap.Make.update stringOrder key f m).
Proof.
  intro Hwf. unfold OCamlMap.Make.update.
  destruct (f (OCamlMap.Make.find_opt stringOrder key m));
    [apply add_wellformed|apply remove_wellformed]; exact Hwf.
Qed.

Lemma find_update {V : Set} (m : list (string × V)) key query f :
  WellFormed m →
  OCamlMap.Make.find_opt stringOrder query (OCamlMap.Make.update stringOrder key f m) =
    if String.eqb key query then f (OCamlMap.Make.find_opt stringOrder key m)
    else OCamlMap.Make.find_opt stringOrder query m.
Proof.
  intro Hwf. unfold OCamlMap.Make.update.
  destruct (String.eqb key query) eqn:Heq.
  - apply String.eqb_eq in Heq. subst query.
    destruct (f (OCamlMap.Make.find_opt stringOrder key m));
      [apply find_add_same|apply find_remove_same]; exact Hwf.
  - apply String.eqb_neq in Heq.
    destruct (f (OCamlMap.Make.find_opt stringOrder key m));
      [apply find_add_other|apply find_remove_other]; assumption.
Qed.

Lemma of_list_wellformed {V : Set} (m : list (string × V)) :
  WellFormed (OCamlMap.Make.of_list stringOrder m).
Proof.
  unfold OCamlMap.Make.of_list.
  assert (Hfold : ∀ acc, WellFormed acc →
    WellFormed (Lists.List.fold_left
      (fun acc entry ⇒ OCamlMap.Make.add stringOrder (fst entry) (snd entry) acc) m acc)).
  { induction m as [|[key value] tail IH]; intros acc Hacc; cbn.
    - exact Hacc.
    - apply IH, add_wellformed, Hacc. }
  apply Hfold, empty_wellformed.
Qed.

Print Assumptions find_update.