BinaryRandomAccessList_proof.v 26.2 KB
Newer Older
Armaël Guéneau's avatar
Armaël Guéneau committed
1 2 3 4 5 6 7
Set Implicit Arguments.
Require Import CFML.CFLib CFML.CFLibCredits.
Require Import BinaryRandomAccessList_ml.
Require Import TLC.LibListZ LibZExtra TLC.LibIntTactics.
Require Import Filter Monotonic Dominated CFMLBigO.
Require Import Procrastination.Procrastination.

Armaël Guéneau's avatar
Armaël Guéneau committed
8 9 10
Require Import Generic DominatedNary LimitNary.
Require Import UltimatelyGreater.

Armaël Guéneau's avatar
Armaël Guéneau committed
11
Open Scope Z_scope.
Armaël Guéneau's avatar
Armaël Guéneau committed
12
Open Scope liblist_scope.
Armaël Guéneau's avatar
Armaël Guéneau committed
13 14 15

Notation "'int'" := Z.

Armaël Guéneau's avatar
Armaël Guéneau committed
16
Module BinaryRandomAccessListSpec.
Armaël Guéneau's avatar
Armaël Guéneau committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30

Import BinaryRandomAccessList_ml.

(** invariant *)

Section Polymorphic.
Variables (a : Type).

Inductive btree : int -> tree_ a -> list a -> Prop :=
  | btree_nil : forall x,
      btree 0 (Leaf x) (x::nil)
  | btree_cons : forall p p' n t1 t2 L1 L2 L',
      btree p t1 L1 ->
      btree p t2 L2 ->
Armaël Guéneau's avatar
Armaël Guéneau committed
31 32 33
      p' = p+1 ->
      n = 2^p' ->
      L' = L1 ++ L2 ->
Armaël Guéneau's avatar
Armaël Guéneau committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
      btree p' (Node n t1 t2) L'.

Inductive inv : int -> rlist_ a -> list a -> Prop :=
  | inv_nil : forall p,
      0 <= p ->
      inv p nil nil
  | inv_cons : forall p (t: tree_ a) ts d L L' T,
      inv (p+1) ts L ->
      L' <> nil ->
      0 <= p ->
      (match d with
       | Zero => L = L'
       | One t => btree p t T /\ L' = T ++ L
       end) ->
      inv p (d :: ts) L'.

Definition Rlist (s: rlist_ a) (L: list a) :=
  inv 0 s L.

End Polymorphic.

Armaël Guéneau's avatar
Armaël Guéneau committed
55 56
Arguments btree [a].
Arguments inv [a].
Armaël Guéneau's avatar
Armaël Guéneau committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70

(** Automation *)

(* Use [maths] hints and properties about btrees in ~-suffixed tactics *)
Create HintDb bral.
Ltac auto_tilde ::= eauto with maths bral.

(* Do not try to unfold Z.mul, e.g. in [2 * n] *)
Opaque Z.mul.

(* Hint Constructors btree inv. *)
(* Hint Extern 1 (@lt nat _ _ _) => rew_list; math. *)
(* Hint Resolve ZNth_zero ZUpdate_here ZUpdate_not_nil. *)
Hint Resolve app_not_empty_l app_not_empty_r.
71
Hint Extern 1 (0 <= length _) => math : zarith.
Armaël Guéneau's avatar
Armaël Guéneau committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

(** useful facts *)

Fixpoint tree_size {a: Type} (t:tree_ a) : nat :=
  match t with
  | Leaf _ => 0%nat
  | Node _ t1 t2 => (1 + tree_size t1 + tree_size t2)%nat
  end.

Definition Size {a: Type} (t:tree_ a) :=
  match t with
  | Leaf _ => 1
  | Node w _ _ => w
  end.

Lemma btree_size_correct : forall a p t L,
  @btree a p t L -> Size t = 2^p.
Proof. introv Rt. inverts~ Rt. Qed.

Lemma btree_size_pos : forall a p t L,
  @btree a p t L -> 0 <= p.
Proof. introv Rt. inductions Rt; math. Qed.

Hint Resolve btree_size_pos : bral.

Lemma btree_length_correct : forall a t p L,
  @btree a p t L -> length L = 2^p.
Proof.
  introv Rt. induction Rt. auto.
Armaël Guéneau's avatar
Armaël Guéneau committed
101
  subst. rew_list. rewrite~ pow2_succ.
Armaël Guéneau's avatar
Armaël Guéneau committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
Qed.

Lemma btree_size_length : forall a t p L,
  @btree a p t L -> length L = Size t.
Proof.
  intros. erewrite~ btree_size_correct. erewrite~ btree_length_correct.
Qed.

Lemma btree_not_empty : forall a p t L,
  @btree a p t L -> L <> nil.
Proof.
  introv Rt. intro; subst.
  forwards~: btree_length_correct. rew_list in *.
  forwards~: @pow2_ge_1 p. math.
Qed.

Hint Resolve btree_not_empty : bral.

Lemma inv_size_pos : forall a p ts L,
  @inv a p ts L ->
  0 <= p.
Proof. introv I. destruct~ I. Qed.

