-
Notifications
You must be signed in to change notification settings - Fork 9
/
pkg_core_definition.v
847 lines (705 loc) · 20.8 KB
/
pkg_core_definition.v
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
(*
This file defines "packages" of stateful probabilistic computation.
It should not be directly required but instead used via Package.v.
* raw_package (computational part of a package without validity conditions)
*)
From Coq Require Import Utf8.
From SSProve.Relational Require Import OrderEnrichedCategory OrderEnrichedRelativeMonadExamples.
Set Warnings "-ambiguous-paths,-notation-overridden,-notation-incompatible-format".
From mathcomp Require Import ssreflect eqtype choice seq ssrfun ssrbool.
Set Warnings "ambiguous-paths,notation-overridden,notation-incompatible-format".
From extructures Require Import ord fset fmap.
From SSProve.Mon Require Import SPropBase.
From SSProve.Crypt Require Import Prelude Axioms ChoiceAsOrd RulesStateProb StateTransformingLaxMorph
choice_type Casts.
Require Import Equations.Prop.DepElim.
From Equations Require Import Equations.
Set Equations With UIP.
Import SPropNotations.
Set Bullet Behavior "Strict Subproofs".
Set Default Goal Selector "!".
Set Primitive Projections.
(* General definitions *)
#[local] Open Scope fset.
#[local] Open Scope fset_scope.
#[local] Open Scope type_scope.
Definition ident := nat.
(* Signature of an operation, including the identifier *)
Definition opsig := ident * (choice_type * choice_type).
(* Record opsig := mkop {
ident : nat ;
src : choice_type ;
tgt : choice_type
}. *)
Definition mkopsig id S T : opsig := (id, (S, T)).
Definition Location := ∑ (t : choice_type), nat.
Definition loc_type (l : Location) := l.π1.
Coercion loc_type : Location >-> choice_type.
Definition Value (t : choice_type) := chElement t.
Definition Interface := {fset opsig}.
Definition ide (v : opsig) : ident :=
let '(n, _) := v in n.
Definition chsrc (v : opsig) : choice_type :=
let '(n, (s, t)) := v in s.
Definition src (v : opsig) : choiceType :=
chsrc v.
Definition chtgt (v : opsig) : choice_type :=
let '(n, (s, t)) := v in t.
Definition tgt (v : opsig) : choiceType :=
chtgt v.
Section Translation.
Definition Prob_ops_collection := FreeProbProg.P_OP.
Definition Prob_arities : Prob_ops_collection → choiceType :=
FreeProbProg.P_AR.
End Translation.
Definition Op := Prob_ops_collection.
Definition Arit := Prob_arities.
Section FreeModule.
Context (Loc : {fset Location}).
Context (import : Interface).
Inductive raw_code (A : choiceType) : Type :=
| ret (x : A)
| opr (o : opsig) (x : src o) (k : tgt o → raw_code A)
| getr (l : Location) (k : l → raw_code A)
| putr (l : Location) (v : l) (k : raw_code A)
| sampler (op : Op) (k : Arit op → raw_code A).
Arguments ret [A] _.
Arguments opr [A] _ _.
Arguments getr [A] _.
Arguments putr [A] _.
Arguments sampler [A] _ _.
Inductive valid_code {A} : raw_code A → Prop :=
| valid_ret :
∀ x,
valid_code (ret x)
| valid_opr :
∀ o x k,
o \in import →
(∀ v, valid_code (k v)) →
valid_code (opr o x k)
| valid_getr :
∀ l k,
l \in Loc →
(∀ v, valid_code (k v)) →
valid_code (getr l k)
| valid_putr :
∀ l v k,
l \in Loc →
valid_code k →
valid_code (putr l v k)
| valid_sampler :
∀ op k,
(∀ v, valid_code (k v)) →
valid_code (sampler op k)
.
Derive NoConfusion NoConfusionHom for choice_type.
Derive NoConfusion NoConfusionHom EqDec for nat.
Derive NoConfusion NoConfusionHom for raw_code.
Derive Signature for valid_code.
(* Inversion lemmata *)
Lemma inversion_valid_opr :
∀ {A o x k},
@valid_code A (opr o x k) →
(o \in import) *
(∀ v, valid_code (k v)).
Proof.
intros A o x k h.
dependent destruction h.
intuition auto.
Qed.
Lemma inversion_valid_getr :
∀ {A l k},
@valid_code A (getr l k) →
(l \in Loc) *
(∀ v, valid_code (k v)).
Proof.
intros A l k h.
dependent destruction h.
intuition auto.
Qed.
Lemma inversion_valid_putr :
∀ {A l v k},
@valid_code A (putr l v k) →
(l \in Loc) *
(valid_code k).
Proof.
intros A l v k h.
dependent destruction h.
intuition auto.
Qed.
Lemma inversion_valid_sampler :
∀ {A op k},
@valid_code A (sampler op k) →
∀ v, valid_code (k v).
Proof.
intros A op k h.
dependent destruction h.
auto.
Qed.
Class ValidCode {A} (p : raw_code A) :=
is_valid_code : valid_code p.
Lemma valid_code_from_class :
∀ A (p : raw_code A),
ValidCode p →
valid_code p.
Proof.
intros A p h. auto.
Defined.
Record code A := mkprog {
prog : raw_code A ;
prog_valid : ValidCode prog
}.
Arguments mkprog {_} _.
Arguments prog {_} _.
Arguments prog_valid {_} _.
Lemma code_ext :
∀ A (u v : code A),
u.(prog) = v.(prog) →
u = v.
Proof.
intros A u v h.
destruct u as [u hu], v as [v hv].
cbn in h. subst.
f_equal. apply proof_irrelevance.
Qed.
Fixpoint bind {A B} (c : raw_code A) (k : A → raw_code B) :
raw_code B :=
match c with
| ret a => k a
| opr o x k' => opr o x (λ p, bind (k' p) k)
| getr l k' => getr l (λ v, bind (k' v) k)
| putr l v k' => putr l v (bind k' k)
| sampler op k' => sampler op (λ a, bind (k' a) k)
end.
Lemma valid_bind :
∀ A B c k,
ValidCode c →
(∀ x, ValidCode (k x)) →
ValidCode (@bind A B c k).
Proof.
intros A B c k hc hk.
induction hc. all: simpl.
all: solve [ try constructor ; eauto ].
Qed.
Lemma inversion_valid_bind :
∀ {A B} {c : raw_code A} {k : A → raw_code B},
ValidCode (bind c k) →
ValidCode c.
Proof.
intros A B c k h.
unfold ValidCode in *.
induction c.
- constructor.
- simpl in h. apply inversion_valid_opr in h. destruct h as [h1 h2].
constructor. all: eauto.
- simpl in h. apply inversion_valid_getr in h.
constructor. all: intuition eauto.
- simpl in h. apply inversion_valid_putr in h.
constructor. all: intuition eauto.
- simpl in h.
constructor. intro.
eapply inversion_valid_sampler in h.
eauto.
Qed.
(* Alternative to bind *)
Inductive command : choiceType → Type :=
| cmd_op o (x : src o) : command (tgt o)
| cmd_get (ℓ : Location) : command (Value ℓ.π1)
| cmd_put (ℓ : Location) (v : Value ℓ.π1) : command unit_choiceType
| cmd_sample op : command (Arit op).
Definition cmd_bind {A B} (c : command A) (k : A → raw_code B) :=
match c in command A return (A → raw_code B) → raw_code B with
| cmd_op o x => opr o x
| cmd_get ℓ => getr ℓ
| cmd_put ℓ v => λ k, putr ℓ v (k Datatypes.tt)
| cmd_sample op => sampler op
end k.
Inductive valid_command : ∀ A, command A → Prop :=
| valid_cmd_op :
∀ o x,
o \in import →
valid_command _ (cmd_op o x)
| valid_cmd_get :
∀ ℓ,
ℓ \in Loc →
valid_command _ (cmd_get ℓ)
| valid_cmd_put :
∀ ℓ v,
ℓ \in Loc →
valid_command _ (cmd_put ℓ v)
| valid_cmd_sample :
∀ op,
valid_command _ (cmd_sample op).
Arguments valid_command [_] _.
Class ValidCommand {A} (c : command A) :=
is_valid_command : valid_command c.
Lemma valid_command_from_class :
∀ A (c : command A),
ValidCommand c →
valid_command c.
Proof.
auto.
Qed.
Lemma valid_cmd_bind :
∀ {A B} (c : command A) (k : A → raw_code B),
ValidCommand c →
(∀ x, ValidCode (k x)) →
ValidCode (cmd_bind c k).
Proof.
intros A B c k hc hk.
unfold ValidCode in *.
induction hc.
- cbn. constructor. all: auto.
- cbn. constructor. all: auto.
- cbn. constructor. all: auto.
- cbn. constructor. auto.
Qed.
Lemma inversion_valid_cmd_bind :
∀ {A B} (c : command A) (k : A → raw_code B),
ValidCode (cmd_bind c k) →
ValidCommand c ∧ (∀ x, ValidCode (k x)).
Proof.
intros A B c k h.
destruct c.
- cbn. apply inversion_valid_opr in h. intuition auto.
constructor. auto.
- cbn. apply inversion_valid_getr in h. intuition auto.
constructor. auto.
- cbn. apply inversion_valid_putr in h. split.
+ constructor. intuition auto.
+ intros []. intuition auto.
- cbn. split.
+ constructor.
+ intro. eapply inversion_valid_sampler in h. eauto.
Qed.
Lemma bind_assoc :
∀ {A B C : choiceType} (v : raw_code A)
(k1 : A → raw_code B) (k2 : B → raw_code C),
bind (bind v k1) k2 =
bind v (λ x, bind (k1 x) k2).
Proof.
intros A B C v k1 k2.
induction v in k1, k2 |- *.
- cbn. reflexivity.
- cbn. f_equal. apply functional_extensionality. auto.
- cbn. f_equal. extensionality z. auto.
- cbn. f_equal. auto.
- cbn. f_equal. extensionality z. auto.
Qed.
Lemma bind_ret :
∀ A (v : raw_code A),
bind v (λ x, ret x) = v.
Proof.
intros A v.
induction v.
all: cbn. 1: reflexivity.
all: try solve [ f_equal ; apply functional_extensionality ; eauto ].
f_equal. auto.
Qed.
Lemma bind_cmd_bind :
∀ {A B C : choiceType}
(c : command A) (k1 : _ → raw_code B) (k2 : _ → raw_code C),
bind (cmd_bind c k1) k2 =
cmd_bind c (λ x, bind (k1 x) k2).
Proof.
intros A B C c k1 k2.
destruct c. all: simpl. all: reflexivity.
Qed.
Lemma prove_code :
∀ {A} (P : code A → Type) p q,
P p →
p.(prog) = q.(prog) →
P q.
Proof.
intros A P p q h e.
apply code_ext in e. subst. auto.
Defined.
Open Scope package_scope.
Import SPropAxioms. Import FunctionalExtensionality.
(* TODO: NEEDED? *)
(* Program Definition rFree : ord_relativeMonad choice_incl :=
@mkOrdRelativeMonad ord_choiceType TypeCat choice_incl code _ _ _ _ _ _.
Next Obligation.
simple refine {code ret _ }.
auto.
Defined.
Next Obligation.
eapply bind. all: eauto. Defined.
Next Obligation.
f_equal. apply functional_extensionality. auto.
Qed.
Next Obligation.
apply functional_extensionality.
intro c. apply code_ext.
destruct c as [c h]. cbn.
induction c.
all: solve [
simpl in * ; try reflexivity ;
f_equal ; try apply functional_extensionality ; intuition auto
].
Qed.
Next Obligation.
extensionality x. apply code_ext.
cbn. reflexivity.
Qed.
Next Obligation.
apply functional_extensionality. intros [c h].
apply code_ext. cbn.
induction c in h |- *.
all: solve [
simpl in * ; try reflexivity ;
f_equal ; try apply functional_extensionality ; intuition auto
].
Qed. *)
Fixpoint mapFree {A B : choiceType} (f : A → B) (m : raw_code A) :
raw_code B :=
match m with
| ret x => ret (f x)
| opr o x k => opr o x (λ r, mapFree f (k r))
| getr l k' => getr l (λ v, mapFree f (k' v))
| putr l v k' => putr l v (mapFree f k')
| sampler op k' => sampler op (λ a, mapFree f (k' a))
end.
End FreeModule.
Arguments ret [A] _.
Arguments opr [A] _ _.
Arguments getr [A] _.
Arguments putr [A] _.
Arguments sampler [A] _ _.
Arguments mkprog [_ _ _] _.
Arguments prog [_ _ _] _.
Arguments prog_valid [_ _ _] _.
Notation "{ 'code' p }" :=
(mkprog p _)
(format "{ code p }") : package_scope.
Notation "{ 'code' p '#with' h }" :=
(mkprog p h)
(only parsing) : package_scope.
(* Having an instance here means that it will use ret when the code
is unknown. Pretty bad.
We will instead use Hint Extern.
*)
(* Instance ValidCode_ret (A : choiceType) (x : A) L I :
ValidCode L I (ret x).
Proof.
apply valid_ret.
Qed. *)
Create HintDb ssprove_valid_db.
#[export] Hint Extern 1 (ValidCode ?L ?I (ret ?x)) =>
apply valid_ret
: typeclass_instances ssprove_valid_db.
#[export] Hint Extern 1 (ValidCode ?L ?I (opr ?o ?x ?k)) =>
eapply valid_opr ; [
auto_in_fset
| intro ; eapply valid_code_from_class
] : typeclass_instances ssprove_valid_db.
#[export] Hint Extern 1 (ValidCode ?L ?I (getr ?o ?k)) =>
eapply valid_getr ; [
auto_in_fset
| intro ; eapply valid_code_from_class
] : typeclass_instances ssprove_valid_db.
#[export] Hint Extern 1 (ValidCode ?L ?I (putr ?o ?x ?k)) =>
eapply valid_putr ; [
auto_in_fset
| eapply valid_code_from_class
] : typeclass_instances ssprove_valid_db.
#[export] Hint Extern 1 (ValidCode ?L ?I (sampler ?op ?k)) =>
eapply valid_sampler ;
intro ; eapply valid_code_from_class
: typeclass_instances ssprove_valid_db.
#[export] Hint Extern 1 (ValidCode ?L ?I (bind ?p ?k)) =>
eapply valid_bind ; [
idtac
| intro
]
: typeclass_instances ssprove_valid_db.
Coercion prog : code >-> raw_code.
(* Only in typeclasses inference to avoid bad progress *)
#[export] Hint Extern 1 (ValidCode ?L ?I (?p.(prog))) =>
eapply p.(prog_valid)
: typeclass_instances.
Arguments valid_command _ _ [_] _.
#[export] Hint Extern 1 (ValidCommand ?L ?I (cmd_op ?o ?x)) =>
eapply valid_cmd_op ;
auto_in_fset
: typeclass_instances ssprove_valid_db.
#[export] Hint Extern 1 (ValidCommand ?L ?I (cmd_get ?l)) =>
eapply valid_cmd_get ;
auto_in_fset
: typeclass_instances ssprove_valid_db.
#[export] Hint Extern 1 (ValidCode ?L ?I (cmd_put ?l ?v)) =>
eapply valid_cmd_put ;
auto_in_fset
: typeclass_instances ssprove_valid_db.
#[export] Hint Extern 1 (ValidCode ?L ?I (cmd_sample ?op)) =>
eapply valid_cmd_sample
: typeclass_instances ssprove_valid_db.
#[export] Hint Extern 1 (ValidCode ?L ?I (cmd_bind ?c ?k)) =>
eapply valid_cmd_bind ; [
idtac
| intro
]
: typeclass_instances ssprove_valid_db.
Section FreeLocations.
Context {import : Interface}.
(* TODO Make this lemma more general? *)
Lemma injectSubset :
∀ {locs1 locs2 : {fset Location}} {l : Location},
fsubset locs1 locs2 →
l \in locs1 →
l \in locs2.
Proof.
intros locs1 locs2 l Hfsubset H.
unfold fsubset in Hfsubset.
move: Hfsubset.
move /eqP => Q.
rewrite -Q.
rewrite in_fsetU.
apply/orP.
by left.
Defined.
Let codeI locs := code locs import.
Lemma valid_injectLocations :
∀ A L1 L2 (v : raw_code A),
fsubset L1 L2 →
ValidCode L1 import v →
ValidCode L2 import v.
Proof.
intros A L1 L2 v h p.
induction p.
all: try solve [ constructor ; eauto ].
- constructor. 2: eauto.
eapply injectSubset. all: eauto.
- constructor. 2: eauto.
eapply injectSubset. all: eauto.
Qed.
Lemma valid_cmd_injectLocations :
∀ A L1 L2 (v : command A),
fsubset L1 L2 →
ValidCommand L1 import v →
ValidCommand L2 import v.
Proof.
intros A L1 L2 v h p.
destruct p.
all: try solve [ constructor ; eauto ].
- constructor.
eapply injectSubset. all: eauto.
- constructor.
eapply injectSubset. all: eauto.
Qed.
End FreeLocations.
Section FreeMap.
Context {Locs : {fset Location}}.
(* TODO Factorise with lemma above. *)
Lemma in_fsubset :
∀ {e} {S1 S2 : Interface},
fsubset S1 S2 →
e \in S1 →
e \in S2.
Proof.
intros e S1 S2 hs h.
unfold fsubset in hs.
move: hs. move /eqP => hs. rewrite -hs.
rewrite in_fsetU.
apply/orP.
auto.
Defined.
Let codeL I := code Locs I.
Lemma valid_injectMap :
∀ {A I1 I2} (v : raw_code A),
fsubset I1 I2 →
ValidCode Locs I1 v →
ValidCode Locs I2 v.
Proof.
intros A I1 I2 v h p.
induction p.
all: try solve [ constructor ; auto ].
constructor. 2: eauto.
eapply in_fsubset. all: eauto.
Qed.
End FreeMap.
Definition typed_raw_function :=
∑ (S T : choice_type), S → raw_code T.
Definition raw_package :=
{fmap ident -> typed_raw_function }.
(* To avoid unification troubles, we wrap this definition in an inductive. *)
Definition valid_package_ext L I (E : Interface) (p : raw_package) :=
∀ o, o \in E →
let '(id, (src, tgt)) := o in
∃ (f : src → raw_code tgt),
p id = Some (src ; tgt ; f) ∧ ∀ x, ValidCode L I (f x).
Inductive valid_package L I E p :=
| prove_valid_package : valid_package_ext L I E p → valid_package L I E p.
Lemma from_valid_package :
∀ L I E p,
valid_package L I E p →
valid_package_ext L I E p.
Proof.
intros L I E p [h]. exact h.
Qed.
Class ValidPackage L I E p :=
is_valid_package : valid_package L I E p.
(* Packages *)
Record package L I E := mkpackage {
pack : raw_package ;
pack_valid : ValidPackage L I E pack
}.
Arguments mkpackage [_ _ _] _ _.
Arguments pack [_ _ _] _.
Arguments pack_valid [_ _ _] _.
(* Packages coming with their set of locations *)
Record loc_package I E := mkloc_package {
locs : {fset Location} ;
locs_pack : package locs I E
}.
Arguments mkloc_package [_ _] _ _.
Arguments locs [_ _] _.
Arguments locs_pack [_ _] _.
Coercion locs_pack : loc_package >-> package.
Lemma loc_package_ext :
∀ {I E} (p1 p2 : loc_package I E),
p1.(locs) = p2.(locs) →
p1.(locs_pack).(pack) =1 p2.(locs_pack).(pack) →
p1 = p2.
Proof.
intros I E p1 p2 e1 e2.
destruct p1 as [l1 [p1 h1]], p2 as [l2 [p2 h2]].
apply eq_fmap in e2.
cbn in *. subst.
f_equal. f_equal. apply proof_irrelevance.
Qed.
Notation "{ 'package' p }" :=
(mkpackage p _)
(format "{ package p }") : package_scope.
Notation "{ 'package' p '#with' h }" :=
(mkpackage p h)
(only parsing) : package_scope.
Coercion pack : package >-> raw_package.
#[export] Hint Extern 1 (ValidPackage ?L ?I ?E (?p.(pack))) =>
eapply p.(pack_valid)
: typeclass_instances ssprove_valid_db.
Lemma valid_package_from_class :
∀ L I E (p : raw_package),
ValidPackage L I E p →
valid_package L I E p.
Proof.
intros A p h. auto.
Defined.
Notation "{ 'locpackage' p }" :=
(mkloc_package _ (mkpackage p _))
(format "{ locpackage p }") : package_scope.
Notation "{ 'locpackage' p '#with' h }" :=
(mkloc_package _ (mkpackage p h))
(only parsing) : package_scope.
(* Some validity lemmata *)
Lemma valid_package_inject_locations :
∀ I E L1 L2 p,
fsubset L1 L2 →
ValidPackage L1 I E p →
ValidPackage L2 I E p.
Proof.
intros I E L1 L2 p hL h.
apply prove_valid_package.
eapply from_valid_package in h.
intros [n [S T]] ho. specialize (h _ ho). cbn in h.
destruct h as [f [ef hf]].
exists f. intuition auto.
eapply valid_injectLocations. all: eauto.
Qed.
Lemma valid_package_inject_export :
∀ L I E1 E2 p,
fsubset E1 E2 →
ValidPackage L I E2 p →
ValidPackage L I E1 p.
Proof.
intros L I E1 E2 p hE h.
apply prove_valid_package.
eapply from_valid_package in h.
intros o ho. specialize (h o).
destruct o as [o [So To]].
forward h.
{ eapply in_fsubset. all: eauto. }
destruct h as [f [ef hf]].
exists f. intuition auto.
Qed.
Lemma valid_package_inject_import :
∀ L I1 I2 E p,
fsubset I1 I2 →
ValidPackage L I1 E p →
ValidPackage L I2 E p.
Proof.
intros L I1 I2 E p hE h.
apply prove_valid_package.
eapply from_valid_package in h.
intros [n [S T]] ho. specialize (h _ ho). cbn in h.
destruct h as [f [ef hf]].
exists f. intuition auto.
eapply valid_injectMap. all: eauto.
Qed.
Lemma package_ext :
∀ {L I E} (p1 p2 : package L I E),
p1.(pack) =1 p2.(pack) →
p1 = p2.
Proof.
intros L I E p1 p2 e.
destruct p1 as [p1 h1], p2 as [p2 h2].
apply eq_fmap in e.
cbn in *. subst.
f_equal. apply proof_irrelevance.
Qed.
(* Rewriting in packages *)
Lemma mkpackage_rewrite :
∀ {L I E T} {x y} (p : T → _) h (e : x = y),
@mkpackage L I E (p x) h = mkpackage (p y) (sig_rewrite_aux p h e).
Proof.
intros L I E T x y p h e. subst. reflexivity.
Qed.
Ltac mkpackage_rewrite e :=
lazymatch type of e with
| ?x = _ =>
match goal with
| |- context [ mkpackage ?p ?h ] =>
lazymatch p with
| context [ x ] =>
lazymatch eval pattern x in p with
| (fun x => @?q x) ?y =>
erewrite (mkpackage_rewrite q _ e)
end
end
end
end.
(** Tactic package rewrite
Usage: you have e : x = y as an hypothesis and you want to rewrite e inside
a term of the form mkpackage u v, specifically inside the term u.
sig rewrite e will replace x by y in u and update v accordingly.
*)
Tactic Notation "package" "rewrite" constr(e) :=
mkpackage_rewrite e.
(* Rewriting in codes *)
Lemma mkprog_rewrite :
∀ {L I A T} {x y} (p : T → _) h (e : x = y),
@mkprog L I A (p x) h = mkprog (p y) (sig_rewrite_aux p h e).
Proof.
intros L I A T x y p h e. subst. reflexivity.
Qed.
Ltac mkprog_rewrite e :=
lazymatch type of e with
| ?x = _ =>
match goal with
| |- context [ mkprog ?p ?h ] =>
lazymatch p with
| context [ x ] =>
lazymatch eval pattern x in p with
| (fun x => @?q x) ?y =>
erewrite (mkprog_rewrite q _ e)
end
end
end
end.
(** Tactic code rewrite
Usage: you have e : x = y as an hypothesis and you want to rewrite e inside
a term of the form mkcode u v, specifically inside the term u.
sig rewrite e will replace x by y in u and update v accordingly.
*)
Tactic Notation "code" "rewrite" constr(e) :=
mkprog_rewrite e.