forked from rems-project/sail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty_print_coq.ml
3354 lines (3213 loc) · 152 KB
/
pretty_print_coq.ml
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
(**************************************************************************)
(* Sail *)
(* *)
(* Copyright (c) 2013-2017 *)
(* Kathyrn Gray *)
(* Shaked Flur *)
(* Stephen Kell *)
(* Gabriel Kerneis *)
(* Robert Norton-Wright *)
(* Christopher Pulte *)
(* Peter Sewell *)
(* Alasdair Armstrong *)
(* Brian Campbell *)
(* Thomas Bauereiss *)
(* Anthony Fox *)
(* Jon French *)
(* Dominic Mulligan *)
(* Stephen Kell *)
(* Mark Wassell *)
(* *)
(* All rights reserved. *)
(* *)
(* This software was developed by the University of Cambridge Computer *)
(* Laboratory as part of the Rigorous Engineering of Mainstream Systems *)
(* (REMS) project, funded by EPSRC grant EP/K008528/1. *)
(* *)
(* Redistribution and use in source and binary forms, with or without *)
(* modification, are permitted provided that the following conditions *)
(* are met: *)
(* 1. Redistributions of source code must retain the above copyright *)
(* notice, this list of conditions and the following disclaimer. *)
(* 2. Redistributions in binary form must reproduce the above copyright *)
(* notice, this list of conditions and the following disclaimer in *)
(* the documentation and/or other materials provided with the *)
(* distribution. *)
(* *)
(* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' *)
(* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *)
(* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *)
(* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR *)
(* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *)
(* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *)
(* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF *)
(* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *)
(* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, *)
(* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT *)
(* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *)
(* SUCH DAMAGE. *)
(**************************************************************************)
open Type_check
open Ast
open Ast_util
open Reporting
open Rewriter
open PPrint
open Pretty_print_common
module StringSet = Set.Make(String)
let rec list_contains cmp l1 = function
| [] -> Some l1
| h::t ->
let rec remove = function
| [] -> None
| h'::t' -> if cmp h h' = 0 then Some t'
else Util.option_map (List.cons h') (remove t')
in Util.option_bind (fun l1' -> list_contains cmp l1' t) (remove l1)
let opt_undef_axioms = ref false
let opt_debug_on : string list ref = ref []
(****************************************************************************
* PPrint-based sail-to-coq pprinter
****************************************************************************)
(* Data representation:
*
* In pure computations we keep values with top level existential types
* (including ranges and nats) separate from the proofs of the accompanying
* constraints, which keeps the terms shorter and more manageable.
* Existentials embedded in types (e.g., in tuples or datatypes) are dependent
* pairs.
*
* Monadic values always includes the proof in a dependent pair because the
* constraint solving tactic won't see the term that defined the value, and
* must rely entirely on the type (like the Sail type checker).
*)
type context = {
early_ret : bool;
kid_renames : kid KBindings.t; (* Plain tyvar -> tyvar renames,
used to avoid variable/type variable name clashes *)
(* Note that as well as these kid renames, we also attempt to replace entire
n_constraints with equivalent variables in doc_nc_exp. *)
kid_id_renames : (id option) KBindings.t; (* tyvar -> argument renames *)
kid_id_renames_rev : kid Bindings.t; (* reverse of kid_id_renames *)
bound_nvars : KidSet.t;
build_at_return : string option;
recursive_fns : (int * int) Bindings.t; (* Number of implicit arguments and constraints for (mutually) recursive definitions *)
debug : bool;
}
let empty_ctxt = {
early_ret = false;
kid_renames = KBindings.empty;
kid_id_renames = KBindings.empty;
kid_id_renames_rev = Bindings.empty;
bound_nvars = KidSet.empty;
build_at_return = None;
recursive_fns = Bindings.empty;
debug = false;
}
let add_single_kid_id_rename ctxt id kid =
let kir =
match Bindings.find_opt id ctxt.kid_id_renames_rev with
| Some kid -> KBindings.add kid None ctxt.kid_id_renames
| None -> ctxt.kid_id_renames
in
{ ctxt with
kid_id_renames = KBindings.add kid (Some id) kir;
kid_id_renames_rev = Bindings.add id kid ctxt.kid_id_renames_rev
}
let debug_depth = ref 0
let rec indent n = match n with
| 0 -> ""
| n -> "| " ^ indent (n - 1)
let debug ctxt m =
if ctxt.debug
then print_endline (indent !debug_depth ^ Lazy.force m)
else ()
let langlebar = string "<|"
let ranglebar = string "|>"
let anglebars = enclose langlebar ranglebar
let enclose_record = enclose (string "{| ") (string " |}")
let enclose_record_update = enclose (string "{[ ") (string " ]}")
let bigarrow = string "=>"
let separate_opt s f l = separate s (Util.map_filter f l)
let is_number_char c =
c = '0' || c = '1' || c = '2' || c = '3' || c = '4' || c = '5' ||
c = '6' || c = '7' || c = '8' || c = '9'
let is_enum env id =
match Env.lookup_id id env with
| Enum _ -> true
| _ -> false
let rec fix_id remove_tick name = match name with
| "assert"
| "lsl"
| "lsr"
| "asr"
| "type"
| "fun"
| "function"
| "raise"
| "try"
| "match"
| "with"
| "check"
| "field"
| "LT"
| "GT"
| "EQ"
| "Z"
| "O"
| "S"
| "mod"
| "M"
| "tt"
-> name ^ "'"
| _ ->
if String.contains name '#' then
fix_id remove_tick (String.concat "_" (Util.split_on_char '#' name))
else if String.contains name '?' then
fix_id remove_tick (String.concat "_pat_" (Util.split_on_char '?' name))
else if String.contains name '^' then
fix_id remove_tick (String.concat "__" (Util.split_on_char '^' name))
else if name.[0] = '\'' then
let var = String.sub name 1 (String.length name - 1) in
if remove_tick then fix_id remove_tick var else (var ^ "'")
else if is_number_char(name.[0]) then
("v" ^ name ^ "'")
else name
let string_id (Id_aux(i,_)) =
match i with
| Id i -> fix_id false i
| Operator x -> Util.zencode_string ("op " ^ x)
let doc_id id = string (string_id id)
let doc_id_type (Id_aux(i,_)) =
match i with
| Id("int") -> string "Z"
| Id("real") -> string "R"
| Id i -> string (fix_id false i)
| Operator x -> string (Util.zencode_string ("op " ^ x))
let doc_id_ctor (Id_aux(i,_)) =
match i with
| Id i -> string (fix_id false i)
| Operator x -> string (Util.zencode_string ("op " ^ x))
let doc_var ctx kid =
match KBindings.find kid ctx.kid_id_renames with
| Some id -> doc_id id
| None -> underscore (* The original id has been shadowed, hope Coq can work it out... TODO: warn? *)
| exception Not_found ->
string (fix_id true (string_of_kid (try KBindings.find kid ctx.kid_renames with Not_found -> kid)))
let doc_docstring (l, _) = match l with
| Parse_ast.Documented (str, _) -> string ("(*" ^ str ^ "*)") ^^ hardline
| _ -> empty
let simple_annot l typ = (Parse_ast.Generated l, Some (Env.empty, typ, no_effect))
let simple_num l n = E_aux (
E_lit (L_aux (L_num n, Parse_ast.Generated l)),
simple_annot (Parse_ast.Generated l)
(atom_typ (Nexp_aux (Nexp_constant n, Parse_ast.Generated l))))
let effectful_set = function
| [] -> false
| _ -> true
(*List.exists
(fun (BE_aux (eff,_)) ->
match eff with
| BE_rreg | BE_wreg | BE_rmem | BE_rmemt | BE_wmem | BE_eamem
| BE_exmem | BE_wmv | BE_wmvt | BE_barr | BE_depend | BE_nondet
| BE_escape -> true
| _ -> false)*)
let effectful (Effect_aux (Effect_set effs, _)) = effectful_set effs
let is_regtyp (Typ_aux (typ, _)) env = match typ with
| Typ_app(id, _) when string_of_id id = "register" -> true
| _ -> false
let doc_nexp ctx ?(skip_vars=KidSet.empty) nexp =
(* Print according to Coq's precedence rules *)
let rec plussub (Nexp_aux (n,l) as nexp) =
match n with
| Nexp_sum (n1, n2) -> separate space [plussub n1; plus; mul n2]
| Nexp_minus (n1, n2) -> separate space [plussub n1; minus; mul n2]
| _ -> mul nexp
and mul (Nexp_aux (n,l) as nexp) =
match n with
| Nexp_times (n1, n2) -> separate space [mul n1; star; uneg n2]
| _ -> uneg nexp
and uneg (Nexp_aux (n,l) as nexp) =
match n with
| Nexp_neg n -> separate space [minus; uneg n]
| _ -> exp nexp
and exp (Nexp_aux (n,l) as nexp) =
match n with
| Nexp_exp n -> separate space [string "2"; caret; exp n]
| _ -> app nexp
and app (Nexp_aux (n,l) as nexp) =
match n with
| Nexp_app (Id_aux (Id "div",_), [n1;n2])
-> separate space [string "ZEuclid.div"; atomic n1; atomic n2]
| Nexp_app (Id_aux (Id "mod",_), [n1;n2])
-> separate space [string "ZEuclid.modulo"; atomic n1; atomic n2]
| Nexp_app (Id_aux (Id "abs_atom",_), [n1])
-> separate space [string "Z.abs"; atomic n1]
| _ -> atomic nexp
and atomic (Nexp_aux (n,l) as nexp) =
match n with
| Nexp_constant i -> string (Big_int.to_string i)
| Nexp_var v when KidSet.mem v skip_vars -> string "_"
| Nexp_var v -> doc_var ctx v
| Nexp_id id -> doc_id id
| Nexp_sum _ | Nexp_minus _ | Nexp_times _ | Nexp_neg _ | Nexp_exp _
| Nexp_app (Id_aux (Id ("div"|"mod"),_), [_;_])
| Nexp_app (Id_aux (Id "abs_atom",_), [_])
-> parens (plussub nexp)
| _ ->
raise (Reporting.err_unreachable l __POS__
("cannot pretty-print nexp \"" ^ string_of_nexp nexp ^ "\""))
in atomic nexp
(* Rewrite mangled names of type variables to the original names *)
let rec orig_nexp (Nexp_aux (nexp, l)) =
let rewrap nexp = Nexp_aux (nexp, l) in
match nexp with
| Nexp_var kid -> rewrap (Nexp_var (orig_kid kid))
| Nexp_times (n1, n2) -> rewrap (Nexp_times (orig_nexp n1, orig_nexp n2))
| Nexp_sum (n1, n2) -> rewrap (Nexp_sum (orig_nexp n1, orig_nexp n2))
| Nexp_minus (n1, n2) -> rewrap (Nexp_minus (orig_nexp n1, orig_nexp n2))
| Nexp_exp n -> rewrap (Nexp_exp (orig_nexp n))
| Nexp_neg n -> rewrap (Nexp_neg (orig_nexp n))
| _ -> rewrap nexp
let rec orig_nc (NC_aux (nc, l) as full_nc) =
let rewrap nc = NC_aux (nc, l) in
match nc with
| NC_equal (nexp1, nexp2) -> rewrap (NC_equal (orig_nexp nexp1, orig_nexp nexp2))
| NC_bounded_ge (nexp1, nexp2) -> rewrap (NC_bounded_ge (orig_nexp nexp1, orig_nexp nexp2))
| NC_bounded_gt (nexp1, nexp2) -> rewrap (NC_bounded_gt (orig_nexp nexp1, orig_nexp nexp2))
| NC_bounded_le (nexp1, nexp2) -> rewrap (NC_bounded_le (orig_nexp nexp1, orig_nexp nexp2))
| NC_bounded_lt (nexp1, nexp2) -> rewrap (NC_bounded_lt (orig_nexp nexp1, orig_nexp nexp2))
| NC_not_equal (nexp1, nexp2) -> rewrap (NC_not_equal (orig_nexp nexp1, orig_nexp nexp2))
| NC_set (kid,s) -> rewrap (NC_set (orig_kid kid, s))
| NC_or (nc1, nc2) -> rewrap (NC_or (orig_nc nc1, orig_nc nc2))
| NC_and (nc1, nc2) -> rewrap (NC_and (orig_nc nc1, orig_nc nc2))
| NC_app (f,args) -> rewrap (NC_app (f,List.map orig_typ_arg args))
| NC_var kid -> rewrap (NC_var (orig_kid kid))
| NC_true | NC_false -> full_nc
and orig_typ_arg (A_aux (arg,l)) =
let rewrap a = (A_aux (a,l)) in
match arg with
| A_nexp nexp -> rewrap (A_nexp (orig_nexp nexp))
| A_bool nc -> rewrap (A_bool (orig_nc nc))
| A_order _ | A_typ _ ->
raise (Reporting.err_unreachable l __POS__ "Tried to pass Type or Order kind to SMT function")
(* Returns the set of type variables that will appear in the Coq output,
which may be smaller than those in the Sail type. May need to be
updated with doc_typ *)
let rec coq_nvars_of_typ (Typ_aux (t,l)) =
let trec = coq_nvars_of_typ in
match t with
| Typ_id _ -> KidSet.empty
| Typ_var kid -> tyvars_of_nexp (orig_nexp (nvar kid))
| Typ_fn (t1,t2,_) -> List.fold_left KidSet.union (trec t2) (List.map trec t1)
| Typ_tup ts ->
List.fold_left (fun s t -> KidSet.union s (trec t))
KidSet.empty ts
| Typ_app(Id_aux (Id "register", _), [A_aux (A_typ etyp, _)]) ->
trec etyp
| Typ_app(Id_aux (Id "implicit", _),_)
(* TODO: update when complex atom types are sorted out *)
| Typ_app(Id_aux (Id "atom", _), _) -> KidSet.empty
| Typ_app(Id_aux (Id "atom_bool", _), _) -> KidSet.empty
| Typ_app (_,tas) ->
List.fold_left (fun s ta -> KidSet.union s (coq_nvars_of_typ_arg ta))
KidSet.empty tas
(* TODO: remove appropriate bound variables *)
| Typ_exist (_,_,t) -> trec t
| Typ_bidir _ -> unreachable l __POS__ "Coq doesn't support bidir types"
| Typ_internal_unknown -> unreachable l __POS__ "escaped Typ_internal_unknown"
and coq_nvars_of_typ_arg (A_aux (ta,_)) =
match ta with
| A_nexp nexp -> tyvars_of_nexp (orig_nexp nexp)
| A_typ typ -> coq_nvars_of_typ typ
| A_order _ -> KidSet.empty
| A_bool nc -> tyvars_of_constraint (orig_nc nc)
let maybe_expand_range_type (Typ_aux (typ,l) as full_typ) =
match typ with
| Typ_app(Id_aux (Id "range", _), [A_aux(A_nexp low,_);
A_aux(A_nexp high,_)]) ->
(* TODO: avoid name clashes *)
let kid = mk_kid "rangevar" in
let var = nvar kid in
let nc = nc_and (nc_lteq low var) (nc_lteq var high) in
Some (Typ_aux (Typ_exist ([mk_kopt K_int kid], nc, atom_typ var),Parse_ast.Generated l))
| Typ_id (Id_aux (Id "nat",_)) ->
let kid = mk_kid "n" in
let var = nvar kid in
Some (Typ_aux (Typ_exist ([mk_kopt K_int kid], nc_gteq var (nconstant Nat_big_num.zero), atom_typ var),
Parse_ast.Generated l))
| _ -> None
let expand_range_type typ = Util.option_default typ (maybe_expand_range_type typ)
let nice_and nc1 nc2 =
match nc1, nc2 with
| NC_aux (NC_true,_), _ -> nc2
| _, NC_aux (NC_true,_) -> nc1
| _,_ -> nc_and nc1 nc2
let nice_iff nc1 nc2 =
match nc1, nc2 with
| NC_aux (NC_true,_), _ -> nc2
| _, NC_aux (NC_true,_) -> nc1
| NC_aux (NC_false,_), _ -> nc_not nc2
| _, NC_aux (NC_false,_) -> nc_not nc1
(* TODO: replace this hacky iff with a proper NC_ constructor *)
| _,_ -> mk_nc (NC_app (mk_id "iff",[arg_bool nc1; arg_bool nc2]))
(* n_constraint functions are currently just Z3 functions *)
let doc_nc_fn (Id_aux (id,_) as full_id) =
match id with
| Id "not" -> string "negb"
| Operator "-->" -> string "implb"
| Id "iff" -> string "Bool.eqb"
| _ -> doc_id full_id
let merge_kid_count = KBindings.union (fun _ m n -> Some (m+n))
let rec count_nexp_vars (Nexp_aux (nexp,_)) =
match nexp with
| Nexp_id _
| Nexp_constant _
-> KBindings.empty
| Nexp_var kid -> KBindings.singleton kid 1
| Nexp_app (_,nes) ->
List.fold_left merge_kid_count KBindings.empty (List.map count_nexp_vars nes)
| Nexp_times (n1,n2)
| Nexp_sum (n1,n2)
| Nexp_minus (n1,n2)
-> merge_kid_count (count_nexp_vars n1) (count_nexp_vars n2)
| Nexp_exp n
| Nexp_neg n
-> count_nexp_vars n
let rec count_nc_vars (NC_aux (nc,_)) =
let count_arg (A_aux (arg,_)) =
match arg with
| A_bool nc -> count_nc_vars nc
| A_nexp nexp -> count_nexp_vars nexp
| A_typ _ | A_order _ -> KBindings.empty
in
match nc with
| NC_or (nc1,nc2)
| NC_and (nc1,nc2)
-> merge_kid_count (count_nc_vars nc1) (count_nc_vars nc2)
| NC_var kid
| NC_set (kid,_)
-> KBindings.singleton kid 1
| NC_equal (n1,n2)
| NC_bounded_ge (n1,n2)
| NC_bounded_gt (n1,n2)
| NC_bounded_le (n1,n2)
| NC_bounded_lt (n1,n2)
| NC_not_equal (n1,n2)
-> merge_kid_count (count_nexp_vars n1) (count_nexp_vars n2)
| NC_true | NC_false
-> KBindings.empty
| NC_app (_,args) ->
List.fold_left merge_kid_count KBindings.empty (List.map count_arg args)
(* Simplify some of the complex boolean types created by the Sail type checker,
whereever an existentially bound variable is used once in a trivial way,
for example exists b, b and exists n, n = 32. *)
type atom_bool_prop =
Bool_boring
| Bool_complex of kinded_id list * n_constraint * n_constraint
let simplify_atom_bool l kopts nc atom_nc =
(*prerr_endline ("simplify " ^ string_of_n_constraint nc ^ " for bool " ^ string_of_n_constraint atom_nc);*)
let counter = ref 0 in
let is_bound kid = List.exists (fun kopt -> Kid.compare kid (kopt_kid kopt) == 0) kopts in
let ty_vars = merge_kid_count (count_nc_vars nc) (count_nc_vars atom_nc) in
let lin_ty_vars = KBindings.filter (fun kid n -> is_bound kid && n = 1) ty_vars in
let rec simplify (NC_aux (nc,l) as nc_full) =
let is_ex_var news (NC_aux (nc,_)) =
match nc with
| NC_var kid when KBindings.mem kid lin_ty_vars -> Some kid
| NC_var kid when KidSet.mem kid news -> Some kid
| NC_equal (Nexp_aux (Nexp_var kid,_), _) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_equal (_, Nexp_aux (Nexp_var kid,_)) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_bounded_ge (Nexp_aux (Nexp_var kid,_), _) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_bounded_ge (_, Nexp_aux (Nexp_var kid,_)) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_bounded_gt (Nexp_aux (Nexp_var kid,_), _) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_bounded_gt (_, Nexp_aux (Nexp_var kid,_)) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_bounded_le (Nexp_aux (Nexp_var kid,_), _) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_bounded_le (_, Nexp_aux (Nexp_var kid,_)) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_bounded_lt (Nexp_aux (Nexp_var kid,_), _) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_bounded_lt (_, Nexp_aux (Nexp_var kid,_)) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_not_equal (Nexp_aux (Nexp_var kid,_), _) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_not_equal (_, Nexp_aux (Nexp_var kid,_)) when KBindings.mem kid lin_ty_vars -> Some kid
| NC_set (kid, _::_) when KBindings.mem kid lin_ty_vars -> Some kid
| _ -> None
in
let replace kills vars =
let v = mk_kid ("simp#" ^ string_of_int !counter) in
let kills = KidSet.union kills (KidSet.of_list vars) in
counter := !counter + 1;
KidSet.singleton v, kills, NC_aux (NC_var v,l)
in
match nc with
| NC_or (nc1,nc2) -> begin
let new1, kill1, nc1 = simplify nc1 in
let new2, kill2, nc2 = simplify nc2 in
let news, kills = KidSet.union new1 new2, KidSet.union kill1 kill2 in
match is_ex_var news nc1, is_ex_var news nc2 with
| Some kid1, Some kid2 -> replace kills [kid1;kid2]
| _ -> news, kills, NC_aux (NC_or (nc1,nc2),l)
end
| NC_and (nc1,nc2) -> begin
let new1, kill1, nc1 = simplify nc1 in
let new2, kill2, nc2 = simplify nc2 in
let news, kills = KidSet.union new1 new2, KidSet.union kill1 kill2 in
match is_ex_var news nc1, is_ex_var news nc2 with
| Some kid1, Some kid2 -> replace kills [kid1;kid2]
| _ -> news, kills, NC_aux (NC_and (nc1,nc2),l)
end
| NC_app (Id_aux (Id "not",_) as id,[A_aux (A_bool nc1,al)]) -> begin
let new1, kill1, nc1 = simplify nc1 in
match is_ex_var new1 nc1 with
| Some kid -> replace kill1 [kid]
| None -> new1, kill1, NC_aux (NC_app (id,[A_aux (A_bool nc1,al)]),l)
end
(* We don't currently recurse into general uses of NC_app, but the
"boring" cases we really want to get rid of won't contain
those. *)
| _ ->
match is_ex_var KidSet.empty nc_full with
| Some kid -> replace KidSet.empty [kid]
| None -> KidSet.empty, KidSet.empty, nc_full
in
let new_nc, kill_nc, nc = simplify nc in
let new_atom, kill_atom, atom_nc = simplify atom_nc in
let new_kids = KidSet.union new_nc new_atom in
let kill_kids = KidSet.union kill_nc kill_atom in
let kopts =
List.map (fun kid -> mk_kopt K_bool kid) (KidSet.elements new_kids) @
List.filter (fun kopt -> not (KidSet.mem (kopt_kid kopt) kill_kids)) kopts
in
(*prerr_endline ("now have " ^ string_of_n_constraint nc ^ " for bool " ^ string_of_n_constraint atom_nc);*)
match atom_nc with
| NC_aux (NC_var kid,_) when KBindings.mem kid lin_ty_vars -> Bool_boring
| NC_aux (NC_var kid,_) when KidSet.mem kid new_kids -> Bool_boring
| _ -> Bool_complex (kopts, nc, atom_nc)
type ex_kind = ExNone | ExGeneral
let string_of_ex_kind = function
| ExNone -> "none"
| ExGeneral -> "general"
(* Should a Sail type be turned into a dependent pair in Coq?
Optionally takes a variable that we're binding (to avoid trivial cases where
the type is exactly the boolean we're binding), and whether to turn bools
with interesting type-expressions into dependent pairs. *)
let classify_ex_type ctxt env ?binding ?(rawbools=false) (Typ_aux (t,l) as t0) =
let is_binding kid =
match binding, KBindings.find_opt kid ctxt.kid_id_renames with
| Some id, Some (Some id') when Id.compare id id' == 0 -> true
| _ -> false
in
let simplify_atom_bool l kopts nc atom_nc =
match simplify_atom_bool l kopts nc atom_nc with
| Bool_boring -> Bool_boring
| Bool_complex (_,_,NC_aux (NC_var kid,_)) when is_binding kid -> Bool_boring
| Bool_complex (x,y,z) -> Bool_complex (x,y,z)
in
match t with
| Typ_exist (kopts,nc,Typ_aux (Typ_app (Id_aux (Id "atom_bool",_), [A_aux (A_bool atom_nc,_)]),_)) -> begin
match simplify_atom_bool l kopts nc atom_nc with
| Bool_boring -> ExNone, [], bool_typ
| Bool_complex _ -> ExGeneral, [], bool_typ
end
| Typ_app (Id_aux (Id "atom_bool",_), [A_aux (A_bool atom_nc,_)]) -> begin
match rawbools, simplify_atom_bool l [] nc_true atom_nc with
| false, _ -> ExNone, [], bool_typ
| _,Bool_boring -> ExNone, [], bool_typ
| _,Bool_complex _ -> ExGeneral, [], bool_typ
end
| Typ_exist (kopts,_,t1) -> ExGeneral,kopts,t1
| _ -> ExNone,[],t0
let rec flatten_nc (NC_aux (nc,l) as nc_full) =
match nc with
| NC_and (nc1,nc2) -> flatten_nc nc1 @ flatten_nc nc2
| _ -> [nc_full]
(* When making changes here, check whether they affect coq_nvars_of_typ *)
let rec doc_typ_fns ctx env =
(* following the structure of parser for precedence *)
let rec typ ty = fn_typ true ty
and typ' ty = fn_typ false ty
and fn_typ atyp_needed ((Typ_aux (t, _)) as ty) = match t with
| Typ_fn(args,ret,efct) ->
let ret_typ =
if effectful efct
then separate space [string "M"; fn_typ true ret]
else separate space [fn_typ false ret] in
let arg_typs = List.map (app_typ false) args in
let tpp = separate (space ^^ arrow ^^ space) (arg_typs @ [ret_typ]) in
(* once we have proper excetions we need to know what the exceptions type is *)
if atyp_needed then parens tpp else tpp
| _ -> tup_typ atyp_needed ty
and tup_typ atyp_needed ((Typ_aux (t, _)) as ty) = match t with
| Typ_tup typs ->
parens (separate_map (space ^^ star ^^ space) (app_typ false) typs)
| _ -> app_typ atyp_needed ty
and app_typ atyp_needed ((Typ_aux (t, l)) as ty) = match t with
| Typ_app(Id_aux (Id "bitvector", _), [
A_aux (A_nexp m, _);
A_aux (A_order ord, _)]) ->
(* TODO: remove duplication with exists, below *)
let tpp = string "mword " ^^ doc_nexp ctx m in
if atyp_needed then parens tpp else tpp
| Typ_app(Id_aux (Id "vector", _), [
A_aux (A_nexp m, _);
A_aux (A_order ord, _);
A_aux (A_typ elem_typ, _)]) ->
(* TODO: remove duplication with exists, below *)
let tpp = string "vec" ^^ space ^^ typ elem_typ ^^ space ^^ doc_nexp ctx m in
if atyp_needed then parens tpp else tpp
| Typ_app(Id_aux (Id "register", _), [A_aux (A_typ etyp, _)]) ->
let tpp = string "register_ref regstate register_value " ^^ typ etyp in
if atyp_needed then parens tpp else tpp
| Typ_app(Id_aux (Id "range", _), _)
| Typ_id (Id_aux (Id "nat", _)) ->
(match maybe_expand_range_type ty with
| Some typ -> atomic_typ atyp_needed typ
| None -> raise (Reporting.err_unreachable l __POS__ "Bad range type"))
| Typ_app(Id_aux (Id "implicit", _),_) ->
(string "Z")
| Typ_app(Id_aux (Id "atom", _), [A_aux(A_nexp n,_)]) ->
(string "Z")
| Typ_app(Id_aux (Id "atom_bool", _), [A_aux (A_bool atom_nc,_)]) ->
begin match simplify_atom_bool l [] nc_true atom_nc with
| Bool_boring -> string "bool"
| Bool_complex (_,_,atom_nc) -> (* simplify won't introduce new kopts *)
let var = mk_kid "_bool" in (* TODO collision avoid *)
let nc = nice_iff atom_nc (nc_var var) in
braces (separate space
[doc_var ctx var; colon; string "bool";
ampersand;
doc_arithfact ctx env nc])
end
| Typ_app(id,args) ->
let tpp = (doc_id_type id) ^^ space ^^ (separate_map space doc_typ_arg args) in
if atyp_needed then parens tpp else tpp
| _ -> atomic_typ atyp_needed ty
and atomic_typ atyp_needed ((Typ_aux (t, l)) as ty) = match t with
| Typ_id (Id_aux (Id "bool",_)) -> string "bool"
| Typ_id (Id_aux (Id "bit",_)) -> string "bitU"
| Typ_id (id) ->
(*if List.exists ((=) (string_of_id id)) regtypes
then string "register"
else*) doc_id_type id
| Typ_var v -> doc_var ctx v
| Typ_app _ | Typ_tup _ | Typ_fn _ ->
(* exhaustiveness matters here to avoid infinite loops
* if we add a new Typ constructor *)
let tpp = typ ty in
if atyp_needed then parens tpp else tpp
(* TODO: handle non-integer kopts *)
| Typ_exist (kopts,nc,ty') -> begin
let kopts,nc,ty' = match maybe_expand_range_type ty' with
| Some (Typ_aux (Typ_exist (kopts',nc',ty'),_)) ->
kopts'@kopts,nc_and nc nc',ty'
| _ -> kopts,nc,ty'
in
match ty' with
| Typ_aux (Typ_app (Id_aux (Id "atom",_),
[A_aux (A_nexp nexp,_)]),_) ->
begin match nexp, kopts with
| (Nexp_aux (Nexp_var kid,_)), [kopt] when Kid.compare kid (kopt_kid kopt) == 0 ->
braces (separate space [doc_var ctx kid; colon; string "Z";
ampersand; doc_arithfact ctx env nc])
| _ ->
let var = mk_kid "_atom" in (* TODO collision avoid *)
let nc = nice_and (nc_eq (nvar var) nexp) nc in
braces (separate space [doc_var ctx var; colon; string "Z";
ampersand; doc_arithfact ctx env ~exists:(List.map kopt_kid kopts) nc])
end
| Typ_aux (Typ_app (Id_aux (Id "bitvector",_),
[A_aux (A_nexp m, _);
A_aux (A_order ord, _)]), _) ->
(* TODO: proper handling of m, complex elem type, dedup with above *)
let var = mk_kid "_vec" in (* TODO collision avoid *)
let kid_set = KidSet.of_list (List.map kopt_kid kopts) in
let m_pp = doc_nexp ctx ~skip_vars:kid_set m in
let tpp, len_pp = string "mword " ^^ m_pp, string "length_mword" in
let length_constraint_pp =
if KidSet.is_empty (KidSet.inter kid_set (nexp_frees m))
then None
else Some (separate space [len_pp; doc_var ctx var; string "=?"; doc_nexp ctx m])
in
braces (separate space
[doc_var ctx var; colon; tpp;
ampersand;
doc_arithfact ctx env ~exists:(List.map kopt_kid kopts) ?extra:length_constraint_pp nc])
| Typ_aux (Typ_app (Id_aux (Id "vector",_),
[A_aux (A_nexp m, _);
A_aux (A_order ord, _);
A_aux (A_typ elem_typ, _)]),_) ->
(* TODO: proper handling of m, complex elem type, dedup with above *)
let var = mk_kid "_vec" in (* TODO collision avoid *)
let kid_set = KidSet.of_list (List.map kopt_kid kopts) in
let m_pp = doc_nexp ctx ~skip_vars:kid_set m in
let tpp, len_pp = string "vec" ^^ space ^^ typ elem_typ ^^ space ^^ m_pp, string "vec_length" in
let length_constraint_pp =
if KidSet.is_empty (KidSet.inter kid_set (nexp_frees m))
then None
else Some (separate space [len_pp; doc_var ctx var; string "=?"; doc_nexp ctx m])
in
braces (separate space
[doc_var ctx var; colon; tpp;
ampersand;
doc_arithfact ctx env ~exists:(List.map kopt_kid kopts) ?extra:length_constraint_pp nc])
| Typ_aux (Typ_app (Id_aux (Id "atom_bool",_), [A_aux (A_bool atom_nc,_)]),_) -> begin
match simplify_atom_bool l kopts nc atom_nc with
| Bool_boring -> string "bool"
| Bool_complex (kopts,nc,atom_nc) ->
let var = mk_kid "_bool" in (* TODO collision avoid *)
let nc = nice_and (nice_iff atom_nc (nc_var var)) nc in
braces (separate space
[doc_var ctx var; colon; string "bool";
ampersand;
doc_arithfact ctx env ~exists:(List.map kopt_kid kopts) nc])
end
| Typ_aux (Typ_tup tys,l) -> begin
(* TODO: boolean existentials *)
let kid_set = KidSet.of_list (List.map kopt_kid kopts) in
let should_keep (Typ_aux (ty,_)) =
match ty with
| Typ_app (Id_aux (Id "atom",_), [A_aux (A_nexp (Nexp_aux (Nexp_var var,_)),_)]) ->
not (KidSet.mem var kid_set)
| _ -> true
in
let out_tys = List.filter should_keep tys in
let binding_of_tyvar (KOpt_aux (KOpt_kind (K_aux (kind,_) as kaux,kid),_)) =
let kind_pp = match kind with
| K_int -> string "Z"
| _ ->
raise (Reporting.err_todo l
("Non-atom existential type over " ^ string_of_kind kaux ^ " not yet supported in Coq: " ^
string_of_typ ty))
in doc_var ctx kid, kind_pp
in
let exvars_pp = List.map binding_of_tyvar kopts in
let pat = match exvars_pp with
| [v,k] -> v ^^ space ^^ colon ^^ space ^^ k
| _ ->
let vars, types = List.split exvars_pp in
squote ^^ parens (separate (string ", ") vars) ^/^
colon ^/^ parens (separate (string " * ") types)
in
group (braces (group (pat ^^ space ^^ ampersand) ^/^
group (tup_typ true (Typ_aux (Typ_tup out_tys,l)) ^^
string "%type ") ^^
ampersand ^/^
doc_arithfact ctx env nc))
end
| _ ->
raise (Reporting.err_todo l
("Non-atom existential type not yet supported in Coq: " ^
string_of_typ ty))
end
(*
let add_tyvar tpp kid =
braces (separate space [doc_var ctx kid; colon; string "Z"; ampersand; tpp])
in
match drop_duplicate_atoms kids ty with
| Some ty ->
let tpp = typ ty in
let tpp = match nc with NC_aux (NC_true,_) -> tpp | _ ->
braces (separate space [underscore; colon; parens (doc_arithfact ctx nc); ampersand; tpp])
in
List.fold_left add_tyvar tpp kids
| None ->
match nc with
(* | NC_aux (NC_true,_) -> List.fold_left add_tyvar (string "Z") (List.tl kids)*)
| _ -> List.fold_left add_tyvar (doc_arithfact ctx nc) kids
end*)
| Typ_bidir _ -> unreachable l __POS__ "Coq doesn't support bidir types"
| Typ_internal_unknown -> unreachable l __POS__ "escaped Typ_internal_unknown"
and doc_typ_arg ?(prop_vars = false) (A_aux(t,_)) = match t with
| A_typ t -> app_typ true t
| A_nexp n -> doc_nexp ctx n
| A_order o -> empty
| A_bool nc -> parens (doc_nc_exp ctx env nc)
in typ', atomic_typ, doc_typ_arg
and doc_typ ctx env = let f,_,_ = doc_typ_fns ctx env in f
and doc_atomic_typ ctx env = let _,f,_ = doc_typ_fns ctx env in f
and doc_typ_arg ctx env = let _,_,f = doc_typ_fns ctx env in f
and doc_arithfact ctxt env ?(exists = []) ?extra nc =
let prop = doc_nc_exp ctxt env nc in
let prop = match extra with
| None -> prop
| Some pp -> separate space [parens pp; string "&&"; parens prop]
in
let prop = prop in
match exists with
| [] -> string "ArithFact" ^^ space ^^ parens prop
| _ -> string "ArithFactP" ^^ space ^^
parens (separate space ([string "exists"]@(List.map (doc_var ctxt) exists)@[comma; prop; equals; string "true"]))
(* Follows Coq precedence levels *)
and doc_nc_exp ctx env nc =
let locals = Env.get_locals env |> Bindings.bindings in
let nc = Env.expand_constraint_synonyms env nc in
let nc_id_map =
List.fold_left
(fun m (v,(_,Typ_aux (typ,_))) ->
match typ with
| Typ_app (id, [A_aux (A_bool nc,_)]) when string_of_id id = "atom_bool" ->
(flatten_nc nc, v)::m
| _ -> m) [] locals
in
(* Look for variables in the environment which exactly express the nc, and use
them instead. As well as often being shorter, this avoids unbound type
variables added by Sail's type checker. *)
let rec newnc f nc =
let ncs = flatten_nc nc in
let candidates =
Util.map_filter (fun (ncs',id) -> Util.option_map (fun x -> x,id) (list_contains NC.compare ncs ncs')) nc_id_map
in
match List.sort (fun (l,_) (l',_) -> compare l l') candidates with
| ([],id)::_ -> doc_id id
| ((h::t),id)::_ -> parens (doc_op (string "&&") (doc_id id) (l10 (List.fold_left nc_and h t)))
| [] -> f nc
and l70 (NC_aux (nc,_) as nc_full) =
match nc with
| NC_equal (ne1, ne2) -> doc_op (string "=?") (doc_nexp ctx ne1) (doc_nexp ctx ne2)
| NC_bounded_ge (ne1, ne2) -> doc_op (string ">=?") (doc_nexp ctx ne1) (doc_nexp ctx ne2)
| NC_bounded_gt (ne1, ne2) -> doc_op (string ">?") (doc_nexp ctx ne1) (doc_nexp ctx ne2)
| NC_bounded_le (ne1, ne2) -> doc_op (string "<=?") (doc_nexp ctx ne1) (doc_nexp ctx ne2)
| NC_bounded_lt (ne1, ne2) -> doc_op (string "<?") (doc_nexp ctx ne1) (doc_nexp ctx ne2)
| _ -> l50 nc_full
and l50 (NC_aux (nc,_) as nc_full) =
match nc with
| NC_or (nc1, nc2) -> doc_op (string "||") (newnc l50 nc1) (newnc l40 nc2)
| _ -> l40 nc_full
and l40 (NC_aux (nc,_) as nc_full) =
match nc with
| NC_and (nc1, nc2) -> doc_op (string "&&") (newnc l40 nc1) (newnc l10 nc2)
| _ -> l10 nc_full
and l10 (NC_aux (nc,_) as nc_full) =
match nc with
| NC_not_equal (ne1, ne2) -> string "negb" ^^ space ^^ parens (doc_op (string "=?") (doc_nexp ctx ne1) (doc_nexp ctx ne2))
| NC_set (kid, is) ->
separate space [string "member_Z_list"; doc_var ctx kid;
brackets (separate (string "; ")
(List.map (fun i -> string (Nat_big_num.to_string i)) is))]
| NC_app (f,args) -> separate space (doc_nc_fn f::List.map doc_typ_arg_exp args)
| _ -> l0 nc_full
and l0 (NC_aux (nc,_) as nc_full) =
match nc with
| NC_true -> string "true"
| NC_false -> string "false"
| NC_var kid -> doc_nexp ctx (nvar kid)
| NC_not_equal _
| NC_set _
| NC_app _
| NC_equal _
| NC_bounded_ge _
| NC_bounded_gt _
| NC_bounded_le _
| NC_bounded_lt _
| NC_or _
| NC_and _ -> parens (l70 nc_full)
and doc_typ_arg_exp (A_aux (arg,l)) =
match arg with
| A_nexp nexp -> doc_nexp ctx nexp
| A_bool nc -> newnc l0 nc
| A_order _ | A_typ _ ->
raise (Reporting.err_unreachable l __POS__ "Tried to pass Type or Order kind to SMT function")
in newnc l70 nc
(* Check for variables in types that would be pretty-printed and are not
bound in the val spec of the function. *)
let contains_t_pp_var ctxt (Typ_aux (t,a) as typ) =
KidSet.subset (coq_nvars_of_typ typ) ctxt.bound_nvars
(* TODO: should we resurrect this?
let replace_typ_size ctxt env (Typ_aux (t,a)) =
match t with
| Typ_app (Id_aux (Id "vector",_) as id, [A_aux (A_nexp size,_);ord;typ']) ->
begin
let mk_typ nexp =
Some (Typ_aux (Typ_app (id, [A_aux (A_nexp nexp,Parse_ast.Unknown);ord;typ']),a))
in
match Type_check.solve env size with
| Some n -> mk_typ (nconstant n)
| None ->
let is_equal nexp =
prove __POS__ env (NC_aux (NC_equal (size,nexp),Parse_ast.Unknown))
in match List.find is_equal (NexpSet.elements ctxt.bound_nexps) with
| nexp -> mk_typ nexp
| exception Not_found -> None
end
| _ -> None*)
let doc_tannot ctxt env eff typ =
let of_typ typ =
let ta = doc_typ ctxt env typ in
if eff then
if ctxt.early_ret
then string " : MR " ^^ parens ta ^^ string " _"
else string " : M " ^^ parens ta
else string " : " ^^ ta
in of_typ typ
(* Only double-quotes need escaped - by doubling them. *)
let coq_escape_string s =
Str.global_replace (Str.regexp "\"") "\"\"" s
let doc_lit (L_aux(lit,l)) =
match lit with
| L_unit -> utf8string "tt"
| L_zero -> utf8string "B0"
| L_one -> utf8string "B1"
| L_false -> utf8string "false"
| L_true -> utf8string "true"
| L_num i ->
let s = Big_int.to_string i in
let ipp = utf8string s in
if Big_int.less i Big_int.zero then parens ipp else ipp
(* Not a typo, the bbv hex notation uses the letter O *)
| L_hex n -> utf8string ("Ox\"" ^ n ^ "\"")
| L_bin n -> utf8string ("'b\"" ^ n ^ "\"")
| L_undef ->
utf8string "(Fail \"undefined value of unsupported type\")"
| L_string s -> utf8string ("\"" ^ (coq_escape_string s) ^ "\"")
| L_real s ->
(* Lem does not support decimal syntax, so we translate a string
of the form "x.y" into the ratio (x * 10^len(y) + y) / 10^len(y).
The OCaml library has a conversion function from strings to floats, but
not from floats to ratios. ZArith's Q library does have the latter, but
using this would require adding a dependency on ZArith to Sail. *)
let parts = Util.split_on_char '.' s in
let (num, denom) = match parts with
| [i] -> (Big_int.of_string i, Big_int.of_int 1)
| [i;f] ->
let denom = Big_int.pow_int_positive 10 (String.length f) in
(Big_int.add (Big_int.mul (Big_int.of_string i) denom) (Big_int.of_string f), denom)
| _ ->
raise (Reporting.err_syntax_loc l "could not parse real literal") in
parens (separate space (List.map string [
"realFromFrac"; Big_int.to_string num; Big_int.to_string denom]))
let doc_quant_item_id ?(prop_vars=false) ctx delimit (QI_aux (qi,_)) =
match qi with
| QI_id (KOpt_aux (KOpt_kind (K_aux (kind,_),kid),_)) -> begin
if KBindings.mem kid ctx.kid_id_renames then None else
match kind with
| K_type -> Some (delimit (separate space [doc_var ctx kid; colon; string "Type"]))
| K_int -> Some (delimit (separate space [doc_var ctx kid; colon; string "Z"]))
| K_order -> None
| K_bool -> Some (delimit (separate space [doc_var ctx kid; colon;
string (if prop_vars then "Prop" else "bool")]))
end
| QI_constraint nc -> None
| QI_constant _ -> None
let quant_item_id_name ctx (QI_aux (qi,_)) =
match qi with
| QI_id (KOpt_aux (KOpt_kind (K_aux (kind,_),kid),_)) -> begin
if KBindings.mem kid ctx.kid_id_renames then None else
match kind with
| K_type -> Some (doc_var ctx kid)
| K_int -> Some (doc_var ctx kid)
| K_order -> None
| K_bool -> Some (doc_var ctx kid)
end
| QI_constraint nc -> None
| QI_constant _ -> None
let doc_quant_item_constr ?(prop_vars=false) ctx env delimit (QI_aux (qi,_)) =
match qi with
| QI_id _ -> None
| QI_constant _ -> None
| QI_constraint nc -> Some (bquote ^^ braces (doc_arithfact ctx env nc))
(* At the moment these are all anonymous - when used we rely on Coq to fill
them in. *)
let quant_item_constr_name ctx (QI_aux (qi,_)) =
match qi with
| QI_id _ -> None
| QI_constant _ -> None
| QI_constraint nc -> Some underscore
let doc_typquant_items ?(prop_vars=false) ctx env delimit (TypQ_aux (tq,_)) =
match tq with
| TypQ_tq qis ->
separate_opt space (doc_quant_item_id ~prop_vars ctx delimit) qis ^^
separate_opt space (doc_quant_item_constr ~prop_vars ctx env delimit) qis
| TypQ_no_forall -> empty
let doc_typquant_items_separate ctx env delimit (TypQ_aux (tq,_)) =
match tq with
| TypQ_tq qis ->
Util.map_filter (doc_quant_item_id ctx delimit) qis,
Util.map_filter (doc_quant_item_constr ctx env delimit) qis
| TypQ_no_forall -> [], []
let typquant_names_separate ctx (TypQ_aux (tq,_)) =
match tq with
| TypQ_tq qis ->
Util.map_filter (quant_item_id_name ctx) qis,
Util.map_filter (quant_item_constr_name ctx) qis
| TypQ_no_forall -> [], []
let doc_typquant ctx env (TypQ_aux(tq,_)) typ = match tq with
| TypQ_tq ((_ :: _) as qs) ->
string "forall " ^^ separate_opt space (doc_quant_item_id ctx braces) qs ^/^
separate_opt space (doc_quant_item_constr ctx env parens) qs ^^ string ", " ^^ typ