Hint Resolve inv_size_pos : bral.

Lemma inv_ts_not_empty : forall a p ts L,
  @inv a p ts L ->
  ts <> nil ->
  L <> nil.
Proof. introv I. destruct~ I. Qed.

Hint Resolve inv_ts_not_empty : bral.

Lemma inv_L_not_empty : forall a p ts L,
  @inv a p ts L ->
  L <> nil ->
  ts <> nil.
Proof. introv I H. destruct~ I. congruence. Qed.

Hint Resolve inv_L_not_empty : bral.

Lemma inv_to_empty : forall a p L,
  @inv a p nil L -> L = nil.
Proof. introv RL. inverts~ RL. Qed.

Lemma inv_from_empty : forall a p ts,
  @inv a p ts nil -> ts = nil.
Proof. introv RL. inverts RL; auto_false. Qed.

Ltac inv :=
  match goal with
  | I : inv _ nil ?L |- _ =>
    let H := fresh in
    pose proof (@inv_to_empty _ _ L I) as H;
    rewrite H in *; clear H; clear L
  | I : inv _ ?ts nil |- _ =>
    let H := fresh in
    pose proof (@inv_from_empty _ _ ts I) as H;
    rewrite H in *; clear H; clear ts
  end;
  rew_list in *.

Lemma group_ineq : forall n m, 0 <= n - m -> m <= n.
Proof. intros. math. Qed.

167
(* True but useless *)
Armaël Guéneau's avatar
Armaël Guéneau committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
(*
Lemma inv_length_correct : forall a ts p L,
    @inv a p ts L ->
    If ts = nil then
      length L = 0
    else
      (2 ^ p <= length L <= 2 ^ (length ts + p) - 2 ^ p).
Proof.
  induction ts.
  - intros. case_if. rewrites~ (>> to_empty ___).
  - { rename a0 into d. case_if; intros p L Rdts.
      - { inverts Rdts as. intros L0 T _t Rts Lnnil ?. forwards IH: IHts Rts.
          forwards~: pow2_ge_1 p.
          destruct d; intros; subst.
          - { assert (ts <> nil). { intro TS. apply Lnnil. subst. applys~ to_empty. }
              case_If. destruct IH as [IH1 IH2]. split.
              - rew_ineq <- IH1. rew_pow~ 2 p.
              - rewrite~ length_cons. rew_ineq IH2. rew_pow~ 2 p. }
          - { case_If.
              - { unpack; subst. forwards~: (>> to_empty ___); subst. split.
                  - forwards~: (>> length_correct ___). rew_list~.
                  - rew_length~. forwards~ Tlen: (>> length_correct ___). rew_pow~ 2 p. }
              - { destruct IH as [IH1 IH2]; unpack; subst.
                  split; forwards~ Tlen: (>> length_correct ___); rew_length~.
                  rew_ineq IH2. rewrite Tlen. rew_pow~ 2 p. } } } }
Qed.
*)

Lemma pow_r_eq : forall x y b,
  x = y -> b ^ x = b ^ y.
Proof. intros; subst~. Qed.

200
Local Ltac epow tac :=
Armaël Guéneau's avatar
Armaël Guéneau committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
  erewrite pow_r_eq; [ tac | auto_tilde ].

Tactic Notation "epow" ":" tactic(tac) :=
  epow tac.

Lemma inv_ts_len : forall a ts (p:int) L,
    @inv a p ts L ->
    ts <> nil ->
    2 ^ (p + (length ts) - 1) <= length L.
