Skip to content
Snippets Groups Projects
Commit fcc1f0de authored by Robbert Krebbers's avatar Robbert Krebbers
Browse files

More efficient conversion of pmap to association lists.

parent bc659ba4
No related branches found
No related tags found
No related merge requests found
...@@ -257,76 +257,84 @@ Lemma Plookup_fmap {A B} (f : A → B) (t : Pmap_raw A) i : ...@@ -257,76 +257,84 @@ Lemma Plookup_fmap {A B} (f : A → B) (t : Pmap_raw A) i :
fmap f t !! i = fmap f (t !! i). fmap f t !! i = fmap f (t !! i).
Proof. revert i. induction t. done. by intros [?|?|]; simpl. Qed. Proof. revert i. induction t. done. by intros [?|?|]; simpl. Qed.
Fixpoint Pto_list_raw {A} (j: positive) (t: Pmap_raw A) : list (positive * A) := Fixpoint Pto_list_raw {A} (j: positive) (t: Pmap_raw A)
(acc : list (positive * A)) : list (positive * A) :=
match t with match t with
| PLeaf => [] | PLeaf => acc
| PNode l o r => default [] o (λ x, [(Preverse j, x)]) ++ | PNode l o r => default [] o (λ x, [(Preverse j, x)]) ++
Pto_list_raw (j~0) l ++ Pto_list_raw (j~1) r Pto_list_raw (j~0) l (Pto_list_raw (j~1) r acc)
end%list. end%list.
Lemma Pelem_of_to_list_aux {A} (t : Pmap_raw A) j i x : Lemma Pelem_of_to_list {A} (t : Pmap_raw A) j i acc x :
(i,x) Pto_list_raw j t i', i = i' ++ Preverse j t !! i' = Some x. (i,x) Pto_list_raw j t acc
( i', i = i' ++ Preverse j t !! i' = Some x) (i,x) acc.
Proof. Proof.
split. split.
* revert j. induction t as [|? IHl [?|] ? IHr]; intros j; simpl. { revert j acc. induction t as [|l IHl [y|] r IHr]; intros j acc; simpl.
+ by rewrite ?elem_of_nil. * by right.
+ rewrite elem_of_cons, !elem_of_app. intros [?|[?|?]]. * rewrite elem_of_cons. intros [?|?]; simplify_equality.
- simplify_equality. exists 1. by rewrite (left_id_L 1 (++))%positive. { left; exists 1. by rewrite (left_id_L 1 (++))%positive. }
- destruct (IHl (j~0)) as (i' &?&?); trivial; subst. destruct (IHl (j~0) (Pto_list_raw j~1 r acc)) as [(i'&->&?)|?]; auto.
exists (i' ~ 0). by rewrite Preverse_xO, (associative_L _). { left; exists (i' ~ 0). by rewrite Preverse_xO, (associative_L _). }
- destruct (IHr (j~1)) as (i' &?&?); trivial; subst. destruct (IHr (j~1) acc) as [(i'&->&?)|?]; auto.
exists (i' ~ 1). by rewrite Preverse_xI, (associative_L _). left; exists (i' ~ 1). by rewrite Preverse_xI, (associative_L _).
+ rewrite !elem_of_app. intros [?|?]. * intros.
- destruct (IHl (j~0)) as (i' &?&?); trivial; subst. destruct (IHl (j~0) (Pto_list_raw j~1 r acc)) as [(i'&->&?)|?]; auto.
exists (i' ~ 0). by rewrite Preverse_xO, (associative_L _). { left; exists (i' ~ 0). by rewrite Preverse_xO, (associative_L _). }
- destruct (IHr (j~1)) as (i' &?&?); trivial; subst. destruct (IHr (j~1) acc) as [(i'&->&?)|?]; auto.
exists (i' ~ 1). by rewrite Preverse_xI, (associative_L _). left; exists (i' ~ 1). by rewrite Preverse_xI, (associative_L _). }
* intros (i' & ?& Hi'); subst. revert i' j Hi'. revert t j i acc. assert ( t j i acc,
induction t as [|? IHl [?|] ? IHr]; intros i j; simpl. (i, x) acc (i, x) Pto_list_raw j t acc) as help.
+ done. { intros t; induction t as [|l IHl [y|] r IHr]; intros j i acc;
+ rewrite elem_of_cons, elem_of_app. destruct i as [i|i|]; simpl in *. simpl; rewrite ?elem_of_cons; auto. }
- right. right. specialize (IHr i (j~1)). intros t j ? acc [(i&->&Hi)|?]; [|by auto]. revert j i acc Hi.
rewrite Preverse_xI, (associative_L _) in IHr. auto. induction t as [|l IHl [y|] r IHr]; intros j i acc ?; simpl.
- right. left. specialize (IHl i (j~0)). * done.
rewrite Preverse_xO, (associative_L _) in IHl. auto. * rewrite elem_of_cons. destruct i as [i|i|]; simplify_equality'.
- left. simplify_equality. by rewrite (left_id_L 1 (++))%positive. + right. apply help. specialize (IHr (j~1) i).
+ rewrite elem_of_app. destruct i as [i|i|]; simpl in *. rewrite Preverse_xI, (associative_L _) in IHr. by apply IHr.
- right. specialize (IHr i (j~1)). + right. specialize (IHl (j~0) i).
rewrite Preverse_xI, (associative_L _) in IHr. auto. rewrite Preverse_xO, (associative_L _) in IHl. by apply IHl.
- left. specialize (IHl i (j~0)). + left. by rewrite (left_id_L 1 (++))%positive.
rewrite Preverse_xO, (associative_L _) in IHl. auto. * destruct i as [i|i|]; simplify_equality'.
- done. + apply help. specialize (IHr (j~1) i).
Qed. rewrite Preverse_xI, (associative_L _) in IHr. by apply IHr.
Lemma Pelem_of_to_list {A} (t : Pmap_raw A) i x : + specialize (IHl (j~0) i).
(i,x) Pto_list_raw 1 t t !! i = Some x. rewrite Preverse_xO, (associative_L _) in IHl. by apply IHl.
Proof.
rewrite Pelem_of_to_list_aux. split. by intros (i'&->&?). intros. by exists i.
Qed. Qed.
Lemma Pto_list_nodup {A} j (t : Pmap_raw A) acc :
Lemma Pto_list_nodup {A} j (t : Pmap_raw A) : NoDup (Pto_list_raw j t). ( i x, (i ++ Preverse j, x) acc t !! i = None)
NoDup acc NoDup (Pto_list_raw j t acc).
Proof. Proof.
revert j. induction t as [|? IHl [?|] ? IHr]; simpl. revert j acc. induction t as [|l IHl [y|] r IHr]; simpl; intros j acc Hin ?.
* constructor. * done.
* intros. rewrite NoDup_cons, NoDup_app. split_ands; trivial. * repeat constructor.
+ rewrite elem_of_app, !Pelem_of_to_list_aux. { rewrite Pelem_of_to_list. intros [(i&Hi&?)|Hj].
intros [(i&Hi&?)|(i&Hi&?)]. { apply (f_equal Plength) in Hi.
- rewrite Preverse_xO in Hi. apply (f_equal Plength) in Hi. rewrite Preverse_xO, !Papp_length in Hi; simpl in *; lia. }
rewrite !Papp_length in Hi. simpl in Hi. lia. rewrite Pelem_of_to_list in Hj. destruct Hj as [(i&Hi&?)|Hj].
- rewrite Preverse_xI in Hi. apply (f_equal Plength) in Hi. { apply (f_equal Plength) in Hi.
rewrite !Papp_length in Hi. simpl in Hi. lia. rewrite Preverse_xI, !Papp_length in Hi; simpl in *; lia. }
+ intros [??]. rewrite !Pelem_of_to_list_aux. specialize (Hin 1 y). rewrite (left_id_L 1 (++))%positive in Hin.
intros (i1&?&?) (i2&Hi&?); subst. discriminate (Hin Hj). }
rewrite Preverse_xO, Preverse_xI, !(associative_L _) in Hi. apply IHl.
by apply (injective (++ _)) in Hi. { intros i x. rewrite Pelem_of_to_list. intros [(?&Hi&?)|Hi].
* intros. rewrite NoDup_app. split_ands; trivial. + rewrite Preverse_xO, Preverse_xI, !(associative_L _) in Hi.
intros [??]. rewrite !Pelem_of_to_list_aux. by apply (injective (++ _)) in Hi.
intros (i1&?&?) (i2&Hi&?); subst. + apply (Hin (i~0) x). by rewrite Preverse_xO, (associative_L _) in Hi. }
rewrite Preverse_xO, Preverse_xI, !(associative_L _) in Hi. apply IHr; auto. intros i x Hi.
by apply (injective (++ _)) in Hi. apply (Hin (i~1) x). by rewrite Preverse_xI, (associative_L _) in Hi.
* apply IHl.
{ intros i x. rewrite Pelem_of_to_list. intros [(?&Hi&?)|Hi].
+ rewrite Preverse_xO, Preverse_xI, !(associative_L _) in Hi.
by apply (injective (++ _)) in Hi.
+ apply (Hin (i~0) x). by rewrite Preverse_xO, (associative_L _) in Hi. }
apply IHr; auto. intros i x Hi.
apply (Hin (i~1) x). by rewrite Preverse_xI, (associative_L _) in Hi.
Qed. Qed.
Global Instance Pto_list {A} : FinMapToList positive A (Pmap A) := Global Instance Pto_list {A} : FinMapToList positive A (Pmap A) :=
λ t, Pto_list_raw 1 (`t). λ t, Pto_list_raw 1 (`t) [].
Fixpoint Pmerge_aux `(f : option A option B) (t : Pmap_raw A) : Pmap_raw B := Fixpoint Pmerge_aux `(f : option A option B) (t : Pmap_raw A) : Pmap_raw B :=
match t with match t with
...@@ -385,7 +393,11 @@ Proof. ...@@ -385,7 +393,11 @@ Proof.
* intros ?? [??] ??. by apply Plookup_alter_ne. * intros ?? [??] ??. by apply Plookup_alter_ne.
* intros ??? [??]. by apply Plookup_fmap. * intros ??? [??]. by apply Plookup_fmap.
* intros ? [??]. apply Pto_list_nodup. * intros ? [??]. apply Pto_list_nodup.
* intros ? [??]. apply Pelem_of_to_list. + intros ??. by rewrite elem_of_nil.
+ constructor.
* intros ? [??] i x; unfold map_to_list, Pto_list.
rewrite Pelem_of_to_list, elem_of_nil.
split. by intros [(?&->&?)|]. by left; exists i.
* intros ??? ?? [??] [??] ?. by apply Pmerge_spec. * intros ??? ?? [??] [??] ?. by apply Pmerge_spec.
Qed. Qed.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment