-
Notifications
You must be signed in to change notification settings - Fork 11
/
Utila.v
1494 lines (1286 loc) · 49.1 KB
/
Utila.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
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
(*
This library contains useful functions for generating Kami
expressions.
*)
Require Import Kami.Syntax Kami.Notations Kami.LibStruct.
Require Import List.
Import Word.Notations.
Require Import Kami.Lib.EclecticLib.
Import ListNotations.
Module EqIndNotations.
Notation "A || B @ X 'by' E"
:= (eq_ind_r (fun X => B) A E) (at level 40, left associativity).
Notation "A || B @ X 'by' <- H"
:= (eq_ind_r (fun X => B) A (eq_sym H)) (at level 40, left associativity).
End EqIndNotations.
Section utila.
Open Scope kami_expr.
Section defs.
Variable ty : Kind -> Type.
Fixpoint tagFrom val T (xs : list T) :=
match xs with
| nil => nil
| y :: ys => (val, y) :: tagFrom (S val) ys
end.
Definition tag := @tagFrom 0.
(* I. Kami Expression Definitions *)
Definition msb
(n m : nat)
(width : Bit n @# ty)
(x : Bit m @# ty)
: Bit m @# ty
:= x >> ($m - width).
Definition lsb
(n m : nat)
(width : Bit n @# ty)
(x : Bit m @# ty)
: Bit m @# ty
:= (x .& ~($$(wones m) << width)).
Definition slice
(n m k : nat)
(offset : Bit n @# ty)
(width : Bit m @# ty)
(x : Bit k @# ty)
: Bit k @# ty
:= ((x >> offset) .& ~($$(wones k) << width)).
Definition utila_opt_pkt
(k : Kind)
(x : k @# ty)
(valid : Bool @# ty)
: Maybe k @# ty
:= STRUCT {
"valid" ::= valid;
"data" ::= x
}.
Definition utila_opt_default
(k : Kind)
(default : k @# ty)
(x : Maybe k @# ty)
: k @# ty
:= ITE (x @% "valid")
(x @% "data")
default.
Definition utila_opt_bind
(j k : Kind)
(x : Maybe j @# ty)
(f : j @# ty -> Maybe k @# ty)
: Maybe k @# ty
:= ITE (x @% "valid")
(f (x @% "data"))
(@Invalid ty k).
Definition utila_all
: list (Bool @# ty) -> Bool @# ty
(* := fold_right (fun x acc => x && acc) ($$true). *)
:= CABool And.
Definition utila_any
: list (Bool @# ty) -> Bool @# ty
(* := fold_right (fun x acc => x || acc) ($$false). *)
:= (@Kor _ Bool).
(*
Note: [f] must only return true for exactly one value in
[xs].
*)
Definition utila_find
(k : Kind)
(f : k @# ty -> Bool @# ty)
(xs : list (k @# ty))
: k @# ty
:= unpack k (Kor (map (fun x => IF f x then pack x else $0) xs)).
(*
Note: exactly one of the packets must be valid.
*)
Definition utila_find_pkt
: forall k : Kind, list (Maybe k @# ty) -> Maybe k @# ty
:= fun k => utila_find (fun x : Maybe k @# ty => x @% "valid").
(*
Note: the key match predicate must never return true for more
than one entry in [entries].
*)
Definition utila_lookup_table
(entry_type : Type)
(entries : list entry_type)
(result_kind : Kind)
(entry_match : entry_type -> Bool @# ty)
(entry_result : entry_type -> result_kind @# ty)
: Maybe result_kind @# ty
:= utila_find_pkt
(map
(fun entry
=> utila_opt_pkt
(entry_result entry)
(entry_match entry))
entries).
(*
Note: the key match predicate must never return true for more
than one entry in [entries].
*)
Definition utila_lookup_table_default
(entry_type : Type)
(entries : list entry_type)
(result_kind : Kind)
(entry_match : entry_type -> Bool @# ty)
(entry_result : entry_type -> result_kind @# ty)
(default : result_kind @# ty)
: result_kind @# ty
:= utila_opt_default
default
(utila_lookup_table
entries
entry_match
entry_result).
(* II. Kami Monadic Definitions *)
Structure utila_monad_type
:= utila_monad {
utila_m
: Kind -> Type;
utila_mbind
: forall (j k : Kind), utila_m j -> (ty j -> utila_m k) -> utila_m k;
utila_munit
: forall k : Kind, k @# ty -> utila_m k;
utila_mite
: forall k : Kind, Bool @# ty -> utila_m k -> utila_m k -> utila_m k
}.
Arguments utila_mbind {u} j k x f.
Arguments utila_munit {u} k x.
Arguments utila_mite {u} k b x y.
Section monad_functions.
Variable monad : utila_monad_type.
Let m := utila_m monad.
Let mbind := @utila_mbind monad.
Let munit := @utila_munit monad.
Let mite := @utila_mite monad.
Definition utila_mopt_pkt
(k : Kind)
(x : k @# ty)
(valid : Bool @# ty)
: m (Maybe k)
:= munit (utila_opt_pkt x valid).
Definition utila_mopt_default
(k : Kind)
(default : k @# ty)
(x_expr : m (Maybe k))
: m k
:= mbind k x_expr
(fun x : ty (Maybe k)
=> mite k
((Var ty (SyntaxKind (Maybe k)) x) @% "valid" : Bool @# ty)
(munit ((Var ty (SyntaxKind (Maybe k)) x) @% "data" : k @# ty))
(munit default)).
Definition utila_mopt_bind
(j k : Kind)
(x_expr : m (Maybe j))
(f : j @# ty -> m (Maybe k))
: m (Maybe k)
:= mbind (Maybe k) x_expr
(fun x : ty (Maybe j)
=> mite (Maybe k)
((Var ty (SyntaxKind (Maybe j)) x) @% "valid" : Bool @# ty)
(f ((Var ty (SyntaxKind (Maybe j)) x) @% "data"))
(munit (@Invalid ty k))).
Definition utila_mfoldr
(j k : Kind)
(f : j @# ty -> k @# ty -> k @# ty)
(init : k @# ty)
: list (m j) -> (m k)
:= fold_right
(fun (x_expr : m j)
(acc_expr : m k)
=> mbind k x_expr
(fun x : ty j
=> mbind k acc_expr
(fun acc : ty k
=> munit
(f (Var ty (SyntaxKind j) x)
(Var ty (SyntaxKind k) acc)))))
(munit init).
Definition utila_mall
: list (m Bool) -> m Bool
:= utila_mfoldr (fun x acc => x && acc) (Const ty true).
Definition utila_many
: list (m Bool) -> m Bool
:= utila_mfoldr (fun x acc => x || acc) (Const ty false).
Definition utila_mfind
(k : Kind)
(f : k @# ty -> Bool @# ty)
(x_exprs : list (m k))
: m k
:= mbind k
(utila_mfoldr
(fun (x : k @# ty) (acc : Bit (size k) @# ty)
=> ((ITE (f x) (pack x) ($0)) .| acc))
($0)
x_exprs)
(fun (y : ty (Bit (size k)))
=> munit (unpack k (Var ty (SyntaxKind (Bit (size k))) y))).
Definition utila_mfind_pkt
(k : Kind)
: list (m (Maybe k)) -> m (Maybe k)
:= utila_mfind
(fun (pkt : Maybe k @# ty)
=> pkt @% "valid").
End monad_functions.
Arguments utila_mopt_pkt {monad}.
Arguments utila_mopt_default {monad}.
Arguments utila_mopt_bind {monad}.
Arguments utila_mfoldr {monad}.
Arguments utila_mall {monad}.
Arguments utila_many {monad}.
Arguments utila_mfind {monad}.
Arguments utila_mfind_pkt {monad}.
(* III. Kami Let Expression Definitions *)
Definition utila_expr_monad
: utila_monad_type
:= utila_monad (LetExprSyntax ty) (fun j k => @LetE ty k j) (@NormExpr ty)
(fun (k : Kind) (b : Bool @# ty) (x_expr y_expr : k ## ty)
=> LETE x : k <- x_expr;
LETE y : k <- y_expr;
RetE (ITE b (#x) (#y))).
Definition utila_expr_opt_pkt := @utila_mopt_pkt utila_expr_monad.
Definition utila_expr_opt_default := @utila_mopt_default utila_expr_monad.
Definition utila_expr_opt_bind := @utila_mopt_bind utila_expr_monad.
Definition utila_expr_foldr := @utila_mfoldr utila_expr_monad.
Definition utila_expr_all := @utila_mall utila_expr_monad.
Definition utila_expr_any := @utila_many utila_expr_monad.
(*
Accepts a Kami predicate [f] and a list of Kami let expressions
that represent values, and returns a Kami let expression that
outputs the value that satisfies f.
Note: [f] must only return true for exactly one value in
[xs_exprs].
*)
Definition utila_expr_find
(k : Kind)
(f : k @# ty -> Bool @# ty)
(xs_exprs : list (k ## ty))
: k ## ty
:= LETE y
: Bit (size k)
<- (utila_expr_foldr
(fun x acc => ((ITE (f x) (pack x) ($0)) .| acc))
($0)
xs_exprs);
RetE (unpack k (#y)).
Arguments utila_expr_find {k} f xs_exprs.
(*
Accepts a list of Maybe packets and returns the packet whose
valid flag equals true.
Note: exactly one of the packets must be valid.
*)
Definition utila_expr_find_pkt
(k : Kind)
(pkt_exprs : list (Maybe k ## ty))
: Maybe k ## ty
:= utila_expr_find
(fun (pkt : Maybe k @# ty)
=> pkt @% "valid")
pkt_exprs.
(*
Generates a lookup table containing entries of type
[result_kind].
Note: the key match predicate must never return true for more
than one entry in [entries].
*)
Definition utila_expr_lookup_table
(entry_type : Type)
(entries : list entry_type)
(result_kind : Kind)
(entry_match : entry_type -> Bool ## ty)
(entry_result : entry_type -> result_kind ## ty)
: Maybe result_kind ## ty
:= utila_expr_find_pkt
(map
(fun entry : entry_type
=> LETE result
: result_kind
<- entry_result entry;
LETE matched
: Bool
<- entry_match entry;
utila_expr_opt_pkt #result #matched)
entries).
(*
Generates a lookup table containing entries of type
[result_kind]. Returns a default value for entries that do
not exist.
Note: the key match predicate must never return true for more
than one entry in [entries].
*)
Definition utila_expr_lookup_table_default
(entry_type : Type)
(entries : list entry_type)
(result_kind : Kind)
(entry_match : entry_type -> Bool ## ty)
(entry_result : entry_type -> result_kind ## ty)
(default : result_kind @# ty)
: result_kind ## ty
:= utila_expr_opt_default
default
(utila_expr_lookup_table
entries
entry_match
entry_result).
(* IV. Kami Action Definitions *)
Open Scope kami_action.
Definition utila_act_monad
: utila_monad_type
:= utila_monad (@ActionT ty) (fun j k => @LetAction ty k j) (@Return ty)
(fun k b (x y : ActionT ty k)
=> If b
then x
else y
as result;
Ret #result).
Definition utila_acts_opt_pkt := @utila_mopt_pkt utila_act_monad.
Definition utila_acts_opt_default := @utila_mopt_default utila_act_monad.
Definition utila_acts_opt_bind := @utila_mopt_bind utila_act_monad.
Definition utila_acts_foldr := @utila_mfoldr utila_act_monad.
Definition utila_acts_all
(xs : list (ActionT ty Bool))
: ActionT ty Bool
:= GatherActions xs as ys;
Ret (CABool And ys).
Definition utila_acts_any
(xs : list (ActionT ty Bool))
: ActionT ty Bool
:= GatherActions xs as ys;
Ret ((@Kor _ Bool) ys).
Definition utila_acts_find
(k : Kind)
(f : k @# ty -> Bool @# ty)
(xs : list (ActionT ty k))
: ActionT ty k
:= GatherActions xs as ys;
Ret (utila_find f ys).
Definition utila_acts_find_pkt
(k : Kind)
(xs : list (ActionT ty (Maybe k)))
: ActionT ty (Maybe k)
:= GatherActions xs as ys;
Ret (utila_find_pkt ys).
Close Scope kami_action.
End defs.
Arguments utila_mopt_pkt {ty} {monad} {k}.
Arguments utila_mopt_default {ty} {monad} {k}.
Arguments utila_mopt_bind {ty} {monad} {j} {k}.
Arguments utila_mfoldr {ty} {monad} {j} {k}.
Arguments utila_mall {ty} {monad}.
Arguments utila_many {ty} {monad}.
Arguments utila_mfind {ty} {monad} {k}.
Arguments utila_mfind_pkt {ty} {monad} {k}.
(* V. Correctness Proofs *)
Section ver.
Local Notation "{{ X }}" := (evalExpr X).
Local Notation "X ==> Y" := (evalExpr X = Y) (at level 75).
Local Notation "==> Y" := (fun x => evalExpr x = Y) (at level 75).
Let utila_is_true (x : Bool @# type) := x ==> true.
Lemma fold_left_andb_forall'
: forall (xs : list (Bool @# type)) a,
fold_left andb (map (@evalExpr _) xs) a = true <->
Forall utila_is_true xs /\ a = true.
Proof.
induction xs; simpl; auto; split; intros; auto.
- tauto.
- rewrite IHxs in H.
rewrite andb_true_iff in H.
split; try tauto.
constructor; simpl; tauto.
- dest.
inv H.
unfold utila_is_true in *; simpl in *.
pose proof (conj H4 H3).
rewrite <- IHxs in H.
auto.
Qed.
Theorem fold_left_andb_forall
: forall xs : list (Bool @# type),
fold_left andb (map (@evalExpr _) xs) true = true <->
Forall utila_is_true xs.
Proof.
intros.
rewrite fold_left_andb_forall'.
tauto.
Qed.
Theorem utila_all_correct
: forall xs : list (Bool @# type),
utila_all xs ==> true <-> Forall utila_is_true xs.
Proof.
apply fold_left_andb_forall.
Qed.
Theorem fold_left_andb_forall_false'
: forall (xs : list (Bool @# type)) a,
fold_left andb (map (@evalExpr _) xs) a = false <->
Exists (fun x : Expr type (SyntaxKind Bool)
=> evalExpr x = false) xs \/ a = false.
Proof.
induction xs; simpl; auto; intros; split; try tauto.
- intros; auto.
destruct H; auto.
inv H.
- rewrite IHxs.
intros.
rewrite andb_false_iff in H.
destruct H.
+ left.
right; auto.
+ destruct H.
* auto.
* left.
left.
auto.
- intros.
rewrite IHxs.
rewrite andb_false_iff.
destruct H.
+ inv H; auto.
+ auto.
Qed.
Theorem fold_left_andb_forall_false
: forall xs : list (Bool @# type),
fold_left andb (map (@evalExpr _) xs) true = false <->
Exists (fun x : Expr type (SyntaxKind Bool)
=> evalExpr x = false) xs.
Proof.
intros.
rewrite fold_left_andb_forall_false'.
split; intros.
- destruct H; congruence.
- auto.
Qed.
Theorem utila_all_correct_false
: forall xs : list (Bool @# type),
utila_all xs ==> false <->
Exists (fun x : Expr type (SyntaxKind Bool)
=> evalExpr x = false) xs.
Proof.
apply fold_left_andb_forall_false.
Qed.
Theorem fold_left_orb_exists'
: forall (xs : list (Bool @# type)) a,
fold_left orb (map (@evalExpr _) xs) a = true <->
Exists utila_is_true xs \/ a = true.
Proof.
induction xs; simpl; auto; split; intros; try discriminate.
- auto.
- destruct H; auto.
inv H.
- rewrite IHxs in H.
rewrite orb_true_iff in H.
destruct H.
+ left.
right.
auto.
+ destruct H; auto.
- assert (sth: Exists utila_is_true xs \/ (a0||evalExpr a)%bool = true). {
destruct H.
- inv H.
+ right.
rewrite orb_true_iff.
auto.
+ auto.
- right.
rewrite orb_true_iff.
auto.
}
rewrite <- IHxs in sth.
auto.
Qed.
Theorem fold_left_orb_exists
: forall xs : list (Bool @# type),
fold_left orb (map (@evalExpr _) xs) false = true <->
Exists utila_is_true xs.
Proof.
intros.
rewrite fold_left_orb_exists'.
split; intros; auto.
destruct H; congruence.
Qed.
Theorem utila_any_correct
: forall xs : list (Bool @# type),
utila_any xs ==> true <-> Exists utila_is_true xs.
Proof.
apply fold_left_orb_exists.
Qed.
Theorem fold_left_orb_exists_false'
: forall (xs : list (Bool @# type)) a,
fold_left orb (map (@evalExpr _) xs) a = false <->
Forall (fun x : Expr type (SyntaxKind Bool)
=> evalExpr x = false) xs /\ a = false.
Proof.
induction xs; simpl; split; auto; intros.
- inv H; auto.
- rewrite IHxs in H.
rewrite orb_false_iff in H.
split; try tauto.
constructor; tauto.
- dest.
inv H.
rewrite IHxs.
rewrite orb_false_iff.
repeat split; auto.
Qed.
Theorem fold_left_orb_exists_false
: forall xs : list (Bool @# type),
fold_left orb (map (@evalExpr _) xs) false = false <->
Forall (fun x : Expr type (SyntaxKind Bool)
=> evalExpr x = false) xs.
Proof.
intros.
rewrite fold_left_orb_exists_false'.
split; intros; dest; auto.
Qed.
Lemma utila_any_correct_false:
forall xs : list (Expr type (SyntaxKind Bool)),
evalExpr (utila_any xs) = false <->
Forall (fun x : Expr type (SyntaxKind Bool)
=> evalExpr x = false) xs.
Proof.
apply fold_left_orb_exists_false.
Qed.
End ver.
(* VI. Denotational semantics for monadic expressions. *)
Structure utila_sem_type
:= utila_sem {
utila_sem_m
: utila_monad_type type;
utila_sem_interp
: forall k : Kind, utila_m utila_sem_m k -> type k;
(*
[[mbind x f]] = [[ f [[x]] ]]
*)
utila_sem_bind_correct
: forall
(j k : Kind)
(x : utila_m utila_sem_m j)
(f : type j -> utila_m utila_sem_m k),
(utila_sem_interp k
(utila_mbind utila_sem_m j k x f)) =
(utila_sem_interp k
(f (utila_sem_interp j x)));
(*
[[munit x]] = {{x}}
*)
utila_sem_unit_correct
: forall (k : Kind) (x : k @# type),
utila_sem_interp k (utila_munit (utila_sem_m) x) =
evalExpr x;
(*
[[ mfoldr f init [] ]] = {{init}}
*)
utila_sem_foldr_nil_correct
: forall (j k : Kind)
(f : j @# type -> k @# type -> k @# type)
(init : k @# type),
(utila_sem_interp k
(utila_mfoldr f init nil) =
evalExpr init);
(*
[[ mfoldr f init (x0 :: xs) ]] = {{ f #[[x0]] #[[mfoldr f init xs]] }}
*)
utila_sem_foldr_cons_correct
: forall (j k : Kind)
(f : j @# type -> k @# type -> k @# type)
(init : k @# type)
(x0 : utila_m utila_sem_m j)
(xs : list (utila_m utila_sem_m j)),
(utila_sem_interp k
(utila_mfoldr f init (x0 :: xs)) =
(evalExpr
(f
(Var type (SyntaxKind j)
(utila_sem_interp j x0))
(Var type (SyntaxKind k)
(utila_sem_interp k
(utila_mfoldr f init xs))))))
}.
Arguments utila_sem_interp {u} {k} x.
Arguments utila_sem_bind_correct {u} {j} {k} x f.
Arguments utila_sem_unit_correct {u} {k} x.
Arguments utila_sem_foldr_nil_correct {u} {j} {k}.
Arguments utila_sem_foldr_cons_correct {u} {j} {k}.
Section monad_ver.
Import EqIndNotations.
Variable sem : utila_sem_type.
Let monad : utila_monad_type type := utila_sem_m sem.
Let m := utila_m monad.
Let mbind := utila_mbind monad.
Let munit := utila_munit monad.
Local Notation "{{ X }}" := (evalExpr X).
Local Notation "[[ X ]]" := (@utila_sem_interp sem _ X).
Local Notation "#{{ X }}" := (Var type (SyntaxKind _) {{X}}).
Local Notation "#[[ X ]]" := (Var type (SyntaxKind _) [[X]]).
Hint Rewrite
(@utila_sem_bind_correct sem)
(@utila_sem_unit_correct sem)
(@utila_sem_foldr_cons_correct sem)
(@utila_sem_unit_correct sem)
: utila_sem_rewrite_db.
Let utila_is_true (x : m Bool)
: Prop
:= [[x]] = true.
Lemma utila_mall_nil
: [[utila_mall ([] : list (m Bool))]] = true.
Proof utila_sem_foldr_nil_correct
(fun x acc => x && acc)
(Const type true).
Lemma utila_mall_cons
: forall (x0 : m Bool) (xs : list (m Bool)), [[utila_mall (x0 :: xs)]] = andb [[x0]] [[utila_mall xs]].
Proof utila_sem_foldr_cons_correct
(fun x acc => x && acc)
(Const type true).
Theorem utila_mall_correct
: forall xs : list (m Bool),
[[utila_mall xs]] = true <-> Forall utila_is_true xs.
Proof.
intro.
split.
- induction xs.
+ intro; exact (Forall_nil utila_is_true).
+ intro H; assert (H0 : [[a]] = true /\ [[utila_mall xs]] = true).
apply (@andb_prop [[a]] [[utila_mall xs]]).
rewrite <- (utila_mall_cons a xs).
assumption.
apply (Forall_cons a).
apply H0.
apply IHxs; apply H0.
- apply (Forall_ind (fun ys => [[utila_mall ys]] = true)).
+ apply utila_mall_nil.
+ intros y0 ys H H0 F.
rewrite utila_mall_cons.
apply andb_true_intro.
auto.
Qed.
Lemma utila_many_nil
: [[utila_many ([] : list (m Bool)) ]] = false.
Proof utila_sem_foldr_nil_correct
(fun x acc => (@Kor _ Bool) [x; acc])
(Const type false).
Lemma utila_many_cons
: forall (x0 : m Bool) (xs : list (m Bool)), [[utila_many (x0 :: xs)]] = orb [[x0]] [[utila_many xs]].
Proof utila_sem_foldr_cons_correct
(fun x acc => (@Kor _ Bool) [x; acc])
(Const type false).
Theorem utila_many_correct
: forall xs : list (m Bool),
[[utila_many xs]] = true <-> Exists utila_is_true xs.
Proof
fun xs
=> conj
(list_ind
(fun ys => [[utila_many ys]] = true -> Exists utila_is_true ys)
(fun H : [[utila_many [] ]] = true
=> let H0
: false = true
:= H || X = true @X by <- utila_many_nil in
False_ind _ (diff_false_true H0))
(fun y0 ys
(F : [[utila_many ys]] = true -> Exists utila_is_true ys)
(H : [[utila_many (y0 :: ys)]] = true)
=> let H0
: [[y0]] = true \/ [[utila_many ys]] = true
:= orb_prop [[y0]] [[utila_many ys]]
(eq_sym
(utila_many_cons y0 ys
|| X = _ @X by <- H)) in
match H0 with
| or_introl H1
=> Exists_cons_hd utila_is_true y0 ys H1
| or_intror H1
=> Exists_cons_tl y0 (F H1)
end)
xs)
(@Exists_ind
(m Bool)
utila_is_true
(fun ys => [[utila_many ys]] = true)
(fun y0 ys
(H : [[y0]] = true)
=> orb_true_l [[utila_many ys]]
|| orb X [[utila_many ys]] = true @X by H
|| X = true @X by utila_many_cons y0 ys)
(fun y0 ys
(H : Exists utila_is_true ys)
(F : [[utila_many ys]] = true)
=> orb_true_r [[y0]]
|| orb [[y0]] X = true @X by F
|| X = true @X by utila_many_cons y0 ys)
xs).
Definition utila_null (k : Kind)
: k @# type
:= unpack k (Var type (SyntaxKind (Bit (size k))) (natToWord (size k) 0)).
Lemma utila_mfind_nil
: forall (k : Kind)
(f : k @# type -> Bool @# type),
[[utila_mfind f ([] : list (m k))]] = {{utila_null k}}.
Proof
fun k f
=> eq_refl {{utila_null k}}
|| X = {{utila_null k}}
@X by utila_sem_unit_correct (unpack k (Var type (SyntaxKind (Bit (size k))) (natToWord (size k) 0)))
|| [[munit (unpack k (Var type (SyntaxKind (Bit (size k))) X))]] = {{utila_null k}}
@X by utila_sem_foldr_nil_correct
(fun x acc => (ITE (f x) (pack x) ($0) .| acc))
($0)
|| X = {{utila_null k}}
@X by utila_sem_bind_correct
(utila_mfoldr
(fun x acc => (ITE (f x) (pack x) ($0) .| acc))
($0)
[])
(fun y => munit (unpack k (Var type (SyntaxKind (Bit (size k))) y))).
Lemma utila_mfind_tl
: forall (k : Kind)
(f : k @# type -> Bool @# type)
(x0 : m k)
(xs : list (m k)),
{{f #[[x0]]}} = false ->
[[utila_mfind f (x0 :: xs)]] = [[utila_mfind f xs]].
Proof.
intros.
unfold utila_mfind.
autorewrite with utila_sem_rewrite_db.
simpl.
rewrite H.
simpl.
repeat (rewrite wor_wzero).
reflexivity.
Qed.
End monad_ver.
Section expr_ver.
Import EqIndNotations.
Local Notation "{{ X }}" := (evalExpr X).
Local Notation "[[ X ]]" := (evalLetExpr X).
Local Notation "#[[ X ]]" := (Var type (SyntaxKind _) [[X]]) (only parsing) : kami_expr_scope.
Local Notation "X ==> Y" := (evalLetExpr X = Y) (at level 75).
Local Notation "==> Y" := (fun x => evalLetExpr x = Y) (at level 75).
Let utila_is_true (x : Bool ## type) := x ==> true.
Let utila_expr_bind (j k : Kind) (x : j ## type) (f : type j -> k ## type)
: k ## type
:= @LetE type k j x f.
Lemma utila_expr_bind_correct
: forall
(j k : Kind)
(x : j ## type)
(f : type j -> k ## type),
[[utila_expr_bind x f]] = [[f [[x]] ]].
Proof fun j k x f => (eq_refl [[utila_expr_bind x f]]).
Lemma utila_expr_unit_correct
: forall (k : Kind) (x : k @# type), [[RetE x]] = {{x}}.
Proof
fun k x => eq_refl.
Theorem utila_expr_foldr_correct_nil
: forall (j k : Kind) (f : j @# type -> k @# type -> k @# type) (init : k @# type),
utila_expr_foldr f init nil ==> {{init}}.
Proof
fun j k f init
=> eq_refl ({{init}}).
Theorem utila_expr_foldr_correct_cons
: forall (j k : Kind)
(f : j @# type -> k @# type -> k @# type)
(init : k @# type)
(x0 : j ## type) (xs : list (j ## type)),
[[utila_expr_foldr f init (x0 :: xs)]] =
{{ f (Var type (SyntaxKind j) [[x0]])
(Var type (SyntaxKind k) [[utila_expr_foldr f init xs]]) }}.
Proof
fun (j k : Kind)
(f : j @# type -> k @# type -> k @# type)
(init : k @# type)
(x0 : j ## type)
(xs : list (j ## type))
=> eq_refl.
Definition utila_expr_sem
: utila_sem_type
:= utila_sem
(utila_expr_monad type)
evalLetExpr
utila_expr_bind_correct
utila_expr_unit_correct
utila_expr_foldr_correct_nil
utila_expr_foldr_correct_cons.
Theorem utila_expr_all_correct
: forall xs : list (Bool ## type),
utila_expr_all xs ==> true <-> Forall utila_is_true xs.
Proof utila_mall_correct utila_expr_sem.
Theorem utila_expr_any_correct
: forall xs : list (Bool ## type),
utila_expr_any xs ==> true <-> Exists utila_is_true xs.
Proof utila_many_correct utila_expr_sem.
Lemma utila_ite_l
: forall (k : Kind) (x y : k @# type) (p : Bool @# type),
{{p}} = true ->
{{ITE p x y}} = {{x}}.
Proof
fun k x y p H
=> eq_ind
true
(fun q : bool => (if q then {{x}} else {{y}}) = {{x}})
(eq_refl {{x}})
{{p}}
(eq_sym H).
Lemma utila_ite_r
: forall (k : Kind) (x y : k @# type) (p : Bool @# type),
{{p}} = false ->
{{ITE p x y}} = {{y}}.
Proof
fun k x y p H
=> eq_ind
false
(fun q : bool => (if q then {{x}} else {{y}}) = {{y}})
(eq_refl {{y}})
{{p}}
(eq_sym H).
(*
The following section proves that the utila_expr_find function
is correct. To prove, this result we make three four intuitive