Proof.
  induction ts as [| d ts IHts ].
  { intros. false. }
  { intros p L Rdts _. inverts Rdts as. intros L0 T _t Rts Lnnil ? ?.
    destruct ts.
    - { (* ts = nil // interesting case *)
        destruct d; intros; subst. (* [d] must be a [One t] for the invariant to hold. *)
        - inv. false.
        - inv. unpack; subst. erewrite~ btree_length_correct. epow: reflexivity. }
    - { forwards IH: IHts Rts. congruence.
        destruct d; unpack; subst; rew_list in *.
        + epow: apply IH.
        + epow: rewrite IH. math. } }
Qed.

Lemma ts_bound : forall a ts p L,
    @inv a p ts L ->
    2 ^ (length ts) <= 2 * (length L) + 1.
Proof.
  destruct ts; rew_list; intros.
  { change (2^0) with 1. math. }
  { forwards I: inv_ts_len H. congruence.
    erewrite pow_r_eq in I; [| rew_list; ring_simplify; reflexivity].
    rew_pow~ 2 1. change (2^1) with 2.
234
    rew_pow~ 2 p in I. rewrite~ <-I.
Armaël Guéneau's avatar
Armaël Guéneau committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    forwards~: pow2_ge_1 p. forwards~: Z.pow_nonneg 2 (length ts). math_nia. }
Qed.

Lemma ts_bound_log : forall a ts p L,
    @inv a p ts L ->
    length ts <= Z.log2 (2 * (length L) + 1).
Proof.
  intros. forwards~: ts_bound. forwards~: Z.log2_le_mono. rewrites~ Z.log2_pow2 in *.
Qed.

(* Useless *)
(*
Lemma p_bound_log : forall a ts p L,
    @inv a p ts L -> If ts <> nil then p <= Z.log2 (length L) else True.
Proof.
  introv Rts. forwards~ inv_bounds: inv_length_correct. case_If.
  - case_If. destruct inv_bounds as [I1 _].
    forwards~: Z.log2_le_mono. rewrites~ Z.log2_pow2 in *.
    inverts~ Rts.
  - auto.
Qed.
*)

(** Verification *)

Lemma empty_spec :
  forall a, Rlist (@empty a) (@nil a).
Proof. intros. rewrite (empty__cf a). constructors~. Qed.

Hint Extern 1 (RegisterSpec empty) => Provide empty_spec.

Lemma is_empty_spec :
  spec1 [cost]
    (forall a (l: rlist_ a),
     app is_empty [l]
       PRE (\$ cost tt)
       POST (fun (b: bool) => \[b = isTrue (l = nil)])).
Proof.
  xspecO_refine straight_line. xcf. xgo~.
  cleanup_cost. monotonic. dominated.
Qed.

Hint Extern 1 (RegisterSpec is_empty) => Provide (provide_specO is_empty_spec).

(* private *)
Lemma size_spec :
  spec1 [cost]
    (forall a (t: tree_ a),
     app size [t]
       PRE (\$ cost tt)
       POST (fun (n: int) => \[n = Size t])).
Proof.
  xspecO_refine straight_line. xcf. xgo~.
  cleanup_cost. monotonic. dominated.
Qed.

Hint Extern 1 (RegisterSpec size) => Provide (provide_specO size_spec).

Lemma link_spec :
  spec1 [cost]
    (forall a (t1 t2: tree_ a) p L1 L2,
     btree p t1 L1 -> btree p t2 L2 ->
     app link [t1 t2]
       PRE (\$ cost tt)
       POST (fun t' => \[btree (p+1) t' (L1 ++ L2)])).
Proof.
  xspecO_refine straight_line. xcf. xgo. constructors~.
  erewrite btree_size_correct in * by eauto. rewrite~ pow2_succ.
  cleanup_cost. monotonic. dominated.
Qed.

Hint Extern 1 (RegisterSpec link) => Provide (provide_specO link_spec).

Lemma cons_tree_spec_aux :
  specZ [cost \in_O (fun n => n)]
    (forall a (t: tree_ a) (ts: rlist_ a) p T L,
     btree p t T -> inv p ts L ->
     app cons_tree [t ts]
       PRE (\$ cost (length ts))
       POST (fun ts' => \[inv p ts' (T ++ L)])).
Proof.
  xspecO_refine recursive. intros ? ? ? ? a t ts. revert t.
  { induction ts; introv Rt Rts; inverts Rts; xcf.
    - weaken.
      xpay. xgo~.
      { constructors~. constructors~. }
321
      { rew_list. rewrite !Z.max_id. rewrite Z.add_0_r. defer. }
Armaël Guéneau's avatar
Armaël Guéneau committed
322 323 324 325 326 327
    - weaken. xpay.
      xmatch; unpack; subst.
      { xret. hsimpl. constructors~. }
      { xapps~. intros. xapps~. intros.
        xret. hsimpl. constructors~. rew_list~. }

328
    rew_cost. rew_list. generalize (length ts) (length_nonneg ts). defer. }
Armaël Guéneau's avatar
Armaël Guéneau committed
329

330
  close_cost. begin defer assuming a b.
Armaël Guéneau's avatar
Armaël Guéneau committed
331
  exists (fun n => a * n + b). repeat split.
332
  { ring_simplify. defer. }
333
  { introv HH. cancel z; case_max; defer. }
Armaël Guéneau's avatar
Armaël Guéneau committed
334 335

  cleanup_cost.
336
  { assert (0 <= a) by (deferred; math_lia). monotonic. }
Armaël Guéneau's avatar
Armaël Guéneau committed
337 338
  { dominated. }

339 340
  end defer. elia.
Qed.
Armaël Guéneau's avatar
Armaël Guéneau committed
341 342 343 344 345 346 347 348 349

Lemma cons_tree_spec_aux' :
  specZ [cost \in_O (fun n => n)]
    (forall a (t: tree_ a) (ts: rlist_ a) p T L,
     btree p t T -> inv p ts L ->
     app cons_tree [t ts]
       PRE (\$ cost (length ts))
       POST (fun ts' => \[inv p ts' (T ++ L)])).
Proof.
350
  begin defer assuming a b.
Armaël Guéneau's avatar
Armaël Guéneau committed
351 352 353 354 355
  xspecO (fun n => a * n + b). intros A t ts. revert t.
  { induction ts; introv Rt Rts; inverts Rts; xcf.
    - weaken.
      xpay. xgo~.
      { constructors~. constructors~. }
356
      { rew_list. rew_cost. defer. }
Armaël Guéneau's avatar
Armaël Guéneau committed
357 358 359 360 361
    - weaken. xpay.
      xmatch; unpack; subst.
      { xret. hsimpl. constructors~. }
      { xapps~. intros. xapps~. intros.
        xret. hsimpl. constructors~. rew_list~. }
Armaël Guéneau's avatar
Armaël Guéneau committed
362 363
      rew_list. rew_cost. cancel (length ts).
      all: case_max; auto with zarith; defer. }
