CFPrimSpec.v 14.3 KB
Newer Older
charguer's avatar
charguer committed
1
Set Implicit Arguments.
charguer's avatar
charguer committed
2 3
Require Export CFSpec CFPrint CFPrim 
  LibListZ LibMap LibInt.
charguer's avatar
charguer committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Generalizable Variables a A. 


(********************************************************************)
(** Imperative Representation for base types *)

Global Opaque heap_is_empty hdata heap_is_single heap_is_empty_st. 
 (* todo: check needed *)

Global Opaque Zplus. (* todo: move *)

Transparent hdata. (* todo: should use hdata_simpl instead *)

Require Import LibSet.
Open Scope container_scope.

(*------------------------------------------------------------------*)
(* ** References *)

Definition Ref a A (T:htype A a) (V:A) (l:loc) :=
  Hexists v, heap_is_single l v \* v ~> T V.

charguer's avatar
charguer committed
26 27 28 29 30 31 32 33
Instance Ref_Heapdata : forall a A (T:htype A a),
  (Heapdata (@Ref a A T)).
Proof using.
  intros. applys Heapdata_prove. intros x X1 X2.
  unfold Ref. hdata_simpl. hextract as x1 x2.
  hchange (@star_is_single_same_loc x). hsimpl. 
Qed.

charguer's avatar
charguer committed
34 35
Open Local Scope heap_scope_advanced.

charguer's avatar
charguer committed
36
Notation "'~~>' v" := (~> Ref (@Id _) v)
charguer's avatar
charguer committed
37
  (at level 32, no associativity) : heap_scope_advanced.
charguer's avatar
charguer committed
38 39 40 41 42 43

(*
Notation "l '~~>' v" := (l ~> Ref (@Id _) v)
  (at level 32, no associativity) : heap_scope. 
*)
Notation "l '~~>' v" := (hdata (@Ref _ _ (@Id _) v) l)
charguer's avatar
charguer committed
44 45 46 47 48 49 50 51 52 53 54
  (at level 32, no associativity) : heap_scope.

Lemma focus_ref : forall (l:loc) a A (T:htype A a) V,
  l ~> Ref T V ==> Hexists v, l ~~> v \* v ~> T V.
Proof. intros. unfold Ref, hdata. unfold Id. hsimpl~. Qed.

Lemma unfocus_ref : forall (l:loc) a (v:a) A (T:htype A a) V,
  l ~~> v \* v ~> T V ==> l ~> Ref T V.
Proof. intros. unfold Ref. hdata_simpl. hsimpl. subst~. Qed.

Lemma heap_is_single_impl_null : forall (l:loc) A (v:A),
charguer's avatar
charguer committed
55
  heap_is_single l v ==> heap_is_single l v \* \[l <> null].
charguer's avatar
charguer committed
56 57 58 59 60
Proof.
  intros. intros h Hh. forwards*: heap_is_single_null. exists___*.
Qed.

Lemma focus_ref_null : forall a A (T:htype A a) V,
charguer's avatar
charguer committed
61
  null ~> Ref T V ==> \[False].
charguer's avatar
charguer committed
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
Proof.
  intros. unfold Ref, hdata. hextract as v.
  hchanges (@heap_is_single_impl_null null).
Qed.

Global Opaque Ref.
Implicit Arguments focus_ref [a A].
Implicit Arguments unfocus_ref [a A].


(*------------------------------------------------------------------*)
(* ** Tuple 2 *)

Definition Tup2 A1 A2 a1 a2 (T1:A1->a1->hprop) (T2:A2->a2->hprop) P p :=
  let '(X1,X2) := P in let '(x1,x2) := p in x1 ~> T1 X1 \* x2 ~> T2 X2.

Lemma focus_tup2 : forall a1 a2 (p:a1*a2) A1 A2 (T1:htype A1 a1) (T2:htype A2 a2) V1 V2,
  p ~> Tup2 T1 T2 (V1,V2) ==> let '(x1,x2) := p in x1 ~> T1 V1 \* x2 ~> T2 V2.
