-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSepAlgInstances.thy
1587 lines (1184 loc) · 54.8 KB
/
SepAlgInstances.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
theory SepAlgInstances
imports SepLogic HOL.Rat "HOL-Library.Product_Order" "HOL-Library.Product_Plus"
begin
section \<open> Product \<close>
declare plus_prod_def[simp]
declare zero_prod_def[simp]
subsection \<open> perm_alg \<close>
instantiation prod :: (perm_alg,perm_alg) perm_alg
begin
definition disjoint_prod :: \<open>'a \<times> 'b \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool\<close> where
\<open>disjoint_prod a b \<equiv> (fst a ## fst b \<and> snd a ## snd b)\<close>
declare disjoint_prod_def[simp]
instance
apply standard
apply (force simp add: partial_add_assoc)
apply (force dest: partial_add_commute)
apply (force simp add: disjoint_sym_iff)
apply (force dest: disjoint_add_rightL)
apply (force dest: disjoint_add_right_commute)
apply (force dest: positivity)
done
end
lemma less_sepadd_prod_eq:
\<open>a \<prec> b \<longleftrightarrow> (fst a \<noteq> fst b \<or> snd a \<noteq> snd b) \<and> fst a \<lesssim> fst b \<and> snd a \<lesssim> snd b\<close>
by (cases a; cases b; force simp add: less_sepadd_def part_of_def)
lemma less_eq_sepadd_prod_eq:
\<open>a \<preceq> b \<longleftrightarrow> fst a = fst b \<and> snd a = snd b \<or> fst a \<lesssim> fst b \<and> snd a \<lesssim> snd b\<close>
by (cases a; cases b; force simp add: less_eq_sepadd_def part_of_def)
lemma part_of_prod_eq:
\<open>a \<lesssim> b \<longleftrightarrow> fst a \<lesssim> fst b \<and> snd a \<lesssim> snd b\<close>
by (cases a; cases b; force simp add: part_of_def)
subsection \<open> mu_sep_alg \<close>
instantiation prod :: (multiunit_sep_alg,multiunit_sep_alg) multiunit_sep_alg
begin
lemma less_sepadd_prod_eq2[simp]:
fixes a :: \<open>'a \<times> 'b\<close>
shows \<open>a \<prec> b \<longleftrightarrow> (fst a \<prec> fst b \<and> snd a \<preceq> snd b \<or> fst a \<preceq> fst b \<and> snd a \<prec> snd b)\<close>
by (cases a, cases b, clarsimp simp add: less_sepadd_def' less_eq_sepadd_def',
metis unitof_disjoint2 unitof_is_unitR2)
lemma less_eq_sepadd_prod_eq2[simp]:
fixes a :: \<open>'a \<times> 'b\<close>
shows \<open>a \<preceq> b \<longleftrightarrow> fst a \<preceq> fst b \<and> snd a \<preceq> snd b\<close>
by (cases a, cases b, clarsimp simp add: less_eq_sepadd_def',
metis unitof_disjoint2 unitof_is_unitR2)
definition unitof_prod :: \<open>'a \<times> 'b \<Rightarrow> 'a \<times> 'b\<close> where
\<open>unitof \<equiv> map_prod unitof unitof\<close>
declare unitof_prod_def[simp]
instance
by standard (simp add: less_eq_sepadd_def)+
end
subsection \<open> sep_alg \<close>
instantiation prod :: (sep_alg,sep_alg) sep_alg
begin
declare bot_prod_def[simp]
instance
by standard (simp add: fun_eq_iff)+
end
lemma prod_sepadd_unit_iff[simp]:
\<open>sepadd_unit (a, b) \<longleftrightarrow> sepadd_unit a \<and> sepadd_unit b\<close>
by (simp add: sepadd_unit_def, force)
subsection \<open> Extended instances \<close>
instance prod :: (dupcl_perm_alg, dupcl_perm_alg) dupcl_perm_alg
by standard (force dest: dup_sub_closure)
(* not an allcompatible_perm_alg *)
instance prod :: (strong_sep_perm_alg, strong_sep_perm_alg) strong_sep_perm_alg
by standard (clarsimp simp add: selfsep_iff)
instance prod :: (disjoint_parts_perm_alg, disjoint_parts_perm_alg) disjoint_parts_perm_alg
by standard simp
instance prod :: (trivial_selfdisjoint_perm_alg, trivial_selfdisjoint_perm_alg) trivial_selfdisjoint_perm_alg
by standard (clarsimp, meson selfdisjoint_same)
instance prod :: (crosssplit_perm_alg, crosssplit_perm_alg) crosssplit_perm_alg
apply standard
apply clarsimp
apply (rename_tac a x b y c z d w)
apply (frule(2) cross_split[of \<open>_::'a\<close>])
apply (frule(2) cross_split[of \<open>_::'b\<close>])
apply clarsimp
apply metis
done
instance prod :: (cancel_perm_alg, cancel_perm_alg) cancel_perm_alg
by standard force
text \<open>
This instance is troublesome. We have that if either the left
or the right lacks a unit, then the entire instance will lack a unit.
However, Isabelle's typeclasses will now allow multiple instances,
even when the instance is completely logical. (I.e. there are no new definitions.)
We pick a right biased implementation, to match the default associativity of prod.
This means that permissions must be placed on the *right* of a tuple if we want to derive
instances like the cancellativity of munit heaps automatically.
\<close>
instance prod :: (perm_alg, no_unit_perm_alg) no_unit_perm_alg
by (standard) (metis no_units prod_eq_decompose(2) prod_sepadd_unit_iff)
(*
instance prod :: (perm_alg, no_unit_perm_alg) no_unit_perm_alg
by (standard) (metis no_units prod_eq_decompose(1) prod_sepadd_unit_iff)
*)
instantiation prod :: (halving_perm_alg, halving_perm_alg) halving_perm_alg
begin
definition \<open>half_prod \<equiv> \<lambda>(a,b). (half a, half b)\<close>
declare half_prod_def[simp]
instance
by standard
(clarsimp simp add: half_additive_split half_self_disjoint half_sepadd_distrib
split: prod.splits)+
end
instance prod :: (all_disjoint_perm_alg, all_disjoint_perm_alg) all_disjoint_perm_alg
by standard simp
subsection \<open> add_fst & add_snd for tuple perm_alg \<close>
lemma perm_alg_plus_fst_accum[simp]:
fixes x :: \<open>'a :: perm_alg\<close>
shows \<open>fst xy ## x \<Longrightarrow> fst xy ## x' \<Longrightarrow> x ## x' \<Longrightarrow> (xy +\<^sub>L x) +\<^sub>L x' = xy +\<^sub>L (x + x')\<close>
by (cases xy, simp add: partial_add_assoc)
lemma perm_alg_plus_snd_accum[simp]:
fixes y :: \<open>'a :: perm_alg\<close>
shows \<open>snd xy ## y \<Longrightarrow> snd xy ## y' \<Longrightarrow> y ## y' \<Longrightarrow> (xy +\<^sub>R y) +\<^sub>R y' = xy +\<^sub>R (y + y')\<close>
by (cases xy, simp add: partial_add_assoc)
lemma perm_alg_plus_fst_plus_snd_eq[simp]:
fixes y :: \<open>'a :: perm_alg\<close>
shows
\<open>xy +\<^sub>L x +\<^sub>R y = xy + (x, y)\<close>
\<open>xy +\<^sub>R y +\<^sub>L x = xy + (x, y)\<close>
by simp+
subsubsection \<open> Sepconj-conj \<close>
definition sepconj_conj
:: \<open>('a::perm_alg \<times> 'b::perm_alg \<Rightarrow> bool) \<Rightarrow> ('a \<times> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<times> 'b \<Rightarrow> bool)\<close>
(infixr \<open>\<^emph>\<and>\<close> 70) where
\<open>p \<^emph>\<and> q \<equiv> \<lambda>h. \<exists>a b c. a ## b \<and> h = (a + b, c) \<and> p (a, c) \<and> q (b, c)\<close>
lemma sepconj_conjI:
\<open>p (a, y) \<Longrightarrow> q (b, y) \<Longrightarrow> a ## b \<Longrightarrow> x = a + b \<Longrightarrow> (p \<^emph>\<and> q) (x, y)\<close>
by (force simp add: sepconj_conj_def)
lemma sepconj_conj_assoc:
\<open>(p \<^emph>\<and> q) \<^emph>\<and> r = p \<^emph>\<and> (q \<^emph>\<and> r)\<close>
apply (clarsimp simp add: sepconj_conj_def fun_eq_iff)
apply (rule iffI)
apply (metis disjoint_add_leftR disjoint_add_swap_lr partial_add_assoc2)
apply (metis disjoint_add_rightL disjoint_add_swap_rl partial_add_assoc3)
done
lemma sepconj_conj_mono:
\<open>p \<le> p' \<Longrightarrow> q \<le> q' \<Longrightarrow> p \<^emph>\<and> q \<le> p' \<^emph>\<and> q'\<close>
by (force simp add: sepconj_conj_def)
lemma sepconj_conj_monoL:
\<open>p \<le> p' \<Longrightarrow> p \<^emph>\<and> q \<le> p' \<^emph>\<and> q\<close>
by (force simp add: sepconj_conj_def)
lemma sepconj_conj_monoR:
\<open>q \<le> q' \<Longrightarrow> p \<^emph>\<and> q \<le> p \<^emph>\<and> q'\<close>
by (force simp add: sepconj_conj_def)
section \<open> (additive) unit \<close>
instantiation unit :: perm_alg
begin
definition plus_unit :: \<open>unit \<Rightarrow> unit \<Rightarrow> unit\<close> where
\<open>plus_unit a b \<equiv> ()\<close>
declare plus_unit_def[simp]
definition disjoint_unit :: \<open>unit \<Rightarrow> unit \<Rightarrow> bool\<close> where
\<open>disjoint_unit a b \<equiv> True\<close>
declare disjoint_unit_def[simp]
instance
by standard simp+
end
instantiation unit :: multiunit_sep_alg
begin
definition unitof_unit :: \<open>unit \<Rightarrow> unit\<close> where
\<open>unitof_unit \<equiv> \<lambda>_. ()\<close>
declare unitof_unit_def[simp]
instance
by standard force+
end
instantiation unit :: sep_alg
begin
definition zero_unit :: \<open>unit\<close> where
\<open>zero_unit \<equiv> ()\<close>
declare zero_unit_def[simp]
definition bot_unit :: \<open>unit\<close> where
\<open>bot_unit \<equiv> ()\<close>
declare bot_unit_def[simp]
instance
by standard simp+
end
subsection \<open> Extended instances \<close>
instance unit :: dupcl_perm_alg
by standard simp
instance unit :: allcompatible_perm_alg
by standard simp
instance unit :: strong_sep_perm_alg
by standard simp
instance unit :: disjoint_parts_perm_alg
by standard simp
instance unit :: trivial_selfdisjoint_perm_alg
by standard simp
instance unit :: crosssplit_perm_alg
by standard simp
instance unit :: cancel_perm_alg
by standard simp
(* not a no_unit_perm_alg *)
instantiation unit :: halving_perm_alg
begin
definition \<open>half_unit \<equiv> \<lambda>_::unit. ()\<close>
declare half_unit_def[simp]
instance by standard simp+
end
instance unit :: all_disjoint_perm_alg
by standard simp
section \<open> Sum \<close>
subsection \<open> order \<close>
instantiation sum :: (order, order) order
begin
definition less_eq_sum :: \<open>'a + 'b \<Rightarrow> 'a + 'b \<Rightarrow> bool\<close> where
\<open>less_eq_sum a b \<equiv>
(\<exists>x y. a = Inl x \<and> b = Inl y \<and> x \<le> y) \<or>
(\<exists>x y. a = Inr x \<and> b = Inr y \<and> x \<le> y)\<close>
lemma less_eq_sum_simps[simp]:
\<open>\<And>x y. Inl x \<le> Inl y \<longleftrightarrow> x \<le> y\<close>
\<open>\<And>x y. Inr x \<le> Inr y \<longleftrightarrow> x \<le> y\<close>
\<open>\<And>x y. Inl x \<le> Inr y \<longleftrightarrow> False\<close>
\<open>\<And>x y. Inr x \<le> Inl y \<longleftrightarrow> False\<close>
by (simp add: less_eq_sum_def)+
definition less_sum :: \<open>'a + 'b \<Rightarrow> 'a + 'b \<Rightarrow> bool\<close> where
\<open>less_sum a b \<equiv>
(\<exists>x y. a = Inl x \<and> b = Inl y \<and> x < y) \<or>
(\<exists>x y. a = Inr x \<and> b = Inr y \<and> x < y)\<close>
lemma less_sum_simps[simp]:
\<open>\<And>x y. Inl x < Inl y \<longleftrightarrow> x < y\<close>
\<open>\<And>x y. Inr x < Inr y \<longleftrightarrow> x < y\<close>
\<open>\<And>x y. Inl x < Inr y \<longleftrightarrow> False\<close>
\<open>\<And>x y. Inr x < Inl y \<longleftrightarrow> False\<close>
by (simp add: less_sum_def less_eq_sum_def)+
instance
apply standard
apply (case_tac x; case_tac y; simp add: less_le_not_le)
apply (case_tac x; simp)
apply (case_tac x; case_tac y; case_tac z; simp)
apply (case_tac x; case_tac y; simp)
done
end
subsection \<open> perm_alg \<close>
instantiation sum :: (perm_alg,perm_alg) perm_alg
begin
definition disjoint_sum :: \<open>'a + 'b \<Rightarrow> 'a + 'b \<Rightarrow> bool\<close> where
\<open>disjoint_sum a b \<equiv>
(\<exists>x y. a = Inl x \<and> b = Inl y \<and> x ## y) \<or>
(\<exists>x y. a = Inr x \<and> b = Inr y \<and> x ## y)\<close>
lemma disjoint_sum_simps[simp]:
\<open>\<And>x y. Inl x ## Inl y = x ## y\<close>
\<open>\<And>x y. Inr x ## Inr y = x ## y\<close>
\<open>\<And>x y. Inl x ## Inr y = False\<close>
\<open>\<And>x y. Inr x ## Inl y = False\<close>
by (simp add: disjoint_sum_def)+
definition plus_sum :: \<open>'a + 'b \<Rightarrow> 'a + 'b \<Rightarrow> 'a + 'b\<close> where
\<open>plus_sum a b \<equiv>
case a of
Inl x \<Rightarrow>
(case b of
Inl y \<Rightarrow> Inl (x + y)
| Inr y \<Rightarrow> undefined)
| Inr x \<Rightarrow>
(case b of
Inl y \<Rightarrow> undefined
| Inr y \<Rightarrow> Inr (x + y))\<close>
lemma plus_sum_simps[simp]:
\<open>\<And>x y. Inl x + Inl y = Inl (x + y)\<close>
\<open>\<And>x y. Inr x + Inr y = Inr (x + y)\<close>
by (simp add: plus_sum_def)+
instance
apply standard
apply (simp add: disjoint_sum_def)
apply (elim disjE; force simp add: partial_add_assoc)
apply (simp add: disjoint_sum_def)
apply (elim disjE; force dest: partial_add_commute)
apply (simp add: disjoint_sum_def)
apply (elim disjE exE conjE; force dest: disjoint_sym)
apply (simp add: disjoint_sum_def)
apply (elim disjE exE conjE; force dest: disjoint_add_rightL)
apply (simp add: disjoint_sum_def)
apply (elim disjE exE conjE; force dest: disjoint_add_right_commute)
apply (simp add: disjoint_sum_def)
apply (elim disjE exE conjE; force dest: positivity)
done
end
lemma part_of_sum_simps[simp]:
\<open>\<And>x y. Inl x \<lesssim> Inl y \<longleftrightarrow> x \<lesssim> y\<close>
\<open>\<And>x y. Inr x \<lesssim> Inr y \<longleftrightarrow> x \<lesssim> y\<close>
\<open>\<And>x y. Inl x \<lesssim> Inr y \<longleftrightarrow> False\<close>
\<open>\<And>x y. Inr x \<lesssim> Inl y \<longleftrightarrow> False\<close>
apply (simp add: part_of_def disjoint_sum_def plus_sum_def split: sum.splits)
apply (metis Inl_inject obj_sumE sum.distinct(1))
apply (simp add: part_of_def disjoint_sum_def plus_sum_def split: sum.splits)
apply (metis Inr_inject obj_sumE sum.distinct(1))
apply (simp add: part_of_def disjoint_sum_def plus_sum_def split: sum.splits)
apply (simp add: part_of_def disjoint_sum_def plus_sum_def split: sum.splits)
done
lemma less_eq_sepadd_sum_simps[simp]:
\<open>\<And>x y. Inl x \<preceq> Inl y \<longleftrightarrow> x \<preceq> y\<close>
\<open>\<And>x y. Inr x \<preceq> Inr y \<longleftrightarrow> x \<preceq> y\<close>
\<open>\<And>x y. Inl x \<preceq> Inr y \<longleftrightarrow> False\<close>
\<open>\<And>x y. Inr x \<preceq> Inl y \<longleftrightarrow> False\<close>
by (simp add: less_eq_sepadd_def)+
lemma less_sepadd_sum_simps[simp]:
\<open>\<And>x y. Inl x \<prec> Inl y \<longleftrightarrow> x \<prec> y\<close>
\<open>\<And>x y. Inr x \<prec> Inr y \<longleftrightarrow> x \<prec> y\<close>
\<open>\<And>x y. Inl x \<prec> Inr y \<longleftrightarrow> False\<close>
\<open>\<And>x y. Inr x \<prec> Inl y \<longleftrightarrow> False\<close>
by (simp add: less_sepadd_def)+
subsection \<open> mu_sep_alg \<close>
instantiation sum :: (multiunit_sep_alg,multiunit_sep_alg) multiunit_sep_alg
begin
definition unitof_sum :: \<open>'a + 'b \<Rightarrow> 'a + 'b\<close> where
\<open>unitof_sum \<equiv> map_sum unitof unitof\<close>
lemmas unitof_simps[simp] =
map_sum.simps[
of \<open>unitof :: 'a \<Rightarrow> _\<close> \<open>unitof :: 'b \<Rightarrow> _\<close>,
unfolded unitof_sum_def[symmetric]]
instance
apply standard
apply (case_tac a; simp)
apply (case_tac a; case_tac b; simp)
done
end
subsection \<open> Extended instances \<close>
instance sum :: (dupcl_perm_alg, dupcl_perm_alg) dupcl_perm_alg
apply standard
apply (simp add: disjoint_sum_def)
apply (elim disjE exE conjE; force dest: dup_sub_closure)
done
section \<open> (multiplicative) unit \<close>
typedef munit = \<open>{()}\<close>
by blast
abbreviation munit :: munit (\<open>\<one>\<close>) where
\<open>\<one> \<equiv> Abs_munit ()\<close>
lemma eq_munit_iff[iff]:
\<open>a = (b::munit)\<close>
using Rep_munit_inject by auto
instantiation munit :: order
begin
definition less_eq_munit :: \<open>munit \<Rightarrow> munit \<Rightarrow> bool\<close> where
\<open>less_eq_munit a b \<equiv> True\<close>
declare less_eq_munit_def[simp]
definition less_munit :: \<open>munit \<Rightarrow> munit \<Rightarrow> bool\<close> where
\<open>less_munit a b \<equiv> False\<close>
declare less_munit_def[simp]
instance
by standard simp+
end
instantiation munit :: perm_alg
begin
definition plus_munit :: \<open>munit \<Rightarrow> munit \<Rightarrow> munit\<close> where
\<open>plus_munit a b \<equiv> undefined\<close>
declare plus_munit_def[simp]
definition disjoint_munit :: \<open>munit \<Rightarrow> munit \<Rightarrow> bool\<close> where
\<open>disjoint_munit a b \<equiv> False\<close>
declare disjoint_munit_def[simp]
instance
by standard simp+
end
subsection \<open> Extended instances \<close>
instance munit :: dupcl_perm_alg
by standard simp
(* not a allcompatible_perm_alg *)
instance munit :: strong_sep_perm_alg
by standard simp
instance munit :: disjoint_parts_perm_alg
by standard simp
instance munit :: trivial_selfdisjoint_perm_alg
by standard simp
instance munit :: crosssplit_perm_alg
by standard simp
instance munit :: cancel_perm_alg
by standard simp
instance munit :: no_unit_perm_alg
by standard (simp add: sepadd_unit_def)
(* not a halving_perm_alg *)
(* not an all_disjoint_perm_alg *)
section \<open> option \<close>
instantiation option :: (perm_alg) perm_alg
begin
definition disjoint_option :: \<open>'a option \<Rightarrow> 'a option \<Rightarrow> bool\<close> where
\<open>disjoint_option a b \<equiv>
case a of None \<Rightarrow> True | Some x \<Rightarrow>
(case b of None \<Rightarrow> True | Some y \<Rightarrow> x ## y)\<close>
lemma disjoint_option_simps[simp]:
\<open>Some x ## Some y \<longleftrightarrow> x ## y\<close>
\<open>None ## b\<close>
\<open>a ## None\<close>
by (simp add: disjoint_option_def split: option.splits)+
lemma disjoint_option_iff:
\<open>Some x ## b \<longleftrightarrow> b = None \<or> (\<exists>y. b = Some y \<and> x ## y)\<close>
\<open>a ## Some y \<longleftrightarrow> a = None \<or> (\<exists>x. a = Some x \<and> x ## y)\<close>
by (simp add: disjoint_option_def split: option.splits)+
lemma disjoint_option_def2:
\<open>a ## b \<longleftrightarrow> a = None \<or> b = None \<or> the a ## the b\<close>
by (cases a; cases b; simp)
definition plus_option :: \<open>'a option \<Rightarrow> 'a option \<Rightarrow> 'a option\<close> where
\<open>plus_option a b \<equiv>
case a of None \<Rightarrow> b | Some x \<Rightarrow>
(case b of None \<Rightarrow> a | Some y \<Rightarrow> Some (x + y))\<close>
lemma plus_option_simps[simp]:
\<open>Some x + Some y = Some (x + y)\<close>
\<open>None + b = b\<close>
\<open>a + None = a\<close>
by (simp add: plus_option_def split: option.splits)+
lemma plus_option_iff:
\<open>Some x + b = Some z \<longleftrightarrow> (b = None \<and> x = z \<or> (\<exists>y. b = Some y \<and> z = x + y))\<close>
\<open>a + Some y = Some z \<longleftrightarrow> (a = None \<and> y = z \<or> (\<exists>x. a = Some x \<and> z = x + y))\<close>
\<open>a + b = None \<longleftrightarrow> a = None \<and> b = None\<close>
by (force simp add: disjoint_option_def plus_option_def split: option.splits)+
instance
apply standard
apply (simp add: disjoint_option_def plus_option_def partial_add_assoc
split: option.splits; fail)
apply (simp add: disjoint_option_def plus_option_def split: option.splits,
metis partial_add_commute; fail)
apply (metis disjoint_option_def2 disjoint_sym)
apply (simp add: disjoint_option_def split: option.splits,
metis disjoint_add_rightL; fail)
apply (simp add: disjoint_option_def disjoint_sym_iff
disjoint_add_right_commute split: option.splits; fail)
apply (simp add: disjoint_option_def positivity split: option.splits; fail)
done
end
lemma less_eq_sepadd_option_simps[simp]:
\<open>None \<preceq> a\<close>
\<open>Some x \<preceq> None \<longleftrightarrow> False\<close>
\<open>Some x \<preceq> Some y \<longleftrightarrow> x \<preceq> y\<close>
by (force simp add: less_eq_sepadd_def' disjoint_option_iff)+
lemma less_sepadd_option_simps[simp]:
\<open>a \<prec> None \<longleftrightarrow> False\<close>
\<open>None \<prec> Some x\<close>
\<open>Some x \<prec> Some y \<longleftrightarrow> x \<prec> y\<close>
by (simp add: less_sepadd_def' disjoint_option_iff plus_option_iff; blast?; force)+
instantiation option :: (perm_alg) multiunit_sep_alg
begin
definition unitof_option :: \<open>'a option \<Rightarrow> 'a option\<close> where
\<open>unitof_option x \<equiv> None\<close>
declare unitof_option_def[simp]
instance
by standard force+
end
instantiation option :: (perm_alg) sep_alg
begin
definition zero_option :: \<open>'a option\<close> where
\<open>zero_option \<equiv> None\<close>
declare zero_option_def[simp]
definition bot_option :: \<open>'a option\<close> where
\<open>bot_option \<equiv> None\<close>
declare bot_option_def[simp]
instance
by standard force+
end
subsection \<open> Extended instances \<close>
instance option :: (dupcl_perm_alg) dupcl_perm_alg
by standard
(simp add: disjoint_option_def split: option.splits,
metis dup_sub_closure)
(* is an allcompatible_perm_alg as it's a sep_alg *)
(* not a strong_sep_perm_alg *)
instance option :: (disjoint_parts_perm_alg) disjoint_parts_perm_alg
by standard
(simp add: disjoint_option_def split: option.splits)
instance option :: (trivial_selfdisjoint_perm_alg) trivial_selfdisjoint_perm_alg
by standard
(force dest: selfdisjoint_same simp add: disjoint_option_def plus_option_def
split: option.splits)
instance option :: (crosssplit_perm_alg) crosssplit_perm_alg
apply standard
apply (clarsimp simp add: disjoint_option_def plus_option_def
split: option.splits)
apply blast
apply blast
apply blast
apply blast
apply blast
apply blast
apply blast
apply blast
apply (frule(2) cross_split)
apply clarsimp
apply (rule_tac x=\<open>Some ac\<close> in exI)
apply (rule_tac x=\<open>Some ad\<close> in exI)
apply simp
apply (rule_tac x=\<open>Some bc\<close> in exI)
apply simp
apply (rule_tac x=\<open>Some bd\<close> in exI)
apply simp
done
text \<open>
The option-instance is only cancellable when the sub-instance is cancellative *and*
that instance has no units.
\<close>
instance option :: (\<open>{cancel_perm_alg,no_unit_perm_alg}\<close>) cancel_perm_alg
by standard
(simp add: disjoint_option_def plus_option_def split: option.splits;
metis cancel_right_to_unit no_units)
(* not no_unit_perm_alg *)
instantiation option :: (halving_perm_alg) halving_perm_alg
begin
definition \<open>half_option \<equiv> map_option half\<close>
instance
by standard
(simp add: half_option_def disjoint_option_def plus_option_def half_additive_split
half_self_disjoint half_sepadd_distrib split: option.splits)+
end
instance option :: (all_disjoint_perm_alg) all_disjoint_perm_alg
by standard (simp add: disjoint_option_def split: option.splits)+
section \<open> functions \<close>
instantiation "fun" :: (type, perm_alg) perm_alg
begin
definition disjoint_fun :: \<open>('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> bool\<close> where
\<open>disjoint_fun f g \<equiv> \<forall>x. f x ## g x\<close>
lemma disjoint_funI[intro!]:
\<open>\<forall>x. f x ## g x \<Longrightarrow> f ## g\<close>
by (simp add: disjoint_fun_def)
definition plus_fun :: \<open>('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)\<close> where
\<open>plus_fun f g \<equiv> \<lambda>x. f x + g x\<close>
lemma plus_fun_apply[simp]:
\<open>(f + g) x = (f x + g x)\<close>
by (simp add: plus_fun_def)
instance
apply standard
apply (simp add: disjoint_fun_def plus_fun_def fun_eq_iff, metis partial_add_assoc)
apply (simp add: disjoint_fun_def plus_fun_def fun_eq_iff, metis partial_add_commute)
apply (simp add: disjoint_fun_def, metis disjoint_sym)
apply (simp add: disjoint_fun_def plus_fun_def, metis disjoint_add_rightL)
apply (simp add: disjoint_fun_def plus_fun_def, metis disjoint_add_right_commute)
apply (simp add: disjoint_fun_def plus_fun_def fun_eq_iff, metis positivity)
done
lemma less_sepadd_fun_eq:
fixes f g :: \<open>'a \<Rightarrow> 'b::perm_alg\<close>
shows \<open>f \<prec> g \<longleftrightarrow> (\<exists>x. f x \<noteq> g x) \<and> (\<forall>x. f x \<lesssim> g x)\<close>
by (simp add: part_of_def less_sepadd_def' fun_eq_iff disjoint_fun_def,
metis)
lemma less_eq_sepadd_fun_eq:
fixes f g :: \<open>'a \<Rightarrow> 'b::perm_alg\<close>
shows \<open>f \<preceq> g \<longleftrightarrow> (\<forall>x. f x = g x) \<or> (\<forall>x. f x \<lesssim> g x)\<close>
by (simp add: part_of_def less_eq_sepadd_def' disjoint_fun_def fun_eq_iff,
metis)
end
lemma fun_all_unit_elems_then_unit:
\<open>\<forall>x. sepadd_unit (f x) \<Longrightarrow> sepadd_unit f\<close>
by (simp add: disjoint_fun_def plus_fun_def sepadd_unit_def)
instantiation "fun" :: (type, multiunit_sep_alg) multiunit_sep_alg
begin
definition unitof_fun :: \<open>('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)\<close> where
\<open>unitof_fun f \<equiv> \<lambda>x. unitof (f x)\<close>
declare unitof_fun_def[simp]
instance
by standard
(simp add: disjoint_fun_def plus_fun_def le_fun_def fun_eq_iff le_iff_sepadd; metis)+
lemma less_sepadd_fun_eq2:
fixes f g :: \<open>'a \<Rightarrow> 'b\<close>
shows \<open>f \<prec> g \<longleftrightarrow> (\<exists>x. f x \<prec> g x) \<and> (\<forall>x. f x \<preceq> g x)\<close>
by (metis le_iff_part_of less_sepadd_def less_sepadd_fun_eq)
lemma less_eq_sepadd_fun_eq2:
fixes f g :: \<open>'a \<Rightarrow> 'b\<close>
shows \<open>f \<preceq> g \<longleftrightarrow> (\<forall>x. f x \<preceq> g x)\<close>
by (metis le_iff_part_of less_eq_sepadd_def' less_eq_sepadd_fun_eq)
end
instantiation "fun" :: (type, sep_alg) sep_alg
begin
definition zero_fun :: \<open>('a \<Rightarrow> 'b)\<close> where
\<open>zero_fun \<equiv> \<lambda>x. 0\<close>
declare zero_fun_def[simp]
definition bot_fun :: \<open>('a \<Rightarrow> 'b)\<close> where
\<open>bot_fun \<equiv> \<lambda>x. 0\<close>
declare bot_fun_def[simp]
instance
by standard
(fastforce simp add: fun_eq_iff less_eq_sepadd_fun_eq2)+
end
subsection \<open> Extended instances \<close>
instance "fun" :: (type, dupcl_perm_alg) dupcl_perm_alg
by standard
(simp add: disjoint_fun_def plus_fun_def fun_eq_iff,
metis dup_sub_closure)
(* not allcompatible_perm_alg *)
instance "fun" :: (type, strong_sep_perm_alg) strong_sep_perm_alg
by standard
(clarsimp simp add: disjoint_fun_def plus_fun_def fun_eq_iff selfsep_iff
fun_all_unit_elems_then_unit)
instance "fun" :: (type, disjoint_parts_perm_alg) disjoint_parts_perm_alg
by standard (simp add: disjoint_fun_def)
instance "fun" :: (type, trivial_selfdisjoint_perm_alg) trivial_selfdisjoint_perm_alg
by standard
(force dest: selfdisjoint_same simp add: disjoint_fun_def plus_fun_def fun_eq_iff)
instance "fun" :: (type, crosssplit_perm_alg) crosssplit_perm_alg
proof standard
fix a b c d :: \<open>'a \<Rightarrow> 'b\<close>
assume
\<open>a ## b\<close>
\<open>c ## d\<close>
\<open>a + b = c + d\<close>
then have assms2:
\<open>\<forall>x. a x ## b x\<close>
\<open>\<forall>x. c x ## d x\<close>
\<open>\<forall>x. a x + b x = c x + d x\<close>
by (simp add: disjoint_fun_def plus_fun_def fun_eq_iff)+
then have \<open>\<forall>x. \<exists>acx adx bcx bdx.
acx ## adx \<and> bcx ## bdx \<and> acx ## bcx \<and> adx ## bdx \<and>
a x = acx + adx \<and> b x = bcx + bdx \<and>
c x = acx + bcx \<and> d x = adx + bdx\<close>
using cross_split[of \<open>a x\<close> \<open>b x\<close> \<open>c x\<close> \<open>d x\<close> for x]
by metis
then show
\<open>\<exists>ac ad bc bd.
ac ## ad \<and> bc ## bd \<and> ac ## bc \<and> ad ## bd \<and>
ac + ad = a \<and> bc + bd = b \<and> ac + bc = c \<and> ad + bd = d\<close>
by (simp add: disjoint_fun_def plus_fun_def fun_eq_iff, metis)
qed
instance "fun" :: (type, cancel_perm_alg) cancel_perm_alg
by standard
(simp add: disjoint_fun_def plus_fun_def fun_eq_iff)
(* not no_unit_perm_alg *)
instantiation "fun" :: (type, halving_perm_alg) halving_perm_alg
begin
definition \<open>half_fun (f :: 'a \<Rightarrow> 'b) \<equiv> \<lambda>x. half (f x)\<close>
instance
by standard
(simp add: half_fun_def disjoint_fun_def plus_fun_def fun_eq_iff
half_additive_split half_self_disjoint half_sepadd_distrib)+
end
instance "fun" :: (type, all_disjoint_perm_alg) all_disjoint_perm_alg
by standard (simp add: disjoint_fun_def)+
section \<open> Discrete Algebra \<close>
typedef 'a discr = \<open>UNIV :: 'a set\<close>
morphisms the_discr Discr
by blast
lemmas Discr_inverse_iff[simp] = Discr_inverse[simplified]
lemmas Discr_inject_iff[simp] = Discr_inject[simplified]
instantiation discr :: (type) order
begin
definition less_eq_discr :: \<open>'a discr \<Rightarrow> 'a discr \<Rightarrow> bool\<close> where
\<open>less_eq_discr a b \<equiv> the_discr a = the_discr b\<close>
declare less_eq_discr_def[simp]
definition less_discr :: \<open>'a discr \<Rightarrow> 'a discr \<Rightarrow> bool\<close> where
\<open>less_discr a b \<equiv> False\<close>
declare less_discr_def[simp]
instance
by standard (simp add: the_discr_inject)+
end
instantiation discr :: (type) perm_alg
begin
definition plus_discr :: \<open>'a discr \<Rightarrow> 'a discr \<Rightarrow> 'a discr\<close> where
\<open>plus_discr a b \<equiv> a\<close>
declare plus_discr_def[simp]
definition disjoint_discr :: \<open>'a discr \<Rightarrow> 'a discr \<Rightarrow> bool\<close> where
\<open>disjoint_discr a b \<equiv> a = b\<close>
declare disjoint_discr_def[simp]
instance
by standard (force simp add: the_discr_inject)+
end
lemma less_eq_discr_iff[simp]:
\<open>Discr x \<preceq> Discr y \<longleftrightarrow> x = y\<close>
by (simp add: less_eq_sepadd_def')
instantiation discr :: (type) multiunit_sep_alg
begin
definition unitof_discr :: \<open>'a discr \<Rightarrow> 'a discr\<close> where
\<open>unitof_discr x = x\<close>
declare unitof_discr_def[simp]
instance by standard (force simp add: the_discr_inject)+
end
subsection \<open> Extended instances \<close>
(* not sep_alg *)
instance discr :: (type) dupcl_perm_alg
by standard force
(* not allcompatible_perm_alg *)
instance discr :: (type) strong_sep_perm_alg
by standard (simp add: sepadd_unit_def)
instance discr :: (type) disjoint_parts_perm_alg
by standard force
instance discr :: (type) trivial_selfdisjoint_perm_alg
by standard force
instance discr :: (type) crosssplit_perm_alg
by standard force
instance discr :: (type) cancel_perm_alg
by standard force
(* not no_unit_perm_alg *)
instantiation discr :: (type) halving_perm_alg
begin
definition \<open>half_discr (a :: 'a discr) \<equiv> a\<close>
declare half_discr_def[simp]
instance by standard simp+
end
(* not all_disjoint_perm_alg *)
section \<open> Fractional FPermissions \<close>
typedef(overloaded) ('a::\<open>{linordered_semiring,zero_less_one}\<close>) fperm =
\<open>{x. (0::'a) < x \<and> x \<le> 1}\<close>
morphisms fperm_val FPerm
using zero_less_one by blast
setup_lifting type_definition_fperm
subsection \<open> helper lemmas \<close>
lemmas FPerm_inverse_iff[simp] = FPerm_inverse[simplified]
lemmas FPerm_inject_iff[simp] = FPerm_inject[simplified]
lemmas fperm_val_inject_rev = fperm_val_inject[symmetric]
lemma FPerm_eq_iff:
\<open>0 < a \<Longrightarrow> a \<le> 1 \<Longrightarrow> FPerm a = pa \<longleftrightarrow> fperm_val pa = a\<close>
using fperm_val_inverse by fastforce
lemma eq_FPerm_iff:
\<open>0 < a \<Longrightarrow> a \<le> 1 \<Longrightarrow> pa = FPerm a \<longleftrightarrow> fperm_val pa = a\<close>
by (metis FPerm_inverse_iff fperm_val_inverse)
lemma fperm_val_conditions:
\<open>0 < fperm_val x\<close>
\<open>fperm_val x \<le> 1\<close>
using fperm_val by force+
lemma fperm_val_never_zero[simp]:
\<open>fperm_val x = 0 \<longleftrightarrow> False\<close>
by (metis less_irrefl fperm_val_conditions(1))
lemma fperm_val_add_gt0:
\<open>0 < fperm_val x + fperm_val y\<close>
by (simp add: add_pos_pos fperm_val_conditions(1))
instantiation fperm :: (\<open>{linordered_semiring,zero_less_one}\<close>) order
begin
definition less_eq_fperm :: \<open>'a fperm \<Rightarrow> 'a fperm \<Rightarrow> bool\<close> where
\<open>less_eq_fperm a b \<equiv> fperm_val a \<le> fperm_val b\<close>
lemma less_eq_fperm_iff[simp]:
\<open>0 < x \<Longrightarrow> x \<le> 1 \<Longrightarrow> 0 < y \<Longrightarrow> y \<le> 1 \<Longrightarrow> FPerm x \<le> FPerm y \<longleftrightarrow> x \<le> y\<close>
by (simp add: less_eq_fperm_def)
definition less_fperm :: \<open>'a fperm \<Rightarrow> 'a fperm \<Rightarrow> bool\<close> where
\<open>less_fperm a b \<equiv> fperm_val a < fperm_val b\<close>
lemma less_fperm_iff[simp]:
\<open>0 < x \<Longrightarrow> x \<le> 1 \<Longrightarrow> 0 < y \<Longrightarrow> y \<le> 1 \<Longrightarrow> FPerm x < FPerm y \<longleftrightarrow> x < y\<close>
by (simp add: less_fperm_def)
instance
apply standard
apply (force simp add: less_eq_fperm_def less_fperm_def)+
apply (fastforce simp add: less_eq_fperm_def fperm_val_inject)
done
end
subsection \<open> perm_alg \<close>
instantiation fperm :: (\<open>{linordered_semiring,zero_less_one}\<close>) one
begin
lift_definition one_fperm :: \<open>'a fperm\<close> is \<open>1\<close> by simp
instance by standard
end
instantiation fperm :: (\<open>{linordered_semiring,zero_less_one}\<close>) perm_alg
begin
lift_definition disjoint_fperm :: \<open>'a fperm \<Rightarrow> 'a fperm \<Rightarrow> bool\<close> is
\<open>\<lambda>a b. a + b \<le> 1\<close> .
lemmas disjoint_fperm_iff = disjoint_fperm.rep_eq
lift_definition plus_fperm :: \<open>'a fperm \<Rightarrow> 'a fperm \<Rightarrow> 'a fperm\<close> is \<open>\<lambda>x y. min 1 (x + y)\<close>
by (force simp add: add_pos_pos min_def)
lemma plus_fperm_iff[simp]:
\<open>0 < x \<Longrightarrow> x \<le> 1 \<Longrightarrow> 0 < y \<Longrightarrow> y \<le> 1 \<Longrightarrow> FPerm x + FPerm y = FPerm (min 1 (x + y))\<close>
by (simp add: plus_fperm.abs_eq eq_onp_same_args)
lemma plus_fperm_eq:
\<open>x + y = FPerm (min 1 (fperm_val x + fperm_val y))\<close>
by (metis fperm_val_inverse plus_fperm.rep_eq)