-
Notifications
You must be signed in to change notification settings - Fork 1
/
Partial_Fun.thy
1444 lines (1043 loc) · 64.7 KB
/
Partial_Fun.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
(******************************************************************************)
(* Project: Isabelle/UTP Toolkit *)
(* File: Partial_Fun.thy *)
(* Authors: Simon Foster and Frank Zeyda *)
(* Emails: simon.foster@york.ac.uk and frank.zeyda@york.ac.uk *)
(******************************************************************************)
section \<open> Partial Functions \<close>
theory Partial_Fun
imports "Optics.Optics" Map_Extra "HOL-Library.Mapping"
begin
no_notation "Stream.stream.SCons" (infixr \<open>##\<close> 65)
text \<open> I'm not completely satisfied with partial functions as provided by Map.thy, since they don't
have a unique type and so we can't instantiate classes, make use of adhoc-overloading
etc. Consequently I've created a new type and derived the laws. \<close>
subsection \<open> Partial function type and operations \<close>
typedef ('a, 'b) pfun = "UNIV :: ('a \<rightharpoonup> 'b) set"
morphisms pfun_lookup pfun_of_map ..
type_notation pfun (infixr "\<Zpfun>" 1)
setup_lifting type_definition_pfun
lemma pfun_lookup_map [simp]: "pfun_lookup (pfun_of_map f) = f"
by (simp add: pfun_of_map_inverse)
lift_bnf ('k, pran: 'v) pfun [wits: "Map.empty :: 'k \<Rightarrow> 'v option"] for map: map_pfun rel: relt_pfun
by auto
declare pfun.map_transfer [transfer_rule]
instantiation pfun :: (type, type) equal
begin
definition "HOL.equal m1 m2 \<longleftrightarrow> (\<forall>k. pfun_lookup m1 k = pfun_lookup m2 k)"
instance
by (intro_classes, auto simp add: equal_pfun_def, transfer, auto)
end
lift_definition pfun_app :: "('a, 'b) pfun \<Rightarrow> 'a \<Rightarrow> 'b" ("_'(_')\<^sub>p" [999,0] 999) is
"\<lambda> f x. if (x \<in> dom f) then the (f x) else undefined" .
lift_definition pfun_upd :: "('a, 'b) pfun \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) pfun"
is "\<lambda> f k v. f(k := Some v)" .
lift_definition pdom :: "('a, 'b) pfun \<Rightarrow> 'a set" is dom .
lemma pran_rep_eq [transfer_rule]: "pran f = ran (pfun_lookup f)"
by (transfer, auto simp add: ran_def)
lift_definition pfun_comp :: "('b, 'c) pfun \<Rightarrow> ('a, 'b) pfun \<Rightarrow> ('a, 'c) pfun" (infixl "\<circ>\<^sub>p" 55) is
"\<lambda> f g. f \<circ>\<^sub>m g" .
lift_definition map_pfun' :: "('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a, 'b) pfun \<Rightarrow> ('c, 'd) pfun"
is "\<lambda>f g m. (map_option g \<circ> m \<circ> f)" parametric map_parametric .
functor map_pfun'
by (transfer, auto simp add: fun_eq_iff option.map_comp option.map_id)+
lift_definition pfun_member :: "'a \<times> 'b \<Rightarrow> ('a, 'b) pfun \<Rightarrow> bool" (infix "\<in>\<^sub>p" 50) is "(\<in>\<^sub>m)" .
lift_definition pfun_inj :: "('a, 'b) pfun \<Rightarrow> bool" is "\<lambda> f. inj_on f (dom f)" .
lift_definition pfun_inv :: "('a, 'b) pfun \<Rightarrow> ('b, 'a) pfun" is map_inv .
lift_definition pId_on :: "'a set \<Rightarrow> ('a, 'a) pfun" is "\<lambda> A x. if (x \<in> A) then Some x else None" .
abbreviation pId :: "('a, 'a) pfun" where
"pId \<equiv> pId_on UNIV"
lift_definition pdom_res :: "'a set \<Rightarrow> ('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun" (infixr "\<lhd>\<^sub>p" 85)
is "\<lambda> A f. restrict_map f A" .
abbreviation pdom_nres (infixr "-\<lhd>\<^sub>p" 85) where "pdom_nres A P \<equiv> (- A) \<lhd>\<^sub>p P"
lift_definition pran_res :: "('a, 'b) pfun \<Rightarrow> 'b set \<Rightarrow> ('a, 'b) pfun" (infixl "\<rhd>\<^sub>p" 86)
is ran_restrict_map .
abbreviation pran_nres (infixr "\<rhd>\<^sub>p-" 66) where "pran_nres P A \<equiv> P \<rhd>\<^sub>p (- A)"
definition pfun_image :: "'a \<Zpfun> 'b \<Rightarrow> 'a set \<Rightarrow> 'b set" where
[simp]: "pfun_image f A = pran (A \<lhd>\<^sub>p f)"
lift_definition pfun_graph :: "('a, 'b) pfun \<Rightarrow> ('a \<times> 'b) set" is map_graph .
lift_definition graph_pfun :: "('a \<times> 'b) set \<Rightarrow> ('a, 'b) pfun" is "graph_map \<circ> mk_functional" .
definition pfun_pfun :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a \<Zpfun> 'b) set" where
"pfun_pfun A B = {f :: 'a \<Zpfun> 'b. pdom(f) \<subseteq> A \<and> pran(f) \<subseteq> B}"
definition pfun_tfun :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a \<Zpfun> 'b) set" where
"pfun_tfun A B = {f \<in> pfun_pfun A B. pdom(f) = UNIV}"
definition pfun_ffun :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a \<Zpfun> 'b) set" where
"pfun_ffun A B = {f \<in> pfun_pfun A B. finite(pdom(f))}"
definition pfun_pinj :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a \<Zpfun> 'b) set" where
"pfun_pinj A B = {f \<in> pfun_pfun A B. pfun_inj f}"
definition pfun_psurj :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a \<Zpfun> 'b) set" where
"pfun_psurj A B = {f \<in> pfun_pfun A B. pran(f) = UNIV}"
definition "pfun_finj A B = pfun_ffun A B \<inter> pfun_pinj A B"
definition "pfun_tinj A B = pfun_tfun A B \<inter> pfun_pinj A B"
definition "pfun_tsurj A B = pfun_tfun A B \<inter> pfun_psurj A B"
definition "pfun_bij A B = pfun_tfun A B \<inter> pfun_pinj A B \<inter> pfun_psurj A B"
lift_definition pfun_entries :: "'k set \<Rightarrow> ('k \<Rightarrow> 'v) \<Rightarrow> ('k, 'v) pfun" is
"\<lambda> d f x. if x \<in> d then Some (f x) else None" .
definition pfuse :: "('a \<Zpfun> 'b) \<Rightarrow> ('a \<Zpfun> 'c) \<Rightarrow> ('a \<Zpfun> 'b \<times> 'c)"
where "pfuse f g = pfun_entries (pdom(f) \<inter> pdom(g)) (\<lambda> x. (pfun_app f x, pfun_app g x))"
lift_definition ptabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) pfun"
is "\<lambda>ks f. (map_of (List.map (\<lambda>k. (k, f k)) ks))" .
lift_definition pcombine ::
"('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun"
is "\<lambda>f m1 m2 x. combine_options f (m1 x) (m2 x)" .
abbreviation "fun_pfun \<equiv> pfun_entries UNIV"
definition pfun_disjoint :: "'a \<Zpfun> 'b set \<Rightarrow> bool" where
"pfun_disjoint S = (\<forall> i \<in> pdom S. \<forall> j \<in> pdom S. i \<noteq> j \<longrightarrow> pfun_app S i \<inter> pfun_app S j = {})"
definition pfun_partitions :: "'a \<Zpfun> 'b set \<Rightarrow> 'b set \<Rightarrow> bool" where
"pfun_partitions S T = (pfun_disjoint S \<and> \<Union> (pran S) = T)"
no_notation disj (infixr "|" 30)
definition pabs :: "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Zpfun> 'b" where
"pabs A P f = (A \<inter> Collect P) \<lhd>\<^sub>p fun_pfun f"
definition pcard :: "('a, 'b) pfun \<Rightarrow> nat"
where "pcard f = card (pdom f)"
unbundle lattice_syntax
instantiation pfun :: (type, type) bot
begin
lift_definition bot_pfun :: "('a, 'b) pfun" is "Map.empty" .
instance ..
end
abbreviation pempty :: "('a, 'b) pfun" ("{}\<^sub>p")
where "pempty \<equiv> bot"
instantiation pfun :: (type, type) oplus
begin
lift_definition oplus_pfun :: "('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun" is "(++)" .
instance ..
end
instantiation pfun :: (type, type) minus
begin
lift_definition minus_pfun :: "('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun" is "(--)" .
instance ..
end
instantiation pfun :: (type, type) inf
begin
lift_definition inf_pfun :: "('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun" is
"\<lambda> f g x. if (x \<in> dom(f) \<inter> dom(g) \<and> f(x) = g(x)) then f(x) else None" .
instance ..
end
abbreviation pfun_inter :: "('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun" (infixl "\<inter>\<^sub>p" 80)
where "pfun_inter \<equiv> inf"
instantiation pfun :: (type, type) order
begin
lift_definition less_eq_pfun :: "('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun \<Rightarrow> bool" is
"\<lambda> f g. f \<subseteq>\<^sub>m g" .
lift_definition less_pfun :: "('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun \<Rightarrow> bool" is
"\<lambda> f g. f \<subseteq>\<^sub>m g \<and> f \<noteq> g" .
instance
by (intro_classes, (transfer, auto intro: map_le_trans simp add: map_le_antisym)+)
end
abbreviation pfun_subset :: "('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun \<Rightarrow> bool" (infix "\<subset>\<^sub>p" 50)
where "pfun_subset \<equiv> less"
abbreviation pfun_subset_eq :: "('a, 'b) pfun \<Rightarrow> ('a, 'b) pfun \<Rightarrow> bool" (infix "\<subseteq>\<^sub>p" 50)
where "pfun_subset_eq \<equiv> less_eq"
instance pfun :: (type, type) semilattice_inf
by (intro_classes, (transfer, auto simp add: map_le_def dom_def)+)
lemma pfun_subset_eq_least [simp]:
"{}\<^sub>p \<subseteq>\<^sub>p f"
by (transfer, auto)
syntax
"_PfunUpd" :: "[('a, 'b) pfun, maplets] => ('a, 'b) pfun" ("_'(_')\<^sub>p" [900,0]900)
"_Pfun" :: "maplets => ('a, 'b) pfun" ("(1{_}\<^sub>p)")
"_pabs" :: "pttrn \<Rightarrow> logic \<Rightarrow> logic \<Rightarrow> logic \<Rightarrow> logic" ("\<lambda> _ \<in> _ | _ \<bullet> _" [0, 0, 0, 10] 10)
"_pabs_mem" :: "pttrn \<Rightarrow> logic \<Rightarrow> logic \<Rightarrow> logic \<Rightarrow> logic" ("\<lambda> _ \<in> _ \<bullet> _" [0, 0, 10] 10)
"_pabs_pred" :: "pttrn \<Rightarrow> logic \<Rightarrow> logic \<Rightarrow> logic \<Rightarrow> logic" ("\<lambda> _ | _ \<bullet> _" [0, 0, 10] 10)
"_pabs_tot" :: "pttrn \<Rightarrow> logic \<Rightarrow> logic" ("\<lambda> _ \<bullet> _" [0, 10] 10)
translations
"_PfunUpd m (_Maplets xy ms)" == "_PfunUpd (_PfunUpd m xy) ms"
"_PfunUpd m (_maplet x y)" == "CONST pfun_upd m x y"
"_Pfun ms" => "_PfunUpd (CONST pempty) ms"
"_Pfun (_Maplets ms1 ms2)" <= "_PfunUpd (_Pfun ms1) ms2"
"_Pfun ms" <= "_PfunUpd (CONST pempty) ms"
"_pabs x A P f" => "CONST pabs A (\<lambda> x. P) (\<lambda> x. f)"
"_pabs x A P f" <= "CONST pabs A (\<lambda> y. P) (\<lambda> x. f)"
"_pabs x A P (f x)" <= "CONST pabs A (\<lambda> x. P) f"
"_pabs_mem x A f" == "_pabs x A (CONST True) f"
"_pabs_pred x P f" == "_pabs x (CONST UNIV) P f"
"_pabs_tot x f" == "_pabs_pred x (CONST True) f"
"_pabs_tot x f" <= "_pabs_mem x (CONST UNIV) f"
subsection \<open> Algebraic laws \<close>
lemma pfun_comp_assoc: "f \<circ>\<^sub>p (g \<circ>\<^sub>p h) = (f \<circ>\<^sub>p g) \<circ>\<^sub>p h"
by (transfer, simp add: map_comp_assoc)
lemma pfun_comp_left_id [simp]: "pId \<circ>\<^sub>p f = f"
by (transfer, auto)
lemma pfun_comp_right_id [simp]: "f \<circ>\<^sub>p pId = f"
by (transfer, auto)
lemma pfun_comp_left_zero [simp]: "{}\<^sub>p \<circ>\<^sub>p f = {}\<^sub>p"
by (transfer, auto)
lemma pfun_comp_right_zero [simp]: "f \<circ>\<^sub>p {}\<^sub>p = {}\<^sub>p"
by (transfer, auto)
lemma pfun_override_dist_comp:
"(f \<oplus> g) \<circ>\<^sub>p h = (f \<circ>\<^sub>p h) \<oplus> (g \<circ>\<^sub>p h)"
apply (transfer)
apply (rule ext)
apply (auto simp add: map_add_def)
apply (rename_tac f g h x)
apply (case_tac "h x")
apply (auto)
apply (rename_tac f g h x y)
apply (case_tac "g y")
apply (auto)
done
lemma pfun_minus_unit [simp]:
fixes f :: "('a, 'b) pfun"
shows "f - \<bottom> = f"
by (transfer, simp add: map_minus_def)
lemma pfun_minus_zero [simp]:
fixes f :: "('a, 'b) pfun"
shows "\<bottom> - f = \<bottom>"
by (transfer, simp add: map_minus_def)
lemma pfun_minus_self [simp]:
fixes f :: "('a, 'b) pfun"
shows "f - f = \<bottom>"
by (transfer, simp add: map_minus_def)
instantiation pfun :: (type, type) override
begin
definition compatible_pfun :: "'a \<Zpfun> 'b \<Rightarrow> 'a \<Zpfun> 'b \<Rightarrow> bool" where
"compatible_pfun R S = ((pdom R) \<lhd>\<^sub>p S = (pdom S) \<lhd>\<^sub>p R)"
lemma pfun_compat_add: "(P :: 'a \<Zpfun> 'b) ## Q \<Longrightarrow> P \<oplus> Q ## R \<Longrightarrow> P ## R"
apply (simp add: compatible_pfun_def oplus_pfun_def)
apply (transfer)
using map_compat_add apply auto
done
lemma pfun_compat_addI: "\<lbrakk> (P :: 'a \<Zpfun> 'b) ## Q; P ## R; Q ## R \<rbrakk> \<Longrightarrow> P \<oplus> Q ## R"
apply (simp add: compatible_pfun_def oplus_pfun_def)
apply (transfer)
apply (auto simp add: restrict_map_def fun_eq_iff dom_def map_add_def option.case_eq_if)
apply (metis option.inject)+
done
instance proof
fix P Q R :: "'a \<Zpfun> 'b"
show "P ## Q \<Longrightarrow> P \<oplus> Q ## R \<Longrightarrow> P ## R"
by (simp add: pfun_compat_add)
show "P ## Q \<Longrightarrow> P ## R \<Longrightarrow> Q ## R \<Longrightarrow> P \<oplus> Q ## R"
by (simp add: pfun_compat_addI)
qed (simp_all add: compatible_pfun_def oplus_pfun_def,
(transfer, auto simp add: map_add_subsumed2 map_add_comm_weak')+)
end
lemma pfun_indep_compat: "pdom(f) \<inter> pdom(g) = {} \<Longrightarrow> f ## g"
unfolding compatible_pfun_def
by (transfer, auto simp add: restrict_map_def fun_eq_iff)
lemma pfun_override_commute:
"pdom(f) \<inter> pdom(g) = {} \<Longrightarrow> f \<oplus> g = g \<oplus> f"
by (transfer, metis map_add_comm)
lemma pfun_override_commute_weak:
"(\<forall> k \<in> pdom(f) \<inter> pdom(g). f(k)\<^sub>p = g(k)\<^sub>p) \<Longrightarrow> f \<oplus> g = g \<oplus> f"
by (transfer, simp, metis IntD1 IntD2 domD map_add_comm_weak option.sel)
lemma pfun_override_fully: "pdom f \<subseteq> pdom g \<Longrightarrow> f \<oplus> g = g"
by (transfer, auto simp add: map_add_def option.case_eq_if fun_eq_iff)
lemma pfun_override_res: "pdom g -\<lhd>\<^sub>p f \<oplus> g = f \<oplus> g"
by (transfer, auto simp add: map_add_restrict[THEN sym])
lemma pfun_minus_override_commute:
"pdom(g) \<inter> pdom(h) = {} \<Longrightarrow> (f - g) \<oplus> h = (f \<oplus> h) - g"
by (transfer, simp add: map_minus_plus_commute)
lemma pfun_override_minus:
"f \<subseteq>\<^sub>p g \<Longrightarrow> (g - f) \<oplus> f = g"
by (transfer, rule ext, auto simp add: map_le_def map_minus_def map_add_def option.case_eq_if)
lemma pfun_minus_common_subset:
"\<lbrakk> h \<subseteq>\<^sub>p f; h \<subseteq>\<^sub>p g \<rbrakk> \<Longrightarrow> (f - h = g - h) = (f = g)"
by (transfer, simp add: map_minus_common_subset)
lemma pfun_minus_override:
"pdom(f) \<inter> pdom(g) = {} \<Longrightarrow> (f \<oplus> g) - g = f"
by (transfer, simp add: map_add_def map_minus_def option.case_eq_if, rule ext, auto)
(metis Int_commute domIff insert_disjoint(1) insert_dom)
lemma pfun_override_pos: "x \<oplus> y = {}\<^sub>p \<Longrightarrow> x = {}\<^sub>p"
by (transfer, simp)
lemma pfun_le_override: "pdom x \<inter> pdom y = {} \<Longrightarrow> x \<le> x \<oplus> y"
by (transfer, auto simp add: map_le_iff_add)
subsection \<open> Membership, application, and update \<close>
lemma pfun_ext: "\<lbrakk> \<And> x y. (x, y) \<in>\<^sub>p f \<longleftrightarrow> (x, y) \<in>\<^sub>p g \<rbrakk> \<Longrightarrow> f = g"
by (transfer, simp add: map_ext)
lemma pfun_member_alt_def:
"(x, y) \<in>\<^sub>p f \<longleftrightarrow> (x \<in> pdom f \<and> f(x)\<^sub>p = y)"
by (transfer, auto simp add: map_member_alt_def map_apply_def)
lemma pfun_member_override:
"(x, y) \<in>\<^sub>p f \<oplus> g \<longleftrightarrow> ((x \<notin> pdom(g) \<and> (x, y) \<in>\<^sub>p f) \<or> (x, y) \<in>\<^sub>p g)"
by (transfer, simp add: map_member_plus)
lemma pfun_member_minus:
"(x, y) \<in>\<^sub>p f - g \<longleftrightarrow> (x, y) \<in>\<^sub>p f \<and> (\<not> (x, y) \<in>\<^sub>p g)"
by (transfer, simp add: map_member_minus)
lemma pfun_app_in_ran [simp]: "x \<in> pdom f \<Longrightarrow> f(x)\<^sub>p \<in> pran f"
by (transfer, auto)
lemma pfun_app_map [simp]: "(pfun_of_map f)(x)\<^sub>p = (if (x \<in> dom(f)) then the (f x) else undefined)"
by (transfer, simp)
lemma pfun_app_upd_1: "x = y \<Longrightarrow> (f(x \<mapsto> v)\<^sub>p)(y)\<^sub>p = v"
by (transfer, simp)
lemma pfun_app_upd_2: "x \<noteq> y \<Longrightarrow> (f(x \<mapsto> v)\<^sub>p)(y)\<^sub>p = f(y)\<^sub>p"
by (transfer, simp)
lemma pfun_app_upd [simp]: "(f(x \<mapsto> e)\<^sub>p)(y)\<^sub>p = (if (x = y) then e else f(y)\<^sub>p)"
by (metis pfun_app_upd_1 pfun_app_upd_2)
lemma pfun_graph_apply [simp]: "rel_apply (pfun_graph f) x = f(x)\<^sub>p"
by (transfer, auto simp add: rel_apply_def map_graph_def)
lemma pfun_upd_ext [simp]: "x \<in> pdom(f) \<Longrightarrow> f(x \<mapsto> f(x)\<^sub>p)\<^sub>p = f"
by (transfer, simp add: domIff)
lemma pfun_app_add [simp]: "x \<in> pdom(g) \<Longrightarrow> (f \<oplus> g)(x)\<^sub>p = g(x)\<^sub>p"
by (transfer, auto)
lemma pfun_upd_add [simp]: "f \<oplus> g(x \<mapsto> v)\<^sub>p = (f \<oplus> g)(x \<mapsto> v)\<^sub>p"
by (transfer, simp)
lemma pfun_upd_add_left [simp]: "x \<notin> pdom(g) \<Longrightarrow> f(x \<mapsto> v)\<^sub>p \<oplus> g = (f \<oplus> g)(x \<mapsto> v)\<^sub>p"
by (transfer, auto, metis domD map_add_upd_left)
lemma pfun_app_add' [simp]: "e \<notin> pdom g \<Longrightarrow> (f \<oplus> g)(e)\<^sub>p = f(e)\<^sub>p"
by (transfer, auto)
lemma pfun_upd_twice [simp]: "f(x \<mapsto> u, x \<mapsto> v)\<^sub>p = f(x \<mapsto> v)\<^sub>p"
by (transfer, simp)
lemma pfun_upd_comm:
assumes "x \<noteq> y"
shows "f(y \<mapsto> u, x \<mapsto> v)\<^sub>p = f(x \<mapsto> v, y \<mapsto> u)\<^sub>p"
using assms by (transfer, auto)
lemma pfun_upd_comm_linorder [simp]:
fixes x y :: "'a :: linorder"
assumes "x < y"
shows "f(y \<mapsto> u, x \<mapsto> v)\<^sub>p = f(x \<mapsto> v, y \<mapsto> u)\<^sub>p"
using assms by (transfer, auto)
lemma pfun_upd_as_ovrd: "f(k \<mapsto> v)\<^sub>p = f \<oplus> {k \<mapsto> v}\<^sub>p"
by (transfer, simp)
lemma pfun_ovrd_single_upd: "x \<in> pdom(g) \<Longrightarrow> f \<oplus> ({x} \<lhd>\<^sub>p g) = f(x \<mapsto> g(x)\<^sub>p)\<^sub>p"
by (transfer, auto simp add: map_add_def restrict_map_def fun_eq_iff)
lemma pfun_app_minus [simp]: "x \<notin> pdom g \<Longrightarrow> (f - g)(x)\<^sub>p = f(x)\<^sub>p"
by (transfer, auto simp add: map_minus_def)
lemma pfun_app_empty [simp]: "{}\<^sub>p(x)\<^sub>p = undefined"
by (transfer, simp)
lemma pfun_app_not_in_dom:
"x \<notin> pdom(f) \<Longrightarrow> f(x)\<^sub>p = undefined"
by (transfer, simp)
lemma pfun_upd_minus [simp]:
"x \<notin> pdom g \<Longrightarrow> (f - g)(x \<mapsto> v)\<^sub>p = (f(x \<mapsto> v)\<^sub>p - g)"
by (transfer, auto simp add: map_minus_def)
lemma pdom_member_minus_iff [simp]:
"x \<notin> pdom g \<Longrightarrow> x \<in> pdom(f - g) \<longleftrightarrow> x \<in> pdom(f)"
by (transfer, simp add: domIff map_minus_def)
lemma psubseteq_pfun_upd1 [intro]:
"\<lbrakk> f \<subseteq>\<^sub>p g; x \<notin> pdom(g) \<rbrakk> \<Longrightarrow> f \<subseteq>\<^sub>p g(x \<mapsto> v)\<^sub>p"
by (transfer, auto simp add: map_le_def dom_def)
lemma psubseteq_pfun_upd2 [intro]:
"\<lbrakk> f \<subseteq>\<^sub>p g; x \<notin> pdom(f) \<rbrakk> \<Longrightarrow> f \<subseteq>\<^sub>p g(x \<mapsto> v)\<^sub>p"
by (transfer, auto simp add: map_le_def dom_def)
lemma psubseteq_pfun_upd3 [intro]:
"\<lbrakk> f \<subseteq>\<^sub>p g; g(x)\<^sub>p = v \<rbrakk> \<Longrightarrow> f \<subseteq>\<^sub>p g(x \<mapsto> v)\<^sub>p"
by (transfer, auto simp add: map_le_def dom_def)
lemma psubseteq_dom_subset:
"f \<subseteq>\<^sub>p g \<Longrightarrow> pdom(f) \<subseteq> pdom(g)"
by (transfer, auto simp add: map_le_def dom_def)
lemma psubseteq_ran_subset:
"f \<subseteq>\<^sub>p g \<Longrightarrow> pran(f) \<subseteq> pran(g)"
by (transfer, auto simp add: map_le_def dom_def ran_def)
lemma pfun_eq_iff: "f = g \<longleftrightarrow> (pdom(f) = pdom(g) \<and> (\<forall> x \<in> pdom(f). f(x)\<^sub>p = g(x)\<^sub>p))"
by (auto, transfer, simp add: map_eq_iff, metis domD option.sel)
lemma pfun_leI: "\<lbrakk> pdom f \<subseteq> pdom g; \<forall>x\<in>pdom f. f(x)\<^sub>p = g(x)\<^sub>p \<rbrakk> \<Longrightarrow> f \<subseteq>\<^sub>p g"
by (transfer, auto simp add: map_le_def)
(metis domD domI option.sel subsetD)
lemma pfun_le_iff: "(f \<subseteq>\<^sub>p g) = (pdom f \<subseteq> pdom g \<and> (\<forall>x\<in>pdom f. f(x)\<^sub>p = g(x)\<^sub>p))"
by (metis pfun_app_add pfun_leI pfun_override_minus psubseteq_dom_subset)
subsection \<open> Map laws \<close>
lemma map_pfun_empty [simp]: "map_pfun f {}\<^sub>p = {}\<^sub>p"
by (transfer, simp)
lemma map_pfun'_empty [simp]: "map_pfun' f g {}\<^sub>p = {}\<^sub>p"
unfolding map_pfun'_def by (transfer, simp add: comp_def)
lemma map_pfun_upd [simp]: "map_pfun f (g(x \<mapsto> v)\<^sub>p) = (map_pfun f g)(x \<mapsto> f v)\<^sub>p"
by (simp add: map_pfun_def pfun_upd.rep_eq pfun_upd.abs_eq)
lemma map_pfun_apply [simp]: "x \<in> pdom G \<Longrightarrow> (map_pfun F G)(x)\<^sub>p = F(G(x)\<^sub>p)"
unfolding map_pfun_def by (auto simp add: pfun_app.rep_eq domD pdom.rep_eq)
lemma map_pfun_as_pabs: "map_pfun f g = (\<lambda> x \<in> pdom(g) \<bullet> f(g(x)\<^sub>p))"
by (simp add: pabs_def, transfer, auto simp add: fun_eq_iff restrict_map_def)
lemma map_pfun_ovrd [simp]: "map_pfun f (g \<oplus> h) = (map_pfun f g) \<oplus> (map_pfun f h)"
by (simp add: map_pfun_def, transfer, auto simp add: map_add_def fun_eq_iff)
(metis bind.bind_lunit comp_apply map_conv_bind_option option.case_eq_if)
lemma map_pfun_dres [simp]: "map_pfun f (A \<lhd>\<^sub>p g) = A \<lhd>\<^sub>p map_pfun f g"
by (simp add: map_pfun_def, transfer, auto simp add: restrict_map_def)
subsection \<open> Domain laws \<close>
lemma pdom_zero [simp]: "pdom \<bottom> = {}"
by (transfer, simp)
lemma pdom_pId_on [simp]: "pdom (pId_on A) = A"
by (transfer, auto)
lemma pdom_plus [simp]: "pdom (f \<oplus> g) = pdom f \<union> pdom g"
by (transfer, auto)
lemma pdom_minus [simp]: "g \<le> f \<Longrightarrow> pdom (f - g) = pdom f - pdom g"
apply (transfer, auto simp add: map_minus_def)
apply (meson option.distinct(1))
apply (metis domIff map_le_def option.simps(3))
done
lemma pdom_inter: "pdom (f \<inter>\<^sub>p g) \<subseteq> pdom f \<inter> pdom g"
by (transfer, auto simp add: dom_def)
lemma pdom_comp [simp]: "pdom (g \<circ>\<^sub>p f) = pdom (f \<rhd>\<^sub>p pdom g)"
by (transfer, auto simp add: ran_restrict_map_def)
lemma pdom_upd [simp]: "pdom (f(k \<mapsto> v)\<^sub>p) = insert k (pdom f)"
by (transfer, simp)
lemma pdom_pdom_res [simp]: "pdom (A \<lhd>\<^sub>p f) = A \<inter> pdom(f)"
by (transfer, auto)
lemma pdom_graph_pfun: "pdom (graph_pfun R) \<subseteq> Domain R"
by (transfer, simp add: graph_map_dom fst_eq_Domain Domain_mk_functional)
lemma pdom_functional_graph_pfun [simp]:
"functional R \<Longrightarrow> pdom (graph_pfun R) = Domain R"
by (transfer, simp add: dom_map_graph mk_functional_fp)
lemma pdom_pran_res_finite [simp]:
"finite (pdom f) \<Longrightarrow> finite (pdom (f \<rhd>\<^sub>p A))"
by (transfer, auto)
lemma pdom_pfun_graph_finite [simp]:
"finite (pdom f) \<Longrightarrow> finite (pfun_graph f)"
by (transfer, simp add: finite_dom_graph)
lemma pdom_map_pfun [simp]: "pdom (map_pfun F G) = pdom G"
unfolding map_pfun_def by (auto; metis dom_map_option_comp pdom.abs_eq pdom.rep_eq)
lemma rel_comp_pfun: "R O pfun_graph f = (\<lambda> p. (fst p, pfun_app f (snd p))) ` (R \<rhd>\<^sub>r pdom(f))"
by (transfer, auto simp add: rel_comp_map rel_ranres_def)
lemma pdom_empty_iff_dom_empty: "f = {}\<^sub>p \<longleftrightarrow> pdom f = {}"
by (transfer, simp)
lemma empty_map_pfunD [dest!]: "{}\<^sub>p = map_pfun f F \<Longrightarrow> F = {}\<^sub>p"
by (metis pdom_empty_iff_dom_empty pdom_map_pfun)
subsection \<open> Range laws \<close>
lemma pran_zero [simp]: "pran \<bottom> = {}"
by (transfer, simp)
lemma pran_pId_on [simp]: "pran (pId_on A) = A"
by (transfer, auto simp add: ran_def)
lemma pran_upd [simp]: "pran (f(k \<mapsto> v)\<^sub>p) = insert v (pran ((- {k}) \<lhd>\<^sub>p f))"
by (transfer, auto simp add: ran_def restrict_map_def)
lemma pran_pran_res [simp]: "pran (f \<rhd>\<^sub>p A) = pran(f) \<inter> A"
by (transfer, auto simp add: ran_restrict_map_def)
lemma pran_comp [simp]: "pran (g \<circ>\<^sub>p f) = pran (pran f \<lhd>\<^sub>p g)"
by (transfer, auto simp add: ran_def restrict_map_def)
lemma pran_finite [simp]: "finite (pdom f) \<Longrightarrow> finite (pran f)"
by (simp add: pdom.rep_eq pran_rep_eq)
lemma pran_pdom: "pran F = pfun_app F ` pdom F"
by (transfer, force simp add: dom_def)
lemma pran_override [simp]: "pran (f \<oplus> g) = pran(g) \<union> pran(pdom(g) -\<lhd>\<^sub>p f)"
by (transfer, auto simp add: restrict_map_def dom_def map_add_def option.case_eq_if)
subsection \<open> Graph laws \<close>
lemma pfun_graph_inv [code_unfold]: "graph_pfun (pfun_graph f) = f"
by (transfer, simp add: mk_functional_fp)
lemma pfun_eq_graph: "f = g \<longleftrightarrow> pfun_graph f = pfun_graph g"
by (metis pfun_graph_inv)
lemma Dom_pfun_graph: "Domain (pfun_graph f) = pdom f"
by (transfer, simp add: dom_map_graph)
lemma Range_pfun_graph: "Range (pfun_graph f) = pran f"
by (transfer, auto simp add: ran_map_graph[THEN sym] ran_def)
lemma card_pfun_graph: "finite (pdom f) \<Longrightarrow> card (pfun_graph f) = card (pdom f)"
by (transfer, simp add: card_map_graph dom_map_graph finite_dom_graph)
lemma functional_pfun_graph [simp]: "functional (pfun_graph f)"
by (transfer, simp)
lemma pfun_graph_zero: "pfun_graph \<bottom> = {}"
by (transfer, simp add: map_graph_def)
lemma pfun_graph_pId_on: "pfun_graph (pId_on A) = Id_on A"
by (transfer, auto simp add: map_graph_def)
lemma pfun_graph_minus: "pfun_graph (f - g) = pfun_graph f - pfun_graph g"
by (transfer, simp add: map_graph_minus)
lemma pfun_graph_inter: "pfun_graph (f \<inter>\<^sub>p g) = pfun_graph f \<inter> pfun_graph g"
apply (transfer, auto simp add: map_graph_def)
apply (metis option.discI)+
done
lemma pfun_graph_domres: "pfun_graph (A \<lhd>\<^sub>p f) = (A \<lhd>\<^sub>r pfun_graph f)"
by (transfer, simp add: rel_domres_math_def map_graph_def restrict_map_def, metis option.simps(3))
lemma pfun_graph_override: "pfun_graph (f \<oplus> g) = pfun_graph f \<oplus> pfun_graph g"
by (transfer, auto simp add: map_add_def oplus_set_def rel_domres_def map_graph_def option.case_eq_if)
(metis option.collapse)+
lemma pfun_graph_update: "pfun_graph (f(k \<mapsto> v)\<^sub>p) = insert (k, v) ((- {k}) \<lhd>\<^sub>r pfun_graph f)"
by (transfer, simp add: map_graph_update)
lemma pfun_graph_comp: "pfun_graph (f \<circ>\<^sub>p g) = pfun_graph g O pfun_graph f"
by (transfer, simp add: map_graph_comp)
lemma comp_pfun_graph: "pfun_graph f O pfun_graph g = pfun_graph (g \<circ>\<^sub>p f)"
by (simp add: pfun_graph_comp)
lemma pfun_graph_pfun_inv: "pfun_inj f \<Longrightarrow> pfun_graph (pfun_inv f) = (pfun_graph f)\<inverse>"
by (transfer, simp add: map_graph_map_inv)
lemma pfun_graph_pabs: "pfun_graph (\<lambda> x \<in> A | P x \<bullet> f x) = {(k, v). k \<in> A \<and> P k \<and> v = f k}"
unfolding pabs_def by (transfer, auto simp add: map_graph_def restrict_map_def)
lemma pfun_graph_le_iff:
"pfun_graph f \<subseteq> pfun_graph g \<longleftrightarrow> f \<subseteq>\<^sub>p g"
by (simp add: inf.order_iff pfun_eq_graph pfun_graph_inter)
lemma pfun_member_iff [simp]: "(k, v) \<in> pfun_graph f \<longleftrightarrow> (k \<in> pdom(f) \<and> pfun_app f k = v)"
by (transfer, auto simp add: map_graph_def)
lemma pfun_graph_rres: "pfun_graph (f \<rhd>\<^sub>p A) = pfun_graph f \<rhd>\<^sub>r A"
by (transfer, auto simp add: map_graph_def rel_ranres_def ran_restrict_map_def)
subsection \<open> Graph Transfer Setup \<close>
definition cr_pfung :: "('a \<leftrightarrow> 'b) \<Rightarrow> 'a \<Zpfun> 'b \<Rightarrow> bool" where
"cr_pfung f g = (f = pfun_graph g)"
lemma Domainp_cr_pfung [transfer_domain_rule]: "Domainp cr_pfung = functional"
unfolding cr_pfung_def Domainp_iff[abs_def]
by (auto simp add: fun_eq_iff, metis graph_map_inv pfun_graph.abs_eq)
bundle pfun_graph_lifting
begin
unbundle lifting_syntax
lemma bi_unique_cr_pfung [transfer_rule]: "bi_unique cr_pfung"
unfolding cr_pfung_def bi_unique_def by (auto simp add: pfun_eq_graph)
lemma right_total_cr_pfung [transfer_rule]: "right_total cr_pfung"
unfolding cr_pfung_def right_total_def by simp
lemma cr_pfung_empty [transfer_rule]: "cr_pfung {} {}\<^sub>p"
unfolding cr_pfung_def by (simp add: pfun_graph_zero)
lemma cr_pfung_dom [transfer_rule]: "(cr_pfung ===> (=)) Domain pdom"
unfolding rel_fun_def cr_pfung_def by (simp add: Dom_pfun_graph)
lemma cr_pfung_ran [transfer_rule]: "(cr_pfung ===> (=)) Range pran"
unfolding rel_fun_def cr_pfung_def by (simp add: Range_pfun_graph)
lemma cr_pfung_id [transfer_rule]: "((=) ===> cr_pfung) Id_on pId_on"
unfolding rel_fun_def cr_pfung_def by (simp add: pfun_graph_pId_on)
lemma cr_pfung_ovrd [transfer_rule]: "(cr_pfung ===> cr_pfung ===> cr_pfung) (\<oplus>) (\<oplus>)"
unfolding rel_fun_def cr_pfung_def by (simp add: pfun_graph_override)
lemma cr_pfung_ovrd [transfer_rule]: "(cr_pfung ===> cr_pfung ===> cr_pfung) (O) (\<lambda> x y. y \<circ>\<^sub>p x)"
unfolding rel_fun_def cr_pfung_def by (simp add: pfun_graph_comp)
lemma cr_pfung_dres [transfer_rule]: "((=) ===> cr_pfung ===> cr_pfung) (\<lhd>\<^sub>r) (\<lhd>\<^sub>p)"
unfolding rel_fun_def cr_pfung_def by (simp add: pfun_graph_domres)
lemma cr_pfung_rres [transfer_rule]: "(cr_pfung ===> (=) ===> cr_pfung) (\<rhd>\<^sub>r) (\<rhd>\<^sub>p)"
unfolding rel_fun_def cr_pfung_def by (simp add: pfun_graph_rres)
lemma cr_pfung_le [transfer_rule]: "(cr_pfung ===> cr_pfung ===> (=)) (\<le>) (\<le>)"
unfolding rel_fun_def cr_pfung_def by (simp add: pfun_graph_le_iff)
lemma cr_pfung_update [transfer_rule]: "(cr_pfung ===> (=) ===> (=) ===> cr_pfung) (\<lambda> f k v. insert (k, v) ((- {k}) \<lhd>\<^sub>r f)) pfun_upd"
unfolding rel_fun_def cr_pfung_def by (simp add: pfun_graph_update)
end
subsection \<open> Partial Injections \<close>
lemma pfun_inj_empty [simp]: "pfun_inj {}\<^sub>p"
by (transfer, simp)
lemma pinj_pId_on [simp]: "pfun_inj (pId_on A)"
by (transfer, auto simp add: inj_on_def)
lemma pfun_inj_inv_inv: "pfun_inj f \<Longrightarrow> pfun_inv (pfun_inv f) = f"
by (transfer, simp)
lemma pfun_inj_inv: "pfun_inj f \<Longrightarrow> pfun_inj (pfun_inv f)"
by (transfer, simp add: inj_map_inv)
lemma f_pfun_inv_f_apply: "\<lbrakk> pfun_inj f; x \<in> pran f \<rbrakk> \<Longrightarrow> f(pfun_inv f(x)\<^sub>p)\<^sub>p = x"
by (transfer, auto simp add: ranI)
lemma pfun_inv_f_f_apply: "\<lbrakk> pfun_inj f; x \<in> pdom f \<rbrakk> \<Longrightarrow> pfun_inv f(f(x)\<^sub>p)\<^sub>p = x"
by (transfer, auto simp add: ranI)
lemma pfun_inj_upd: "\<lbrakk> pfun_inj f; v \<notin> pran f \<rbrakk> \<Longrightarrow> pfun_inj (f(k \<mapsto> v)\<^sub>p)"
by (transfer, auto, meson f_the_inv_into_f inj_on_fun_updI)
lemma pfun_inj_dres: "pfun_inj f \<Longrightarrow> pfun_inj (A \<lhd>\<^sub>p f)"
by (transfer, auto simp add: inj_on_def)
lemma pfun_inj_rres: "pfun_inj f \<Longrightarrow> pfun_inj (f \<rhd>\<^sub>p A)"
by (transfer, auto simp add: inj_on_def ran_restrict_map_def, metis domI option.simps(3))
lemma pfun_inj_comp: "\<lbrakk> pfun_inj f; pfun_inj g \<rbrakk> \<Longrightarrow> pfun_inj (f \<circ>\<^sub>p g)"
by (transfer, auto simp add: inj_on_def map_comp_def option.case_eq_if dom_def)
lemma pfun_inj_ovrd: "\<lbrakk> pfun_inj f; pfun_inj g; pran f \<inter> pran g = {} \<rbrakk> \<Longrightarrow> pfun_inj (f \<oplus> g)"
by (transfer, force simp add: inj_on_def map_add_def option.case_eq_if dom_def)
lemma pfun_inv_dres: "pfun_inj f \<Longrightarrow> pfun_inv (A \<lhd>\<^sub>p f) = (pfun_inv f) \<rhd>\<^sub>p A"
by (transfer, simp add: map_inv_dom_res)
lemma pfun_inv_rres: "pfun_inj f \<Longrightarrow> pfun_inv (f \<rhd>\<^sub>p A) = A \<lhd>\<^sub>p (pfun_inv f)"
by (transfer, simp add: map_inv_ran_res)
lemma pfun_inv_empty [simp]: "pfun_inv {}\<^sub>p = {}\<^sub>p"
by (transfer, simp)
lemma pdom_pfun_inv [simp]: "pdom (pfun_inv f) = pran f"
by (simp add: pran_rep_eq, transfer, simp)
lemma pfun_inv_add:
assumes "pfun_inj f" "pfun_inj g" "pran f \<inter> pran g = {}"
shows "pfun_inv (f \<oplus> g) = (pfun_inv f \<rhd>\<^sub>p (- pdom g)) \<oplus> pfun_inv g"
using assms by (simp add: pran_rep_eq, transfer, auto, meson map_inv_add)
lemma pfun_inv_upd:
assumes "pfun_inj f" "v \<notin> pran f"
shows "pfun_inv (f(k \<mapsto> v)\<^sub>p) = (pfun_inv ((- {k}) \<lhd>\<^sub>p f))(v \<mapsto> k)\<^sub>p"
using assms by (simp add: pran_rep_eq, transfer, meson map_inv_upd)
subsection \<open> Domain restriction laws \<close>
lemma pdom_res_zero [simp]: "A \<lhd>\<^sub>p {}\<^sub>p = {}\<^sub>p"
by (transfer, auto)
lemma pdom_res_empty [simp]:
"({} \<lhd>\<^sub>p f) = {}\<^sub>p"
by (transfer, auto)
lemma pdom_res_pdom [simp]:
"pdom(f) \<lhd>\<^sub>p f = f"
by (transfer, auto)
lemma pdom_res_UNIV [simp]: "UNIV \<lhd>\<^sub>p f = f"
by (transfer, auto)
lemma pdom_res_alt_def: "A \<lhd>\<^sub>p f = f \<circ>\<^sub>p pId_on A"
by (transfer, rule ext, auto simp add: restrict_map_def)
lemma pdom_res_upd_in [simp]:
"k \<in> A \<Longrightarrow> A \<lhd>\<^sub>p f(k \<mapsto> v)\<^sub>p = (A \<lhd>\<^sub>p f)(k \<mapsto> v)\<^sub>p"
by (transfer, auto)
lemma pdom_res_upd_out [simp]:
"k \<notin> A \<Longrightarrow> A \<lhd>\<^sub>p f(k \<mapsto> v)\<^sub>p = A \<lhd>\<^sub>p f"
by (transfer, auto)
lemma pfun_pdom_antires_upd [simp]:
"k \<in> A \<Longrightarrow> ((- A) \<lhd>\<^sub>p m)(k \<mapsto> v)\<^sub>p = ((- (A - {k})) \<lhd>\<^sub>p m)(k \<mapsto> v)\<^sub>p"
by (transfer, simp)
lemma pdom_antires_insert_notin [simp]:
"k \<notin> pdom(f) \<Longrightarrow> (- insert k A) \<lhd>\<^sub>p f = (- A) \<lhd>\<^sub>p f"
by (transfer, auto simp add: restrict_map_def)
lemma pdom_res_override [simp]: "A \<lhd>\<^sub>p (f \<oplus> g) = (A \<lhd>\<^sub>p f) \<oplus> (A \<lhd>\<^sub>p g)"
by (simp add: pdom_res_alt_def pfun_override_dist_comp)
lemma pdom_res_minus [simp]: "A \<lhd>\<^sub>p (f - g) = (A \<lhd>\<^sub>p f) - g"
by (transfer, auto simp add: map_minus_def restrict_map_def)
lemma pdom_res_swap: "(A \<lhd>\<^sub>p f) \<rhd>\<^sub>p B = A \<lhd>\<^sub>p (f \<rhd>\<^sub>p B)"
by (transfer, auto simp add: restrict_map_def ran_restrict_map_def)
lemma pdom_res_twice [simp]: "A \<lhd>\<^sub>p (B \<lhd>\<^sub>p f) = (A \<inter> B) \<lhd>\<^sub>p f"
by (transfer, auto simp add: Int_commute)
lemma pdom_res_comp [simp]: "A \<lhd>\<^sub>p (g \<circ>\<^sub>p f) = g \<circ>\<^sub>p (A \<lhd>\<^sub>p f)"
by (simp add: pdom_res_alt_def pfun_comp_assoc)
lemma pdom_res_apply [simp]:
"x \<in> A \<Longrightarrow> (A \<lhd>\<^sub>p f)(x)\<^sub>p = f(x)\<^sub>p"
by (transfer, auto)
lemma pdom_res_frame_update [simp]:
"\<lbrakk> x \<in> pdom(f); (-{x}) \<lhd>\<^sub>p f = (-{x}) \<lhd>\<^sub>p g \<rbrakk> \<Longrightarrow> g(x \<mapsto> f(x)\<^sub>p)\<^sub>p = f"
by (transfer, auto, metis fun_upd_triv fun_upd_upd restrict_complement_singleton_eq)
lemma pdres_rres_commute: "A \<lhd>\<^sub>p (P \<rhd>\<^sub>p B) = (A \<lhd>\<^sub>p P) \<rhd>\<^sub>p B"
by (transfer, simp add: map_dres_rres_commute)
lemma pdom_nres_disjoint: "pdom(f) \<inter> A = {} \<Longrightarrow> (- A) \<lhd>\<^sub>p f = f"
by (metis disjoint_eq_subset_Compl inf.absorb2 pdom_res_pdom pdom_res_twice)
lemma pranres_pdom [simp]: "pdom (f \<rhd>\<^sub>p A) \<lhd>\<^sub>p f = f \<rhd>\<^sub>p A"
by (transfer, auto simp add: restrict_map_def fun_eq_iff ran_restrict_map_def option.case_eq_if)
(metis not_None_eq)
lemma pdom_pranres [simp]: "pdom (f \<rhd>\<^sub>p A) \<subseteq> pdom f"
by (metis inf.absorb_iff1 inf.commute pdom_pdom_res pdom_res_pdom pdom_res_swap)
lemma pfun_split_domain: "A \<lhd>\<^sub>p f \<oplus> (- A) \<lhd>\<^sub>p f = f"
by (transfer, auto simp add: restrict_map_def map_add_def fun_eq_iff option.case_eq_if)
subsection \<open> Range restriction laws \<close>
lemma pran_res_UNIV [simp]: "f \<rhd>\<^sub>p UNIV = f"
by (transfer, simp add: ran_restrict_map_def)
lemma pran_res_empty [simp]: "f \<rhd>\<^sub>p {} = {}\<^sub>p"
by (transfer, auto simp add: ran_restrict_map_def)
lemma pran_res_zero [simp]: "{}\<^sub>p \<rhd>\<^sub>p A = {}\<^sub>p"
by (transfer, auto simp add: ran_restrict_map_def)
lemma pran_res_upd_1 [simp]: "v \<in> A \<Longrightarrow> f(x \<mapsto> v)\<^sub>p \<rhd>\<^sub>p A = (f \<rhd>\<^sub>p A)(x \<mapsto> v)\<^sub>p"
by (transfer, auto simp add: ran_restrict_map_def)
lemma pran_res_upd_2 [simp]: "v \<notin> A \<Longrightarrow> f(x \<mapsto> v)\<^sub>p \<rhd>\<^sub>p A = ((- {x}) \<lhd>\<^sub>p f) \<rhd>\<^sub>p A"
by (transfer, auto simp add: ran_restrict_map_def)
lemma pran_res_twice [simp]: "f \<rhd>\<^sub>p A \<rhd>\<^sub>p B = f \<rhd>\<^sub>p (A \<inter> B)"
by (transfer, simp)
lemma pran_res_alt_def: "f \<rhd>\<^sub>p A = pId_on A \<circ>\<^sub>p f"
by (transfer, rule ext, auto simp add: ran_restrict_map_def)
lemma pran_res_override: "(f \<oplus> g) \<rhd>\<^sub>p A \<subseteq>\<^sub>p (f \<rhd>\<^sub>p A) \<oplus> (g \<rhd>\<^sub>p A)"
apply (transfer, auto simp add: map_add_def ran_restrict_map_def map_le_def)
apply (rename_tac f g A a y x)
apply (case_tac "g a")
apply (auto)
done
lemma pcomp_ranres [simp]: "(f \<circ>\<^sub>p g) \<rhd>\<^sub>p A = (f \<rhd>\<^sub>p A) \<circ>\<^sub>p g"
by (simp add: pfun_comp_assoc pran_res_alt_def)
lemma pranres_le: "A \<subseteq> B \<Longrightarrow> f \<rhd>\<^sub>p A \<le> f \<rhd>\<^sub>p B"
by (simp add: pfun_graph_le_iff[THEN sym] pfun_graph_comp pfun_graph_rres relcomp_mono rel_ranres_le)
lemma pranres_neg_ran [simp]: "P \<rhd>\<^sub>p- pran P = {}\<^sub>p"
by (transfer, auto simp add: ran_restrict_map_def fun_eq_iff option.case_eq_if bind_eq_None_conv, meson option.exhaust_sel)
subsection \<open> Preimage Laws \<close>
lemma ppreimageI [intro!]: "\<lbrakk> x \<in> pdom(f); f(x)\<^sub>p \<in> A \<rbrakk> \<Longrightarrow> x \<in> pdom (f \<rhd>\<^sub>p A)"
by (metis (full_types) insertI1 pdom_upd pfun_upd_ext pran_res_upd_1)
lemma ppreimageD: "x \<in> pdom (f \<rhd>\<^sub>p A) \<Longrightarrow> \<exists> y \<in> A. f(x)\<^sub>p = y"
by (transfer, auto simp add: ran_restrict_map_def)
lemma ppreimageE [elim!]: "\<lbrakk> x \<in> pdom (f \<rhd>\<^sub>p A); \<And> y. \<lbrakk> x \<in> pdom(f); y \<in> A; f(x)\<^sub>p = y \<rbrakk> \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
by (metis (no_types) pdom_pranres ppreimageD subsetD)
lemma mem_pimage_iff: "x \<in> pran (A \<lhd>\<^sub>p f) \<longleftrightarrow> (\<exists> y \<in> A \<inter> pdom(f). f(y)\<^sub>p = x)"
by (auto simp add: pran_pdom)
lemma ppreimage_inter [simp]: "pdom (f \<rhd>\<^sub>p (A \<inter> B)) = pdom (f \<rhd>\<^sub>p A) \<inter> pdom (f \<rhd>\<^sub>p B)"
by fastforce
subsection \<open> Composition \<close>
lemma pcomp_apply [simp]: "\<lbrakk> x \<in> pdom(g) \<rbrakk> \<Longrightarrow> (f \<circ>\<^sub>p g)(x)\<^sub>p = f(g(x)\<^sub>p)\<^sub>p"
by (transfer, auto)
lemma pcomp_mono: "\<lbrakk> f \<le> f'; g \<le> g' \<rbrakk> \<Longrightarrow> f \<circ>\<^sub>p g \<le> f' \<circ>\<^sub>p g'"
by (simp add: pfun_graph_le_iff[THEN sym] pfun_graph_comp relcomp_mono)
lemma pdom_UNIV_comp: "pdom f = UNIV \<Longrightarrow> pdom (f \<circ>\<^sub>p g) = pdom g"
by simp
subsection \<open> Entries \<close>
lemma pfun_entries_empty [simp]: "pfun_entries {} f = {}\<^sub>p"
by (transfer, simp)
lemma pdom_pfun_entries [simp]: "pdom (pfun_entries A f) = A"
by (transfer, auto)
lemma pran_pfun_entries [simp]: "pran (pfun_entries A f) = f ` A"
by (transfer, simp add: ran_def, auto)
lemma pfun_entries_apply_1 [simp]:
"x \<in> d \<Longrightarrow> (pfun_entries d f)(x)\<^sub>p = f x"
by (transfer, auto)
lemma pfun_entries_apply_2 [simp]:
"x \<notin> d \<Longrightarrow> (pfun_entries d f)(x)\<^sub>p = undefined"
by (transfer, auto)
lemma pdom_res_entries: "A \<lhd>\<^sub>p pfun_entries B f = pfun_entries (A \<inter> B) f"
by (transfer, auto simp add: fun_eq_iff restrict_map_def)
lemma pfuse_empty [simp]: "pfuse {}\<^sub>p g = {}\<^sub>p"
by (simp add: pfuse_def)
lemma pfuse_app [simp]:
"\<lbrakk> e \<in> pdom F; e \<in> pdom G \<rbrakk> \<Longrightarrow> (pfuse F G)(e)\<^sub>p = (F(e)\<^sub>p, G(e)\<^sub>p)"
by (metis (no_types, lifting) IntI pfun_entries_apply_1 pfuse_def)
lemma pdom_pfuse [simp]: "pdom (pfuse f g) = pdom(f) \<inter> pdom(g)"
by (auto simp add: pfuse_def)
lemma pfuse_upd:
"pfuse (f(k \<mapsto> v)\<^sub>p) g =
(if k \<in> pdom g then (pfuse ((-{k}) \<lhd>\<^sub>p f) g)(k \<mapsto> (v, pfun_app g k))\<^sub>p else pfuse f g)"
by (simp add: pfuse_def, transfer, auto simp add: fun_eq_iff)
subsection \<open> Lambda abstraction \<close>
lemma pabs_cong:
assumes "A = B" "\<And> x. x \<in> A \<Longrightarrow> P(x) = Q(x)" "\<And> x. \<lbrakk> x \<in> A; P x \<rbrakk> \<Longrightarrow> F(x) = G(x)"
shows "(\<lambda> x \<in> A | P x \<bullet> F(x)) = (\<lambda> x \<in> B | Q x \<bullet> G(x))"
using assms unfolding pabs_def
by (transfer, auto simp add: restrict_map_def fun_eq_iff)
lemma pabs_apply [simp]: "\<lbrakk> y \<in> A; P y \<rbrakk> \<Longrightarrow> (\<lambda> x \<in> A | P x \<bullet> f x) (y)\<^sub>p = f y"
by (simp add: pabs_def)
lemma pdom_pabs [simp]: "pdom (\<lambda> x \<in> A | P x \<bullet> f x) = A \<inter> Collect P"
by (simp add: pabs_def)
lemma pran_pabs [simp]: "pran (\<lambda> x \<in> A | P x \<bullet> f x) = {f x | x. x \<in> A \<and> P x}"
unfolding pabs_def
by (transfer, auto simp add: ran_def restrict_map_def)
lemma pabs_eta [simp]: "(\<lambda> x \<in> pdom(f) \<bullet> f(x)\<^sub>p) = f"
by (simp add: pabs_def, transfer, auto simp add: fun_eq_iff domIff restrict_map_def)
lemma pabs_id [simp]: "(\<lambda> x \<in> A | P x \<bullet> x) = pId_on {x\<in>A. P x}"
unfolding pabs_def by (transfer, simp add: restrict_map_def)
lemma pfun_entries_pabs: "pfun_entries A f = (\<lambda> x \<in> A \<bullet> f x)"
by (simp add: pabs_def, transfer, auto)
lemma pabs_empty [simp]: "(\<lambda> x\<in>{} \<bullet> f(x)) = {}\<^sub>p"
by (simp add: pabs_def)
lemma pabs_insert_maplet: "(\<lambda> x\<in>insert y A \<bullet> f(x)) = (\<lambda> x\<in>A \<bullet> f(x)) \<oplus> {y \<mapsto> f(y)}\<^sub>p"
by (simp add: pabs_def, transfer, auto simp add: restrict_map_insert)
text \<open> This rule can perhaps be simplified \<close>
lemma pcomp_pabs:
"(\<lambda> x \<in> A | P x \<bullet> f x) \<circ>\<^sub>p (\<lambda> x \<in> B | Q x \<bullet> g x)
= (\<lambda> x \<in> pdom (pabs B Q g \<rhd>\<^sub>p (A \<inter> Collect P)) \<bullet> (f (g x)))"
apply (subst pabs_eta[THEN sym, of "(\<lambda> x \<in> A | P x \<bullet> f x) \<circ>\<^sub>p (\<lambda> x \<in> B | Q x \<bullet> g x)"])
apply (simp)
apply (simp add: pabs_def)
apply (transfer, auto simp add: restrict_map_def map_comp_def ran_restrict_map_def fun_eq_iff)
done
lemma pabs_rres [simp]: "pabs A P f \<rhd>\<^sub>p B = pabs A (\<lambda> x. P x \<and> f x \<in> B) f"
by (simp add: pabs_def, transfer, auto simp add: ran_restrict_map_def restrict_map_def)
(* This law should be generalised *)
lemma pabs_simple_comp [simp]: "(\<lambda> x \<bullet> f x) \<circ>\<^sub>p g(k \<mapsto> v)\<^sub>p = ((\<lambda> x \<bullet> f x) \<circ>\<^sub>p g)(k \<mapsto> f v)\<^sub>p"
by (simp add: pabs_def, transfer, auto)
lemma pabs_comp: "(\<lambda> x \<in> A \<bullet> f x) \<circ>\<^sub>p g = (\<lambda> x \<in> pdom (g \<rhd>\<^sub>p A) \<bullet> f (pfun_app g x))"
by (metis pabs_eta pcomp_pabs pdom_pId_on pdom_pabs)
lemma map_pfun_pabs [simp]: "map_pfun f (\<lambda> x\<in>A | B(x) \<bullet> g(x)) = (\<lambda> x\<in>A | B(x) \<bullet> f(g(x)))"
by (simp add: pfun_eq_iff)
subsection \<open> Singleton Partial Functions \<close>
definition pfun_singleton :: "('a \<Zpfun> 'b) \<Rightarrow> bool" where
"pfun_singleton f = (\<exists> k v. f = {k \<mapsto> v}\<^sub>p)"
lemma pfun_singleton_dom: "pfun_singleton f \<longleftrightarrow> (\<exists> k. pdom(f) = {k})"
by (auto simp add: pfun_singleton_def)
(metis insertI1 override_lzero pdom_res_pdom pfun_ovrd_single_upd)
lemma pfun_singleton_maplet [simp]:
"pfun_singleton {k \<mapsto> v}\<^sub>p"
by (auto simp add: pfun_singleton_def)
definition dest_pfsingle :: "('a \<Zpfun> 'b) \<Rightarrow> 'a \<times> 'b" where
"dest_pfsingle f = (THE (k, v). f = {k \<mapsto> v}\<^sub>p)"
lemma dest_pfsingle_maplet [simp]: "dest_pfsingle {k \<mapsto> v}\<^sub>p = (k, v)"
apply (auto intro!:the_equality simp add: dest_pfsingle_def)
apply (metis pdom_upd pdom_zero singleton_insert_inj_eq)
apply (metis pdom_upd pdom_zero pfun_app_upd_1 singleton_insert_inj_eq)
done
subsection \<open> Summation \<close>
definition pfun_sum :: "('k, 'v::comm_monoid_add) pfun \<Rightarrow> 'v" where
"pfun_sum f = sum (pfun_app f) (pdom f)"
lemma pfun_sum_empty [simp]: "pfun_sum {}\<^sub>p = 0"