Proof. auto. Qed.

Lemma unfocus_tup2 : forall a1 (x1:a1) a2 (x2:a2) A1 A2 (T1:htype A1 a1) (T2:htype A2 a2) V1 V2,
  x1 ~> T1 V1 \* x2 ~> T2 V2 ==> (x1,x2) ~> Tup2 T1 T2 (V1,V2).
Proof. intros. unfold Tup2. hdata_simpl. auto. Qed.

Global Opaque Tup2.

(*------------------------------------------------------------------*)
(* ** Tuple 3 *)

Definition Tup3 A1 A2 A3 a1 a2 a3 (T1:A1->a1->hprop) (T2:A2->a2->hprop) (T3:A3->a3->hprop) P p :=
  let '(X1,X2,X3) := P in let '(x1,x2,x3) := p in x1 ~> T1 X1 \* x2 ~> T2 X2 \* x3 ~> T3 X3.

Lemma focus_tup3 : forall a1 a2 a3 (p:a1*a2*a3) A1 A2 A3 (T1:htype A1 a1) (T2:htype A2 a2) (T3:A3->a3->hprop) V1 V2 V3,
  p ~> Tup3 T1 T2 T3 (V1,V2,V3) ==> let '(x1,x2,x3) := p in x1 ~> T1 V1 \* x2 ~> T2 V2 \* x3 ~> T3 V3.
Proof. auto. Qed.

Lemma unfocus_tup3 : forall a1 (x1:a1) a2 (x2:a2) a3 (x3:a3) A1 A2 A3 (T1:htype A1 a1) (T2:htype A2 a2) (T3:A3->a3->hprop) V1 V2 V3,
  x1 ~> T1 V1 \* x2 ~> T2 V2 \* x3 ~> T3 V3 ==> (x1,x2,x3) ~> Tup3 T1 T2 T3 (V1,V2,V3).
Proof. intros. unfold Tup3. hdata_simpl. auto. Qed.

Global Opaque Tup3.


(*------------------------------------------------------------------*)
(* ** Lists *)

Fixpoint List A a (T:A->a->hprop) (L:list A) (l:list a) : hprop :=
  match L,l with
charguer's avatar
charguer committed
110
  | nil, nil => \[l = nil] (* %todo: True*)
charguer's avatar
charguer committed
111
  | X::L', x::l' => x ~> T X \* l' ~> List T L'
charguer's avatar
charguer committed
112
  | _,_=> \[False]
charguer's avatar
charguer committed
113 114 115
  end.

Lemma focus_nil : forall A a (T:A->a->hprop),
charguer's avatar
charguer committed
116
  \[] ==> nil ~> List T nil.
charguer's avatar
charguer committed
117 118 119
Proof. intros. simpl. hdata_simpl. hsimpl~. Qed.

Lemma unfocus_nil : forall a (l:list a) A (T:A->a->hprop),
charguer's avatar
charguer committed
120
  l ~> List T nil ==> \[l = nil].
charguer's avatar
charguer committed
121 122 123
Proof. intros. simpl. hdata_simpl. destruct l. auto. hsimpl. Qed.

Lemma unfocus_nil' : forall A (L:list A) a (T:A->a->hprop),
charguer's avatar
charguer committed
124
  nil ~> List T L ==> \[L = nil].
charguer's avatar
charguer committed
125 126 127 128 129 130 131 132
Proof.
  intros. destruct L.
  simpl. hdata_simpl. hsimpl~.
  simpl. hdata_simpl. hsimpl.
Qed.

