Demo_proof.v 13 KB
Newer Older
charguer's avatar
init  
charguer committed
1
Set Implicit Arguments.
charguer's avatar
charguer committed
2
Require Import CFLib LibInt LibWf (*Facts*) Demo_ml.
charguer's avatar
init  
charguer committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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 101 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 167 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 200 201 202 203 204 205

(*************************************************************************)
(* If demo *)



Lemma if_demo_spec :
  Spec if_demo (b:bool) |B>>
  B [] (fun (x:int) => [0 <= x <= 1]).
Proof.
  xcf. intros. xapps. xapps. 
  xseq (Hexists (x:int), r ~~> x \* [x = 0 \/ x = 1] \* s ~~> 5).
  xif. xapps. hsimpl~. xrets~.
  introv C. xapps. hsimpl. subst. invert~ C. 
Qed.

(*

let if_demo b =
  let r = ref 0 in
  let s = ref 5 in
  if b 
     then incr r; 
  !r

*)


(*************************************************************************)
(* Arrays as sequences *)

Ltac index_solve := subst; rew_arr; math.
Hint Extern 1 (index _ _) => index_solve.

Lemma array_demo_spec :
  Spec array_demo () |B>> 
    B [] (fun n => [n = 3]).
Proof.
  xcf.
  xapp. math. intros L (LL&LV).
  xapp. math. intros Ex.
  (* xapp. math. sets_eq L': (L[2:=4]). *)
  (* xapp_spec ml_array_set_named_spec. math. intros L' EL'. *)
  xapp_by named. math. intros L' EL'. 
  xapp. subst. rew_arr. math. intros Ey. rewrite EL' in Ey. (*rewrite read_write_eq in Ey.*) rew_arrs.
  xapp. auto. (* index_solve. *) (* subst. rew_arr. math. *) intros Ez. 
    rewrite EL' in Ez. rew_arrs. case_if.
  xapp. hsimpl. subst. rew_arrs. math.  
Qed.

(*
let array_demo () =
  let t = Array.make 3 0 in
  let x = t.(1) in
  t.(2) <- 4;
  let y = t.(2) in
  let z = t.(1) in
  Array.length t

*)


(*************************************************************************)
(* Simple records *)

(* this definition will later not be needed *)
Notation "'RMyrec'" := (Myrec Id Id).

(* "r ~> RMyrec n L" asserts that at location "r" 
   we find a record of type "myrec" with field "nb"
   storing "n" and the field "items" storing "L". *)

(* Note that "myrec A" is a type equivalent to "loc" *)

(*
let myrec_incr_nb r =
  r.nb <- r.nb + 1 
*)

Lemma myrec_incr_nb_spec : forall A,
  Spec myrec_incr_nb (r:myrec A) |B>> 
    forall n (L:list A),
    B (r ~> RMyrec n L) (# r ~> RMyrec (n+1) L).
Proof.
  xcf. intros.
  xapp. intro_subst. (* read *)
  xapps. (* write *)
  hsimpl.
Qed.

(* below, "keep B H Q" is the same as "B H (Q \*+ H)";
   here, it is the same as 
   "B (r ~> RMyrec n L)
      (fun (k:int) => [k = LibList.length L] \* r ~> RMyrec n L)."
*)

(*
let myrec_length_items r =
  List.length r.items
*)

Lemma myrec_length_items : forall A,
  Spec myrec_length_items (r:myrec A) |B>> 
    forall n (L:list A),
    keep B (r ~> RMyrec n L) 
           (fun (k:int) => [k = LibList.length L]).
Proof.
  xcf. intros. xapps. xrets. auto. 
Qed.

(* Definition of an invariant on the record, in the form
   of a representation predicate. The predicate
   "r ~> SizedList Q" asserts that at location "r" we find
   a record with fields "n" and "Q" such that "n" is always
   equal to the length of the list Q. *)

Definition SizedList A (Q:list A) (r:myrec A) :=
  Hexists n, r ~> RMyrec n Q \* [n = LibList.length Q].

Lemma SizedList_unfocus : forall A (r:myrec A) (Q:list A) (n:int),
  n = LibList.length Q ->
  (r ~> RMyrec n Q) ==> (r ~> SizedList Q).
Proof.
  introv E. hunfold SizedList. hsimpl. auto.
Qed.

Implicit Arguments SizedList_unfocus [A].


(* remark: the following definition is equivalent ot the above.
*)
  
Definition SizedList' A (Q:list A) (r:myrec A) :=
  Hexists n L, r ~> RMyrec n L \* 
   [n = LibList.length L /\ L = Q].

Lemma SizedList_equiv : forall A (Q:list A) (r:myrec A),
  r ~> SizedList Q  ==>  r ~> SizedList' Q.
Proof.
  intros. hunfold SizedList. (* at 1 *) 
  (* unfold SizedList. hdata_simpl. *)
  hunfold SizedList'.
  hextract as n En. hsimpl. auto.
Qed.

Lemma SizedList_equiv2 : forall A (Q:list A) (r:myrec A),
  r ~> SizedList' Q  ==>  r ~> SizedList Q.
Proof.
  intros. hunfold SizedList. hunfold SizedList'.
  hextract as n L (En&EL). hsimpl. subst~.
Qed.

Lemma myrec_inv_length_items : forall A,
  Spec myrec_inv_length_items (r:myrec A) |B>> 
    forall (Q:list A),
    keep B (r ~> SizedList Q)
           (fun (k:int) => [k = LibList.length Q]).
Proof.
  xcf. intros. hunfold SizedList at 1. 
  xextract as n En. xapp. intros x. hextract as Ex. subst.
  hchange (SizedList_unfocus r). auto.
  hsimpl. auto.

(*

  (* if we run "xapp" here, we're stuck; try it! *)
  (* xapp. *)
  (* use "hunfold" to unfold SizedList in the heap predicate *)
  hunfold SizedList at 1. 
  (* note that we added "at 1" to avoid unfolding the definition 
     in the post-condition as well; but it would be fine to just
     do "hunfold SizedList" if we didn't care for readability *)
  (* extract the existential that appears in the pre-condition *)
  xextract as n En.
  (* read the field nb *)
  xapp.
  (* if we run "hsimpl" here, we're stuck: try it! *)
  (* hsimpl. *)
  (* unfold "SizedList" in the post-condition *)
  hunfold SizedList.
  (* simplify *)
  hsimpl. subst~. subst~.
*)
Qed.

(* here, we show that by adding an item to "items" and by 
   incrementing "nb", we correctly preserve the invariant. *)

Lemma myrec_inv_push : forall A,
  Spec myrec_inv_push (x:A) (r:myrec A) |B>> 
    forall (Q:list A),
    B (r ~> SizedList Q) (# r ~> SizedList (x::Q)).
Proof.
  xcf. intros. 
   (* hunfold SizedList. xapps.
   xapp. xapps. xapp. hsimpl. rew_list. math. *)
  hunfold SizedList at 1. xapps.
   xapp. xapps. xapp.
  (*  hunfold SizedList. hsimpl. rew_list. math. *)
  hchange (SizedList_unfocus r). rew_list. math. hsimpl.    
Qed.


charguer's avatar
charguer committed
206 207 208 209

(*************************************************************************)
(*************************************************************************)

charguer's avatar
init  
charguer committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 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 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
(*

(*************************************************************************)
(* Partial recursive function *)

(** Step-by-step proof, by induction on [n] *)

Lemma half_spec_1 :
  forall n x, n >= 0 -> x = n+n -> 
  (App half x;) [] (\= n).
Proof.
  intros n. induction_wf IH: (int_downto_wf 0) n.
  introv P D. xcf_app.
  xif.
    xret. hsimpl. math.
    xif.
      xfail. math.
      xlet. 
        xapply (IH (n-1)).
          hnf. math. (* or auto with maths. *)
          math.
          math.
          hsimpl.
          xok.
        xextract as E. xret. hsimpl. math.
Qed.

(** Step-by-step proof, by induction on [x] *)

Lemma half_spec_2 :
  forall x n, n >= 0 -> x = n+n -> 
  (App half x;) [] (\= n).
Proof.
  intros x. induction_wf IH: (int_downto_wf 0) x.
  introv P D. xcf_app.
  xif.
    xret. hsimpl. math.
    xif.
      xfail. math.
      xlet. 
        xapply (>> IH (x-2) (n-1)).
          hnf. math. (* or auto with maths. *)
          math.
          math.
          hsimpl.
          xok.
        xextract as E. xret. hsimpl. math.
Qed.

(** A better proofs, by induction on [x] *)

(* TODO: fix this
Lemma half_spec_3 :
  Spec half (x:int) |R>>
    forall n, n >= 0 -> x = n+n -> 
    R [] (\= n).
Proof.
  xinduction (downto 0). xcf.
  introv IH P D.
  repeat xif.
  xrets. math.
  xfail. math.
  xapp (n-1).
    auto with maths.
    math.
    math.
  intro_subst. xrets. math.
Qed.
*)

(** An optimized proofs, by induction on [x] *)

Ltac auto_tilde ::= auto with maths.

(* 
TODO: fix this
Lemma half_spec_4 :
  Spec half (x:int) |R>>
    forall n, n >= 0 -> x = n+n -> 
    R [] (\= n).
Proof.
  xinduction (downto 0). xcf. introv IH P D.
  repeat xif. xgo~. xgo~. xapp~ (n-1). xgo~.
Qed.
*)

(** An optimized proofs, by induction on [n] *)

Lemma half_spec_5 :
  Spec half (x:int) |R>>
    forall n, n >= 0 -> x = n+n -> 
    R [] (\= n).
Proof.
  xinduction_heap (unproj21 int (downto 0)).
  xcf. intros x n IH P D.
  repeat xif. xgo~. xgo~. xapp~ (n-1). xgo~.
Qed.




(*************************************************************************)
(* Partial apps *)

Lemma add_spec : 
  Spec add x y |R>> R [] (\= (x + y)).
Proof. xgo*. Qed.

Hint Extern 1 (RegisterSpec add) => Provide add_spec.

Lemma hof_spec : forall A B,
  Spec hof (x:A) (f:func) |R>> forall (H:hprop) (Q:B->hprop),
    (App f x; H Q) -> R H Q.
Proof. xcf. auto. Qed.

Hint Extern 1 (RegisterSpec hof) => Provide hof_spec.

Lemma test_partial_app_1_spec : 
  Spec test_partial_app_1 () |R>> R [] (\= 3).
Proof.
  xcf. xapp_partial. intros Sg. xapp. hsimpl*. 
  (* or: xcf. xlet. xframe - []. xapp_partial. xok. xextract. intros Sg. xapp. hsimpl*. *)
Qed. 


Lemma test_partial_app_1_spec' : forall x,
  Spec test_partial_app_1 () |R>> R (x ~~> 4) (fun r => [r = 3] \* x ~~> 4).
Proof.
  xcf. xapp_partial. intros Sg. xapp. hsimpl*.
Qed.


Lemma test_partial_app_1_spec'' : forall x,
  Spec test_partial_app_1 () |R>> R (x ~~> 4) (fun r => [r = 3] \* x ~~> 4).
Proof.
  xcf. xlet.  (* how to execute xapp_partial step by step *)
  
  eapply local_wframe. xlocal.
  xapp_show_spec.
  let arity_goal := spec_goal_arity tt in
   let arity_hyp := spec_term_arity H in
   let lemma := get_spec_elim_x_y arity_hyp arity_goal in
   lets K: lemma;
   lets K': (>> lemma H).
  apply K'.
  hsimpl.
  try xok.
  xextract. 

  intros Sg. xapp. hsimpl*.
Qed.



Lemma test_partial_app_2_spec : 
  Spec test_partial_app_2 () |R>> R [] (\= 4).
Proof.
  xcf. xapp_partial as P1. intros HP1.
  xapp []. (* effectively inline the code of the function [hof] *)
           (* -> the empty heap is used to instantiate the variable [H] from [hof_spec] *)
  xapp. (* reason about the application of function [P1] *)
  hsimpl*.
Qed. 

Lemma test_partial_app_2_spec' : 
  Spec test_partial_app_2 () |R>> R [] (\= 4).
Proof.
  xcf. xapp_partial as P1. intros HP1.
  xapp. (* if we do not provide the argument [H] *)
  xapp.
  instantiate (1 := []). hsimpl. (* then we later need to instantiate it manually *)
     (* even though in this particular example, a more powerful version of 
        hsimpl should be able to do the unification properly *)
  hsimpl*.
Qed.
 
Lemma func4_spec : 
  Spec func4 a b c d |R>> R [] (\= (a + b + c + d)).
Proof. xgo*. Qed.

Hint Extern 1 (RegisterSpec func4) => Provide func4_spec.


Lemma test_partial_app_3_spec : 
  Spec test_partial_app_3 () |R>> R [] (\= 30).
Proof.
  xcf.
  xapp_partial. intros Hf1. 
  xapp_partial. intros Hf2.
  xapp_partial. intros Hf3.
  xapps.
  xapps.
  xapps.
  xret*.
Qed.


(*************************************************************************)
(* Inlining *)

Lemma test_inlining_spec : 
  Spec test_inlining () |R>> R [] (\= 4).
Proof.
  xcf.
  xfun f. (* use the most-general spec for specifying the local function *)
  simpl in Sf. (* optional clean up *)
  xcf_app. (* reason on [hof] using directly its caracteristic formula *)
  xcf_app. (* reason on [f] using [Sf] *)
  xret*.
Qed.


(*************************************************************************)
(* Linked lists *)

(*

(* [cell A] is equivalent to [loc] *)
Print cell.

(* [Cell] is the representation predicate, 
   so you can write [x ~> Cell T1 T2 V1 V2]. *)

Check Cell.

(* Focus and unfocus operations *)

Check Cell_focus.
Check Cell_unfocus.

(* Specification of creation and accessors for fields *)

Check _new_cell_spec.
Check _get_next_spec.
Check _set_next_spec.
Check _get_content_spec.
Check _set_content_spec.

*)

(* We can define our own representation on top of [Cell] *)

Fixpoint CList a A (T:A->a->hprop) (L:list A) (l:loc) : hprop :=
  match L with
  | nil => []
  | X::L' => l ~> Cell T (CList T) X L'
  end.

(* We can show that allocating a Cell extends a CList *)

Lemma CList_from_Cell : forall l a x q A (T:A->a->hprop) X Q,
  l ~> Cell Id Id x q \* x ~> T X \* q ~> CList T Q
  ==> l ~> CList T (X::Q).
Proof. intros. hchange (Cell_unfocus l). hsimpl. Qed.

Lemma CList_to_Cell : forall l a A (T:A->a->hprop) X Q,
  l ~> CList T (X::Q) ==> Hexists x q,
  l ~> Cell Id Id x q \* x ~> T X \* q ~> CList T Q.
Proof. intros. hdata_simpl CList. fold CList. hchange (Cell_focus l). hsimpl. Qed.

Implicit Arguments CList_from_Cell [a A].
Implicit Arguments CList_to_Cell [a A].
Opaque CList.

(** From there, we can prove the function [newx] *)

(*
let newx x q1 q2 = 
   let machin = {content = x; next = q1} in 
   machin.next <- q2 
*)

Lemma newx_spec : forall a,
  Spec newx (x:a) (q1:loc) (q2:loc) |R>>
    forall A (T:A->a->hprop) (X:A) (L1 L2 : list A),
    R (x ~> T X \* q1 ~> CList T L1 \* q2 ~> CList T L2) 
      (fun l => l ~> CList T (X::L2)).
Proof.
  xcf. (* apply the characteristic formula *)
  intros. (* introduces the arguments *)
  xapp. (* reason on application *)
  (* at this point we could call [xapp] directly, but let's
     first see how we can build a clean CList first *)
  xchange (CList_from_Cell machin). 
  (* at this point we cannot call [xapp] because 
     updating the record requires a [Cell] *)
  xchange (CList_to_Cell machin). xextract as y q.
  (* now we can continue *)
  xapp. (* reason on application *) 
  (* We could be tempted to conclude with [xret], but that
     would not work since we need to do some garbage collection *)
  xret. (* conclude and create an existential for the discarded heap *)
  (* finally we need to rebuild the CList as earlier on *)
  hchange (CList_from_Cell machin). 
  hsimpl.
Qed.

(** A specification proof is always followed with a line of the 
    following form, for registering the specification so that it
    can be automatically used to reason about a call to the function *)

Hint Extern 1 (RegisterSpec newx) => Provide newx_spec.

*)