-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProc.v
More file actions
1385 lines (1118 loc) · 30.5 KB
/
Proc.v
File metadata and controls
1385 lines (1118 loc) · 30.5 KB
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
(* Time-stamp: <Wed 11/22/23 11:21 Dan Dougherty Proc.v> *)
From Coq Require Import
String
List
Relations
.
From RC Require Import
CpdtTactics
TacticsMatch
Utilities
ListUtil
Decidability
Act
Sorts
Term
(* ATerm *)
Role
.
Unset Implicit Arguments.
(** *** Locations and tdecls *)
Inductive loc := L : nat -> loc.
(** *** loc equality *)
Scheme Equality for loc.
(** Defines
[loc_beq] and
[loc_eq_dec : ∀ x y : loc, {x = y} + {x ≠ y}]
internal_loc_dec_bl :
∀ x y : loc, loc_beq x y = true → x = y]
[internal_loc_dec_lb]:
∀ x y : loc, x = y → loc_beq x y = true
*)
(** Thus loc is decidable *)
#[export] Instance loc_eqDec : EqDecision loc
:= loc_eq_dec.
(** And the internal functions imply loc is an instance of EqbDec *)
Lemma beq_eq_loc : forall x y,
if loc_beq x y then x = y else x <> y.
Proof.
intros.
assert (abl := @internal_loc_dec_bl x y).
assert (alb := @internal_loc_dec_lb x y).
destruct (loc_beq x y); auto. intros F.
firstorder; easy.
Qed.
#[global] Instance loc_eqbdec : EqbDec loc :=
{ beq:= loc_beq;
beq_eq := beq_eq_loc
}.
(** Now since loc is an Decidability.EqbDec, we have reflection [Decidability.beq_reflect]
*)
(** Show *)
Open Scope string_scope.
#[global] Instance show_loc : Show loc :=
{
show x :=
match x with
L i => "L " ++(show i)
end}.
Close Scope string_scope.
(** A default location *)
Definition default_loc := L 0.
Definition loc_ltb (l1 l2 : loc) : bool :=
match (l1,l2) with
(L i, L j) => Nat.ltb i j
end .
(** Tdecls *)
Definition tdecl: Set := Term * loc.
(** *** tdecl equality *)
Definition tdecl_dec:
forall x y: tdecl, {x = y} + {x <> y}.
Proof.
intros.
decide equality. decide equality. decide equality.
(* apply eqDecision_term. *)
apply Term_eq_dec.
Defined.
#[export] Instance eqDecision_tdecl : EqDecision tdecl
:= tdecl_dec.
#[global] Hint Resolve tdecl_dec : core.
Definition tdecl_beq (x y: tdecl) : bool :=
match x, y with
| (t1,l1) , (t2, l2) => beq t1 t2 && beq l1 l2
end.
Lemma beq_eq_tdecl : forall x y,
if tdecl_beq x y then x = y else x <> y.
Proof.
intros.
destruct (tdecl_beq x y) eqn:eq1.
{
destruct x; destruct y.
unfold tdecl_beq in *.
destruct (beq_reflect t t0);
destruct (beq_reflect l l0); subst;
simpl in *; auto; try easy.
}
{
destruct x; destruct y.
unfold tdecl_beq in *.
destruct (beq_reflect t t0);
destruct (beq_reflect l l0); subst;
simpl in *; auto; try easy; try congruence.
}
Qed.
#[global] Instance tdecl_eqbdec : EqbDec tdecl :=
{ beq:= tdecl_beq;
beq_eq := beq_eq_tdecl
}.
(** *** Expressions *)
Inductive Expr: Set :=
| Pair: loc -> loc -> Expr
| Encr: loc -> loc -> Expr
| Hash: loc -> Expr
| Frst: loc -> Expr
| Scnd: loc -> Expr
| Decr: loc -> loc -> Expr
| Quote: string -> Expr
| PubOf: loc -> Expr
| Genr: nat -> sort -> Expr
| Param: nat -> Expr
| Read: nat -> Expr
.
(** *** Statements *)
Inductive Stmt: Type :=
| Bind: tdecl -> Expr -> Stmt
| Evnt : (Act loc) -> Stmt
(* value in 1st loc = value in 2nd *)
| Csame: loc -> loc -> Stmt
(* value in 1st loc is hash of value in 2nd *)
| Chash : loc -> loc -> Stmt
(* values in the two locs make a key pair, with the public key first *)
| Ckypr: loc -> loc -> Stmt
(* value in the loc has the sort given *)
| Csrt: loc -> sort -> Stmt
(* value in the loc is the string given *)
| Cquote: loc -> string -> Stmt
(* comment *)
| Comm: string -> Stmt
.
Definition Proc := (list Stmt).
Definition Stmts := (list Stmt).
(** * Equivalance closure of (speaking informally) the "Same" relation *)
(** there is a "Same ptr" from location 1 to location 2 *)
Definition same_ptr (pr: Proc) (l1 l2 : loc) : Prop :=
In (Csame l1 l2) pr.
(** the equivalence closure of same_ptr *)
Definition same_linked (pr: Proc) ( l1 l2 : loc) : Prop :=
clos_refl_sym_trans loc (same_ptr pr) l1 l2 .
(** ** Decidable equality *)
(** *** tdecl *)
(** *** Expr *)
#[export] Instance eqDecision_expr : EqDecision Expr.
Proof. repeat solve_decision.
Defined.
#[export] Instance eqDecision_stmt : EqDecision Stmt.
Proof. repeat solve_decision.
Defined.
#[export] Instance eqDecision_stmts : EqDecision (list Stmt).
Proof. apply Decidability.list_eq_dec. Defined.
(* ---------------------------------- *)
(** ** Taxonomy of Expr *)
Definition is_pair_expb (exp: Expr) : bool :=
match exp with
| Pair _ _ => true
| _ => false
end.
Definition is_pair_exp (exp: Expr) : Prop :=
exists l1 l2, exp = Pair l1 l2.
#[export] Instance is_pair_exp_dec e :
Decision (is_pair_exp e).
Proof.
intros. hnf. unfold is_pair_exp.
destruct e.
left; exists l; exists l0; easy.
all: right; intros F;
destruct F as [l1 [l2 P ] ] ; congruence.
Defined.
Definition is_encr_expb (exp: Expr) : bool :=
match exp with
| Encr _ _ => true
| _ => false
end.
Definition is_encr_exp (exp: Expr) : Prop :=
exists l1 l2, exp = Encr l1 l2.
Global Instance is_encr_exp_dec e :
Decision (is_encr_exp e).
Proof.
intros. hnf.
(* unfold is_pair_exp. *)
destruct e.
all: try (right; intros F;
destruct F as [l1 [l2 P ] ]; congruence).
left; exists l; exists l0; easy.
Defined.
Definition is_hash_expb (exp: Expr) : bool :=
match exp with
| Hash _ => true
| _ => false
end.
Definition is_hash_exp (exp: Expr) : Prop :=
exists l, exp = Hash l.
Global Instance is_hash_exp_dec e :
Decision (is_hash_exp e).
Proof.
intros. hnf.
destruct e.
all: try (right; intros F;
destruct F; congruence).
left; exists l; easy.
Defined.
Definition is_quote_exp (exp: Expr) : Prop :=
exists (s: string), exp = Quote s.
Global Instance is_quote_exp_dec e :
Decision (is_quote_exp e).
Proof.
intros. hnf.
destruct e.
all: try (right; intros F;
destruct F; congruence).
left; exists s; easy.
Defined.
Definition is_quote_expb (exp: Expr) : bool :=
match exp with
| Quote _ => true
| _ => false
end.
Definition is_I_expr (exp: Expr) : bool :=
match exp with
| Pair _ _ => true
| Encr _ _ => true
| Hash _ => true
| Quote _ => true
| _ => false
end
.
(** *** Genr indices *)
(** The largest gen index not usd in pr *)
(** But: initialize with 0, so [fresh_gen_index]
will start with 1.
*)
Fixpoint max_gen_index (pr: Proc) : nat :=
match pr with
[] => 0
| (Bind (_ , _) (Genr i _)) :: rest =>
S (max_gen_index rest)
| _ :: rest => (max_gen_index rest)
end.
Definition fresh_gen_index (pr: Proc) : nat :=
S (max_gen_index pr) .
(* ---------------------------------- *)
(** ** Extract the bindings in a Proc *)
Definition is_binding (st: Stmt) : bool :=
match st with
| Bind _ _ => true
| _ => false
end.
Definition is_binding_for (t: Term) (st: Stmt) : bool :=
match st with
| Bind (t,_) _ => true
| _ => false
end.
Definition bindings (pr: Proc) : list Stmt :=
filter is_binding pr.
Fixpoint locs_of pr : list loc :=
match pr with
[] => []
| (Bind (_ , l) _ ) :: rest =>
l :: (locs_of rest)
| _ :: rest => (locs_of rest)
end.
Lemma locs_of_append pr ls :
locs_of (pr++ls) =
(locs_of pr) ++ (locs_of ls).
Proof.
induction pr as [| s rest IH]; simpl; auto.
- destruct_all_matches.
rewrite IH; auto.
Qed.
Corollary in_loc_append_L pr ls l :
In l (locs_of pr) ->
In l (locs_of (pr++ls)).
Proof.
intros H.
rewrite locs_of_append; auto.
Qed.
Corollary in_loc_append_R pr ls l :
In l (locs_of ls) ->
In l (locs_of (pr++ls)).
Proof.
intros H.
rewrite locs_of_append ; auto.
Qed.
(** The largest location not bound in pr *)
(** But: initialize with 0, so [fresh_loc]
will start with 1.
*)
Fixpoint max_loc (pr: Proc) : loc :=
match pr with
[] => L 0
| (Bind (_ , (L i)) _) :: rest =>
match (max_loc rest) with
L m =>
L (max m i)
end
| _ :: rest => (max_loc rest)
end.
Definition next_loc (l: loc) : loc :=
match l with L i => L (S i)
end.
Definition fresh_loc (pr: Proc) : loc :=
next_loc (max_loc pr) .
(** Convenient when we need two fresh locations,
perhaps with intervening statments, this keeps the bookkeepping simpler
than two calls to [fresh_loc]
*)
Definition two_fresh_locs (pr: Proc) : loc*loc :=
let newloc := next_loc (max_loc pr) in
(newloc , next_loc newloc).
(* ======================= *)
(** ** First loc for a term *)
(** some flailing around here...could be streamlined *)
Fixpoint first_loc_for
(pr: Proc) (t: Term): optionE loc :=
match pr with
[] => NoneE ("failed first_loc_for: " ++ (show t))
| (Bind (t', l) e) :: rest =>
match decide (t=t') with
| left _ => SomeE l
| right _ => first_loc_for rest t
end
| _ :: rest => first_loc_for rest t
end.
Lemma first_loc_exists pr t l e :
In (Bind (t,l) e) pr ->
forall s, first_loc_for pr t <> NoneE s.
Proof.
intros. induction pr. simpl in H; tauto.
simpl in H. destruct H.
- subst; simpl. destruct (decide (t=t)); easy.
- pose proof (IHpr H).
intros F.
(* unfold first_loc_for in F. *)
destruct a.
simpl in F.
destruct t0.
destruct (decide (t=t0)).
subst; easy. cong.
all: simpl in F; easy.
Qed.
Lemma in_first_loc pr t l e :
In (Bind (t,l) e) pr ->
exists l', first_loc_for pr t = SomeE l'.
Proof.
intros.
assert (h:= @first_loc_exists pr t l e H).
destruct (first_loc_for pr t) eqn:eq1; auto.
exists x; auto. cong.
Qed.
Lemma first_loc_for_elim_None pr t s :
first_loc_for pr t = NoneE s ->
forall (l: loc) (e: Expr),
~ In (Bind (t,l) e) pr.
Proof.
intros H l e F.
apply in_first_loc in F.
destruct F as [l' P].
congruence.
Qed.
Proposition first_loc_for_elim pr t l :
first_loc_for pr t = SomeE l ->
exists (e: Expr), In (Bind (t,l) e) pr.
Proof.
intros H.
induction pr as [| smt rest IH ] .
- inv H.
- destruct smt as
[[ t0 l0 ] e0 | ev |l1 l2 |l1 l2| l1 l2| l0 s | l0 s | s ] .
{ (* first stmt is a Bind *)
destruct (decide (t = t0)) eqn:et.
{ (* t=t0 *)
simpl in H. rewrite et in H.
inv H.
exists e0. auto. }
{ (* t<>t0 *)
simpl in H.
rewrite et in H.
assert (h1:= IH H).
destruct h1 as [e Hin ] .
exists e. auto.
}
}
(* first stmt is not a Bind, use IH *)
all : (simpl in H;
assert (h1:= IH H);
destruct h1 as [e Hin];
exists e; auto).
Qed.
Proposition first_loc_for_intro pr t l e:
In (Bind (t,l) e) pr ->
exists (lf: loc) (ef: Expr),
In (Bind (t,lf) ef) pr /\
first_loc_for pr t = SomeE lf .
Proof.
intros.
assert (h1:= first_loc_exists pr t l e H).
apply not_NoneE_SomeE in h1.
destruct h1 as [lf P].
assert (h2:= first_loc_for_elim pr t lf P ).
destruct h2 as [ef Q].
exists lf ; exists ef. tauto.
Qed.
(** *** first_loc with a default value *)
Fixpoint first_loc_for_default
(pr: Proc) (t: Term) : loc :=
match pr with
[] => default_loc
| (Bind (t', l) e) :: rest =>
match decide (t=t') with
| left _ => l
| right _ => first_loc_for_default rest t
end
| _ :: rest => first_loc_for_default rest t
end.
(** just a shorthand for readability) *)
Definition a_loc_for (pr: Proc) (t: Term) :=
first_loc_for_default pr t.
(** If we know there is some first_loc for t then
first_loc_for_default finds it *)
Lemma first_loc_for_def_works pr t l :
first_loc_for pr t = SomeE l ->
first_loc_for_default pr t = l .
Proof.
intros H.
induction pr as [| a rest IH]; auto.
- simpl in H. congruence.
- destruct a; auto.
destruct t0; auto.
destruct (decide (t=t0)).
+ subst. simpl in *.
destruct (decide (t0=t0)); congruence.
+ simpl in *.
destruct (decide (t=t0)); try congruence.
apply IH; easy.
Qed.
(** Boolean relational version *)
Definition first_loc_forRb
(pr: Proc) (t: Term) (l: loc) : bool :=
match first_loc_for pr t with
SomeE l' => beq l l'
| _ => false
end.
Global Hint Unfold first_loc_forRb : core.
Lemma first_loc_forRb_default pr t l :
first_loc_forRb pr t l = true
-> first_loc_for_default pr t = l.
Proof.
intros.
apply first_loc_for_def_works.
unfold first_loc_forRb in *.
destruct (first_loc_for pr t) eqn:eq1.
destruct (beq_reflect l x).
- congruence.
- easy.
- easy.
Qed.
Lemma first_locR_unique pr t l l' :
first_loc_forRb pr t l = true ->
first_loc_forRb pr t l' =true ->
l = l'.
Proof.
intros H1 H2.
unfold first_loc_forRb in *.
destruct_all_matches.
destruct (beq_reflect l x);
destruct (beq_reflect l' x);
congruence.
Qed.
(* ----------------- *)
(** An indirect definition, easier to show decidable *)
Definition term_is_bound_fl (pr: Proc)(t: Term) : Prop :=
exists x, first_loc_for pr t = SomeE x.
Global Instance term_is_bound_fl_dec pr t :
Decision (term_is_bound_fl pr t).
Proof.
hnf.
unfold term_is_bound_fl.
destruct (first_loc_for pr t).
- left; exists x; easy.
- right; intros F; destruct F; congruence.
Defined.
Definition term_is_boundb (pr: Proc)(t: Term) : bool :=
match first_loc_for pr t with
NoneE _ => false
| SomeE _ => true
end.
(** direct defn, without first_loc. We get decidability by proving
equivalence with [term_is_bound_fl] *)
Definition term_is_bound pr t : Prop :=
exists l e, In (Bind (t, l) e) pr.
Lemma term_is_bound_iff pr t :
term_is_bound_fl pr t <->
term_is_bound pr t.
Proof.
unfold term_is_bound_fl, term_is_bound.
split.
- intros. destruct H as [v P].
exists v.
now apply first_loc_for_elim.
- intros.
destruct H as [v [e Q]].
assert (h:= first_loc_for_intro pr t v e Q).
destruct h as [v' [e' [Q1 Q2]]].
exists v'; easy.
Defined.
Global Hint Resolve term_is_bound_iff :core.
Global Instance term_is_bound_dec pr t :
Decision (term_is_bound pr t).
Proof.
apply dec_prop_iff with (term_is_bound_fl pr t).
auto. apply _.
Defined.
(** useful in reasoning about redexes later *)
Lemma term_bound_extend_proc pr ls trm :
term_is_bound pr trm ->
term_is_bound (pr ++ ls) trm.
Proof.
intros H.
unfold term_is_bound in *.
destruct H as [l [e Q]].
exists l; exists e; auto.
Qed.
Theorem bound_then_first_loc_for_def (pr: Proc) (t: Term) :
term_is_bound pr t ->
exists e, In (Bind (t, (first_loc_for_default pr t)) e) pr.
Proof.
intros H.
destruct H as [l [e P]].
assert (h0:= @first_loc_for_intro pr t l e P).
destruct h0 as [lf [ef [P1 P2]]].
assert (h1:= @first_loc_for_def_works pr t lf P2
).
rewrite h1.
now exists ef.
Qed.
Fixpoint terms_bound_in (pr: Proc) : list Term :=
match pr with
nil => nil
| (Bind (t, _) _) :: rest =>
t :: (terms_bound_in rest)
| _ :: rest => terms_bound_in rest
end.
Fixpoint term_for_loc
(pr: Proc) (l: loc ): optionE Term :=
match pr with
[] => NoneE ("failed term_for: " ++ (show l))
| (Bind (t, l') e) :: rest =>
if decide (l = l')
then SomeE t
else term_for_loc rest l
| _ :: rest => term_for_loc rest l
end.
(** ugly defn but easy to show decidable *)
Definition term_exp_in_proc pr (t: Term) (e: Expr): Prop :=
exists (s: Stmt),
In s pr /\
match s with
| Bind (t' , _) e' =>
t=t' /\ e=e'
| _ => False
end.
#[export] Instance term_exp_dec :
forall pr t e , Decision (term_exp_in_proc pr t e).
Proof.
intros.
(* hnf. *)
apply list_exists_dec.
intros.
destruct x;
apply _.
Defined.
(** how to use [term_exp_in_proc] *)
Lemma term_exp_in_proc_elim
pr (t: Term) (e: Expr) :
term_exp_in_proc pr t e ->
exists (l: loc),
In (Bind (t,l) e) pr.
Proof.
intros H.
inv H. destruct x. destruct t0.
destruct H0 as [H1 [H2 H3] ]; subst.
exists l; easy.
all: easy.
Qed.
(** useful in reasoning about redexes later *)
Lemma term_exp_extend_proc pr ls trm exp :
term_exp_in_proc pr trm exp ->
term_exp_in_proc (pr ++ ls) trm exp.
Proof.
intros H.
apply term_exp_in_proc_elim in H.
destruct H as [l Q].
exists (Bind (trm,l) exp); auto.
Qed.
(** Existentially quantify an expression out of a Bind stmt s
*)
Definition ex_expr_bind (t: Term) (l: loc) (s: Stmt) :=
exists e, s = Bind (t,l) e.
Global Instance ex_expr_bind_dec :
forall t l smt, Decision (ex_expr_bind t l smt).
Proof.
intros t l smt.
destruct smt.
shelve.
all: try (right; intros F;
destruct F as [x H]; inv H).
Unshelve.
destruct t0.
destruct (decide (t=t0));
destruct (decide (l=l0)); subst.
left. exists e; easy.
all: right;
intros F; destruct F as [x H]; inv H; congruence.
Defined.
Definition binding_in_proc t l pr :=
exists s, In s pr /\
exists e, s = (Bind (t,l) e).
Global Instance binding_in_proc_dec t l pr :
Decision (binding_in_proc t l pr).
Proof.
hnf; unfold binding_in_proc.
assert (h:= list_exists_dec pr).
apply h.
intros.
apply ex_expr_bind_dec.
Defined.
Equations binding_in_procb
(t: Term) (l: loc) (pr: Proc) : bool :=
binding_in_procb t l [] := false ;
binding_in_procb t l ((Bind (t1, l1) e) :: rest) :=
( (beq t t1) && (beq l l1) )
|| binding_in_procb t l rest ;
binding_in_procb t l (x :: rest) :=
binding_in_procb t l rest .
(** NO WAY this should be this complicated. Revisit (someday)
*)
Lemma reflect_bind t l pr :
reflect (binding_in_proc t l pr)
(binding_in_procb t l pr).
Proof.
intros.
apply iff_reflect.
split.
-
intros.
inv H. destruct H0 as [Hin [e Q]]; subst. clear H.
induction pr as [| a rest IH] ; auto.
destruct a; auto; try easy.
destruct t0; auto; try easy.
-- destruct (beq_reflect t0 t);
destruct (beq_reflect l0 l).
{ subst.
simp binding_in_procb.
do 2 rewrite beq_rfl. now simpl. }
{ (* must have (Bind t l e) in rest *)
assert (a1: In (Bind (t, l) e) rest).
{
(* simp binding_in_procb. *)
inv Hin.
-- inv H. easy.
-- easy.
}
apply IH in a1.
simp binding_in_procb in *.
apply orb_true_intro; now right.
}
{ (* must have (Bind t l e) in rest *)
assert (a1: In (Bind (t, l) e) rest).
{
(* simp binding_in_procb. *)
inv Hin.
-- inv H. easy.
-- easy.
}
apply IH in a1.
simp binding_in_procb in *.
apply orb_true_intro; now right.
}
{
(* must have (Bind t l e) in rest *)
assert (a1: In (Bind (t, l) e) rest).
{
(* simp binding_in_procb. *)
inv Hin.
-- inv H. easy.
-- easy.
}
apply IH in a1.
simp binding_in_procb in *.
apply orb_true_intro; now right.
}
-- inv Hin; try easy;
apply IH in H;
simp binding_in_procb in *.
-- inv Hin; try easy;
apply IH in H;
simp binding_in_procb in *.
-- inv Hin; try easy;
apply IH in H;
simp binding_in_procb in *.
-- inv Hin; try easy;
apply IH in H;
simp binding_in_procb in *.
-- inv Hin; try easy.
apply IH in H.
simp binding_in_procb in *.
-- inv Hin; try easy.
apply IH in H.
simp binding_in_procb in *.
-- inv Hin; try easy.
apply IH in H.
simp binding_in_procb in *.
-
apply FunctionalElimination_binding_in_procb ; intros; auto; try easy.
+ apply orb_prop in H0.
destruct H0 as [H01 | H02].
apply andb_prop in H01.
destruct H01 as [H1 H2].
destruct (beq_reflect t1 t0); subst; auto; subst; try easy;
destruct (beq_reflect l1 l0); auto; subst; try easy.
-- exists (Bind (t0,l0) e).
split.
++ auto.
++ exists e; auto.
-- destruct (beq_reflect l0 l1); auto; try easy. congruence.
-- destruct (beq_reflect t0 t1); auto; try easy. congruence.
-- destruct (beq_reflect l0 l1); auto; try easy. congruence.
-- apply H in H02.
inv H02. destruct H0 as [Hin [e0 Q]]; subst.
exists (Bind (t0,l0) e0).
split. auto. exists e0; auto.
+ apply H in H0;
inv H0; destruct H1 as [Hin [e0 Q]]; subst;
exists (Bind (t0,l0) e0);
split; auto; exists e0; auto.
+ apply H in H0;
inv H0; destruct H1 as [Hin [e0 Q]]; subst;
exists (Bind (t0,l0) e0);
split; auto; exists e0; auto.
+ apply H in H0;
inv H0; destruct H1 as [Hin [e0 Q]]; subst;
exists (Bind (t0,l0) e0);
split; auto; exists e0; auto.
+ apply H in H0;
inv H0; destruct H1 as [Hin [e0 Q]]; subst;
exists (Bind (t0,l0) e0);
split; auto; exists e0; auto.
+ apply H in H0;
inv H0; destruct H1 as [Hin [e0 Q]]; subst;
exists (Bind (t0,l0) e0);
split; auto; exists e0; auto.
+ apply H in H0;
inv H0; destruct H1 as [Hin [e0 Q]]; subst;
exists (Bind (t0,l0) e0);
split; auto; exists e0; auto.
+ apply H in H0;
inv H0; destruct H1 as [Hin [e0 Q]]; subst;
exists (Bind (t0,l0) e0);
split; auto; exists e0; auto.
Qed.
Lemma get_binding t l pr :
binding_in_procb t l pr = true ->
exists e, In (Bind (t,l) e) pr.
Proof.
destruct (reflect_bind t l pr).
inv b.
destruct H as [H1 H2].
destruct H2 as [e Q]; subst.
now exists e. easy.
Qed.
Lemma get_binding' t l pr :
binding_in_procb t l pr <> false ->
exists e, In (Bind (t,l) e) pr.
Proof.
intros.
apply (get_binding t l pr).
destruct (binding_in_procb t l pr) ; easy.
Qed.
Equations binding_in_proc_opt
(t: Term) (l: loc) (pr: Proc) : option Expr :=
binding_in_proc_opt t l [] := None ;
binding_in_proc_opt t l ((Bind (t1, l1) e) :: rest) :=
if ( (Term_beq t t1) && (loc_beq l l1) )
then Some e
else binding_in_proc_opt t l rest ;
binding_in_proc_opt t l (x :: rest) :=
binding_in_proc_opt t l rest .
Definition is_pair_expression_for
(pr: Proc) (t: Term) (e: Expr) : bool :=
match (t,e) with
((Pr t1 t2), (Pair l1 l2) ) =>
(binding_in_procb t1 l1 pr) &&
(binding_in_procb t2 l2 pr)
| _ => false
end.
Definition is_encr_expression_for
(pr: Proc) (t: Term) (e: Expr) : bool :=
match (t,e) with
((En tp tke), (Encr lp lke) ) =>
(binding_in_procb tp lp pr) &&
(binding_in_procb tke lke pr)
| _ => false
end.
(** aka the compile-time-store *)