forked from mit-plv/fiat-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Language.v
1782 lines (1688 loc) · 92.3 KB
/
Language.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
Require Import Coq.ZArith.ZArith.
Require Import Coq.MSets.MSetPositive.
Require Import Coq.FSets.FMapPositive.
Require Import Coq.micromega.Lia.
Require Import Coq.Strings.String.
Require Import Coq.Strings.Ascii.
Require Import Coq.Bool.Bool.
Require Import Coq.Structures.Orders.
Require Import Coq.Structures.OrdersEx.
Require Import Coq.MSets.MSetInterface.
Require Import Coq.MSets.MSetPositive.
Require Import Coq.Strings.HexString.
Require Import Crypto.Util.ListUtil Coq.Lists.List.
Require Import Crypto.Util.Sigma.
Require Import Crypto.Util.Prod.
Require Crypto.Util.Strings.String.
Require Import Crypto.Util.Structures.Equalities.Sum.
Require Import Crypto.Util.Structures.Equalities.Iso.
Require Import Crypto.Util.Structures.Orders.Sum.
Require Import Crypto.Util.Structures.Orders.Iso.
Require Import Crypto.Util.MSets.MSetIso.
Require Import Crypto.Util.MSets.MSetSum.
Require Import Crypto.Util.Strings.Decimal.
Require Import Crypto.Util.Strings.Show.
Require Import Crypto.Util.Strings.NamingConventions.
Require Import Crypto.Util.ZRange.
Require Import Crypto.Util.ZRange.Operations.
Require Import Crypto.Util.ZRange.Show.
Require Import Crypto.Util.Option.
Require Import Crypto.Util.OptionList.
Require Import Crypto.Util.MSets.MSetPositive.Show.
Require Import Crypto.Util.MSets.Show.
Require Import Crypto.Util.Level.
Require Import Rewriter.Language.Language.
Require Import Crypto.Language.API.
Require Import Crypto.AbstractInterpretation.ZRange.
Require Import Crypto.Util.Sum.
Require Import Crypto.Util.Bool.Equality.
Require Import Crypto.Util.Notations.
Import Coq.Lists.List ListNotations. Local Open Scope zrange_scope. Local Open Scope Z_scope.
Module Compilers.
Local Set Boolean Equality Schemes.
Local Set Decidable Equality Schemes.
Export Language.Language.Compilers.
Export Language.API.Compilers.
Export AbstractInterpretation.ZRange.Compilers.
Import invert_expr.
Import Compilers.API.
Local Notation tZ := (base.type.type_base base.type.Z).
Module Export Options.
Module PHOAS.
(** Print casts in PHOAS? *)
Class with_casts := with_castsv : bool.
#[global] Typeclasses Opaque with_casts.
(** Print all casts in PHOAS? *)
Class with_all_casts := with_all_castsv : bool.
#[global] Typeclasses Opaque with_all_casts.
Definition default_with_casts : with_casts := true.
Definition default_with_all_casts : with_all_casts := false.
End PHOAS.
#[global] Typeclasses Opaque PHOAS.with_casts PHOAS.with_all_casts.
#[global] Existing Instances PHOAS.default_with_casts PHOAS.default_with_all_casts.
(** How to relax zranges *)
Class relax_zrange_opt := relax_zrange : zrange -> zrange.
#[global]
Typeclasses Opaque relax_zrange_opt.
(** What's the package name? *)
Class package_name_opt := internal_package_name : option string.
#[global]
Typeclasses Opaque package_name_opt.
(** What's the class name? *)
Class class_name_opt := internal_class_name : option string.
#[global]
Typeclasses Opaque class_name_opt.
(** Should we emit typedefs or not? *)
Class skip_typedefs_opt := skip_typedefs : bool.
#[global]
Typeclasses Opaque skip_typedefs_opt.
(** Which adc/sbb bitwidth-split-carries should be relaxed to bitwidth *)
Class relax_adc_sbb_return_carry_to_bitwidth_opt := relax_adc_sbb_return_carry_to_bitwidth : list Z.
#[global]
Typeclasses Opaque relax_adc_sbb_return_carry_to_bitwidth_opt.
(** Do language-specific cast adjustment *)
Class language_specific_cast_adjustment_opt := language_specific_cast_adjustment : bool.
#[global]
Typeclasses Opaque language_specific_cast_adjustment_opt.
Class language_naming_conventions_opt :=
{ public_function_naming_convention : option capitalization_convention
; private_function_naming_convention : option capitalization_convention
; public_type_naming_convention : option capitalization_convention
; private_type_naming_convention : option capitalization_convention
; variable_naming_convention : option capitalization_convention
; package_naming_convention : option capitalization_convention
; class_naming_convention : option capitalization_convention
}.
Definition default_language_naming_conventions : language_naming_conventions_opt
:= {| public_function_naming_convention := None
; private_function_naming_convention := None
; public_type_naming_convention := None
; private_type_naming_convention := None
; variable_naming_convention := None
; package_naming_convention := None
; class_naming_convention := None
|}.
Definition convert_to_naming_convention (convention : option capitalization_convention) (s : string) : string
:= match convention with
| Some convention => convert_case snake_case convention s
| None => s
end.
Definition package_name {pkg_name : package_name_opt} {language_naming_conventions : language_naming_conventions_opt} (prefix : string) : string
:= match pkg_name with
| Some name => name
| None => convert_to_naming_convention
package_naming_convention
(if String.endswith "_" prefix then substring 0 (String.length prefix - 1) prefix else prefix)
end.
Definition class_name {cls_name : class_name_opt} {language_naming_conventions : language_naming_conventions_opt} (prefix : string) : string
:= match cls_name with
| Some name => name
| None => convert_to_naming_convention
class_naming_convention
(if String.endswith "_" prefix then substring 0 (String.length prefix - 1) prefix else prefix)
end.
Definition default_text_before_function_name : string := "The function ".
Definition default_text_before_type_name : string := "The type ".
Class documentation_options_opt :=
{
(** Text to insert before the function name *)
text_before_function_name_opt : option string;
text_before_function_name : string := Option.value text_before_function_name_opt default_text_before_function_name;
(** Text to insert before the type name *)
text_before_type_name_opt : option string;
text_before_type_name : string := Option.value text_before_type_name_opt default_text_before_type_name;
(** Stick an extra newline before the package declaration *)
newline_before_package_declaration : bool;
(** Stick a newline between the "Bounds:" and the bounds of typedefs *)
newline_in_typedef_bounds : bool;
}.
Definition default_documentation_options : documentation_options_opt
:= {| text_before_function_name_opt := None
; text_before_type_name_opt := None
; newline_before_package_declaration := false
; newline_in_typedef_bounds := false
|}.
Class output_options_opt :=
{ #[global] skip_typedefs_ :: skip_typedefs_opt
; #[global] relax_adc_sbb_return_carry_to_bitwidth_ :: relax_adc_sbb_return_carry_to_bitwidth_opt
; #[global] language_specific_cast_adjustment_ :: language_specific_cast_adjustment_opt
}.
Definition default_output_options : output_options_opt
:= {| skip_typedefs_ := true
; relax_adc_sbb_return_carry_to_bitwidth_ := []
; language_specific_cast_adjustment_ := true
|}.
End Options.
Module ToString.
Local Open Scope string_scope.
Local Open Scope Z_scope.
Module Import ZRange.
Module Export type.
Module Export base.
Fixpoint show_lvl_interp {t} : ShowLevel (ZRange.type.base.interp t)
:= match t return ShowLevel (ZRange.type.base.interp t) with
| base.type.unit => @show_lvl unit _
| base.type.type_base base.type.Z => @show_lvl zrange _
| base.type.type_base base.type.positive => @show_lvl positive _
| base.type.type_base base.type.bool => @show_lvl bool _
| base.type.type_base base.type.nat => @show_lvl nat _
| base.type.type_base base.type.zrange => @show_lvl zrange _
| base.type.type_base base.type.string => @show_lvl string _
| base.type.prod A B => @show_lvl (ZRange.type.base.interp A * ZRange.type.base.interp B) _
| base.type.list A => @show_lvl (list (ZRange.type.base.interp A)) _
| base.type.option A => @show_lvl (option (ZRange.type.base.interp A)) _
end%string.
Global Existing Instance show_lvl_interp.
Global Instance show_interp {t} : Show (ZRange.type.base.interp t) := show_lvl_interp.
Module Export option.
Fixpoint show_lvl_interp {t} : ShowLevel (ZRange.type.base.option.interp t)
:= match t return ShowLevel (ZRange.type.base.option.interp t) with
| base.type.unit => @show_lvl unit _
| base.type.type_base _ as t => @show_lvl (option (ZRange.type.base.interp t)) _
| base.type.prod A B => @show_lvl (ZRange.type.base.option.interp A * ZRange.type.base.option.interp B) _
| base.type.list A => @show_lvl (option (list (ZRange.type.base.option.interp A))) _
| base.type.option A => @show_lvl (option (option (ZRange.type.base.option.interp A))) _
end.
Global Existing Instance show_lvl_interp.
Global Instance show_interp {t} : Show (ZRange.type.base.option.interp t) := show_lvl_interp.
End option.
End base.
Module option.
Global Instance show_lvl_interp {t} : ShowLevel (ZRange.type.option.interp t)
:= match t return ShowLevel (ZRange.type.option.interp t) with
| type.base t => @show_lvl (ZRange.type.base.option.interp t) _
| type.arrow s d => fun _ _ => "λ"
end.
Global Instance show_interp {t} : Show (ZRange.type.option.interp t) := show_lvl_interp.
End option.
End type.
End ZRange.
Module PHOAS.
Module type.
Module base.
Global Instance show_base : Show base.type.base
:= fun t => match t with
| base.type.Z => "ℤ"
| base.type.positive => "ℤ⁺"
| base.type.bool => "𝔹"
| base.type.nat => "ℕ"
| base.type.zrange => "zrange"
| base.type.string => "string"
end.
Global Instance show_lvl_base : ShowLevel base.type.base := show_base.
Global Instance show_lvl_type : ShowLevel base.type
:= fix show_lvl_type (t : base.type) : Level.Level -> string
:= match t with
| base.type.unit => fun _ => "()"
| base.type.type_base t => show_lvl t
| base.type.prod A B => show_lvl_binop
mul_assoc mul_lvl
(show_lvl_type A) " * " (show_lvl_type B)
| base.type.list A => fun _ => "[" ++ show_lvl_type A term_lvl ++ "]"
| base.type.option A
=> show_lvl_app
(fun 'tt => "option") (show_lvl_type A)
end.
Global Instance show_type : Show base.type := show_lvl_type.
Global Instance show_lvl_base_interp {t} : ShowLevel (base.base_interp t)
:= match t with
| base.type.Z => @show_lvl Z _
| base.type.bool => @show_lvl bool _
| base.type.positive => @show_lvl positive _
| base.type.nat => @show_lvl nat _
| base.type.zrange => @show_lvl zrange _
| base.type.string => @show_lvl string _
end.
Global Instance show_base_interp {t} : Show (base.base_interp t) := show_lvl_base_interp.
Fixpoint show_lvl_interp {t} : ShowLevel (base.interp t)
:= match t with
| base.type.unit => @show_lvl unit _
| base.type.type_base t => @show_lvl (base.base_interp t) _
| base.type.prod A B => @show_lvl (base.interp A * base.interp B) _
| base.type.list A => @show_lvl (list (base.interp A)) _
| base.type.option A => @show_lvl (option (base.interp A)) _
end.
Global Existing Instance show_lvl_interp.
Global Instance show_interp {t} : Show (base.interp t) := show_lvl_interp.
Global Instance show_lvl : ShowLevel base.type := show_lvl_type.
Global Instance show : Show base.type := show_type.
End base.
Fixpoint show_lvl_for_each_lhs_of_arrow {base_type} {f : type.type base_type -> Type} {show_f : forall t, ShowLevel (f t)} {t : type.type base_type} {struct t} : ShowLevel (type.for_each_lhs_of_arrow f t)
:= match t return ShowLevel (type.for_each_lhs_of_arrow f t) with
| type.base t => @show_lvl unit _
| type.arrow s d
=> Option.value
(match d with
| type.base _ => Some (fun '(x, _) => @show_lvl (f s) _ x)
| type.arrow _ _ => None
end)
(@show_lvl (f s * type.for_each_lhs_of_arrow f d) _)
end.
Global Existing Instance show_lvl_for_each_lhs_of_arrow.
Definition show_for_each_lhs_of_arrow {base_type} {f : type.type base_type -> Type} {show_f : forall t, Show (f t)} {t : type.type base_type} : Show (type.for_each_lhs_of_arrow f t)
:= let _ := fun t => @ShowLevel_of_Show (f t) in
show_lvl_for_each_lhs_of_arrow.
Global Instance show_lvl_type : forall {base_type} {S : ShowLevel base_type}, ShowLevel (type.type base_type)
:= fix show_lvl_type {base_type} {S : ShowLevel base_type} (t : type.type base_type) : Level.Level -> string
:= match t with
| type.base t => show_lvl t
| type.arrow s d
=> show_lvl_binop impl_assoc impl_lvl (show_lvl_type s) " → " (show_lvl_type d)
end.
Global Instance show_lvl {base_type} {S : ShowLevel base_type} : ShowLevel (type.type base_type) := show_lvl_type.
Global Instance show_type {base_type} {S : Show base_type} : Show (type.type base_type)
:= let _ := @ShowLevel_of_Show base_type in
show_lvl_type.
Global Instance show {base_type} {S : Show base_type} : Show (type.type base_type) := show_type.
End type.
Definition bitwidth_to_string (v : Z) : string
:= (if v =? 2^Z.log2 v then "2^" ++ Decimal.Z.to_string (Z.log2 v) else HexString.of_Z v).
Definition show_range_or_ctype : Show zrange
:= fun v
=> if (v.(lower) =? 0) && (v.(upper) =? 2^(Z.log2 (v.(upper) + 1)) - 1)
then ("uint" ++ Decimal.Z.to_string (Z.log2 (v.(upper) + 1)) ++ "_t")%string
else let lg2 := 1 + Z.log2 (-v.(lower)) in
if (v.(lower) =? -2^(lg2-1)) && (v.(upper) =? 2^(lg2-1)-1)
then ("int" ++ Decimal.Z.to_string lg2 ++ "_t")%string
else show v.
Definition show_lvl_compact_Z : ShowLevel Z
:= fun v
=> let is_neg := v <? 0 in
(if is_neg then show_lvl_unop opp_lvl "-" else show_lvl)
(let v := Z.abs v in
if v <=? 2^8
then Decimal.show_lvl_Z v
else if v =? 2^(Z.log2 v)
then show_lvl_binop pow_assoc pow_lvl 2 "^" (Decimal.show_lvl_Z (Z.log2 v))
else if v =? 2^(Z.log2_up v) - 1
then show_lvl_binop
sub_assoc sub_lvl
(show_lvl_binop pow_assoc pow_lvl 2 "^" (Decimal.show_lvl_Z (Z.log2_up v))) "-" 1
else Hex.show_lvl_Z v).
Definition show_compact_Z : Show Z := show_lvl_compact_Z.
Fixpoint make_castb {t} : ZRange.type.base.option.interp t -> option string
:= match t return ZRange.type.base.option.interp t -> option string with
| base.type.type_base base.type.Z => option_map show_range_or_ctype
| base.type.prod A B
=> fun '(r1, r2)
=> match @make_castb A r1, @make_castb B r2 with
| Some c1, Some c2 => Some (c1 ++ ", " ++ c2)
| None, Some c => Some ("??, " ++ c)
| Some c, None => Some (c ++ ", ??")
| None, None => None
end
| base.type.list A
=> fun r
=> match r with
| None => None
| Some nil => Some "void[0]"
| Some ((r :: rs) as ls)
=> let n := show (List.length ls) in
let c1 := @make_castb A r in
let all_same := List.forallb (ZRange.type.base.option.interp_beq r) rs in
Some
(match all_same, c1 with
| true, Some c1 => c1
| _, _ => "??"
end
++ "[" ++ n ++ "]")
end
| base.type.unit
| base.type.type_base _
| base.type.option _
=> fun _ => None
end.
Definition make_cast {t} : ZRange.type.option.interp t -> option string
:= match t with
| type.base t => @make_castb t
| type.arrow _ _ => fun _ => None
end.
Definition maybe_wrap_cast (with_cast : bool) {t} (xr : (Level.Level -> string) * ZRange.type.option.interp t) (lvl : Level.Level) : string
:= match with_cast, make_cast (snd xr) with
| true, Some c => "(" ++ c ++ ")" ++ fst xr (Level.next app_lvl)
| _, _ => fst xr lvl
end.
Section without_prod.
Local Remove Hints show_lvl_prod : typeclass_instances.
Definition show_application (with_casts : bool) {t : type} (f : Level.Level -> string) (args : type.for_each_lhs_of_arrow (fun t => (Level.Level -> string) * ZRange.type.option.interp t)%type t)
: Level.Level -> string
:= match t return type.for_each_lhs_of_arrow (fun t => (Level.Level -> string) * ZRange.type.option.interp t)%type t -> Level.Level -> string with
| type.base _ => fun 'tt lvl => f lvl
| type.arrow s d
=> fun xs lvl
=> let _ : forall t, Show ((Level.Level -> string) * ZRange.type.option.interp t)%type
:= (fun t x => maybe_wrap_cast with_casts x app_lvl) in
let _ : forall t, ShowLevel ((Level.Level -> string) * ZRange.type.option.interp t)%type
:= fun t => ShowLevel_of_Show in
maybe_wrap_parens (Level.ltb lvl (Level.prev app_lvl)) (f (Level.prev app_lvl) ++ show_lvl xs (-∞))
end args.
End without_prod.
Module ident.
Global Instance show_lvl_ident {t} : ShowLevel (ident.ident t)
:= fun idc
=> match idc with
| ident.Literal base.type.Z v => show_lvl_compact_Z v
| ident.Literal _t v => show_lvl v
| ident.value_barrier => neg_wrap_parens "value_barrier"
| ident.comment _ => neg_wrap_parens "comment"
| ident.comment_no_keep _ => neg_wrap_parens "comment_no_keep"
| ident.tt => fun _ => "()"
| ident.Nat_succ => neg_wrap_parens "Nat.succ"
| ident.Nat_pred => neg_wrap_parens "Nat.pred"
| ident.Nat_max => neg_wrap_parens "Nat.max"
| ident.Nat_mul => neg_wrap_parens "Nat.mul"
| ident.Nat_add => neg_wrap_parens "Nat.add"
| ident.Nat_sub => neg_wrap_parens "Nat.sub"
| ident.Nat_eqb => neg_wrap_parens "Nat.eqb"
| ident.Pos_mul => neg_wrap_parens "Pos.mul"
| ident.Pos_add => neg_wrap_parens "Pos.add"
| ident.nil t => neg_wrap_parens "[]"
| ident.cons t => fun _ => "(::)"
| ident.pair A B => fun _ => "(,)"
| ident.fst A B => neg_wrap_parens "fst"
| ident.snd A B => neg_wrap_parens "snd"
| ident.prod_rect A B T => neg_wrap_parens "prod_rect"
| ident.bool_rect T => neg_wrap_parens "bool_rect"
| ident.bool_rect_nodep T => neg_wrap_parens "bool_rect_nodep"
| ident.nat_rect P => neg_wrap_parens "nat_rect"
| ident.eager_nat_rect P => neg_wrap_parens "eager_nat_rect"
| ident.nat_rect_arrow P Q => neg_wrap_parens "nat_rect(→)"
| ident.eager_nat_rect_arrow P Q => neg_wrap_parens "eager_nat_rect(→)"
| @ident.nat_rect_fbb_b A B C => neg_wrap_parens "nat_rect_fbb_b"
| @ident.nat_rect_fbb_b_b A B C D => neg_wrap_parens "nat_rect_fbb_b_b"
| @ident.list_rect_fbb_b T A B C => neg_wrap_parens "list_rect_fbb_b"
| @ident.list_rect_fbb_b_b T A B C D => neg_wrap_parens "list_rect_fbb_b_b"
| @ident.list_rect_fbb_b_b_b T A B C D E => neg_wrap_parens "list_rect_fbb_b_b"
| @ident.list_rect_fbb_b_b_b_b T A B C D E F => neg_wrap_parens "list_rect_fbb_b_b"
| @ident.list_rect_fbb_b_b_b_b_b T A B C D E F G => neg_wrap_parens "list_rect_fbb_b_b"
| ident.list_rect A P => neg_wrap_parens "list_rect"
| ident.eager_list_rect A P => neg_wrap_parens "eager_list_rect"
| ident.list_rect_arrow A P Q => neg_wrap_parens "list_rect(→)"
| ident.eager_list_rect_arrow A P Q => neg_wrap_parens "eager_list_rect(→)"
| ident.list_case A P => neg_wrap_parens "list_case"
| ident.List_length T => neg_wrap_parens "length"
| ident.List_seq => neg_wrap_parens "seq"
| ident.List_repeat A => neg_wrap_parens "repeat"
| ident.List_firstn A => neg_wrap_parens "firstn"
| ident.List_skipn A => neg_wrap_parens "skipn"
| ident.List_combine A B => neg_wrap_parens "combine"
| ident.List_map A B => neg_wrap_parens "map"
| ident.List_app A => fun _ => "(++)"
| ident.List_rev A => neg_wrap_parens "rev"
| ident.List_flat_map A B => neg_wrap_parens "flat_map"
| ident.List_partition A => neg_wrap_parens "partition"
| ident.List_filter A => neg_wrap_parens "filter"
| ident.List_fold_right A B => neg_wrap_parens "fold_right"
| ident.List_update_nth T => neg_wrap_parens "update_nth"
| ident.List_nth_default T => neg_wrap_parens "nth"
| ident.eager_List_nth_default T => neg_wrap_parens "eager_nth"
| ident.Some _ => neg_wrap_parens "Some"
| ident.None _ => neg_wrap_parens "None"
| ident.option_rect _ _ => neg_wrap_parens "option_rect"
| ident.Z_add => fun _ => "(+)"
| ident.Z_mul => fun _ => "( * )"
| ident.Z_pow => fun _ => "(^)"
| ident.Z_sub => fun _ => "(-)"
| ident.Z_opp => fun _ => "-"
| ident.Z_div => fun _ => "(/)"
| ident.Z_modulo => fun _ => "(mod)"
| ident.Z_eqb => fun _ => "(=)"
| ident.Z_leb => fun _ => "(≤)"
| ident.Z_ltb => fun _ => "(<)"
| ident.Z_geb => fun _ => "(≥)"
| ident.Z_gtb => fun _ => "(>)"
| ident.Z_min => neg_wrap_parens "min"
| ident.Z_max => neg_wrap_parens "max"
| ident.Z_abs => neg_wrap_parens "abs"
| ident.Z_log2 => neg_wrap_parens "log₂"
| ident.Z_log2_up => neg_wrap_parens "⌈log₂⌉"
| ident.Z_of_nat => fun _ => "(ℕ→ℤ)"
| ident.Z_to_nat => fun _ => "(ℤ→ℕ)"
| ident.Z_pos => fun _ => "(ℤ⁺→ℤ)"
| ident.Z_to_pos => fun _ => "(ℤ→ℤ⁺)"
| ident.Z_shiftr => fun _ => "(>>)"
| ident.Z_shiftl => fun _ => "(<<)"
| ident.Z_land => fun _ => "(&)"
| ident.Z_lor => fun _ => "(|)"
| ident.Z_lnot_modulo => neg_wrap_parens "~"
| ident.Z_lxor => neg_wrap_parens "⊕"
| ident.Z_bneg => neg_wrap_parens "!"
| ident.Z_mul_split => neg_wrap_parens "Z.mul_split"
| ident.Z_mul_high => neg_wrap_parens "Z.mul_high"
| ident.Z_add_get_carry => neg_wrap_parens "Z.add_get_carry"
| ident.Z_add_with_carry => neg_wrap_parens "Z.add_with_carry"
| ident.Z_add_with_get_carry => neg_wrap_parens "Z.add_with_get_carry"
| ident.Z_sub_get_borrow => neg_wrap_parens "Z.sub_get_borrow"
| ident.Z_sub_with_get_borrow => neg_wrap_parens "Z.sub_with_get_borrow"
| ident.Z_ltz => neg_wrap_parens "Z.ltz"
| ident.Z_zselect => neg_wrap_parens "Z.zselect"
| ident.Z_add_modulo => neg_wrap_parens "Z.add_modulo"
| ident.Z_truncating_shiftl => neg_wrap_parens "Z.truncating_shiftl"
| ident.Z_rshi => neg_wrap_parens "Z.rshi"
| ident.Z_cc_m => neg_wrap_parens "Z.cc_m"
| ident.Z_combine_at_bitwidth => neg_wrap_parens "Z.combine_at_bitwidth"
| ident.Z_cast => neg_wrap_parens "Z.cast"
| ident.Z_cast2 => neg_wrap_parens "Z.cast2"
| ident.Build_zrange => neg_wrap_parens "Build_zrange"
| ident.zrange_rect _ => neg_wrap_parens "zrange_rect"
| ident.fancy_add => neg_wrap_parens "fancy.add"
| ident.fancy_addc => neg_wrap_parens "fancy.addc"
| ident.fancy_sub => neg_wrap_parens "fancy.sub"
| ident.fancy_subb => neg_wrap_parens "fancy.subb"
| ident.fancy_mulll => neg_wrap_parens "fancy.mulll"
| ident.fancy_mullh => neg_wrap_parens "fancy.mullh"
| ident.fancy_mulhl => neg_wrap_parens "fancy.mulhl"
| ident.fancy_mulhh => neg_wrap_parens "fancy.mulhh"
| ident.fancy_rshi => neg_wrap_parens "fancy.rshi"
| ident.fancy_selc => neg_wrap_parens "fancy.selc"
| ident.fancy_selm => neg_wrap_parens "fancy.selm"
| ident.fancy_sell => neg_wrap_parens "fancy.sell"
| ident.fancy_addm => neg_wrap_parens "fancy.addm"
end.
Global Instance show_ident {t} : Show (ident.ident t) := show_lvl_ident.
(** N.B. Even though we are nominally printing Gallina
identifiers here, we parenthesize bitwise operators much
more frequently. See, e.g.,
https://github.com/mit-plv/fiat-crypto/pull/792#issuecomment-627647069,
where Andres makes the point that in C, [+] binds more
tightly than [<<]. *)
Definition conservative_binop_precedence_table : list (string * (Associativity * Level))
:= [("*ℕ", (mul_assoc, mul_lvl))
; ("+ℕ", (add_assoc, add_lvl))
; ("-ℕ", (sub_assoc, sub_lvl))
; ("=ℕ", (NoAssoc, Level.level 70))
; ("*ℤ⁺", (mul_assoc, mul_lvl))
; ("+ℤ⁺", (add_assoc, add_lvl))
; ("::", (RightAssoc, Level.level 60))
; ("++", (FullyAssoc, Level.level 60))
; ("*", (mul_assoc, mul_lvl))
; ("/", (div_assoc, div_lvl))
; ("+", (add_assoc, add_lvl))
; ("-", (sub_assoc, sub_lvl))
; ("^", (pow_assoc, pow_lvl))
; ("⊕", (LeftAssoc, Level.level 50))
; ("mod", (LeftAssoc, Level.level 40))
; ("=", (NoAssoc, Level.level 70))
; ("<", (NoAssoc, Level.level 70))
; ("≤", (NoAssoc, Level.level 70))
; (">", (NoAssoc, Level.level 70))
; ("≥", (NoAssoc, Level.level 70))
; (">>", (ExplicitAssoc 35 35, Level.level 55))
; ("<<", (ExplicitAssoc 35 35, Level.level 55))
; ("&", (ExplicitAssoc 35 35, Level.level 55))
; ("|", (ExplicitAssoc 35 35, Level.level 55))
; (", ", (pair_assoc, pair_lvl))
].
Definition conservative_preop_precedence_table : list (string * (Associativity * Level))
:= [("-", (RightAssoc, opp_lvl))
; ("!", (RightAssoc, Level.level 75))
].
Definition conservative_postop_precedence_table : list (string * (Associativity * Level))
:= [(".+1", (LeftAssoc, app_lvl))
; (".-1", (LeftAssoc, app_lvl))
; ("₁", (LeftAssoc, Level.level 0))
; ("₂", (LeftAssoc, Level.level 0))
].
Inductive Not_found := not_found.
Class with_space_opt := with_space : bool.
Global Instance with_space_default : with_space_opt | 1000 := true.
Definition lookup_lvl (table : list (string * (Associativity * Level))) (op : string)
:= match List.find (fun '(op', _) => String.eqb op op') table as x return if x then Level else Not_found with
| Some (_, (assoc, lvl)) => lvl
| None => not_found
end.
Definition lookup_assoc (table : list (string * (Associativity * Level))) (op : string)
:= match List.find (fun '(op', _) => String.eqb op op') table as x return if x then Associativity else Not_found with
| Some (_, (assoc, lvl)) => assoc
| None => not_found
end.
Definition lookup_show_lvl_binop {with_space : bool} (table : list (string * (Associativity * Level))) (binop : string)
:= match List.find (fun '(binop', _) => String.eqb binop binop') table as x return if x then (Level -> string) -> (Level -> string) -> (Level -> string) else Not_found with
| Some (_, (binop_assoc, binop_lvl)) => fun x y => show_lvl_binop binop_assoc binop_lvl x (if with_space then " " ++ binop ++ " " else binop) y
| None => not_found
end.
Definition lookup_show_lvl_preop (table : list (string * (Associativity * Level))) (preop : string)
:= match List.find (fun '(op', _) => String.eqb preop op') table as x return if x then (Level -> string) -> (Level -> string) else Not_found with
| Some (op, (op_assoc, op_lvl)) => fun x => show_lvl_preop_assoc op_assoc op_lvl op x
| None => not_found
end.
Definition lookup_show_lvl_postop (table : list (string * (Associativity * Level))) (postop : string)
:= match List.find (fun '(op', _) => String.eqb postop op') table as x return if x then (Level -> string) -> (Level -> string) else Not_found with
| Some (op, (op_assoc, op_lvl)) => fun x => show_lvl_postop_assoc op_assoc op_lvl x op
| None => not_found
end.
(** We give the ident type [a -> b] to force patterns like [|
ident.Nat_mul as idc => ident_to_binop_string idc] to bind
[ident.Nat_mul] rather than the discriminee *)
Definition ident_to_op_string {a b} (idc : ident.ident (a -> b)) : string
:= match idc with
| ident.Nat_mul => "*ℕ"
| ident.Nat_add => "+ℕ"
| ident.Nat_sub => "-ℕ"
| ident.Nat_eqb => "=ℕ"
| ident.Pos_mul => "*ℤ⁺"
| ident.Pos_add => "+ℤ⁺"
| ident.cons _ => "::"
| ident.List_app _ => "++"
| ident.Z_mul => "*"
| ident.Z_add => "+"
| ident.Z_sub => "-"
| ident.Z_pow => "^"
| ident.Z_lxor => "⊕"
| ident.Z_div => "/"
| ident.Z_modulo => "mod"
| ident.Z_eqb => "="
| ident.Z_ltb => "<"
| ident.Z_leb => "≤"
| ident.Z_gtb => ">"
| ident.Z_geb => "≥"
| ident.Z_shiftr => ">>"
| ident.Z_shiftl => "<<"
| ident.Z_land => "&"
| ident.Z_lor => "|"
| ident.pair _ _ => ", "
| ident.Nat_succ => ".+1"
| ident.Nat_pred => ".-1"
| ident.fst _ _ => "₁"
| ident.snd _ _ => "₂"
| ident.Z_opp => "-"
| ident.Z_bneg => "!"
| _ => ""
end.
Local Notation show_lvl_binop_no_space idc := (lookup_show_lvl_binop (with_space:=false) conservative_binop_precedence_table (ident_to_op_string idc)).
Local Notation show_lvl_binop idc := (lookup_show_lvl_binop (with_space:=true) conservative_binop_precedence_table (ident_to_op_string idc)).
Local Notation show_lvl_preop idc := (lookup_show_lvl_preop conservative_preop_precedence_table (ident_to_op_string idc)).
Local Notation show_lvl_postop idc := (lookup_show_lvl_postop conservative_postop_precedence_table (ident_to_op_string idc)).
Definition show_ident_lvl (with_casts : bool) (with_all_casts : bool) {t} (idc : ident.ident t)
: type.for_each_lhs_of_arrow (fun t => (Level.Level -> string) * ZRange.type.option.interp t)%type t -> (Level.Level -> string) * ZRange.type.base.option.interp (type.final_codomain t)
:= let with_casts := (with_casts && negb with_all_casts)%bool in
match idc in ident.ident t return type.for_each_lhs_of_arrow (fun t => (Level.Level -> string) * ZRange.type.option.interp t)%type t -> (Level.Level -> string) * ZRange.type.base.option.interp (type.final_codomain t) with
| ident.Literal base.type.Z v => fun 'tt => (show_lvl_compact_Z v, ZRange.type.base.option.None)
| ident.Literal t v => fun 'tt => (show_lvl v, ZRange.type.base.option.Some (t:=t) v)
| ident.tt => fun _ => (fun _ => "()", tt)
| ident.Nat_max as idc
=> fun '((x, xr), ((y, yr), tt)) => (show_lvl_app2 (show_lvl_ident idc) x y, ZRange.type.base.option.None)
| ident.Nat_succ as idc
| ident.Nat_pred as idc
=> fun '((x, xr), tt) => (show_lvl_postop idc x, ZRange.type.base.option.None)
| ident.Z_opp as idc
| ident.Z_bneg as idc
=> fun '((x, xr), tt) => (show_lvl_preop idc x, ZRange.type.base.option.None)
| ident.Nat_mul as idc
| ident.Nat_add as idc
| ident.Nat_sub as idc
| ident.Nat_eqb as idc
| ident.Pos_mul as idc
| ident.Pos_add as idc
| ident.cons _ as idc
| ident.List_app _ as idc
| ident.Z_mul as idc
| ident.Z_add as idc
| ident.Z_sub as idc
| ident.Z_pow as idc
| ident.Z_lxor as idc
| ident.Z_div as idc
| ident.Z_modulo as idc
| ident.Z_shiftr as idc
| ident.Z_shiftl as idc
| ident.Z_land as idc
| ident.Z_lor as idc
=> fun '((x, xr), ((y, yr), tt)) => (show_lvl_binop idc x y, ZRange.type.base.option.None)
| ident.Z_eqb as idc
| ident.Z_ltb as idc
| ident.Z_leb as idc
| ident.Z_gtb as idc
| ident.Z_geb as idc
=> fun '(x, (y, tt)) => (show_lvl_binop idc (maybe_wrap_cast with_casts x) (maybe_wrap_cast with_casts y), ZRange.type.base.option.None)
| ident.pair _ _ as idc => fun '((x, xr), ((y, yr), tt)) => (show_lvl_binop_no_space idc x y, (xr, yr))
| ident.fst _ _ as idc
=> fun '((x, xr), tt) => (show_lvl_postop idc x, fst xr)
| ident.snd _ _ as idc
=> fun '((x, xr), tt) => (show_lvl_postop idc x, snd xr)
| ident.None _ => fun 'tt => (neg_wrap_parens "None", ZRange.type.base.option.None)
| ident.nil t => fun 'tt => (neg_wrap_parens "[]", ZRange.type.base.option.None)
| ident.prod_rect A B T => fun '((f, fr), ((p, pr), tt)) => (neg_wrap_parens ("match " ++ show_lvl p term_lvl ++ " with " ++ show_lvl f term_lvl ++ " end"), ZRange.type.base.option.None)
| ident.bool_rect _
| ident.bool_rect_nodep _
=> fun '(t, (f, ((b, br), tt))) => (fun lvl => maybe_wrap_parens (Level.ltb lvl term_lvl) ("if " ++ show_lvl b term_lvl ++ " then " ++ maybe_wrap_cast with_casts t term_lvl ++ " else " ++ maybe_wrap_cast with_casts f term_lvl), ZRange.type.base.option.None)
| ident.eager_List_nth_default _
=> fun '((d, dr), ((ls, lsr), ((i, ir), tt))) => (fun lvl => maybe_wrap_parens (Level.ltb lvl app_lvl) (show_lvl ls app_lvl ++ "[[" ++ show_lvl i term_lvl ++ "]]"), ZRange.type.base.option.None)
| ident.List_nth_default _
=> fun '((d, dr), ((ls, lsr), ((i, ir), tt))) => (fun lvl => maybe_wrap_parens (Level.ltb lvl app_lvl) (show_lvl ls app_lvl ++ "[" ++ show_lvl i term_lvl ++ "]"), ZRange.type.base.option.None)
| ident.Z_lnot_modulo => fun '((x, xr), ((m, mr), tt)) => (fun lvl => maybe_wrap_parens (Level.ltb lvl 75) ("~" ++ show_lvl x 75 ++ (if with_casts then " (mod " ++ show_lvl m term_lvl ++ ")" else "")), ZRange.type.base.option.None)
| ident.Z_cast as idc
| ident.Z_cast2 as idc
=> fun '((srange, range), ((x, xr), tt))
=> let t := (fun t (idc : ident.ident (_ -> t -> _)) => t) _ idc in
(* if we don't do the above, we pick up the wrong type in maybe_wrap_cast below *)
(if with_all_casts && Option.is_None (ZRange.type.base.option.lift_Some range)
then (fun _ => "(" ++ srange term_lvl ++ ")" ++ x 0)
else maybe_wrap_cast (t:=t) with_all_casts (x, range),
range)
| ident.Build_zrange
=> fun '((x, xr), ((y, yr), tt))
=> (neg_wrap_parens ("r[" ++ show_lvl x 60 ++ " ~> " ++ show_lvl y term_lvl),
match ZRange.ident.option.to_literal xr, ZRange.ident.option.to_literal yr with
| Some l, Some h => Some r[l ~> h]
| _, _ => (*ZRange.type.base.option.*)None
end)
| ident.comment _ as idc
| ident.comment_no_keep _ as idc
| ident.value_barrier as idc
| ident.Some _ as idc
| ident.nat_rect _ as idc
| ident.eager_nat_rect _ as idc
| ident.eager_nat_rect_arrow _ _ as idc
| ident.nat_rect_arrow _ _ as idc
| @ident.nat_rect_fbb_b _ _ _ as idc
| @ident.nat_rect_fbb_b_b _ _ _ _ as idc
| @ident.list_rect_fbb_b _ _ _ _ as idc
| @ident.list_rect_fbb_b_b _ _ _ _ _ as idc
| @ident.list_rect_fbb_b_b_b _ _ _ _ _ _ as idc
| @ident.list_rect_fbb_b_b_b_b _ _ _ _ _ _ _ as idc
| @ident.list_rect_fbb_b_b_b_b_b _ _ _ _ _ _ _ _ as idc
| ident.option_rect _ _ as idc
| ident.list_rect _ _ as idc
| ident.eager_list_rect _ _ as idc
| ident.list_rect_arrow _ _ _ as idc
| ident.eager_list_rect_arrow _ _ _ as idc
| ident.list_case _ _ as idc
| ident.List_length _ as idc
| ident.List_seq as idc
| ident.List_repeat _ as idc
| ident.List_firstn _ as idc
| ident.List_skipn _ as idc
| ident.List_combine _ _ as idc
| ident.List_map _ _ as idc
| ident.List_rev _ as idc
| ident.List_flat_map _ _ as idc
| ident.List_partition _ as idc
| ident.List_filter _ as idc
| ident.List_fold_right _ _ as idc
| ident.List_update_nth _ as idc
| ident.Z_log2 as idc
| ident.Z_log2_up as idc
| ident.Z_of_nat as idc
| ident.Z_to_nat as idc
| ident.Z_pos as idc
| ident.Z_to_pos as idc
| ident.Z_min as idc
| ident.Z_max as idc
| ident.Z_abs as idc
| ident.Z_mul_split as idc
| ident.Z_mul_high as idc
| ident.Z_add_get_carry as idc
| ident.Z_add_with_carry as idc
| ident.Z_add_with_get_carry as idc
| ident.Z_sub_get_borrow as idc
| ident.Z_sub_with_get_borrow as idc
| ident.Z_ltz as idc
| ident.Z_zselect as idc
| ident.Z_add_modulo as idc
| ident.Z_truncating_shiftl as idc
| ident.Z_rshi as idc
| ident.Z_cc_m as idc
| ident.Z_combine_at_bitwidth as idc
| ident.zrange_rect _ as idc
| ident.fancy_add as idc
| ident.fancy_addc as idc
| ident.fancy_sub as idc
| ident.fancy_subb as idc
| ident.fancy_mulll as idc
| ident.fancy_mullh as idc
| ident.fancy_mulhl as idc
| ident.fancy_mulhh as idc
| ident.fancy_rshi as idc
| ident.fancy_selc as idc
| ident.fancy_selm as idc
| ident.fancy_sell as idc
| ident.fancy_addm as idc
=> fun args => (show_application with_casts (fun _ => show idc) args, ZRange.type.base.option.None)
end.
End ident.
Module expr.
Local Notation show_ident := ident.show_ident.
Local Notation show_ident_lvl := ident.show_ident_lvl.
Fixpoint get_eta_cps_args {A} (t : type) (idx : positive) {struct t}
: (type.for_each_lhs_of_arrow (fun y => (Level -> string) * ZRange.type.option.interp y)%type t -> positive -> A) -> list string * A
:= match t with
| type.arrow s d
=> fun k
=> let n := "x" ++ Decimal.Pos.to_string idx in
let '(args, show_f) := @get_eta_cps_args A d (Pos.succ idx) (fun arglist => k (((fun _ => n), ZRange.type.option.None), arglist)) in
(n :: args, show_f)
| type.base _
=> fun k => (nil, k tt idx)
end.
Section helper.
Context {var}
(of_string : forall t, string -> option (var t))
(k : forall t, @API.expr var t -> type.for_each_lhs_of_arrow (fun t => (Level -> string) * ZRange.type.option.interp t)%type t -> positive -> (positive * (Level -> list string)) * ZRange.type.base.option.interp (type.final_codomain t)).
Definition show_eta_abs_aux
(aux : forall t (idx : positive) (e : @API.expr var t),
(positive * (list string * (Level -> list string))) * ZRange.type.base.option.interp (type.final_codomain t))
s d (idx : positive) (f : var s -> @API.expr var d)
: (positive * (list string * (Level -> list string))) * ZRange.type.base.option.interp (type.final_codomain d)
:= let n := match s with
| type.base base.type.unit => "()_" ++ Decimal.Pos.to_string idx
| _ => "x" ++ Decimal.Pos.to_string idx
end in
match of_string s n with
| Some n'
=> let '(_, (args, show_f), r) := aux d (Pos.succ idx) (f n') in
(idx,
(n :: args, show_f),
r)
| None
=> (idx,
([n], (fun _ => ["λ_(" ++ show s ++ ")"])),
ZRange.type.base.option.None)
end.
Fixpoint show_eta_abs_cps' {t} (idx : positive) (e : @API.expr var t)
: (positive * (list string * (Level -> list string))) * ZRange.type.base.option.interp (type.final_codomain t)
:= match e in expr.expr t return (unit -> _ * ZRange.type.base.option.interp (type.final_codomain t)) -> _ * ZRange.type.base.option.interp (type.final_codomain t) with
| expr.Abs s d f
=> fun _
=> show_eta_abs_aux (@show_eta_abs_cps') s d idx f
| _
=> fun default
=> default tt
end (fun _
=> let '(args, (idx, show_f, r)) := get_eta_cps_args _ idx (@k _ e) in
((idx, (args, show_f)), r)).
Definition show_eta_abs_cps (with_casts : bool) {s d} (idx : positive) (f : var s -> @API.expr var d) (extraargs : type.for_each_lhs_of_arrow (fun t => (Level -> string) * ZRange.type.option.interp t)%type (s -> d))
: (positive * (Level -> list string)) * ZRange.type.base.option.interp (type.final_codomain d)
:= let '(idx, (args, show_f), r) := show_eta_abs_aux (@show_eta_abs_cps') s d idx f in
let argstr := String.concat " " args in
(idx,
fun lvl
=> match show_f lvl with
| nil => [show_application with_casts (fun _ => "(λ " ++ argstr ++ ", (* NOTHING‽ *))") extraargs (Level.prev app_lvl)]%string
| show_f::nil
=> [show_application with_casts (fun _ => "(λ " ++ argstr ++ ", " ++ show_f ++ ")") extraargs (Level.prev app_lvl)]%string
| show_f
=> ["(λ " ++ argstr ++ ","]%string ++ (List.map (fun v => String " " (String " " v)) show_f) ++ [")" ++ show_application with_casts (fun _ => "") extraargs (Level.prev app_lvl)]%string
end%list,
r).
Definition show_eta_cps {t} (idx : positive) (e : @API.expr var t)
: (positive * (Level -> list string)) * ZRange.type.option.interp t
:= let '(idx, (args, show_f), r) := @show_eta_abs_cps' t idx e in
let argstr := String.concat " " args in
(idx,
(fun lvl
=> match args, show_f lvl with
| nil, show_f => show_f
| _, nil => ["(λ " ++ argstr ++ ", (* NOTHING‽ *))"]%string
| _, show_f::nil
=> ["(λ " ++ argstr ++ ", " ++ show_f ++ ")"]%string
| _, show_f
=> ["(λ " ++ argstr ++ ","]%string ++ (List.map (fun v => String " " (String " " v)) show_f) ++ [")"]
end%list),
match t return ZRange.type.base.option.interp (type.final_codomain t) -> ZRange.type.option.interp t with
| type.base _ => fun r => r
| type.arrow _ _ => fun _ => ZRange.type.option.None
end r).
End helper.
Fixpoint show_expr_lines_gen (with_casts : bool) (with_all_casts : bool) {var} (to_string : forall t, var t -> string) (of_string : forall t, string -> option (var t)) {t} (e : @API.expr var t) (args : type.for_each_lhs_of_arrow (fun t => (Level -> string) * ZRange.type.option.interp t)%type t) (idx : positive) {struct e} : (positive * (Level -> list string)) * ZRange.type.base.option.interp (type.final_codomain t)
:= let show_expr_lines_gen := @show_expr_lines_gen with_casts with_all_casts var to_string of_string in
match e in expr.expr t return type.for_each_lhs_of_arrow (fun t => (Level -> string) * ZRange.type.option.interp t)%type t -> (positive * (Level -> list string)) * ZRange.type.base.option.interp (type.final_codomain t) with
| expr.Ident t idc
=> fun args => let '(v, r) := @show_ident_lvl with_casts with_all_casts t idc args in
(idx, fun lvl => [v lvl], r)
| expr.Var t v
=> fun args => (idx, fun lvl => [show_application with_casts (fun _ => to_string _ v) args lvl], ZRange.type.base.option.None)
| expr.Abs s d f
=> fun args
=> show_eta_abs_cps of_string (fun t e args idx => let '(idx, v, r) := @show_expr_lines_gen t e args idx in (idx, fun _ => v term_lvl, r)) with_casts idx f args
| expr.App s d f x
=> fun args
=> let '(idx', x', xr) := show_eta_cps of_string (fun t e args idx => @show_expr_lines_gen t e args idx) idx x in
@show_expr_lines_gen
_ f
(((fun lvl => String.concat String.NewLine (x' lvl)), xr),
args)
idx
| expr.LetIn A (type.base B) x f
=> fun 'tt
=> let n := "x" ++ Decimal.Pos.to_string idx in
let '(_, show_x, xr) := show_eta_cps of_string (fun t e args idx => @show_expr_lines_gen t e args idx) idx x in
let '(idx, show_f, fr)
:= match of_string A n with
| Some n' => show_eta_cps of_string (fun t e args idx => @show_expr_lines_gen t e args idx) (Pos.succ idx) (f n')
| None => (idx, (fun _ => ["_"]), ZRange.type.option.None)
end in
let '(ty_str, comment_ty_str, space_comment_ty_str)
:= match make_cast xr with
| Some c => let ty_str := " : " ++ c in
let comment_ty_str := "(*" ++ ty_str ++ " *)" in
(ty_str, comment_ty_str, " " ++ comment_ty_str)
| None => ("", "", "")
end in
let expr_let_line := "let " ++ n ++ " := " in
(idx,
(fun lvl
=> match show_x term_lvl with
| nil => [expr_let_line ++ "(* NOTHING‽ *)" ++ space_comment_ty_str ++ " in"]%string ++ show_f term_lvl
| show_x::nil => [expr_let_line ++ show_x ++ "" ++ space_comment_ty_str ++ " in"]%string ++ show_f term_lvl
| show_x::rest
=> ([expr_let_line ++ show_x]%string)
++ (List.map (fun l => String.string_of_list_ascii (List.repeat " "%char (String.length expr_let_line)) ++ l)%string
rest)
++ (if ty_str =? "" then ["(*" ++ ty_str ++ " *)"] else [])%string
++ ["in"]
++ show_f term_lvl
end%list),
fr)
| expr.LetIn A B x f
=> fun args
=> let n := "x" ++ Decimal.Pos.to_string idx in
let '(_, show_x, xr) := show_eta_cps of_string (fun t e args idx => @show_expr_lines_gen t e args idx) idx x in
let '(idx, show_f, fr)
:= match of_string A n with
| Some n' => show_eta_cps of_string (fun t e args idx => @show_expr_lines_gen t e args idx) (Pos.succ idx) (f n')
| None => (idx, (fun _ => ["_"]), ZRange.type.option.None)
end in
let '(ty_str, comment_ty_str, space_comment_ty_str)
:= match make_cast xr with
| Some c => let ty_str := " : " ++ c in
let comment_ty_str := "(*" ++ ty_str ++ " *)" in
(ty_str, comment_ty_str, " " ++ comment_ty_str)
| None => ("", "", "")
end in
let expr_let_line := "let " ++ n ++ " := " in
(idx,
(fun lvl
=> (["("]
++ (map
(String " ")
match show_x term_lvl with
| nil => [expr_let_line ++ "(* NOTHING‽ *)" ++ space_comment_ty_str ++ " in"]%string ++ show_f term_lvl
| show_x::nil => [expr_let_line ++ show_x ++ "" ++ space_comment_ty_str ++ " in"]%string ++ show_f term_lvl
| show_x::rest
=> ([expr_let_line ++ show_x]%string)
++ (List.map (fun l => String.string_of_list_ascii (List.repeat " "%char (String.length expr_let_line)) ++ l)%string
rest)
++ (if ty_str =? "" then ["(*" ++ ty_str ++ " *)"] else [])%string
++ ["in"]
++ show_f term_lvl
end%list)
++ [")"; show_application with_casts (fun _ => "") args (Level.prev app_lvl)])%list),
ZRange.type.base.option.None)
end args.
Definition show_expr_lines (with_casts : bool) (with_all_casts : bool) {t} (e : @API.expr (fun _ => string) t) (args : type.for_each_lhs_of_arrow (fun t => (Level -> string) * ZRange.type.option.interp t)%type t) (idx : positive) : (positive * (Level -> list string)) * ZRange.type.base.option.interp (type.final_codomain t)
:= @show_expr_lines_gen with_casts with_all_casts (fun _ => string) (fun _ x => x) (fun _ x => Some x) t e args idx.
Definition show_lvl_var_expr : forall {var t}, ShowLevel (@API.expr var t)
:= fix show_lvl_var_expr {var t} (e : @API.expr var t) : Level -> string
:= match e with
| expr.Ident t idc => show_lvl idc
| expr.Var t v => neg_wrap_parens ("VAR_" ++ show_lvl t 0)
| expr.Abs s d f => neg_wrap_parens ("λ_" ++ show_lvl t 0)
| expr.App s d f x => show_lvl_binop LeftAssoc app_lvl (show_lvl_var_expr f) " @ " (show_lvl_var_expr x)
| expr.LetIn A B x f
=> fun lvl => maybe_wrap_parens (Level.ltb lvl term_lvl) ("expr_let _ := " ++ show_lvl_var_expr x term_lvl ++ " in _")
end%string.
Definition show_var_expr {var t} : Show (@API.expr var t) := show_lvl_var_expr.
Definition partially_show_expr {var t} : Show (@API.expr var t) := show_var_expr.
Section Show_gen.
Context {with_casts : PHOAS.with_casts}
{with_all_casts : PHOAS.with_all_casts}
{var : API.type -> Type}
(to_string : forall t, var t -> string)
(of_string : forall t, string -> option (var t)).
Definition show_lines_expr_gen {t} : ShowLines (@API.expr var t)
:= fun e => let '(_, v, _) := show_eta_cps of_string (fun t e args idx => @show_expr_lines_gen with_casts with_all_casts var to_string of_string t e args idx) 1%positive e in v (Level.prev term_lvl).
Definition show_expr_gen {t} : Show (@API.expr var t)
:= fun e => String.concat String.NewLine (show_lines_expr_gen e).
End Show_gen.
Global Instance show_lines_expr {with_casts : PHOAS.with_casts} {with_all_casts : PHOAS.with_all_casts} {t} : ShowLines (@API.expr (fun _ => string) t)
:= @show_lines_expr_gen with_casts with_all_casts (fun _ => string) (fun _ x => x) (fun _ x => Some x) t.
Global Instance show_lines_Expr {with_casts : PHOAS.with_casts} {with_all_casts : PHOAS.with_all_casts} {t} : ShowLines (@API.Expr t)
:= fun e => show_lines (e _).
Global Instance show_expr {with_casts : PHOAS.with_casts} {with_all_casts : PHOAS.with_all_casts} {t} : Show (@API.expr (fun _ => string) t)
:= fun e => String.concat String.NewLine (show_lines e).
Global Instance show_Expr {with_casts : PHOAS.with_casts} {with_all_casts : PHOAS.with_all_casts} {t} : Show (@API.Expr t)
:= fun e => show (e _).
End expr.
End PHOAS.
Definition LinesToString (lines : list string)
: string
:= String.concat String.NewLine lines.
Definition format_typedef_name
{language_naming_conventions : language_naming_conventions_opt}
(prefix : string)
(private : bool)
(name : string)
: string
:= convert_to_naming_convention (if private then private_type_naming_convention else public_type_naming_convention) (prefix ++ name).
Module int.
Inductive type := signed (lgbitwidth : nat) | unsigned (lgbitwidth : nat).
Definition lgbitwidth_of (t : type) : nat
:= match t with
| signed lgbitwidth => lgbitwidth
| unsigned lgbitwidth => lgbitwidth
end.