-
Notifications
You must be signed in to change notification settings - Fork 108
/
HeapLift.thy
1740 lines (1546 loc) · 77.5 KB
/
HeapLift.thy
1
2
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
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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
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
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
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
*
* SPDX-License-Identifier: BSD-2-Clause
*)
theory HeapLift
imports
TypHeapSimple
CorresXF
L2Defs
ExecConcrete
AbstractArrays
"CParser.LemmaBucket_C"
begin
definition "L2Tcorres st A C = corresXF st (\<lambda>r _. r) (\<lambda>r _. r) \<top> A C"
lemma L2Tcorres_id:
"L2Tcorres id C C"
by (metis L2Tcorres_def corresXF_id)
lemma L2Tcorres_fail:
"L2Tcorres st L2_fail X"
apply (clarsimp simp: L2Tcorres_def L2_defs)
apply (rule corresXF_fail)
done
(* Abstraction predicates for inner expressions. *)
definition "abs_guard st A C \<equiv> \<forall>s. A (st s) \<longrightarrow> C s"
definition "abs_expr st P A C \<equiv> \<forall>s. P (st s) \<longrightarrow> C s = A (st s)"
definition "abs_modifies st P A C \<equiv> \<forall>s. P (st s) \<longrightarrow> st (C s) = A (st s)"
(* Predicates to enable some transformations on the input expressions
(namely, rewriting uses of field_lvalue) that are best done
as a preprocessing stage (st = id).
The corresTA rules should ensure that these are used to rewrite
any inner expressions before handing off to the predicates above. *)
definition "struct_rewrite_guard A C \<equiv> \<forall>s. A s \<longrightarrow> C s"
definition "struct_rewrite_expr P A C \<equiv> \<forall>s. P s \<longrightarrow> C s = A s"
definition "struct_rewrite_modifies P A C \<equiv> \<forall>s. P s \<longrightarrow> C s = A s"
(* Standard heap abstraction rules. *)
named_theorems heap_abs
(* Rules that require first-order matching. *)
named_theorems heap_abs_fo
(* fun_app2 is like fun_app, but it skips an abstraction.
* We use this for terms like "\<lambda>s a. Array.update a k (f s)".
* FIXME: ideally, the first order conversion code can skip abstractions. *)
lemma abs_expr_fun_app2 [heap_abs_fo]:
"\<lbrakk> abs_expr st P f' f;
abs_expr st Q g' g \<rbrakk> \<Longrightarrow>
abs_expr st (\<lambda>s. P s \<and> Q s) (\<lambda>s a. f' s a (g' s a)) (\<lambda>s a. f s a $ g s a)"
by (simp add: abs_expr_def)
lemma abs_expr_fun_app [heap_abs_fo]:
"\<lbrakk> abs_expr st Y b' b; abs_expr st X a' a \<rbrakk> \<Longrightarrow>
abs_expr st (\<lambda>s. X s \<and> Y s) (\<lambda>s. a' s (b' s)) (\<lambda>s. a s $ b s)"
apply (clarsimp simp: abs_expr_def)
done
lemma abs_expr_constant [heap_abs]:
"abs_expr st \<top> (\<lambda>s. a) (\<lambda>s. a)"
apply (clarsimp simp: abs_expr_def)
done
lemma abs_guard_expr [heap_abs]:
"abs_expr st P a' a \<Longrightarrow> abs_guard st (\<lambda>s. P s \<and> a' s) a"
by (simp add: abs_expr_def abs_guard_def)
lemma abs_guard_constant [heap_abs]:
"abs_guard st (\<lambda>_. P) (\<lambda>_. P)"
by (clarsimp simp: abs_guard_def)
lemma abs_guard_conj [heap_abs]:
"\<lbrakk> abs_guard st G G'; abs_guard st H H' \<rbrakk>
\<Longrightarrow> abs_guard st (\<lambda>s. G s \<and> H s) (\<lambda>s. G' s \<and> H' s)"
by (clarsimp simp: abs_guard_def)
lemma L2Tcorres_modify [heap_abs]:
"\<lbrakk> struct_rewrite_modifies P b c; abs_guard st P' P;
abs_modifies st Q a b \<rbrakk> \<Longrightarrow>
L2Tcorres st (L2_seq (L2_guard (\<lambda>s. P' s \<and> Q s)) (\<lambda>_. (L2_modify a))) (L2_modify c)"
apply (monad_eq simp: corresXF_def L2Tcorres_def L2_defs abs_modifies_def abs_guard_def struct_rewrite_modifies_def struct_rewrite_guard_def)
done
lemma L2Tcorres_gets [heap_abs]:
"\<lbrakk> struct_rewrite_expr P b c; abs_guard st P' P;
abs_expr st Q a b \<rbrakk> \<Longrightarrow>
L2Tcorres st (L2_seq (L2_guard (\<lambda>s. P' s \<and> Q s)) (\<lambda>_. L2_gets a n)) (L2_gets c n)"
apply (monad_eq simp: corresXF_def L2Tcorres_def L2_defs abs_expr_def abs_guard_def struct_rewrite_expr_def struct_rewrite_guard_def)
done
lemma L2Tcorres_gets_const [heap_abs]:
"L2Tcorres st (L2_gets (\<lambda>_. a) n) (L2_gets (\<lambda>_. a) n)"
apply (monad_eq simp: corresXF_def L2Tcorres_def L2_defs)
done
lemma L2Tcorres_guard [heap_abs]:
"\<lbrakk> struct_rewrite_guard b c; abs_guard st a b \<rbrakk> \<Longrightarrow>
L2Tcorres st (L2_guard a) (L2_guard c)"
apply (monad_eq simp: corresXF_def L2Tcorres_def L2_defs abs_guard_def struct_rewrite_guard_def)
done
lemma L2Tcorres_recguard [heap_abs]:
"\<lbrakk> L2Tcorres st A C \<rbrakk> \<Longrightarrow> L2Tcorres st (L2_recguard n A) (L2_recguard n C)"
apply (monad_eq simp: corresXF_def L2Tcorres_def L2_defs Ball_def Bex_def split: sum.splits)
done
lemma L2Tcorres_while [heap_abs]:
assumes body_corres: "\<And>x. L2Tcorres st (B' x) (B x)"
and cond_rewrite: "\<And>r. struct_rewrite_expr (G r) (C' r) (C r)"
and guard_abs: "\<And>r. abs_guard st (G' r) (G r)"
and guard_impl_cond: "\<And>r. abs_expr st (H r) (C'' r) (C' r)"
shows "L2Tcorres st (L2_guarded_while (\<lambda>i s. G' i s \<and> H i s) C'' B' i n) (L2_while C B i n)"
proof -
have cond_match: "\<And>r s. G' r (st s) \<and> H r (st s) \<Longrightarrow> C'' r (st s) = C r s"
using cond_rewrite guard_abs guard_impl_cond
by (clarsimp simp: abs_expr_def abs_guard_def struct_rewrite_expr_def)
have "corresXF st (\<lambda>r _. r) (\<lambda>r _. r) (\<lambda>_. True)
(doE _ \<leftarrow> guardE (\<lambda>s. G' i s \<and> H i s);
whileLoopE C''
(\<lambda>i. doE r \<leftarrow> B' i;
_ \<leftarrow> guardE (\<lambda>s. G' r s \<and> H r s);
returnOk r
odE) i
odE)
(whileLoopE C B i)"
apply (rule corresXF_guard_imp)
apply (rule corresXF_guarded_while [where P="\<lambda>_ _. True" and P'="\<lambda>_ _. True"])
apply (clarsimp cong: corresXF_cong)
apply (rule corresXF_guard_imp)
apply (rule body_corres [unfolded L2Tcorres_def])
apply simp
apply (clarsimp simp: cond_match)
apply clarsimp
apply (rule hoareE_TrueI)
apply simp
apply simp
apply simp
done
thus ?thesis
by (clarsimp simp: L2Tcorres_def L2_defs
guardE_def returnOk_liftE)
qed
definition "abs_spec st P (A :: ('a \<times> 'a) set) (C :: ('c \<times> 'c) set)
\<equiv> (\<forall>s t. P (st s) \<longrightarrow> (((s, t) \<in> C) \<longrightarrow> ((st s, st t) \<in> A)))
\<and> (\<forall>s. P (st s) \<longrightarrow> (\<exists>x. (st s, x) \<in> A) \<longrightarrow> (\<exists>x. (s, x) \<in> C))"
lemma L2Tcorres_spec [heap_abs]:
"\<lbrakk> abs_spec st P A C \<rbrakk>
\<Longrightarrow> L2Tcorres st (L2_seq (L2_guard P) (\<lambda>_. (L2_spec A))) (L2_spec C)"
by (monad_eq simp: corresXF_def L2Tcorres_def L2_defs image_def split_def Ball_def
state_select_def abs_spec_def)
lemma abs_spec_constant [heap_abs]:
"abs_spec st \<top> {(a, b). C} {(a, b). C}"
apply (clarsimp simp: abs_spec_def)
done
lemma L2Tcorres_condition [heap_abs]:
"\<lbrakk> L2Tcorres st L L';
L2Tcorres st R R';
struct_rewrite_expr P C' C;
abs_guard st P' P;
abs_expr st Q C'' C' \<rbrakk> \<Longrightarrow>
L2Tcorres st (L2_seq (L2_guard (\<lambda>s. P' s \<and> Q s)) (\<lambda>_. L2_condition C'' L R)) (L2_condition C L' R')"
apply (clarsimp simp: L2_defs L2Tcorres_def abs_expr_def abs_guard_def struct_rewrite_expr_def struct_rewrite_guard_def)
apply (rule corresXF_exec_abs_guard [unfolded guardE_def])
apply (rule corresXF_cond)
apply (blast intro: corresXF_guard_imp)
apply (blast intro: corresXF_guard_imp)
apply simp
done
lemma L2Tcorres_seq [heap_abs]:
"\<lbrakk> L2Tcorres st L' L; \<And>r. L2Tcorres st (\<lambda>s. R' r s) (\<lambda>s. R r s) \<rbrakk>
\<Longrightarrow> L2Tcorres st (L2_seq L' (\<lambda>r s. R' r s)) (L2_seq L (\<lambda>r s. R r s))"
apply (clarsimp simp: L2Tcorres_def L2_defs)
apply (rule corresXF_guard_imp)
apply (erule corresXF_join [where P'="\<lambda>x y s. x = y" and Q="\<lambda>_. True"])
apply (fastforce intro: corresXF_assume_pre)
apply simp
apply (rule hoareE_TrueI)
apply simp
apply simp
done
lemma L2Tcorres_catch [heap_abs]:
"\<lbrakk> L2Tcorres st L L';
\<And>r. L2Tcorres st (\<lambda>s. R r s) (\<lambda>s. R' r s)
\<rbrakk> \<Longrightarrow> L2Tcorres st (L2_catch L (\<lambda>r s. R r s)) (L2_catch L' (\<lambda>r s. R' r s))"
apply (clarsimp simp: L2Tcorres_def L2_defs)
apply (rule corresXF_guard_imp)
apply (erule corresXF_except [where P'="\<lambda>x y s. x = y" and Q="\<lambda>_. True"])
apply (fastforce intro: corresXF_assume_pre)
apply simp
apply (rule hoareE_TrueI)
apply simp
apply simp
done
lemma L2Tcorres_unknown [heap_abs]:
"L2Tcorres st (L2_unknown name) (L2_unknown name)"
apply (clarsimp simp: L2_unknown_def selectE_def[symmetric])
apply (clarsimp simp: L2Tcorres_def)
apply (auto intro!: corresXF_select_select)
done
lemma L2Tcorres_throw [heap_abs]:
"L2Tcorres st (L2_throw x n) (L2_throw x n)"
apply (clarsimp simp: L2Tcorres_def L2_defs)
apply (rule corresXF_throw)
apply simp
done
lemma L2Tcorres_split [heap_abs]:
"\<lbrakk> \<And>x y. L2Tcorres st (P x y) (P' x y) \<rbrakk> \<Longrightarrow>
L2Tcorres st (case a of (x, y) \<Rightarrow> P x y) (case a of (x, y) \<Rightarrow> P' x y)"
apply (clarsimp simp: split_def)
done
lemma L2Tcorres_seq_unused_result [heap_abs]:
"\<lbrakk> L2Tcorres st L L'; L2Tcorres st R R' \<rbrakk> \<Longrightarrow> L2Tcorres st (L2_seq L (\<lambda>_. R)) (L2_seq L' (\<lambda>_. R'))"
apply (rule L2Tcorres_seq, auto)
done
lemma abs_expr_split [heap_abs]:
"\<lbrakk> \<And>a b. abs_expr st (P a b) (A a b) (C a b) \<rbrakk>
\<Longrightarrow> abs_expr st (case r of (a, b) \<Rightarrow> P a b)
(case r of (a, b) \<Rightarrow> A a b) (case r of (a, b) \<Rightarrow> C a b)"
apply (auto simp: split_def)
done
lemma abs_guard_split [heap_abs]:
"\<lbrakk> \<And>a b. abs_guard st (A a b) (C a b) \<rbrakk>
\<Longrightarrow> abs_guard st (case r of (a, b) \<Rightarrow> A a b) (case r of (a, b) \<Rightarrow> C a b)"
apply (auto simp: split_def)
done
lemma L2Tcorres_recguard_0:
"L2Tcorres st (L2_recguard 0 A) C"
apply (monad_eq simp: corresXF_def L2Tcorres_def L2_defs)
done
lemma L2Tcorres_abstract_fail [heap_abs]:
"L2Tcorres st L2_fail L2_fail"
apply (clarsimp simp: L2Tcorres_def L2_defs)
apply (rule corresXF_fail)
done
lemma abs_expr_id [heap_abs]:
"abs_expr id \<top> A A"
apply (clarsimp simp: abs_expr_def)
done
lemma abs_expr_lambda_null [heap_abs]:
"abs_expr st P A C \<Longrightarrow> abs_expr st P (\<lambda>s r. A s) (\<lambda>s r. C s)"
apply (clarsimp simp: abs_expr_def)
done
lemma abs_modify_id [heap_abs]:
"abs_modifies id \<top> A A"
apply (clarsimp simp: abs_modifies_def)
done
lemma L2Tcorres_exec_concrete [heap_abs]:
"L2Tcorres id A C \<Longrightarrow> L2Tcorres st (exec_concrete st (L2_call A)) (L2_call C)"
apply (clarsimp simp: L2Tcorres_def L2_call_def)
apply (rule corresXF_exec_concrete)
apply (rule corresXF_except)
apply assumption
apply (rule corresXF_fail[where P="\<top>"])
apply wp
apply simp
done
lemma L2Tcorres_exec_concrete_simpl [heap_abs]:
"L2Tcorres id A C \<Longrightarrow> L2Tcorres st (exec_concrete st (L2_call_L1 arg_xf gs ret_xf A)) (L2_call_L1 arg_xf gs ret_xf C)"
apply (clarsimp simp: L2Tcorres_def L2_call_L1_def)
apply (rule corresXF_exec_concrete)
apply (clarsimp simp: corresXF_def)
apply (monad_eq split: sum.splits simp add: select_f_def)
apply fastforce
done
lemma L2Tcorres_exec_abstract [heap_abs]:
"L2Tcorres st A C \<Longrightarrow> L2Tcorres id (exec_abstract st (L2_call A)) (L2_call C)"
apply (clarsimp simp: L2_call_def L2Tcorres_def)
apply (rule corresXF_exec_abstract)
apply (rule corresXF_except)
apply assumption
apply (rule corresXF_fail[where P="\<top>"])
apply wp
apply simp
done
lemma L2Tcorres_call [heap_abs]:
"L2Tcorres st A C \<Longrightarrow> L2Tcorres st (L2_call A) (L2_call C)"
unfolding L2Tcorres_def L2_call_def
apply (rule corresXF_except)
apply simp
apply (rule corresXF_fail)
apply (rule hoareE_TrueI)
apply simp
done
lemma L2Tcorres_measure_call [heap_abs]:
"\<lbrakk> monad_mono C; \<And>m. L2Tcorres st (A m) (C m) \<rbrakk>
\<Longrightarrow> L2Tcorres st (measure_call A) (measure_call C)"
apply (unfold L2Tcorres_def)
apply (erule corresXF_measure_call)
apply assumption
done
(*
* Assert the given abstracted heap (accessed using "getter" and "setter") for type
* "'a" is a valid abstraction w.r.t. the given state translation functions.
*)
definition
"read_write_valid r w \<equiv>
(\<forall>f s. r (w f s) = f (r s))
\<and> (\<forall>s f. f (r s) = (r s) \<longrightarrow> w f s = s)
\<and> (\<forall>f f' s. (f (r s) = f' (r s)) \<longrightarrow> w f s = w f' s)
\<and> (\<forall>f g s. w f (w g s) = w (\<lambda>x. f (g x)) s)"
lemma read_write_validI:
"\<lbrakk> \<And>f s. r (w f s) = f (r s);
\<And>f s. f (r s) = r s \<Longrightarrow> w f s = s;
\<And>f f' s. f (r s) = f' (r s) \<Longrightarrow> w f s = w f' s;
\<And>f g s. w f (w g s) = w (\<lambda>x. f (g x)) s
\<rbrakk> \<Longrightarrow> read_write_valid r w"
unfolding read_write_valid_def by metis
lemma read_write_write_id: "read_write_valid r w \<Longrightarrow> w (\<lambda>x. x) s = s"
by (simp add: read_write_valid_def)
lemma read_write_valid_def1:
"read_write_valid r w \<Longrightarrow> r (w f s) = f (r s)"
by (metis read_write_valid_def)
lemma read_write_valid_def2:
"\<lbrakk> read_write_valid r w; f (r s) = r s \<rbrakk> \<Longrightarrow> w f s = s"
by (metis read_write_valid_def)
lemma read_write_valid_def3:
"\<lbrakk> read_write_valid r w; f (r s) = f' (r s) \<rbrakk> \<Longrightarrow> w f s = w f' s"
by (metis read_write_valid_def)
lemma read_write_o:
"\<lbrakk> read_write_valid r w; \<And>x. h x = f (g x) \<rbrakk> \<Longrightarrow> w f (w g s) = w h s"
apply (subst (asm) read_write_valid_def)
apply metis
done
definition [simp]:
"valid_implies_cguard st v\<^sub>r \<equiv> \<forall>s p. v\<^sub>r (st s) p \<longrightarrow> c_guard p"
definition [simp]:
"heap_decode_bytes st v\<^sub>r h\<^sub>r t_hrs\<^sub>r \<equiv> \<forall>s p. v\<^sub>r (st s) p \<longrightarrow>
h\<^sub>r (st s) p = h_val (hrs_mem (t_hrs\<^sub>r s)) p"
definition [simp]:
"heap_encode_bytes st v\<^sub>r h\<^sub>w t_hrs\<^sub>w \<equiv>
\<forall>s p x. v\<^sub>r (st s) p \<longrightarrow>
st (t_hrs\<^sub>w (hrs_mem_update (heap_update p x)) s) =
h\<^sub>w (\<lambda>f. f(p := x)) (st s)"
definition [simp]:
"write_preserves_valid v\<^sub>r h\<^sub>w \<equiv>
(\<forall>p f s. v\<^sub>r s p \<longrightarrow> v\<^sub>r (h\<^sub>w f s) p)"
definition
valid_typ_heap ::
"('s \<Rightarrow> 't) \<Rightarrow>
('t \<Rightarrow> ('a::c_type) ptr \<Rightarrow> 'a) \<Rightarrow>
((('a ptr \<Rightarrow> 'a) \<Rightarrow> ('a ptr \<Rightarrow> 'a)) \<Rightarrow> 't \<Rightarrow> 't) \<Rightarrow>
('t \<Rightarrow> ('a::c_type) ptr \<Rightarrow> bool) \<Rightarrow>
((('a ptr \<Rightarrow> bool) \<Rightarrow> ('a ptr \<Rightarrow> bool)) \<Rightarrow> 't \<Rightarrow> 't) \<Rightarrow>
('s \<Rightarrow> heap_raw_state) \<Rightarrow>
((heap_raw_state \<Rightarrow> heap_raw_state) \<Rightarrow> 's \<Rightarrow> 's) \<Rightarrow>
bool"
where
"valid_typ_heap st getter setter vgetter vsetter t_hrs t_hrs_update \<equiv>
(read_write_valid getter setter)
\<and> (read_write_valid vgetter vsetter)
\<and> (read_write_valid t_hrs t_hrs_update)
\<and> (valid_implies_cguard st vgetter)
\<and> (heap_decode_bytes st vgetter getter t_hrs)
\<and> (heap_encode_bytes st vgetter setter t_hrs_update)
\<and> (write_preserves_valid vgetter setter)"
lemma valid_typ_heapI [intro!]:
assumes getter_setter_idem: "\<And>s x. getter (setter x s) = x (getter s)"
and setter_getter_idem: "\<And>s f. f (getter s) = (getter s) \<Longrightarrow> setter f s = s"
and setter_static: "\<And>s f f'. f (getter s) = f' (getter s) \<Longrightarrow> setter f s = setter f' s"
and setter_chain: "\<And>s f g. setter f (setter g s) = setter (\<lambda>x. f (g x)) s"
and vgetter_setter_idem: "\<And>s x. vgetter (vsetter x s) = x (vgetter s)"
and vsetter_getter_idem: "\<And>s f. f (vgetter s) = (vgetter s) \<Longrightarrow> vsetter f s = s"
and vsetter_static: "\<And>s f f'. f (vgetter s) = f' (vgetter s) \<Longrightarrow> vsetter f s = vsetter f' s"
and vsetter_chain: "\<And>s f g. vsetter f (vsetter g s) = vsetter (\<lambda>x. f (g x)) s"
and getter_implies_safe: "\<And>s p. vgetter (st s) p \<Longrightarrow> c_guard p"
and getter_data_correct: "\<And>s p. vgetter (st s) p \<Longrightarrow>
getter (st s) p = h_val (hrs_mem (t_hrs s)) p"
and setter_keeps_vgetter: "\<And>s f p. vgetter s p \<Longrightarrow> vgetter (setter f s) p"
and abs_update_matches_conc_update:
"\<And>s p v. vgetter (st s) p \<Longrightarrow>
st (t_hrs_update (hrs_mem_update (heap_update p v)) s) =
setter (\<lambda>x. x(p := v)) (st s)"
and t_hrs_set_get: "\<And>s x. t_hrs (t_hrs_update x s) = x (t_hrs s)"
and t_hrs_get_set: "\<And>s f. f (t_hrs s) = t_hrs s \<Longrightarrow> t_hrs_update f s = s"
and t_hrs_set_static: "\<And>s f f'. f (t_hrs s) = f' (t_hrs s) \<Longrightarrow> t_hrs_update f s = t_hrs_update f' s"
and t_hrs_set_chain: "\<And>s f g. t_hrs_update f (t_hrs_update g s) = t_hrs_update (\<lambda>x. f (g x)) s"
shows "valid_typ_heap st getter setter vgetter vsetter t_hrs t_hrs_update"
apply (clarsimp simp: valid_typ_heap_def read_write_valid_def)
apply (safe | fact | rule ext)+
done
lemma read_write_valid_fg_cons:
"read_write_valid r w \<Longrightarrow> fg_cons r (w \<circ> (\<lambda>x _. x))"
unfolding read_write_valid_def fg_cons_def o_def
by metis
(*
* Assert the given field ("field_getter", "field_setter") of the given structure
* can be abstracted into the heap, and then accessed as a HOL object.
*)
(*
* This can deal with nested structures, but they must be packed_types.
* FIXME: generalise this framework to mem_types
*)
definition
valid_struct_field
:: "('s \<Rightarrow> 't)
\<Rightarrow> string list
\<Rightarrow> (('p::packed_type) \<Rightarrow> ('f::packed_type))
\<Rightarrow> (('f \<Rightarrow> 'f) \<Rightarrow> ('p \<Rightarrow> 'p))
\<Rightarrow> ('s \<Rightarrow> heap_raw_state)
\<Rightarrow> ((heap_raw_state \<Rightarrow> heap_raw_state) \<Rightarrow> 's \<Rightarrow> 's)
\<Rightarrow> bool"
where
"valid_struct_field st field_name field_getter field_setter t_hrs t_hrs_update \<equiv>
(read_write_valid field_getter field_setter
\<and> field_ti TYPE('p) field_name =
Some (adjust_ti (typ_info_t TYPE('f)) field_getter (field_setter \<circ> (\<lambda>x _. x)))
\<and> (\<forall>p :: 'p ptr. c_guard p \<longrightarrow> c_guard (Ptr &(p\<rightarrow>field_name) :: 'f ptr))
\<and> read_write_valid t_hrs t_hrs_update)"
lemma valid_struct_fieldI [intro]:
fixes st :: "'s \<Rightarrow> 't"
fixes field_getter :: "('a::packed_type) \<Rightarrow> ('f::packed_type)"
shows "\<lbrakk>
\<And>s f. f (field_getter s) = (field_getter s) \<Longrightarrow> field_setter f s = s;
\<And>s f f'. f (field_getter s) = f' (field_getter s) \<Longrightarrow> field_setter f s = field_setter f' s;
\<And>s f. field_getter (field_setter f s) = f (field_getter s);
\<And>s f g. field_setter f (field_setter g s) = field_setter (f \<circ> g) s;
field_ti TYPE('a) field_name =
Some (adjust_ti (typ_info_t TYPE('f)) field_getter (field_setter \<circ> (\<lambda>x _. x)));
\<And>(p::'a ptr). c_guard p \<Longrightarrow> c_guard (Ptr &(p\<rightarrow>field_name) :: 'f ptr);
\<And>s x. t_hrs (t_hrs_update x s) = x (t_hrs s);
\<And>s f. f (t_hrs s) = t_hrs s \<Longrightarrow> t_hrs_update f s = s;
\<And>s f f'. f (t_hrs s) = f' (t_hrs s) \<Longrightarrow> t_hrs_update f s = t_hrs_update f' s;
\<And>s f g. t_hrs_update f (t_hrs_update g s) = t_hrs_update (\<lambda>x. f (g x)) s
\<rbrakk> \<Longrightarrow>
valid_struct_field st field_name field_getter field_setter t_hrs t_hrs_update"
apply (unfold valid_struct_field_def read_write_valid_def o_def)
apply (safe | assumption | rule ext)+
done
(*
* This cannot deal with struct nesting, but works for general mem_types.
*)
definition
valid_struct_field_legacy
:: "('s \<Rightarrow> 't)
\<Rightarrow> string list
\<Rightarrow> ('p \<Rightarrow> ('f::c_type))
\<Rightarrow> ('f \<Rightarrow> 'p \<Rightarrow> 'p)
\<Rightarrow> ('t \<Rightarrow> (('p::c_type) ptr \<Rightarrow> 'p))
\<Rightarrow> ((('p ptr \<Rightarrow> 'p) \<Rightarrow> ('p ptr \<Rightarrow> 'p)) \<Rightarrow> 't \<Rightarrow> 't)
\<Rightarrow> ('t \<Rightarrow> (('p::c_type) ptr \<Rightarrow> bool))
\<Rightarrow> ((('p ptr \<Rightarrow> bool) \<Rightarrow> ('p ptr \<Rightarrow> bool)) \<Rightarrow> 't \<Rightarrow> 't)
\<Rightarrow> ('s \<Rightarrow> heap_raw_state)
\<Rightarrow> ((heap_raw_state \<Rightarrow> heap_raw_state) \<Rightarrow> 's \<Rightarrow> 's)
\<Rightarrow> bool"
where
"valid_struct_field_legacy st field_name field_getter field_setter
getter setter vgetter vsetter t_hrs t_hrs_update \<equiv>
(\<forall>s p. vgetter (st s) p \<longrightarrow>
h_val (hrs_mem (t_hrs s)) (Ptr &(p\<rightarrow>field_name))
= field_getter (getter (st s) p))
\<and> (\<forall>s p val. vgetter (st s) p \<longrightarrow>
st (t_hrs_update (hrs_mem_update (heap_update (Ptr &(p\<rightarrow>field_name)) val)) s) =
setter (\<lambda>old. old(p := (field_setter val (old p)))) (st s))
\<and> (\<forall>s p. vgetter (st s) p \<longrightarrow> c_guard p)
\<and> (\<forall>p. c_guard (p :: 'p ptr) \<longrightarrow> c_guard (Ptr &(p\<rightarrow>field_name) :: 'f ptr))"
lemma valid_struct_field_legacyI [intro]:
fixes st :: "'s \<Rightarrow> 't"
fixes field_getter :: "('a::c_type) \<Rightarrow> ('f::c_type)"
shows "\<lbrakk> \<And>s p. vgetter (st s) p \<Longrightarrow>
h_val (hrs_mem (t_hrs s)) (Ptr &(p\<rightarrow>field_name)) = field_getter (getter (st s) p);
\<And>s p val. vgetter (st s) p \<Longrightarrow>
st (t_hrs_update (hrs_mem_update (heap_update (Ptr &(p\<rightarrow>field_name)) val)) s) =
setter (\<lambda>old. old(p := (field_setter val (old p)))) (st s);
\<And>s p. vgetter (st s) p \<Longrightarrow> c_guard p;
\<And>(p::'a ptr). c_guard p \<Longrightarrow> c_guard (Ptr &(p\<rightarrow>field_name) :: 'f ptr) \<rbrakk> \<Longrightarrow>
valid_struct_field_legacy st field_name field_getter field_setter getter setter vgetter vsetter t_hrs t_hrs_update"
apply (fastforce simp: valid_struct_field_legacy_def)
done
lemma valid_typ_heap_get_hvalD:
"\<lbrakk> valid_typ_heap st getter setter vgetter vsetter
t_hrs t_hrs_update; vgetter (st s) p \<rbrakk> \<Longrightarrow>
h_val (hrs_mem (t_hrs s)) p = getter (st s) p"
apply (clarsimp simp: valid_typ_heap_def)
done
lemma valid_typ_heap_t_hrs_updateD:
"\<lbrakk> valid_typ_heap st getter setter vgetter vsetter
t_hrs t_hrs_update; vgetter (st s) p \<rbrakk> \<Longrightarrow>
st (t_hrs_update (hrs_mem_update (heap_update p v')) s) =
setter (\<lambda>x. x(p := v')) (st s)"
apply (clarsimp simp: valid_typ_heap_def)
done
lemma heap_abs_expr_guard [heap_abs]:
"\<lbrakk> valid_typ_heap st getter setter vgetter vsetter t_hrs t_hrs_update;
abs_expr st P x' x \<rbrakk> \<Longrightarrow>
abs_guard st (\<lambda>s. P s \<and> vgetter s (x' s)) (\<lambda>s. (c_guard (x s :: ('a::c_type) ptr)))"
apply (clarsimp simp: abs_expr_def abs_guard_def
simple_lift_def heap_ptr_valid_def valid_typ_heap_def)
done
lemma heap_abs_expr_h_val [heap_abs]:
"\<lbrakk> valid_typ_heap st getter setter vgetter vsetter t_hrs t_hrs_update;
abs_expr st P x' x \<rbrakk> \<Longrightarrow>
abs_expr st
(\<lambda>s. P s \<and> vgetter s (x' s))
(\<lambda>s. (getter s (x' s)))
(\<lambda>s. (h_val (hrs_mem (t_hrs s))) (x s))"
apply (clarsimp simp: abs_expr_def simple_lift_def)
apply (metis valid_typ_heap_get_hvalD)
done
lemma heap_abs_modifies_heap_update__unused:
"\<lbrakk> valid_typ_heap st getter setter vgetter vsetter t_hrs t_hrs_update;
abs_expr st Pb b' b;
abs_expr st Pc c' c \<rbrakk> \<Longrightarrow>
abs_modifies st (\<lambda>s. Pb s \<and> Pc s \<and> vgetter s (b' s))
(\<lambda>s. setter (\<lambda>x. x(b' s := (c' s))) s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (b s :: ('a::c_type) ptr) (c s))) s)"
apply (clarsimp simp: typ_simple_heap_simps abs_expr_def abs_modifies_def)
apply (metis valid_typ_heap_t_hrs_updateD)
done
(* See comment for heap_lift__wrap_h_val. *)
definition "heap_lift__h_val \<equiv> h_val"
(* See the comment for struct_rewrite_modifies_field.
* In this case we rely on nice unification for ?c.
* The heap_abs_syntax generator also relies on this rule
* and would need to be modified if the previous rule was used instead. *)
lemma heap_abs_modifies_heap_update [heap_abs]:
"\<lbrakk> valid_typ_heap st getter setter vgetter vsetter t_hrs t_hrs_update;
abs_expr st Pb b' b;
\<And>v. abs_expr st Pc (c' v) (c v) \<rbrakk> \<Longrightarrow>
abs_modifies st (\<lambda>s. Pb s \<and> Pc s \<and> vgetter s (b' s))
(\<lambda>s. setter (\<lambda>x. x(b' s := c' (x (b' s)) s)) s)
(\<lambda>s. t_hrs_update (hrs_mem_update
(heap_update (b s :: ('a::c_type) ptr)
(c (heap_lift__h_val (hrs_mem (t_hrs s)) (b s)) s))) s)"
apply (clarsimp simp: typ_simple_heap_simps abs_expr_def abs_modifies_def heap_lift__h_val_def)
apply (rule_tac t = "h_val (hrs_mem (t_hrs s)) (b' (st s))"
and s = "getter (st s) (b' (st s))" in subst)
apply (clarsimp simp: valid_typ_heap_def)
apply (rule_tac f1 = "(\<lambda>x. x(b' (st s) := c' (getter (st s) (b' (st s))) (st s)))"
in subst[OF read_write_valid_def3[where r = getter and w = setter]])
apply (clarsimp simp: valid_typ_heap_def)
apply (rule refl)
apply (metis valid_typ_heap_t_hrs_updateD)
done
(* Legacy rules for non-packed types. *)
lemma abs_expr_field_getter_legacy [heap_abs]:
"\<lbrakk> valid_struct_field_legacy st field_name field_getter field_setter
getter setter vgetter vsetter t_hrs t_hrs_setter;
abs_expr st P a c \<rbrakk> \<Longrightarrow>
abs_expr st (\<lambda>s. P s \<and> vgetter s (a s))
(\<lambda>s. field_getter (getter s (a s)))
(\<lambda>s. h_val (hrs_mem (t_hrs s)) (Ptr &((c s)\<rightarrow>field_name)))"
apply (clarsimp simp: abs_expr_def valid_struct_field_legacy_def valid_typ_heap_def)
done
lemma abs_expr_field_setter_legacy [heap_abs]:
"\<lbrakk> valid_struct_field_legacy st field_name
field_getter field_setter getter setter vgetter vsetter t_hrs t_hrs_update;
abs_expr st P p p'; abs_expr st Q val val' \<rbrakk> \<Longrightarrow>
abs_modifies st (\<lambda>s. P s \<and> Q s \<and> vgetter s (p s))
(\<lambda>s. setter (\<lambda>old. old((p s) := field_setter (val s) (old (p s)))) s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (Ptr &((p' s)\<rightarrow>field_name)) (val' s))) s)"
apply (clarsimp simp: abs_expr_def valid_struct_field_legacy_def valid_typ_heap_def abs_modifies_def)
done
lemma abs_expr_field_guard_legacy [heap_abs]:
"\<lbrakk> valid_struct_field_legacy st field_name
(field_getter :: 'p \<Rightarrow> 'f) field_setter getter setter vgetter vsetter t_hrs t_hrs_update;
abs_expr st P p p' \<rbrakk> \<Longrightarrow>
abs_guard st (P and (\<lambda>s. vgetter s (p s :: 'p :: {c_type} ptr )))
(\<lambda>s. c_guard (Ptr &((p' s)\<rightarrow>field_name) :: 'f::{c_type} ptr))"
apply (clarsimp simp: abs_guard_def abs_expr_def valid_struct_field_legacy_def valid_typ_heap_def)
done
(*
* struct_rewrite: remove uses of field_lvalue. (field_lvalue p a = &(p\<rightarrow>a))
* We do three transformations:
* c_guard (p\<rightarrow>a) \<Longleftarrow> c_guard p
* h_val s (p\<rightarrow>a) = p_C.a_C (h_val s p)
* heap_update (p\<rightarrow>a) v s = heap_update p (p_C.a_C_update (\<lambda>_. v) (h_val s p)) s
* However, an inner expression may nest h_vals arbitrarily.
*
* Any output of a struct_rewrite rule should be fully rewritten.
* By doing this, each rule only needs to rewrite the parts of a term that it
* introduces by itself.
*)
(* struct_rewrite_guard rules *)
lemma struct_rewrite_guard_expr [heap_abs]:
"struct_rewrite_expr P a' a \<Longrightarrow> struct_rewrite_guard (\<lambda>s. P s \<and> a' s) a"
by (simp add: struct_rewrite_expr_def struct_rewrite_guard_def)
lemma struct_rewrite_guard_constant [heap_abs]:
"struct_rewrite_guard (\<lambda>_. P) (\<lambda>_. P)"
by (simp add: struct_rewrite_guard_def)
lemma struct_rewrite_guard_conj [heap_abs]:
"\<lbrakk> struct_rewrite_guard b' b; struct_rewrite_guard a' a \<rbrakk> \<Longrightarrow>
struct_rewrite_guard (\<lambda>s. a' s \<and> b' s) (\<lambda>s. a s \<and> b s)"
by (clarsimp simp: struct_rewrite_guard_def)
lemma struct_rewrite_guard_split [heap_abs]:
"\<lbrakk> \<And>a b. struct_rewrite_guard (A a b) (C a b) \<rbrakk>
\<Longrightarrow> struct_rewrite_guard (case r of (a, b) \<Rightarrow> A a b) (case r of (a, b) \<Rightarrow> C a b)"
apply (auto simp: split_def)
done
lemma struct_rewrite_guard_c_guard_field [heap_abs]:
"\<lbrakk> valid_struct_field st field_name (field_getter :: ('a :: packed_type) \<Rightarrow> ('f :: packed_type)) field_setter t_hrs t_hrs_update;
struct_rewrite_expr P p' p;
struct_rewrite_guard Q (\<lambda>s. c_guard (p' s)) \<rbrakk> \<Longrightarrow>
struct_rewrite_guard (\<lambda>s. P s \<and> Q s)
(\<lambda>s. c_guard (Ptr (field_lvalue (p s :: 'a ptr) field_name) :: 'f ptr))"
by (simp add: valid_struct_field_def struct_rewrite_expr_def struct_rewrite_guard_def)
lemma align_of_array: "align_of TYPE(('a :: array_outer_max_size)['b' :: array_max_count]) = align_of TYPE('a)"
by (simp add: align_of_def align_td_array)
lemma c_guard_array:
"\<lbrakk> 0 \<le> k; nat k < CARD('b); c_guard (p :: (('a::array_outer_max_size)['b::array_max_count]) ptr) \<rbrakk>
\<Longrightarrow> c_guard (ptr_coerce p +\<^sub>p k :: 'a ptr)"
apply (clarsimp simp: CTypesDefs.ptr_add_def c_guard_def c_null_guard_def)
apply (rule conjI[rotated])
apply (erule contrapos_nn)
apply (clarsimp simp: intvl_def)
apply (rename_tac i, rule_tac x = "nat k * size_of TYPE('a) + i" in exI)
apply clarsimp
apply (rule conjI)
apply (simp add: field_simps)
apply (rule_tac y = "Suc (nat k) * size_of TYPE('a)" in less_le_trans)
apply simp
apply (metis less_eq_Suc_le mult_le_mono2 mult.commute)
apply (subgoal_tac "ptr_aligned (ptr_coerce p :: 'a ptr)")
apply (frule_tac p = "ptr_coerce p" and i = "k" in ptr_aligned_plus)
apply (clarsimp simp: CTypesDefs.ptr_add_def)
apply (clarsimp simp: ptr_aligned_def align_of_array)
done
lemma struct_rewrite_guard_c_guard_Array_field [heap_abs]:
"\<lbrakk> valid_struct_field st field_name (field_getter :: ('a :: packed_type) \<Rightarrow> ('f::array_outer_packed ['n::array_max_count])) field_setter t_hrs t_hrs_update;
struct_rewrite_expr P p' p;
struct_rewrite_guard Q (\<lambda>s. c_guard (p' s)) \<rbrakk> \<Longrightarrow>
struct_rewrite_guard (\<lambda>s. P s \<and> Q s \<and> 0 \<le> k \<and> nat k < CARD('n))
(\<lambda>s. c_guard (ptr_coerce (Ptr (field_lvalue (p s :: 'a ptr) field_name) :: (('f['n]) ptr)) +\<^sub>p k :: 'f ptr))"
by (simp del: ptr_coerce.simps add: valid_struct_field_def struct_rewrite_expr_def struct_rewrite_guard_def c_guard_array)
(* struct_rewrite_expr rules *)
(* This is only used when heap lifting is turned off,
* where we expect no rewriting to happen anyway.
* TODO: it might be safe to enable this unconditionally,
* as long as it happens after heap_abs_fo. *)
lemma struct_rewrite_expr_id:
"struct_rewrite_expr \<top> A A"
by (simp add: struct_rewrite_expr_def)
lemma struct_rewrite_expr_fun_app2 [heap_abs_fo]:
"\<lbrakk> struct_rewrite_expr P f' f;
struct_rewrite_expr Q g' g \<rbrakk> \<Longrightarrow>
struct_rewrite_expr (\<lambda>s. P s \<and> Q s) (\<lambda>s a. f' s a (g' s a)) (\<lambda>s a. f s a $ g s a)"
by (simp add: struct_rewrite_expr_def)
lemma struct_rewrite_expr_fun_app [heap_abs_fo]:
"\<lbrakk> struct_rewrite_expr Y b' b; struct_rewrite_expr X a' a \<rbrakk> \<Longrightarrow>
struct_rewrite_expr (\<lambda>s. X s \<and> Y s) (\<lambda>s. a' s (b' s)) (\<lambda>s. a s $ b s)"
by (clarsimp simp: struct_rewrite_expr_def)
lemma struct_rewrite_expr_constant [heap_abs]:
"struct_rewrite_expr \<top> (\<lambda>_. a) (\<lambda>_. a)"
by (clarsimp simp: struct_rewrite_expr_def)
lemma struct_rewrite_expr_lambda_null [heap_abs]:
"struct_rewrite_expr P A C \<Longrightarrow> struct_rewrite_expr P (\<lambda>s _. A s) (\<lambda>s _. C s)"
by (clarsimp simp: struct_rewrite_expr_def)
lemma struct_rewrite_expr_split [heap_abs]:
"\<lbrakk> \<And>a b. struct_rewrite_expr (P a b) (A a b) (C a b) \<rbrakk>
\<Longrightarrow> struct_rewrite_expr (case r of (a, b) \<Rightarrow> P a b)
(case r of (a, b) \<Rightarrow> A a b) (case r of (a, b) \<Rightarrow> C a b)"
apply (auto simp: split_def)
done
lemma struct_rewrite_expr_basecase_h_val [heap_abs]:
"struct_rewrite_expr \<top> (\<lambda>s. h_val (h s) (p s)) (\<lambda>s. h_val (h s) (p s))"
by (simp add: struct_rewrite_expr_def)
lemma struct_rewrite_expr_field [heap_abs]:
"\<lbrakk> valid_struct_field st field_name (field_getter :: ('a :: packed_type) \<Rightarrow> ('f :: packed_type)) field_setter t_hrs t_hrs_update;
struct_rewrite_expr P p' p;
struct_rewrite_expr Q a (\<lambda>s. h_val (hrs_mem (t_hrs s)) (p' s)) \<rbrakk>
\<Longrightarrow> struct_rewrite_expr (\<lambda>s. P s \<and> Q s) (\<lambda>s. field_getter (a s))
(\<lambda>s. h_val (hrs_mem (t_hrs s)) (Ptr (field_lvalue (p s) field_name)))"
apply (clarsimp simp: valid_struct_field_def struct_rewrite_expr_def)
apply (subst h_val_field_from_bytes')
apply assumption
apply (rule export_tag_adjust_ti(1)[rule_format])
apply (simp add: read_write_valid_fg_cons)
apply simp
apply simp
done
(* Descend into struct fields that are themselves arrays. *)
lemma struct_rewrite_expr_Array_field [heap_abs]:
"\<lbrakk> valid_struct_field st field_name
(field_getter :: ('a :: packed_type) \<Rightarrow> 'f::array_outer_packed ['n::array_max_count])
field_setter t_hrs t_hrs_update;
struct_rewrite_expr P p' p;
struct_rewrite_expr Q a (\<lambda>s. h_val (hrs_mem (t_hrs s)) (p' s)) \<rbrakk>
\<Longrightarrow> struct_rewrite_expr (\<lambda>s. P s \<and> Q s \<and> k \<ge> 0 \<and> nat k < CARD('n))
(\<lambda>s. index (field_getter (a s)) (nat k))
(\<lambda>s. h_val (hrs_mem (t_hrs s))
(ptr_coerce (Ptr (field_lvalue (p s) field_name) :: ('f['n]) ptr) +\<^sub>p k))"
apply (case_tac k)
apply (clarsimp simp: struct_rewrite_expr_def simp del: ptr_coerce.simps)
apply (subst struct_rewrite_expr_field
[unfolded struct_rewrite_expr_def, simplified, rule_format, symmetric,
where field_getter = field_getter and P = P and Q = Q and p = p and p' = p'])
apply assumption
apply simp
apply simp
apply simp
apply (rule_tac s = "p s" and t = "p' s" in subst)
apply simp
apply (rule heap_access_Array_element[symmetric])
apply simp
apply (simp add: struct_rewrite_expr_def)
done
declare struct_rewrite_expr_Array_field [unfolded ptr_coerce.simps, heap_abs]
(* struct_rewrite_modifies rules *)
lemma struct_rewrite_modifies_id [heap_abs]:
"struct_rewrite_modifies \<top> A A"
by (simp add: struct_rewrite_modifies_def)
(* We need some valid_typ_heap, but we're really only after t_hrs_update.
* We artificially constrain the type of v to limit backtracking,
* since specialisation of valid_typ_heap will generate one rule per 'a. *)
lemma struct_rewrite_modifies_basecase [heap_abs]:
"\<lbrakk> valid_typ_heap st (getter :: 's \<Rightarrow> 'a ptr \<Rightarrow> ('a::c_type)) setter vgetter vsetter t_hrs t_hrs_update;
struct_rewrite_expr P p' p;
struct_rewrite_expr Q v' v \<rbrakk> \<Longrightarrow>
struct_rewrite_modifies (\<lambda>s. P s \<and> Q s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (p' s) (v' s :: 'a))) s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (p s) (v s :: 'a))) s)"
by (simp add: struct_rewrite_expr_def struct_rewrite_modifies_def)
(* \<approx> heap_update_field.
* We probably need this rule to generalise struct_rewrite_modifies_field. *)
lemma heap_update_field_unpacked:
"\<lbrakk> field_ti TYPE('a::mem_type) f = Some (t :: 'a field_desc typ_desc);
c_guard (p :: 'a::mem_type ptr);
export_uinfo t = export_uinfo (typ_info_t TYPE('b::mem_type)) \<rbrakk> \<Longrightarrow>
heap_update (Ptr &(p\<rightarrow>f) :: 'b ptr) v hp =
heap_update p (update_ti t (to_bytes_p v) (h_val hp p)) hp"
oops
(* \<approx> heap_update_Array_element. Would want this for struct_rewrite_modifies_Array_field. *)
lemma heap_update_Array_element_unpacked:
"n < CARD('b::array_max_count) \<Longrightarrow>
heap_update (ptr_coerce p' +\<^sub>p int n) w hp =
heap_update (p'::('a::array_outer_max_size['b::array_max_count]) ptr)
(Arrays.update (h_val hp p') n w) hp"
oops
(* helper *)
lemma read_write_valid_hrs_mem:
"read_write_valid hrs_mem hrs_mem_update"
by (clarsimp simp: hrs_mem_def hrs_mem_update_def read_write_valid_def)
(*
* heap_update is a bit harder.
* Recall that we want to rewrite
* "heap_update (ptr\<rightarrow>a\<rightarrow>b\<rightarrow>c) val s" to
* "heap_update ptr (c_update (b_update (a_update (\<lambda>_. val))) (h_val s ptr)) s".
* In the second term, c_update is the outer update even though
* c is the innermost field.
*
* We introduce a schematic update function ?u that would eventually be
* instantiated to be the chain "\<lambda>f. c_update (b_update (a_update f))".
* Observe that when we find another field "\<rightarrow>d", we can instantiate
* ?u' = \<lambda>f. ?u (d_update f)
* so that u' is the correct update function for "ptr\<rightarrow>a\<rightarrow>b\<rightarrow>c\<rightarrow>d".
*
* This is a big hack because:
* - We rely on a particular behaviour of the unifier (see below).
* - We will have a chain of flex-flex pairs
* ?u1 =?= \<lambda>f. ?u0 (a_update f)
* ?u2 =?= \<lambda>f. ?u1 (b_update f)
* etc.
* - Because we are doing this transformation in steps, moving
* one component of "ptr\<rightarrow>a\<rightarrow>..." at a time, we end up invoking
* struct_rewrite_expr on the same subterms over and over again.
* In case we find out this hack doesn't scale, we can avoid the schematic ?u
* by traversing the chain and constructing ?u in a separate step.
*)
(*
* There's more. heap_update rewrites for "ptr\<rightarrow>a\<rightarrow>b := RHS" cause a
* "h_val s ptr" to appear in the RHS.
* When we lift to the typed heap, we want this h_val to be treated
* differently to other "h_val s ptr" terms that were already in the RHS.
* Thus we define heap_lift__h_val \<equiv> h_val to carry this information around.
*)
definition "heap_lift__wrap_h_val \<equiv> (=)"
lemma heap_lift_wrap_h_val [heap_abs]:
"heap_lift__wrap_h_val (heap_lift__h_val s p) (h_val s p)"
by (simp add: heap_lift__h_val_def heap_lift__wrap_h_val_def)
lemma heap_lift_wrap_h_val_skip [heap_abs]:
"heap_lift__wrap_h_val (h_val s (Ptr (field_lvalue p f))) (h_val s (Ptr (field_lvalue p f)))"
by (simp add: heap_lift__wrap_h_val_def)
lemma heap_lift_wrap_h_val_skip_array [heap_abs]:
"heap_lift__wrap_h_val (h_val s (ptr_coerce p +\<^sub>p k))
(h_val s (ptr_coerce p +\<^sub>p k))"
by (simp add: heap_lift__wrap_h_val_def)
(* These are valid rules, but produce redundant output. *)
lemma struct_rewrite_modifies_field__unused:
"\<lbrakk> valid_struct_field (st :: 's \<Rightarrow> 't) field_name (field_getter :: ('a::packed_type) \<Rightarrow> ('f::packed_type)) field_setter t_hrs t_hrs_update;
struct_rewrite_expr P p' p;
struct_rewrite_expr Q f' f;
struct_rewrite_modifies R
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (p'' s)
(u s (field_setter (\<lambda>_. f' s))))) s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (p' s)
(field_setter (\<lambda>_. f' s) (h_val (hrs_mem (t_hrs s)) (p' s))))) s);
struct_rewrite_guard S (\<lambda>s. c_guard (p' s)) \<rbrakk> \<Longrightarrow>
struct_rewrite_modifies (\<lambda>s. P s \<and> Q s \<and> R s \<and> S s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (p'' s)
(u s (field_setter (\<lambda>_. f' s))))) s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (Ptr (field_lvalue (p s) field_name))
(f s))) s)"
apply (clarsimp simp: struct_rewrite_expr_def struct_rewrite_guard_def struct_rewrite_modifies_def valid_struct_field_def)
apply (erule_tac x = s in allE)+
apply (erule impE, assumption)+
apply (erule_tac t = "t_hrs_update (hrs_mem_update (heap_update (p'' s)
(u s (field_setter (\<lambda>_. f' s))))) s"
and s = "t_hrs_update (hrs_mem_update (heap_update (p' s)
(field_setter (\<lambda>_. f' s) (h_val (hrs_mem (t_hrs s)) (p' s))))) s"
in subst)
apply (rule read_write_valid_def3[where r = t_hrs and w = t_hrs_update])
apply assumption
apply (rule read_write_valid_def3[OF read_write_valid_hrs_mem])
apply (subst heap_update_field)
apply assumption+
apply (simp add: export_tag_adjust_ti(1)[rule_format] read_write_valid_fg_cons)
apply (subst update_ti_update_ti_t)
apply (simp add: size_of_def)
apply (subst update_ti_s_adjust_ti_to_bytes_p)
apply (erule read_write_valid_fg_cons)
apply simp
done
lemma struct_rewrite_modifies_Array_field__unused:
"\<lbrakk> valid_struct_field (st :: 's \<Rightarrow> 't) field_name (field_getter :: ('a::packed_type) \<Rightarrow> (('f::array_outer_packed)['n::array_max_count])) field_setter t_hrs t_hrs_update;
struct_rewrite_expr P p' p;
struct_rewrite_expr Q f' f;
struct_rewrite_modifies R
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (p'' s)
(u s (field_setter (\<lambda>a. Arrays.update a (nat k) (f' s)))))) s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (p' s)
(field_setter (\<lambda>a. Arrays.update a (nat k) (f' s))
(h_val (hrs_mem (t_hrs s)) (p' s))))) s);
struct_rewrite_guard S (\<lambda>s. c_guard (p' s)) \<rbrakk> \<Longrightarrow>
struct_rewrite_modifies (\<lambda>s. P s \<and> Q s \<and> R s \<and> S s \<and> 0 \<le> k \<and> nat k < CARD('n))
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (p'' s)
(u s (field_setter (\<lambda>a. Arrays.update a (nat k) (f' s)))))) s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update
(ptr_coerce (Ptr (field_lvalue (p s) field_name) :: ('f['n]) ptr) +\<^sub>p k) (f s))) s)"
using ptr_coerce.simps [simp del]
apply (clarsimp simp: struct_rewrite_expr_def struct_rewrite_guard_def struct_rewrite_modifies_def valid_struct_field_def)
apply (erule_tac x = s in allE)+
apply (erule impE, assumption)+
apply (erule_tac t = "t_hrs_update (hrs_mem_update (heap_update (p'' s)
(u s(field_setter (\<lambda>a. Arrays.update a (nat k) (f' s)))))) s"
and s = "t_hrs_update (hrs_mem_update (heap_update (p' s)
(field_setter (\<lambda>a. Arrays.update a (nat k) (f' s))
(h_val (hrs_mem (t_hrs s)) (p' s))))) s"
in subst)
apply (rule read_write_valid_def3[where r = t_hrs and w = t_hrs_update])
apply assumption
apply (rule read_write_valid_def3[OF read_write_valid_hrs_mem])
apply (case_tac k, clarsimp)
apply (subst heap_update_Array_element[symmetric])
apply assumption
apply (subst heap_update_field)
apply assumption+
apply (simp add: export_tag_adjust_ti(1)[rule_format] read_write_valid_fg_cons)
apply (subst h_val_field_from_bytes')
apply assumption+
apply (simp add: export_tag_adjust_ti(1)[rule_format] read_write_valid_fg_cons)
apply clarsimp
apply (subst update_ti_update_ti_t)
apply (simp add: size_of_def)
apply (subst update_ti_s_adjust_ti_to_bytes_p)
apply (erule read_write_valid_fg_cons)
apply clarsimp
apply (subst read_write_valid_def3[of field_getter field_setter])
apply auto
done
(*
* These produce less redundant output (we avoid "t_update (\<lambda>_. foo (t x)) x"
* where x is some huge term).
* The catch: we rely on the unifier to produce a "greedy" instantiation for ?f.
* Namely, if we are matching "?f s (h_val s p)" on
* "b_update (a_update (\<lambda>_. foo (h_val s p))) (h_val s p)",
* we expect ?f to be instantiated to
* "\<lambda>s v. b_update (a_update (\<lambda>_. foo v)) v"
* even though there are other valid ones.
* It just so happens that isabelle's unifier produces such an instantiation.
* Are we lucky, or presumptuous?
*)
lemma struct_rewrite_modifies_field [heap_abs]:
"\<lbrakk> valid_struct_field (st :: 's \<Rightarrow> 't) field_name (field_getter :: ('a::packed_type) \<Rightarrow> ('f::packed_type)) field_setter t_hrs t_hrs_update;
struct_rewrite_expr P p' p;
struct_rewrite_expr Q f' f;
\<And>s. heap_lift__wrap_h_val (h_val_p' s) (h_val (hrs_mem (t_hrs s)) (p' s));
struct_rewrite_modifies R
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (p'' s)
(u s (field_setter (f' s))))) s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (p' s)
(field_setter (f' s) (h_val_p' s)))) s);
struct_rewrite_guard S (\<lambda>s. c_guard (p' s)) \<rbrakk> \<Longrightarrow>
struct_rewrite_modifies (\<lambda>s. P s \<and> Q s \<and> R s \<and> S s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (p'' s)
(u s (field_setter (f' s))))) s)
(\<lambda>s. t_hrs_update (hrs_mem_update (heap_update (Ptr (field_lvalue (p s) field_name))
(f s (h_val (hrs_mem (t_hrs s)) (Ptr (field_lvalue (p s) field_name)))))) s)"
apply (clarsimp simp: struct_rewrite_expr_def struct_rewrite_guard_def struct_rewrite_modifies_def valid_struct_field_def heap_lift__wrap_h_val_def)
apply (erule_tac x = s in allE)+
apply (erule impE, assumption)+
apply (erule_tac t = "t_hrs_update (hrs_mem_update (heap_update (p'' s)
(u s (field_setter (f' s))))) s"
and s = "t_hrs_update (hrs_mem_update (heap_update (p' s)
(field_setter (f' s) (h_val (hrs_mem (t_hrs s)) (p' s))))) s"
in subst)
apply (rule read_write_valid_def3[where r = t_hrs and w = t_hrs_update])