Lemma focus_cons : forall a (l:list a) A (X:A) (L':list A) (T:A->a->hprop),
  (l ~> List T (X::L')) ==>
charguer's avatar
charguer committed
133
  Hexists x l', (x ~> T X) \* (l' ~> List T L') \* \[l = x::l'].
charguer's avatar
charguer committed
134 135 136 137 138 139 140 141
Proof.
  intros. simpl. hdata_simpl. destruct l as [|x l'].
  hsimpl.
  hsimpl~ x l'.
Qed.

Lemma focus_cons' : forall a (x:a) (l:list a) A (L:list A) (T:A->a->hprop),
  (x::l) ~> List T L ==> 
charguer's avatar
charguer committed
142
  Hexists X L', \[L = X::L'] \* (x ~> T X) \* (l ~> List T L').
charguer's avatar
charguer committed
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
Proof. intros. destruct L; simpl; hdata_simpl; hsimpl~. Qed.

Lemma unfocus_cons : forall a (x:a) (l:list a) A (X:A) (L:list A) (T:A->a->hprop),
  (x ~> T X) \* (l ~> List T L) ==> 
  ((x::l) ~> List T (X::L)).
Proof. intros. simpl. hdata_simpl. hsimpl. Qed.

Global Opaque List.


(************************************************************)
(** Locations *)

Parameter ml_phy_eq_spec : Spec ml_phy_eq x y |R>> 
  pure R (= (x '= y :> loc)).

Parameter ml_phy_neq_spec : Spec ml_phy_neq x y |R>> 
  pure R (= (x '<> y :> loc)).

Hint Extern 1 (RegisterSpec ml_phy_eq) => Provide ml_phy_eq_spec.
Hint Extern 1 (RegisterSpec ml_phy_neq) => Provide ml_phy_neq_spec.


(************************************************************)
(** References *)

Parameter ml_ref_spec : forall a,
  Spec ml_ref (v:a) |R>> 
charguer's avatar
charguer committed
171
    R \[] (~~> v).
charguer's avatar
charguer committed
172 173 174

Parameter ml_get_spec : forall a,
  Spec ml_get (l:loc) |R>> 
charguer's avatar
charguer committed
175
    forall (v:a), keep R (l ~~> v) (fun x => \[x = v]).
charguer's avatar
charguer committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

Parameter ml_set_spec : forall a,
  Spec ml_set (l:loc) (v:a) |R>> 
    forall (v':a), R (l ~> Ref Id v') (# l ~> Ref Id v).

Parameter ml_sset_spec : forall a a',
  Spec ml_sset (l:loc) (v:a) |R>> 
    forall (v':a'), R (l ~~> v') (# l ~~> v).

Parameter ml_incr_spec : 
  Spec ml_incr (l:loc) |R>> 
    forall n, R (l ~~> n) (# l ~~> (n+1)).
 
Parameter ml_decr_spec : 
  Spec ml_decr (l:loc) |R>> 
    forall n, R (l ~~> n) (# l ~~> (n-1)).

Parameter ml_free_spec : 
  Spec ml_free (l:loc) |R>> 
charguer's avatar
charguer committed
195
    forall A (v:A), R (l ~> Ref Id v) (# \[]).
charguer's avatar
charguer committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

Hint Extern 1 (RegisterSpec ml_ref) => Provide ml_ref_spec.
Hint Extern 1 (RegisterSpec ml_get) => Provide ml_get_spec.
Hint Extern 1 (RegisterSpec ml_set) => Provide ml_set_spec.
Hint Extern 1 (RegisterSpec ml_sset) => Provide ml_sset_spec.
Hint Extern 1 (RegisterSpec ml_incr) => Provide ml_incr_spec.
Hint Extern 1 (RegisterSpec ml_decr) => Provide ml_decr_spec.
Hint Extern 1 (RegisterSpec ml_free) => Provide ml_free_spec.


(************************************************************)
(** Group of References *)

Require Import CFTactics.

Parameter ml_ref_spec_group : forall a,
  Spec ml_ref (v:a) |R>> forall (M:map loc a),
    R (Group (Ref Id) M) (fun (l:loc) => 
charguer's avatar
charguer committed
214
       Group (Ref Id) (M[l:=v]) \* \[l \notindom M]).
charguer's avatar
charguer committed
215 216 217 218

Lemma ml_get_spec_group : forall a,
  Spec ml_get (l:loc) |R>> forall (M:map loc a), 
    forall `{Inhab a}, l \indom M ->
charguer's avatar
charguer committed
219
    keep R (Group (Ref Id) M) (fun x => \[x = M[l]]).
charguer's avatar
charguer committed
220 221
Proof.
  intros. xweaken. intros l R LR HR M IA IlM. simpls.
charguer's avatar
charguer committed
222
  rewrite~ (Group_rem l M). xapply (HR (M[l])); hsimpl~. 
charguer's avatar
charguer committed
223 224 225 226 227
Qed.

Lemma ml_set_spec_group : forall a, 
  Spec ml_set (l:loc) (v:a) |R>> forall (M:map loc a), 
    forall `{Inhab a}, l \indom M ->
charguer's avatar
charguer committed
228
    R (Group (Ref Id) M) (# Group (Ref Id) (M[l:=v])).
charguer's avatar
charguer committed
229 230 231
Proof.
  intros. xweaken. intros l v R LR HR M IA IlM. simpls.
  rewrite~ (Group_rem l M).
charguer's avatar
charguer committed
232
  xapply (HR (M[l])). hsimpl.
charguer's avatar
charguer committed
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
  intros _. hchanges~ (Group_add' l M).
Qed.


(************************************************************)
(** Boolean *)

Parameter ml_eqb_int_spec : Spec ml_eqb (x:int) (y:int) |R>>
  pure R (= (x '= y)).

Parameter ml_and_spec : Spec ml_and x y |R>> 
  pure R (= (x && y)).

Parameter ml_or_spec : Spec ml_or x y |R>> 
  pure R (= (x || y)).

Hint Extern 1 (RegisterSpec ml_eqb) => Provide ml_eqb_int_spec.
Hint Extern 1 (RegisterSpec ml_and) => Provide ml_and_spec.
Hint Extern 1 (RegisterSpec ml_and) => Provide ml_or_spec.


(************************************************************)
(** Arithmetic *)

Parameter ml_plus_spec : Spec ml_plus x y |R>> 
  pure R (= (x + y)%Z).

Parameter ml_minus_spec : Spec ml_minus x y |R>> 
  pure R (= (x - y)%Z).

Parameter ml_div_spec : Spec ml_div x y |R>>
  (y <> 0) ->
265 266 267 268 269 270 271 272 273 274 275 276 277
  R \[] (fun z => \[z = Z.quot x y]).

Goal Z.quot 5 3 = 1.
Proof. reflexivity. Qed.

Goal Z.quot (-5) 3 = -1.
Proof. reflexivity. Qed.

Goal Z.quot 5 (-3) = -1.
Proof. reflexivity. Qed.

Goal Z.quot (-5) (-3) = 1.
Proof. reflexivity. Qed.
charguer's avatar
charguer committed
278 279 280

Parameter ml_mod_spec : Spec ml_mod x y |R>>
  (y <> 0) ->
281 282 283 284 285 286 287 288 289 290 291 292 293
  R \[] (fun z => \[z = Z.rem x y]).

Goal Z.rem 5 3 = 2.
Proof. reflexivity. Qed.

Goal Z.rem (-5) 3 = -2.
Proof. reflexivity. Qed.

Goal Z.rem 5 (-3) = 2.
Proof. reflexivity. Qed.

Goal Z.rem (-5) (-3) = -2.
Proof. reflexivity. Qed.
charguer's avatar
charguer committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

Parameter ml_leq_spec : Spec ml_leq x y |R>> 
  pure R (= (x <= y)%I).

Parameter ml_geq_spec : Spec ml_geq x y |R>> 
  pure R (= (x >= y)%I).

Parameter ml_lt_spec : Spec ml_lt x y |R>> 
  pure R (= (x < y)%I).

Parameter ml_gt_spec : Spec ml_gt x y |R>> 
  pure R (= (x > y)%I).

Hint Extern 1 (RegisterSpec ml_plus) => Provide ml_plus_spec.
Hint Extern 1 (RegisterSpec ml_minus) => Provide ml_minus_spec.
Hint Extern 1 (RegisterSpec ml_div) => Provide ml_div_spec.
Hint Extern 1 (RegisterSpec ml_mod) => Provide ml_mod_spec.
Hint Extern 1 (RegisterSpec ml_leq) => Provide ml_leq_spec.
Hint Extern 1 (RegisterSpec ml_geq) => Provide ml_geq_spec.
Hint Extern 1 (RegisterSpec ml_lt) => Provide ml_lt_spec.
Hint Extern 1 (RegisterSpec ml_gt) => Provide ml_gt_spec.

316
(* ********************************************************************** *)
317
(* Bitwise arithmetic operators. *)
318 319

Parameter ml_land_spec : Spec ml_land x y |R>>
320 321 322 323 324 325 326 327 328 329 330 331 332
  pure R (= (Z.land x y)).

Goal Z.land 7 28 = 4.
Proof. reflexivity. Qed.

Goal Z.land (-7) 28 = 24.
Proof. reflexivity. Qed.

Goal Z.land 7 (-28) = 4.
Proof. reflexivity. Qed.

Goal Z.land (-7) (-28) = -32.
Proof. reflexivity. Qed.
333 334

Hint Extern 1 (RegisterSpec ml_land) => Provide ml_land_spec.
charguer's avatar
charguer committed
335

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
Parameter ml_lor_spec : Spec ml_lor x y |R>>
  pure R (= (Z.lor x y)).

Goal Z.lor 7 28 = 31.
Proof. reflexivity. Qed.

Goal Z.lor (-7) 28 = -3.
Proof. reflexivity. Qed.

Goal Z.lor 7 (-28) = -25.
Proof. reflexivity. Qed.

Goal Z.lor (-7) (-28) = -3.
Proof. reflexivity. Qed.

Hint Extern 1 (RegisterSpec ml_lor) => Provide ml_lor_spec.

POTTIER Francois's avatar
POTTIER Francois committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
Parameter ml_lxor_spec : Spec ml_lxor x y |R>>
  pure R (= (Z.lxor x y)).

Goal Z.lxor 7 28 = 27.
Proof. reflexivity. Qed.

Goal Z.lxor (-7) 28 = -27.
Proof. reflexivity. Qed.

Goal Z.lxor 7 (-28) = -29.
Proof. reflexivity. Qed.

Goal Z.lxor (-7) (-28) = 29.
Proof. reflexivity. Qed.

Hint Extern 1 (RegisterSpec ml_lxor) => Provide ml_lxor_spec.

POTTIER Francois's avatar
POTTIER Francois committed
370 371 372 373 374 375 376 377 378 379 380 381 382
Definition lnot (x : Z) : Z := -(x + 1).

Parameter ml_lnot_spec : Spec ml_lnot x |R>>
  pure R (= (lnot x)).

Goal lnot 44 = -45.
Proof. reflexivity. Qed.

Goal lnot (-44) = 43.
Proof. reflexivity. Qed.

Hint Extern 1 (RegisterSpec ml_lnot) => Provide ml_lnot_spec.

POTTIER Francois's avatar
POTTIER Francois committed
383 384 385 386 387 388 389 390 391 392 393 394 395
Parameter ml_lsl_spec : Spec ml_lsl x y |R>>
  0 <= y ->
  (* y < Sys.word_size -> *)
  pure R (= (Z.shiftl x y)).

Goal Z.shiftl 7 2 = 28.
Proof. reflexivity. Qed.

Goal Z.shiftl (-7) 2 = -28.
Proof. reflexivity. Qed.

Hint Extern 1 (RegisterSpec ml_lsl) => Provide ml_lsl_spec.

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
Parameter ml_lsr_spec : Spec ml_lsr x y |R>>
  (* We temporarily? restrict [lsr] to nonnegative integers,
     so it behaves like [asr]. Anyway, [lsr] really operates
     on unsigned integers, and this notion is missing in CFML. *)
  0 <= x ->
  0 <= y ->
  (* y < Sys.word_size -> *)
  pure R (= (Z.shiftr x y)).

Goal Z.shiftr 7 2 = 1.
Proof. reflexivity. Qed.

Hint Extern 1 (RegisterSpec ml_lsr) => Provide ml_lsr_spec.

Parameter ml_asr_spec : Spec ml_asr x y |R>>
  0 <= y ->
  (* y < Sys.word_size -> *)
  pure R (= (Z.shiftr x y)).

Goal Z.shiftr 7 2 = 1.
Proof. reflexivity. Qed.

Goal Z.shiftr (-7) 2 = -2.
Proof. reflexivity. Qed.

Hint Extern 1 (RegisterSpec ml_asr) => Provide ml_asr_spec.

charguer's avatar
charguer committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
(************************************************************)
(** Pairs *)

Parameter ml_fst_spec : forall a1 a2,
  Spec ml_fst (p:a1*a2) |R>> 
    pure R (= fst p).

Parameter ml_snd_spec : forall a1 a2,
  Spec ml_snd (p:a1*a2) |R>> 
    pure R (= snd p).

Hint Extern 1 (RegisterSpec ml_fst) => Provide ml_fst_spec.
Hint Extern 1 (RegisterSpec ml_snd) => Provide ml_snd_spec.


(************************************************************)
(** Arrays *)

441 442 443 444
Parameter ml_max_array_length_spec :
  0 <= ml_max_array_length.
  (* We could also axiomatize that this is a power of 2. *)

charguer's avatar
charguer committed
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
(************************************************************)
(** Lists *)

Parameter ml_list_iter_spec : forall a,
  Spec ml_list_iter f l |R>> forall (I:list a->hprop),
    (forall x t, (App f x;) (I (x::t)) (# I t)) -> 
    R (I l) (# I nil).

Hint Extern 1 (RegisterSpec ml_list_iter) => Provide ml_list_iter_spec.


(************************************************************)
(** Stacks *)

Module Stack_ml.
Definition t (A:Type) := loc.
End Stack_ml. 

Parameter ml_stack_create : func.
Parameter ml_stack_is_empty : func.
Parameter ml_stack_push : func.
Parameter ml_stack_pop : func.


(************************************************************)
(** Stream *)

(* TODO *)
Definition stream := list.
Definition stream_cell := list.


(********************************************************************)
(** IO manipulations *)

Parameter Channel : forall (L:list dynamic) (l:loc), hprop.

Notation "l ~>> L" := (l ~> Channel L)
  (at level 32, no associativity).

Parameter ml_read_int_spec :
  Spec ml_read_int () |R>> forall L (n:int),
charguer's avatar
charguer committed
487
    R (stdin ~>> (dyn n::L)) (fun x => \[x = n] \* stdin ~>> L).
charguer's avatar
charguer committed
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 514 515

Parameter ml_print_int_spec :
  Spec ml_print_int (n:int) |R>> forall L,
    R (stdout ~>> L) (# stdout ~>> L & (dyn n)).

Hint Extern 1 (RegisterSpec ml_read_int) => Provide ml_read_int_spec.
Hint Extern 1 (RegisterSpec ml_print_int) => Provide ml_print_int_spec.

(* Tools for stdio *)

Definition list_dyn A (L:list A) :=
  LibList.map dyn L.

Lemma list_dyn_nil : forall A,
  list_dyn (@nil A) = nil.
Proof. auto. Qed.

Lemma list_dyn_cons : forall A X (L:list A),
  list_dyn (X::L) = (dyn X)::(list_dyn L).
Proof. auto. Qed.

Lemma list_dyn_last : forall A X (L:list A),
  list_dyn (L&X) = (list_dyn L) & (dyn X).
Proof. intros. unfold list_dyn. rew_list~. Qed.

Hint Rewrite list_dyn_nil list_dyn_cons list_dyn_last: rew_app.
Hint Rewrite list_dyn_nil list_dyn_cons list_dyn_last : rew_map.
Hint Rewrite list_dyn_nil list_dyn_cons list_dyn_last: rew_list.