Armaël Guéneau's avatar
Armaël Guéneau committed
364

365
  { assert (0 <= a) by (deferred; math_lia). monotonic. }
Armaël Guéneau's avatar
Armaël Guéneau committed
366 367
  { dominated. }

368
  end defer. elia.
Armaël Guéneau's avatar
Armaël Guéneau committed
369 370 371 372 373 374 375 376 377 378
Qed.

Lemma cons_tree_spec :
  specZ [ cost \in_O Z.log2 ]
    (forall a (t: tree_ a) (ts: rlist_ a) p T L,
     btree p t T -> inv p ts L ->
     app cons_tree [t ts]
       PRE (\$ cost (length L))
       POST (fun ts' => \[inv p ts' (T ++ L)])).
Proof.
379
  xspecO_refine straight_line. intros. weaken. xapplys~ cons_tree_spec_aux.
Armaël Guéneau's avatar
Armaël Guéneau committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  apply cost_monotonic. rewrite~ ts_bound_log. generalize (length L). reflexivity.
  cleanup_cost. monotonic. dominated.
Qed.

Hint Extern 1 (RegisterSpec cons_tree) => Provide (provide_specO cons_tree_spec).

Lemma cons_spec :
  specZ [cost \in_O Z.log2]
    (forall a (x: a) (l: rlist_ a) L,
     Rlist l L ->
     app cons [x l]
       PRE (\$ cost (length L))
       POST (fun l' => \[Rlist l' (x::L)])).
Proof.
  xspecO_refine straight_line. xcf. xpay.
  weaken. xapp~. constructors~. hsimpl. rew_list~ in *.
  generalize (length L). reflexivity.
  cleanup_cost. (* FIXME: cleanup_cost should replace [cost cons_tree_spec] by its bound *)
  monotonic. dominated.
Qed.

Hint Extern 1 (RegisterSpec cons) => Provide (provide_specO cons_spec).

Lemma uncons_tree_spec_aux :
  specZ [cost \in_O (fun n => n)]
    (forall a (ts: rlist_ a) p L,
     inv p ts L -> ts <> nil ->
     app uncons_tree [ts]
       PRE (\$ cost (length ts))
       POST (fun '(t', ts') =>
         \[exists T' L', btree p t' T' /\ inv p ts' L' /\ L = T' ++ L'])).
Proof.
412
  begin defer assuming a b. xspecO (fun n => a * n + b).
Armaël Guéneau's avatar
Armaël Guéneau committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  induction ts as [| t ts']; introv Rts Ne; inverts Rts as.
  { xcf; xgo. }
  { introv ? I. intros. xcf. weaken. xpay. xmatch.
    { xcleanpat. inv. unpack; subst.
      xrets. exists. splits~. constructors~. rew_list~. }
    { forwards: inv_ts_not_empty I. congruence. xcleanpat.
      unpack; subst. xrets. exists. splits~. constructors~. }
    { xcleanpat. xapp~ as [t l]. intros; unpack; subst. xgo.
      - match goal with
        | B: btree _ (Node _ ?t1_ ?t2_) _, I: inv _ _ ?L_ |- _ =>
          rename t1_ into t1; rename t2_ into t2; rename L_ into L;
          inverts B
        end; subst.
        match goal with
        | H1: btree _ t1 ?L1_, H2: btree _ t2 ?L2_, E: (?p + 1) = (?p' + 1) |- _ =>
          rename L1 into L1; rename L2 into L2;
          maths (p = p'); clear E
        end; subst.
        eexists L1, (L2 ++ L). splits~. constructors~. rew_list~.
      - match goal with B: btree _ _ _ |- _ => inverts B end; subst.
        { math. } { jauto. } }

435
    rew_cost. rew_list. cancel (length ts'). all: case_max; defer. }
Armaël Guéneau's avatar
Armaël Guéneau committed
436

437
  { assert (0 <= a) by (deferred; math_lia). monotonic. }
Armaël Guéneau's avatar
Armaël Guéneau committed
438 439
  { dominated. }

440
  end defer. elia.
Armaël Guéneau's avatar
Armaël Guéneau committed
441 442 443 444 445 446 447 448 449 450 451 452
  Grab Existential Variables.  apply heap_is_gc. (* XXX *)
Qed.

Lemma uncons_tree_spec :
  specZ [cost \in_O Z.log2]
    (forall a (ts: rlist_ a) p L,
     inv p ts L -> ts <> nil ->
     app uncons_tree [ts]
       PRE (\$ cost (length L))
       POST (fun '(t', ts') =>
         \[exists T' L', btree p t' T' /\ inv p ts' L' /\ L = T' ++ L'])).
Proof.
453
  xspecO_refine straight_line. intros. weaken. xapplys~ uncons_tree_spec_aux.
Armaël Guéneau's avatar
Armaël Guéneau committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  apply cost_monotonic. rewrite~ ts_bound_log. generalize (length L); reflexivity.
  cleanup_cost. monotonic. dominated.
Qed.

Hint Extern 1 (RegisterSpec uncons_tree) => Provide (provide_specO uncons_tree_spec).

Lemma head_spec :
  specZ [cost \in_O Z.log2]
    (forall a (l: rlist_ a) L,
     Rlist l L -> l <> nil ->
     app head [l]
       PRE (\$ cost (length L))
       POST (fun (x:a) => \[exists L', L = x :: L'])).
Proof.
  xspecO_refine straight_line. xcf. weaken. xpay. xapp~ as [t ts].
  intros (? & ? & B & I & ?); subst. xmatch.
  { xrets. inverts B. rew_list~. }
  { xfail. inverts* B; subst. forwards~: btree_size_pos. math. }
  rew_cost. generalize (length L); reflexivity.
  cleanup_cost. monotonic. dominated.
Qed.

Hint Extern 1 (RegisterSpec head) => Provide (provide_specO head_spec).

Lemma tail_spec :
  specZ [cost \in_O Z.log2]
    (forall a (l: rlist_ a) L,
     Rlist l L -> l <> nil ->
     app tail [l]
       PRE (\$ cost (length L))
       POST (fun tl => \[exists TL x, Rlist tl TL /\ L = x :: TL])).
Proof.
  xspecO_refine straight_line. xcf. weaken. xpay. xapp~ as [t ts].
  intros (? & ? & B & I & ?); subst. xmatch. xrets. inverts B; subst.
  { rew_list~ in *. }
  { forwards~: btree_size_pos. math. }
  rew_cost. generalize (length L); reflexivity.
  cleanup_cost. monotonic. dominated.
Qed.

Hint Extern 1 (RegisterSpec tail) => Provide (provide_specO tail_spec).

Lemma lookup_tree_spec :
  specZ [cost \in_O (fun n => n)]
498
    (forall a `{Inhab a} i (t: tree_ a) p L,
Armaël Guéneau's avatar
Armaël Guéneau committed
499 500 501 502
     btree p t L ->
     0 <= i < length L ->
     app lookup_tree [i t]
       PRE (\$ cost p)
503
       POST (fun x => \[ x = L[i] ])).
Armaël Guéneau's avatar
Armaël Guéneau committed
504
Proof.
505
  begin defer assuming a b. xspecO (fun n => a * n + b).
506
  intros a_ Ia i t. revert i.
Armaël Guéneau's avatar
Armaël Guéneau committed
507
  induction t; introv BT Bi; inverts BT; xcf.
508
  { weaken. xgo. subst. rewrite~ read_zero. rew_list~ in *.
509
    rew_cost. defer. }
Armaël Guéneau's avatar
Armaël Guéneau committed
510 511 512 513 514
  { weaken. xpay. xmatch. subst.
    match goal with B1: btree ?p _ _, B2: btree ?p _ _ |- _ =>
      forwards~: btree_length_correct B1; forwards~: btree_length_correct B2
    end. rew_list in Bi.
    xif; rewrites~ pow_succ_quot in *.
515 516 517
    { xapplys~ IHt1. rewrite read_app. case_if~. }
    { xapplys~ IHt2. rewrite read_app. case_if~.
      now match goal with H: length L1 = _ |- _ => rewrite H end. }
Armaël Guéneau's avatar
Armaël Guéneau committed
518

519 520 521 522
    rew_cost. subst. cancel p0. case_max; auto; defer. apply~ btree_size_pos.
    case_max; defer. }
  { assert (0 <= a) by (deferred; math_lia). monotonic. } { dominated. }
  end defer. elia.
Armaël Guéneau's avatar
Armaël Guéneau committed
523 524 525 526 527 528
Qed.

Hint Extern 1 (RegisterSpec lookup_tree) => Provide (provide_specO lookup_tree_spec).

Lemma update_tree_spec :
  specZ [cost \in_O (fun n => n)]
529
    (forall a `{Inhab a} i (x: a) (t: tree_ a) p L,
Armaël Guéneau's avatar
Armaël Guéneau committed
530 531 532 533
     btree p t L ->
     0 <= i < length L ->
     app update_tree [i x t]
       PRE (\$ cost p)
534
       POST (fun t' => \[btree p t' L[i := x]])).
Armaël Guéneau's avatar
Armaël Guéneau committed
535
Proof.
536
  begin defer assuming a b. xspecO (fun n => a * n + b).
537 538
  intros a_ Ia i x t. revert i x. induction t; introv BT Bi; inverts BT; xcf.
  { weaken. xgo~. subst. rewrite update_zero. constructors. now rew_list~ in *.
539
    rew_cost. defer. }
Armaël Guéneau's avatar
Armaël Guéneau committed
540 541 542 543 544
  { weaken. xpay. xmatch. subst. xcleanpat.
    match goal with B1: btree ?p _ _, B2: btree ?p _ _ |- _ =>
      forwards~: btree_length_correct B1; forwards~: btree_length_correct B2
    end. rew_list in Bi.
    xif; rewrites~ pow_succ_quot in *.
545 546 547 548
    { xapp_spec~ IHt1. intros. xret. hsimpl.
      rewrite~ update_app_l. constructors~. }
    { xapp_spec~ IHt2. intros. xret. hsimpl.
      erewrite~ update_app_r. constructors~. all: math. }
Armaël Guéneau's avatar
Armaël Guéneau committed
549

550 551 552 553
    rew_cost. subst. cancel p0. case_max; auto; defer. apply~ btree_size_pos.
    case_max; defer. }
  { assert (0 <= a) by (deferred; math). monotonic. } { dominated. }
  end defer. elia.
554
Qed.
Armaël Guéneau's avatar
Armaël Guéneau committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

Hint Extern 1 (RegisterSpec update_tree) => Provide (provide_specO update_tree_spec).

Definition product_positive_order :=
  product_filterType positive_filterType Z_filterType.

Lemma limit_positive_order_x_plus_y :
  limit product_positive_order Z_filterType (fun '(x,y) => x + y).
Proof.
  apply_nary (@limit_sum_ultimately_ge_l_nary 0).
  - apply ultimately_lift1. rewrite~ positiveP.
  - limit.
Qed.

Hint Resolve limit_positive_order_x_plus_y : limit.

Lemma limit_positive_0 :
  limit Z_filterType positive_filterType (fun _ => 0).
Proof.
  rewrite limitP. intros. rewrite positiveP, ZP in *.
  exists~ 0.
Qed.

Hint Resolve limit_positive_0 : limit.

Armaël Guéneau's avatar
Armaël Guéneau committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
(* XX *)
Ltac fold_product :=
  rewrite_strat (subterms (fold (product_filterType positive_filterType Z_filterType))).

Lemma dominated_positive_order_x_x_plus_y :
  dominated product_positive_order
    (fun '(x, y) => x) (fun '(x,y) => x + y).
Proof.
  apply_nary dominated_sum_r_nonneg_1_nary; fold_product.
  { (* ultimately_greater FIXME *)
    apply ultimately_lift1. rewrite~ positiveP. }
  { apply ultimately_lift2. ultimately_greater. }
  dominated.
Qed.

Hint Resolve dominated_positive_order_x_x_plus_y.

Lemma dominated_positive_order_y_x_plus_y :
  dominated product_positive_order
    (fun '(x, y) => y) (fun '(x,y) => x + y).
Proof.
  apply_nary dominated_sum_r_nonneg_2_nary; fold_product.
  { apply ultimately_lift1. rewrite~ positiveP. }
  { apply ultimately_lift2. ultimately_greater. }
  reflexivity.
Qed.

Hint Resolve dominated_positive_order_y_x_plus_y.

Armaël Guéneau's avatar
Armaël Guéneau committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
(* HACK HACK HACK

   - Partly because of https://github.com/coq/coq/issues/6805 which introduces
   buggy definitions in the context of side-goals spawned by setoid_rewrite;

   - Partly because we call [math] on one of these side-goals (through
   [auto_tilde] then [eauto with maths]), and CFML.CFTactics redefined [math_0]
   (called by math) to call [xclean] -- which in turns messes up with the buggy
   definition...
*)
Ltac math_0 ::=
  repeat (match goal with x := Morphisms.do_subrelation : _ |- _ => clear x end);
  xclean.

(* - est-ce qu'on sait prouver cette spec directement ? il semblerait que non *)
(* - est-ce que cette spec découle de la spec avec filtre produit ? oui! *)
(* - si on a cette spec, est-ce qu'on sait bien prouver la spec finale ?
     oui. (sans trop de surprise) *)
Definition lookup_spec_ind__external_forall :=
  forall p,
  0 <= p ->
  specO Z_filterType Z.le
    (fun cost =>
     forall a i (ts: rlist_ a) L,
     inv p ts L ->
     0 <= i < length L ->
     app lookup [i ts]
       PRE (\$ cost (length ts))
       POST (fun x => \[Nth (abs i) L x]))
    (fun n => n).

Definition lookup_spec_ind__nonstandard_product :=
  specO product_positive_order ZZle
    (fun cost =>
     forall a i (ts: rlist_ a) p L,
     inv p ts L ->
     0 <= i < length L ->
     app lookup [i ts]
       PRE (\$ cost (p, length ts))
       POST (fun x => \[Nth (abs i) L x]))
    (fun '(m,n) => m + n).

Definition lookup_spec__final :=
  specZ [cost \in_O Z.log2]
    (forall a i (ts: rlist_ a) L,
     Rlist ts L ->
     0 <= i < length L ->
     app lookup [i ts]
       PRE (\$ cost (length L))
       POST (fun x => \[Nth (abs i) L x])).

Goal lookup_spec_ind__nonstandard_product -> lookup_spec_ind__external_forall.
Proof.
  intro S.
  unfold lookup_spec_ind__external_forall, lookup_spec_ind__nonstandard_product in *.
  intros p NP.
  destruct S.
  eapply (@SpecO _ _ _ _ (fun len_ts => cost (p, len_ts))); auto.
  { monotonic. }
  { etransitivity.
    eapply dominated_comp_eq. apply cost_dominated.
670
    2: intros; reflexivity.
Armaël Guéneau's avatar
Armaël Guéneau committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    { rewrite limitP. intros P UP. unfold product_positive_order in UP.
      rewrite productP in UP. destruct UP as (P1 & P2 & UP1 & UP2 & HH).
      rewrite positiveP, ZP in *. destruct UP2 as (n0 & HP2).
      exists (Z.max 0 n0). intros. apply HH. apply UP1. auto. apply HP2.
      eapply Z.max_lub_r. eauto. }
    { simpl. reflexivity. }
    dominated.
  }
Qed.

Goal lookup_spec_ind__external_forall -> lookup_spec__final.
Proof.
  intro S.
  unfold lookup_spec_ind__external_forall, lookup_spec__final in *.
  specializes S 0 __. xspecO (fun len_L => cost S (Z.log2 (2 * len_L + 1))).
686
  intros. weaken. xapplys~ S. apply cost_monotonic. rewrite~ ts_bound_log. math.
Armaël Guéneau's avatar
Armaël Guéneau committed
687 688 689 690 691 692 693 694
  monotonic.
  { etransitivity. apply dominated_comp. apply cost_dominated.
    unfold product_positive_order. limit. dominated. }
Qed.

Lemma lookup_spec_ind :
  specO product_positive_order ZZle
    (fun cost =>
695
     forall a `{Inhab a} i (ts: rlist_ a) p L,
Armaël Guéneau's avatar
Armaël Guéneau committed
696 697 698 699
     inv p ts L ->
     0 <= i < length L ->
     app lookup [i ts]
       PRE (\$ cost (p, length ts))
700
       POST (fun x => \[x = L[i]]))
Armaël Guéneau's avatar
Armaël Guéneau committed
701 702 703
    (fun '(m,n) => m + n).
Proof.
  xspecO_refine recursive. intros costf ? ? ?.
704
  intros a_ Ia i ts. revert i. induction ts; introv I Bi.
Armaël Guéneau's avatar
Armaël Guéneau committed
705 706 707
  { inv. math. }
  { inverts I. xcf. weaken. xpay. xmatch. { xapps~. }
    xcleanpat. unpack; subst. xapps~. xif.
708 709 710
    { xapps~. erewrite~ btree_size_length. math. hsimpl.
      rewrite read_app. case_if~.
      false. forwards~: btree_size_length b. }
Armaël Guéneau's avatar
Armaël Guéneau committed
711
    { xapps~. forwards~: btree_size_length. rew_list~ in *.
712 713
      xapps~. hsimpl. rewrite read_app. case_if~.
      now match goal with H: length T = _ |- _ => rewrite H end. }
Armaël Guéneau's avatar
Armaël Guéneau committed
714 715 716

    rew_list. rew_cost.
    asserts~ [HH1 HH2]: (0 <= p /\ 0 <= length ts).
717
    generalize p (length ts) HH1 HH2; defer. }
Armaël Guéneau's avatar
Armaël Guéneau committed
718

719
  close_cost.
Armaël Guéneau's avatar
Armaël Guéneau committed
720 721 722 723 724 725 726 727

  (* ici on est un peu triste car les équations de récurrence (et la solution
     qu'il faut deviner) sont polluées par les fonctions de coût opaques pour
     les fonctions auxiliaires.

     Mais on ne voit pas bien comment faire mieux.
   *)

728
  begin defer assuming a b c d.
Armaël Guéneau's avatar
Armaël Guéneau committed
729 730
  exists (fun '(m,n) => a * m + b * n + c * cost lookup_tree_spec (m+n) + d).
  repeat split.
731 732 733 734
  { intros m n Hm Hn. math_rewrite (m+(1+n) = m+1+n).
    rewrite~ (cost_monotonic lookup_tree_spec m (m+1+n)).
    cancel (cost lookup_tree_spec (m+1+n)). case_max; auto with zarith; defer.
    cancel m; cancel n. all: repeat case_max; auto with zarith. all: defer. }
Armaël Guéneau's avatar
Armaël Guéneau committed
735 736
  cleanup_cost.
  { intros [m n] [m' n'] [Hm Hn].
737
    assert (0 <= a /\ 0 <= b /\ 1 <= c) as (?&?&?) by (deferred; math_lia).
Armaël Guéneau's avatar
Armaël Guéneau committed
738
    forwards~ M: cost_monotonic lookup_tree_spec (m + n) (m' + n').
739
    rewrite~ M. auto with zarith. }
Armaël Guéneau's avatar
Armaël Guéneau committed
740

Armaël Guéneau's avatar
Armaël Guéneau committed
741 742 743 744 745 746
  repeat apply_nary dominated_sum_distr_nary;
    try apply_nary dominated_mul_cst_l_1_nary; dominated.
  eapply dominated_comp_eq with
    (J := Z_filterType) (p := fun '(t,x) => t + x).
  apply (cost_dominated lookup_tree_spec). limit.
  intros [? ?]. reflexivity. intros [? ?]. reflexivity.
Armaël Guéneau's avatar
Armaël Guéneau committed
747

748
  end defer. elia.
Armaël Guéneau's avatar
Armaël Guéneau committed
749 750 751 752
Qed.

Lemma lookup_spec :
  specZ [cost \in_O Z.log2]
753
    (forall a `{Inhab a} i (ts: rlist_ a) L,
Armaël Guéneau's avatar
Armaël Guéneau committed
754 755 756 757
     Rlist ts L ->
     0 <= i < length L ->
     app lookup [i ts]
       PRE (\$ cost (length L))
758
       POST (fun x => \[x = L[i]])).
Armaël Guéneau's avatar
Armaël Guéneau committed
759 760
Proof.
  xspecO (fun x => cost lookup_spec_ind (0, Z.log2 ((2 * x) + 1))).
761
  { intros. weaken. xapplys~ lookup_spec_ind.
Armaël Guéneau's avatar
Armaël Guéneau committed
762 763 764 765 766 767 768 769 770
    apply cost_monotonic. splits~. unfold Rlist in *. rewrite~ ts_bound_log.
    math. }

  { monotonic. }
  { etransitivity. apply dominated_comp. apply cost_dominated.
    unfold product_positive_order. limit. dominated. }
Qed.

Lemma update_spec_ind :
771 772 773 774 775 776 777 778
  specO product_positive_order ZZle (fun cost =>
    forall a `{Inhab a} i (x: a) (ts: rlist_ a) p L,
    inv p ts L ->
    0 <= i < length L ->
    app update [i x ts]
      PRE (\$ cost (p, length ts))
      POST (fun ts' => \[inv p ts' L[i := x]]))
  (fun '(m, n) => m + n).
Armaël Guéneau's avatar
Armaël Guéneau committed
779
Proof.
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
  xspecO_refine recursive. intros costf ? ? ?.
  intros a_ Ia i x ts. revert i x. induction ts; introv I Bi.
  { inv. math. }
  { inverts I; xcf. weaken. xpay.
    xmatch.
    { subst. xapps~. intros; unpack. xret~. hsimpl~. constructors~.
      intros Hnil. rewrite <-length_zero_eq_eq_nil in Hnil.
      now rewrite length_update in Hnil. }
    xapps~. unpack; subst.
    forwards~: btree_length_correct. forwards~: btree_size_correct.
    xif.
    { xapps~. intros. xret. hsimpl. rewrite~ update_app_l. constructors~. }
    { xapps~. xapps~. now rew_list~ in *. intros. xret. hsimpl.
      erewrite update_app_r with (i := 2^p). constructors~. all: math. }

    rew_list. rew_cost. asserts~ [HH1 HH2]: (0 <= p /\ 0 <= length ts).
    generalize p (length ts) HH1 HH2; defer. }

  close_cost.
  begin defer assuming a b c d.
  exists (fun '(m, n) => a*m + b*n + c*cost update_tree_spec (m+n) + d).
  split.
  { intros m n Hm Hn. math_rewrite (m+(1+n) = m+1+n).
    rewrite~ (cost_monotonic update_tree_spec m (m+1+n)).
    cancel (cost update_tree_spec (m+1+n)). case_max; auto with zarith; defer.
    cancel m; cancel n. all: repeat case_max. all: defer. }
  cleanup_cost.
  { intros [m n] [m' n'] [Hm Hn].
    assert (0 <= a /\ 0 <= b /\ 0 <= c) by (deferred; math_lia); unpack.
    forwards~ M: cost_monotonic update_tree_spec (m+n) (m'+n').
    auto with zarith. }

Armaël Guéneau's avatar
Armaël Guéneau committed
812 813 814 815 816 817
  repeat apply_nary dominated_sum_distr_nary;
    try apply_nary dominated_mul_cst_l_1_nary; dominated.
  eapply dominated_comp_eq with
    (J := Z_filterType) (p := fun '(t,x) => t + x).
  apply (cost_dominated update_tree_spec). limit.
  intros [? ?]. reflexivity. intros [? ?]. reflexivity.
818 819

  end defer. elia.
Armaël Guéneau's avatar
Armaël Guéneau committed
820 821 822
Qed.

Lemma update_spec :
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
  specZ [cost \in_O Z.log2]
    (forall a `{Inhab a} i (x: a) (ts: rlist_ a) L,
     Rlist ts L ->
     0 <= i < length L ->
     app update [i x ts]
       PRE (\$ cost (length L))
       POST (fun l' => \[Rlist l' L[i := x]])).
Proof.
  xspecO (fun x => cost update_spec_ind (0, Z.log2 ((2 * x) + 1))).
  { intros. weaken. xapplys~ update_spec_ind.
    apply cost_monotonic. splits~. rewrite~ ts_bound_log. math. }
  { monotonic. }
  { etransitivity. apply dominated_comp. apply cost_dominated.
    unfold product_positive_order. limit. dominated. }
Qed.
Armaël Guéneau's avatar
Armaël Guéneau committed
838 839

End BinaryRandomAccessListSpec.