summaryrefslogtreecommitdiff
path: root/compiler/ExtractBase.ml
blob: 438a3475962643e3f28f470b732a81f678969099 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
(** Define base utilities for the extraction *)

open Pure
open TranslateCore
module C = Contexts
module RegionVarId = T.RegionVarId
module F = Format
open ExtractAssumed

(** The local logger *)
let log = L.pure_to_extract_log

type region_group_info = {
  id : RegionGroupId.id;
      (** The id of the region group.
          Note that a simple way of generating unique names for backward
          functions is to use the region group ids.
       *)
  region_names : string option list;
      (** The names of the region variables included in this group.
          Note that names are not always available...
       *)
}

module StringSet = Collections.MakeSet (Collections.OrderedString)
module StringMap = Collections.MakeMap (Collections.OrderedString)

type name = Names.name
type type_name = Names.type_name
type global_name = Names.global_name
type fun_name = Names.fun_name

(** Characterizes a declaration.

    Is in particular useful to derive the proper keywords to introduce the
    declarations/definitions.
 *)
type decl_kind =
  | SingleNonRec
      (** A single, non-recursive definition.

          F*:  [let x = ...]
          Coq: [Definition x := ...]
       *)
  | SingleRec
      (** A single, recursive definition.

          F*:  [let rec x = ...]
          Coq: [Fixpoint x := ...]
       *)
  | MutRecFirst
      (** The first definition of a group of mutually-recursive definitions.

          F*:  [type x0 = ... and x1 = ...]
          Coq: [Fixpoint x0 := ... with x1 := ...]
       *)
  | MutRecInner
      (** An inner definition in a group of mutually-recursive definitions. *)
  | MutRecLast
      (** The last definition in a group of mutually-recursive definitions.

          We need this because in some theorem provers like Coq, we need to
          delimit group of mutually recursive definitions (in particular, we
          need to insert an end delimiter).
       *)
  | Assumed
      (** An assumed definition.

         F*:  [assume val x]
         Coq: [Axiom x : Type.]
      *)
  | Declared
      (** Declare a type in an interface or a module signature.

          Rem.: for now, in Coq, we don't declare module signatures: we
          thus assume the corresponding declarations.

          F*:  [val x : Type0]
          Coq: [Axiom x : Type.]
       *)

(** Return [true] if the declaration is the last from its group of declarations.

    We need this because in some provers (e.g., Coq), we need to delimit the
    end of a (group of) definition(s) (in Coq: with a ".").
 *)
let decl_is_last_from_group (kind : decl_kind) : bool =
  match kind with
  | SingleNonRec | SingleRec | MutRecLast | Assumed | Declared -> true
  | MutRecFirst | MutRecInner -> false

let decl_is_from_rec_group (kind : decl_kind) : bool =
  match kind with
  | SingleNonRec | Assumed | Declared -> false
  | SingleRec | MutRecFirst | MutRecInner | MutRecLast -> true

let decl_is_from_mut_rec_group (kind : decl_kind) : bool =
  match kind with
  | SingleNonRec | SingleRec | Assumed | Declared -> false
  | MutRecFirst | MutRecInner | MutRecLast -> true

let decl_is_first_from_group (kind : decl_kind) : bool =
  match kind with
  | SingleNonRec | SingleRec | MutRecFirst | Assumed | Declared -> true
  | MutRecLast | MutRecInner -> false

(** Return [true] if the declaration is not the last from its group of declarations.

    We need this because in some provers (e.g., HOL4), we need to delimit
    the inner declarations (with `/\` for instance).
 *)
let decl_is_not_last_from_group (kind : decl_kind) : bool =
  not (decl_is_last_from_group kind)

type type_decl_kind = Enum | Struct

(* TODO: this should be a module we give to a functor! *)

(** A formatter's role is twofold:
    1. Come up with name suggestions.
    For instance, provided some information about a function (its basename,
    information about the region group, etc.) it should come up with an
    appropriate name for the forward/backward function.

    It can of course apply many transformations, like changing to camel case/
    snake case, adding prefixes/suffixes, etc.

    2. Format some specific terms, like constants.

    TODO: unclear that this is useful now that all the backends are so much
    entangled in Extract.ml
 *)
type formatter = {
  bool_name : string;
  char_name : string;
  int_name : integer_type -> string;
  str_name : string;
  type_decl_kind_to_qualif :
    decl_kind -> type_decl_kind option -> string option;
      (** Compute the qualified for a type definition/declaration.

          For instance: "type", "and", etc.

          Remark: can return [None] for some backends like HOL4.
       *)
  fun_decl_kind_to_qualif : decl_kind -> string option;
      (** Compute the qualified for a function definition/declaration.

          For instance: "let", "let rec", "and", etc.

          Remark: can return [None] for some backends like HOL4.
       *)
  field_name : name -> FieldId.id -> string option -> string;
      (** Inputs:
          - type name
          - field id
          - field name

          Note that fields don't always have names, but we still need to
          generate some names if we want to extract the structures to records...
          We might want to extract such structures to tuples, later, but field
          access then causes trouble because not all provers accept syntax like
          [x.3] where [x] is a tuple.
       *)
  variant_name : name -> string -> string;
      (** Inputs:
          - type name
          - variant name
       *)
  struct_constructor : name -> string;
      (** Structure constructors are used when constructing structure values.

          For instance, in F*:
          {[
            type pair = { x : nat; y : nat }
            let p : pair = Mkpair 0 1
          ]}

          Inputs:
          - type name
       *)
  type_name : type_name -> string;
      (** Provided a basename, compute a type name. *)
  global_name : global_name -> string;
      (** Provided a basename, compute a global name. *)
  fun_name :
    fun_name ->
    int ->
    LoopId.id option ->
    int ->
    region_group_info option ->
    bool * int ->
    string;
      (** Compute the name of a regular (non-assumed) function.

          Inputs:
          - function basename (TODO: shouldn't appear for assumed functions?...)
          - number of loops in the function (useful to check if we need to use
            indices to derive unique names for the loops for instance - if there is
            exactly one loop, we don't need to use indices)
          - loop id (if pertinent)
          - number of region groups
          - region group information in case of a backward function
            ([None] if forward function)
          - pair:
            - do we generate the forward function (it may have been filtered)?
            - the number of *extracted backward functions* (same comment as for
              the number of loops)
              The number of extracted backward functions if not necessarily
              equal to the number of region groups, because we may have
              filtered some of them.
          TODO: use the fun id for the assumed functions.
       *)
  termination_measure_name :
    A.FunDeclId.id -> fun_name -> int -> LoopId.id option -> string;
      (** Generates the name of the termination measure used to prove/reason about
          termination. The generated code uses this clause where needed,
          but its body must be defined by the user.

          F* and Lean only.

          Inputs:
          - function id: this is especially useful to identify whether the
            function is an assumed function or a local function
          - function basename
          - the number of loops in the parent function. This is used for
            the same purpose as in {!field:fun_name}.
          - loop identifier, if this is for a loop
       *)
  decreases_proof_name :
    A.FunDeclId.id -> fun_name -> int -> LoopId.id option -> string;
      (** Generates the name of the proof used to prove/reason about
          termination. The generated code uses this clause where needed,
          but its body must be defined by the user.

          Lean only.

          Inputs:
          - function id: this is especially useful to identify whether the
            function is an assumed function or a local function
          - function basename
          - the number of loops in the parent function. This is used for
            the same purpose as in {!field:fun_name}.
          - loop identifier, if this is for a loop
       *)
  trait_decl_name : trait_decl -> string;
  trait_impl_name : trait_impl -> string;
  trait_parent_clause_name : trait_decl -> trait_clause -> string;
  trait_const_name : trait_decl -> string -> string;
  trait_type_name : trait_decl -> string -> string;
  trait_method_name : trait_decl -> string -> string;
  trait_type_clause_name : trait_decl -> string -> trait_clause -> string;
  opaque_pre : unit -> string;
      (** TODO: obsolete, remove.

          The prefix to use for opaque definitions.

          We need this because for some backends like Lean and Coq, we group
          opaque definitions in module signatures, meaning that using those
          definitions requires to prefix them with a module parameter name (such
          as "opaque_defs.").

          For instance, if we have an opaque function [f : int -> int], which
          is used by the non-opaque function [g], we would generate (in Coq):
          {[
            (* The module signature declaring the opaque definitions *)
            module type OpaqueDefs = {
              f_fwd : int -> int
              ... (* Other definitions *)
            }

            (* The definitions generated for the non-opaque definitions *)
            module Funs (opaque: OpaqueDefs) = {
              let g ... =
                ...
                opaque_defs.f_fwd
                ...
            }
          ]}

          Upon using [f] in [g], we don't directly use the the name "f_fwd",
          but prefix it with the "opaque_defs." identifier.
       *)
  var_basename : StringSet.t -> string option -> ty -> string;
      (** Generates a variable basename.

          Inputs:
          - the set of names used in the context so far
          - the basename we got from the symbolic execution, if we have one
          - the type of the variable (can be useful for heuristics, in order
            not to always use "x" for instance, whenever naming anonymous
            variables)

          Note that once the formatter generated a basename, we add an index
          if necessary to prevent name clashes: the burden of name clashes checks
          is thus on the caller's side.
       *)
  type_var_basename : StringSet.t -> string -> string;
      (** Generates a type variable basename. *)
  const_generic_var_basename : StringSet.t -> string -> string;
      (** Generates a const generic variable basename. *)
  trait_self_clause_basename : string;
  trait_clause_basename : StringSet.t -> trait_clause -> string;
      (** Return a base name for a trait clause. We might add a suffix to prevent
          collisions.

          In the traduction we explicitely manipulate the trait clause instances,
          that is we introduce one input variable for each trait clause.
       *)
  append_index : string -> int -> string;
      (** Appends an index to a name - we use this to generate unique
          names: when doing so, the role of the formatter is just to concatenate
          indices to names, the responsability of finding a proper index is
          delegated to helper functions.
       *)
  extract_literal : F.formatter -> bool -> literal -> unit;
      (** Format a constant value.

          Inputs:
          - formatter
          - [inside]: if [true], the value should be wrapped in parentheses
            if it is made of an application (ex.: [U32 3])
          - the constant value
       *)
  extract_unop :
    (bool -> texpression -> unit) ->
    F.formatter ->
    bool ->
    unop ->
    texpression ->
    unit;
      (** Format a unary operation

          Inputs:
          - a formatter for expressions (called on the argument of the unop)
          - extraction context (see below)
          - formatter
          - expression formatter
          - [inside]
          - unop
          - argument
       *)
  extract_binop :
    (bool -> texpression -> unit) ->
    F.formatter ->
    bool ->
    E.binop ->
    integer_type ->
    texpression ->
    texpression ->
    unit;
      (** Format a binary operation

          Inputs:
          - a formatter for expressions (called on the arguments of the binop)
          - extraction context (see below)
          - formatter
          - expression formatter
          - [inside]
          - binop
          - argument 0
          - argument 1
       *)
}

(** We use identifiers to look for name clashes *)
type id =
  | GlobalId of A.GlobalDeclId.id
  | FunId of fun_id
  | TerminationMeasureId of (A.fun_id * LoopId.id option)
      (** The definition which provides the decreases/termination measure.
          We insert calls to this clause to prove/reason about termination:
          the body of those clauses must be defined by the user, in the
          proper files.

          More specifically:
          - in F*, this is the content of the [decreases] clause.
            Example:
            ========
            {[
              let rec sum (ls : list nat) : Tot nat (decreases ls) = ...
            ]}
          - in Lean, this is the content of the [termination_by] clause.
       *)
  | DecreasesProofId of (A.fun_id * LoopId.id option)
      (** The definition which provides the decreases/termination proof.
          We insert calls to this clause to prove/reason about termination:
          the body of those clauses must be defined by the user, in the
          proper files.

          More specifically:
          - F* doesn't use this.
          - in Lean, this is the tactic used by the [decreases_by] annotations.
       *)
  | TypeId of type_id
  | StructId of type_id
      (** We use this when we manipulate the names of the structure
          constructors.

          For instance, in F*:
          {[
            type pair = { x: nat; y : nat }
            let p : pair = Mkpair 0 1
          ]}
       *)
  | VariantId of type_id * VariantId.id
      (** If often happens that variant names must be unique (it is the case in
          F* ) which is why we register them here.
       *)
  | FieldId of type_id * FieldId.id
      (** If often happens that in the case of structures, the field names
          must be unique (it is the case in F* ) which is why we register
          them here.
       *)
  | TypeVarId of TypeVarId.id
  | ConstGenericVarId of ConstGenericVarId.id
  | VarId of VarId.id
  | TraitDeclId of TraitDeclId.id
  | TraitImplId of TraitImplId.id
  | LocalTraitClauseId of TraitClauseId.id
  | TraitMethodId of TraitDeclId.id * string * T.RegionGroupId.id option
      (** Something peculiar with trait methods: because we have to take into
          account forward/backward functions, we may need to generate fields
          items per method.
       *)
  | TraitItemId of TraitDeclId.id * string
      (** A trait associated item which is not a method *)
  | TraitParentClauseId of TraitDeclId.id * TraitClauseId.id
  | TraitItemClauseId of TraitDeclId.id * string * TraitClauseId.id
  | TraitSelfClauseId
      (** Specifically for the clause: [Self : Trait].

          For now, we forbid provided methods (methods in trait declarations
          with a default implementation) from being overriden in trait implementations.
          We extract trait provided methods such that they take an instance of
          the trait as input: this instance is given by the trait self clause.

          For instance:
          {[
            //
            // Rust
            //
            trait ToU64 {
              fn to_u64(&self) -> u64;

              // Provided method
              fn is_pos(&self) -> bool {
                self.to_u64() > 0
              }
            }

            //
            // Generated code
            //
            struct ToU64 (T : Type) {
              to_u64 : T -> u64;
            }

            //                    The trait self clause
            //                    vvvvvvvvvvvvvvvvvvvvvv
            let is_pos (T : Type) (trait_self : ToU64 T) (self : T) : bool =
              trait_self.to_u64 self > 0
          ]}
       *)
  | UnknownId
      (** Used for stored various strings like keywords, definitions which
          should always be in context, etc. and which can't be linked to one
          of the above.
       *)
[@@deriving show, ord]

module IdOrderedType = struct
  type t = id

  let compare = compare_id
  let to_string = show_id
  let pp_t = pp_id
  let show_t = show_id
end

module IdMap = Collections.MakeMap (IdOrderedType)
module IdSet = Collections.MakeSet (IdOrderedType)

(** The names map stores the mappings from names to identifiers and vice-versa.

    We use it for lookups (during the translation) and to check for name clashes.

    [id_to_name] is for debugging.
  *)
type names_map = {
  id_to_name : string IdMap.t;
  name_to_id : id StringMap.t;
      (** The name to id map is used to look for name clashes, and generate nice
          debugging messages: if there is a name clash, it is useful to know
          precisely which identifiers are mapped to the same name...
       *)
  names_set : StringSet.t;
  opaque_ids : IdSet.t;
      (** TODO: this is obsolete. Remove.

          The set of opaque definitions.

          See {!formatter.opaque_pre} for detailed explanations about why
          we need to know which definitions are opaque to compute names.

          Also note that the opaque ids don't contain the ids of the assumed
          definitions. In practice, assumed definitions are opaque_defs. However, they
          are not grouped in the opaque module, meaning we never need to
          prefix them (with, say, "opaque_defs."): we thus consider them as non-opaque
          with regards to the names map.
       *)
}

(** Small helper to report name collision *)
let report_name_collision (id_to_string : id -> string) (id1 : id) (id2 : id)
    (name : string) : unit =
  let id1 = "\n- " ^ id_to_string id1 in
  let id2 = "\n- " ^ id_to_string id2 in
  let err =
    "Name clash detected: the following identifiers are bound to the same name \
     \"" ^ name ^ "\":" ^ id1 ^ id2
    ^ "\nYou may want to rename some of your definitions, or report an issue."
  in
  log#serror err;
  raise (Failure err)

let names_map_get_id_from_name (name : string) (nm : names_map) : id option =
  StringMap.find_opt name nm.name_to_id

let names_map_check_collision (id_to_string : id -> string) (id : id)
    (name : string) (nm : names_map) : unit =
  match names_map_get_id_from_name name nm with
  | None -> () (* Ok *)
  | Some clash ->
      (* There is a clash: print a nice debugging message for the user *)
      report_name_collision id_to_string clash id name

let names_map_add (id_to_string : id -> string) (is_opaque : bool) (id : id)
    (name : string) (nm : names_map) : names_map =
  (* Check if there is a clash *)
  names_map_check_collision id_to_string id name nm;
  (* Sanity check *)
  assert (not (StringSet.mem name nm.names_set));
  (* Insert *)
  let id_to_name = IdMap.add id name nm.id_to_name in
  let name_to_id = StringMap.add name id nm.name_to_id in
  let names_set = StringSet.add name nm.names_set in
  let opaque_ids =
    if is_opaque then IdSet.add id nm.opaque_ids else nm.opaque_ids
  in
  { id_to_name; name_to_id; names_set; opaque_ids }

let names_map_add_assumed_type (id_to_string : id -> string) (id : assumed_ty)
    (name : string) (nm : names_map) : names_map =
  let is_opaque = false in
  names_map_add id_to_string is_opaque (TypeId (Assumed id)) name nm

let names_map_add_assumed_struct (id_to_string : id -> string) (id : assumed_ty)
    (name : string) (nm : names_map) : names_map =
  let is_opaque = false in
  names_map_add id_to_string is_opaque (StructId (Assumed id)) name nm

let names_map_add_assumed_variant (id_to_string : id -> string)
    (id : assumed_ty) (variant_id : VariantId.id) (name : string)
    (nm : names_map) : names_map =
  let is_opaque = false in
  names_map_add id_to_string is_opaque
    (VariantId (Assumed id, variant_id))
    name nm

let names_map_add_function (id_to_string : id -> string) (is_opaque : bool)
    (fid : fun_id) (name : string) (nm : names_map) : names_map =
  names_map_add id_to_string is_opaque (FunId fid) name nm

(** The unsafe names map stores mappings from identifiers to names which might
    collide. For some backends and some names, it might be acceptable to have
    collisions. For instance, in Lean, different records can have fields with
    the same name because Lean uses the typing information to resolve the
    ambiguities.

    This map complements the {!names_map}, which checks for collisions.
  *)
type unsafe_names_map = { id_to_name : string IdMap.t }

let unsafe_names_map_add (id : id) (name : string) (nm : unsafe_names_map) :
    unsafe_names_map =
  { id_to_name = IdMap.add id name nm.id_to_name }

(** Make a (variable) basename unique (by adding an index).

    We do this in an inefficient manner (by testing all indices starting from
    0) but it shouldn't be a bottleneck.

    Also note that at some point, we thought about trying to reuse names of
    variables which are not used anymore, like here:
    {[
      let x = ... in
      ...
      let x0 = ... in // We could use the name "x" if [x] is not used below
      ...
    ]}

    However it is a good idea to keep things as they are for F*: as F* is
    designed for extrinsic proofs, a proof about a function follows this
    function's structure. The consequence is that we often end up
    copy-pasting function bodies. As in the proofs (in assertions and
    when calling lemmas) we often need to talk about the "past" (i.e.,
    previous values), it is very useful to generate code where all variable
    names are assigned at most once.

    [append]: function to append an index to a string
 *)
let basename_to_unique (names_set : StringSet.t)
    (append : string -> int -> string) (basename : string) : string =
  let rec gen (i : int) : string =
    let s = append basename i in
    if StringSet.mem s names_set then gen (i + 1) else s
  in
  if StringSet.mem basename names_set then gen 0 else basename

type fun_name_info = { keep_fwd : bool; num_backs : int }

(** Extraction context.

    Note that the extraction context contains information coming from the
    LLBC AST (not only the pure AST). This is useful for naming, for instance:
    we use the region information to generate the names of the backward
    functions, etc.
 *)
type extraction_ctx = {
  crate : A.crate;
  trans_ctx : trans_ctx;
  names_map : names_map;
      (** The map for id to names, where we forbid name collisions
          (ex.: we always forbid function name collisions). *)
  unsafe_names_map : unsafe_names_map;
      (** The map for id to names, where we allow name collisions
          (ex.: we might allow record field name collisions). *)
  fmt : formatter;
  indent_incr : int;
      (** The indent increment we insert whenever we need to indent more *)
  use_opaque_pre : bool;
      (** Do we use the "opaque_defs." prefix for the opaque definitions?

          Opaque function definitions might refer opaque types: if we are in the
          opaque module, we musn't use the "opaque_defs." prefix, otherwise we
          use it.
          Also see {!names_map.opaque_ids}.
       *)
  use_dep_ite : bool;
      (** For Lean: do we use dependent-if then else expressions?

          Example:
          {[
            if h: b then ... else ...
            -- ^^
            -- makes the if then else dependent
          ]}
        *)
  fun_name_info : fun_name_info PureUtils.RegularFunIdMap.t;
      (** Information used to filter and name functions - we use it
          to print comments in the generated code, to help link
          the generated code to the original code (information such
          as: "this function is the backward function of ...", or
          "this function is the merged forward/backward function of ..."
          in case a Rust function only has one backward translation
          and we filter the forward function because it returns unit.
        *)
  trait_decl_id : trait_decl_id option;
      (** If we are extracting a trait declaration, identifies it *)
  is_provided_method : bool;
  trans_types : Pure.type_decl Pure.TypeDeclId.Map.t;
  trans_funs : pure_fun_translation A.FunDeclId.Map.t;
  functions_with_decreases_clause : PureUtils.FunLoopIdSet.t;
  trans_trait_decls : Pure.trait_decl Pure.TraitDeclId.Map.t;
  trans_trait_impls : Pure.trait_impl Pure.TraitImplId.Map.t;
}

(** Debugging function, used when communicating name collisions to the user,
    and also to print ids for internal debugging (in case of lookup miss for
    instance).
 *)
let id_to_string (id : id) (ctx : extraction_ctx) : string =
  let global_decls = ctx.trans_ctx.global_context.global_decls in
  let fun_decls = ctx.trans_ctx.fun_context.fun_decls in
  let type_decls = ctx.trans_ctx.type_context.type_decls in
  let trait_decls = ctx.trans_ctx.trait_decls_context.trait_decls in
  (* TODO: factorize the pretty-printing with what is in PrintPure *)
  let get_type_name (id : type_id) : string =
    match id with
    | AdtId id ->
        let def = TypeDeclId.Map.find id type_decls in
        Print.name_to_string def.name
    | Assumed aty -> show_assumed_ty aty
    | Tuple -> raise (Failure "Unreachable")
  in
  match id with
  | GlobalId gid ->
      let name = (A.GlobalDeclId.Map.find gid global_decls).name in
      "global name: " ^ Print.global_name_to_string name
  | FunId fid -> (
      match fid with
      | FromLlbc (fid, lp_id, rg_id) ->
          let fun_name =
            match fid with
            | FunId (Regular fid) ->
                Print.fun_name_to_string
                  (A.FunDeclId.Map.find fid fun_decls).name
            | FunId (Assumed aid) -> A.show_assumed_fun_id aid
            | TraitMethod _ ->
                (* Shouldn't happen *)
                raise (Failure "Unexpected")
          in

          let lp_kind =
            match lp_id with
            | None -> ""
            | Some lp_id -> "loop " ^ LoopId.to_string lp_id ^ ", "
          in

          let fwd_back_kind =
            match rg_id with
            | None -> "forward"
            | Some rg_id -> "backward " ^ RegionGroupId.to_string rg_id
          in
          "fun name (" ^ lp_kind ^ fwd_back_kind ^ "): " ^ fun_name
      | Pure fid -> PrintPure.pure_assumed_fun_id_to_string fid)
  | DecreasesProofId (fid, lid) ->
      let fun_name =
        match fid with
        | Regular fid ->
            Print.fun_name_to_string (A.FunDeclId.Map.find fid fun_decls).name
        | Assumed aid -> A.show_assumed_fun_id aid
      in
      let loop =
        match lid with
        | None -> ""
        | Some lid -> ", loop: " ^ LoopId.to_string lid
      in
      "decreases proof for function: " ^ fun_name ^ loop
  | TerminationMeasureId (fid, lid) ->
      let fun_name =
        match fid with
        | Regular fid ->
            Print.fun_name_to_string (A.FunDeclId.Map.find fid fun_decls).name
        | Assumed aid -> A.show_assumed_fun_id aid
      in
      let loop =
        match lid with
        | None -> ""
        | Some lid -> ", loop: " ^ LoopId.to_string lid
      in
      "termination measure for function: " ^ fun_name ^ loop
  | TypeId id -> "type name: " ^ get_type_name id
  | StructId id -> "struct constructor of: " ^ get_type_name id
  | VariantId (id, variant_id) ->
      let variant_name =
        match id with
        | Tuple -> raise (Failure "Unreachable")
        | Assumed Result ->
            if variant_id = result_return_id then "@result::Return"
            else if variant_id = result_fail_id then "@result::Fail"
            else raise (Failure "Unreachable")
        | Assumed Error ->
            if variant_id = error_failure_id then "@error::Failure"
            else if variant_id = error_out_of_fuel_id then "@error::OutOfFuel"
            else raise (Failure "Unreachable")
        | Assumed Option ->
            if variant_id = option_some_id then "@option::Some"
            else if variant_id = option_none_id then "@option::None"
            else raise (Failure "Unreachable")
        | Assumed (State | Vec | Fuel | Array | Slice | Str | Range) ->
            raise (Failure "Unreachable")
        | AdtId id -> (
            let def = TypeDeclId.Map.find id type_decls in
            match def.kind with
            | Struct _ | Opaque -> raise (Failure "Unreachable")
            | Enum variants ->
                let variant = VariantId.nth variants variant_id in
                Print.name_to_string def.name ^ "::" ^ variant.variant_name)
      in
      "variant name: " ^ variant_name
  | FieldId (id, field_id) ->
      let field_name =
        match id with
        | Tuple -> raise (Failure "Unreachable")
        | Assumed
            ( State | Result | Error | Fuel | Option | Vec | Array | Slice | Str
            | Range ) ->
            (* We can't directly have access to the fields of those types *)
            raise (Failure "Unreachable")
        | AdtId id -> (
            let def = TypeDeclId.Map.find id type_decls in
            match def.kind with
            | Enum _ | Opaque -> raise (Failure "Unreachable")
            | Struct fields ->
                let field = FieldId.nth fields field_id in
                let field_name =
                  match field.field_name with
                  | None -> FieldId.to_string field_id
                  | Some name -> name
                in
                Print.name_to_string def.name ^ "." ^ field_name)
      in
      "field name: " ^ field_name
  | UnknownId -> "keyword"
  | TypeVarId id -> "type_var_id: " ^ TypeVarId.to_string id
  | ConstGenericVarId id ->
      "const_generic_var_id: " ^ ConstGenericVarId.to_string id
  | VarId id -> "var_id: " ^ VarId.to_string id
  | TraitDeclId id -> "trait_decl_id: " ^ TraitDeclId.to_string id
  | TraitImplId id -> "trait_impl_id: " ^ TraitImplId.to_string id
  | LocalTraitClauseId id ->
      "local_trait_clause_id: " ^ TraitClauseId.to_string id
  | TraitParentClauseId (id, clause_id) ->
      "trait_parent_clause_id: decl_id:" ^ TraitDeclId.to_string id
      ^ ", clause_id: "
      ^ TraitClauseId.to_string clause_id
  | TraitItemClauseId (id, item_name, clause_id) ->
      "trait_item_clause_id: decl_id:" ^ TraitDeclId.to_string id
      ^ ", item name: " ^ item_name ^ ", clause_id: "
      ^ TraitClauseId.to_string clause_id
  | TraitItemId (id, name) ->
      "trait_item_id: decl_id:" ^ TraitDeclId.to_string id ^ ", type name: "
      ^ name
  | TraitMethodId (trait_decl_id, fun_name, rg_id) ->
      let trait_name =
        Print.fun_name_to_string
          (A.TraitDeclId.Map.find trait_decl_id trait_decls).name
      in
      let fwd_back_kind =
        match rg_id with
        | None -> "forward"
        | Some rg_id -> "backward " ^ RegionGroupId.to_string rg_id
      in
      "trait " ^ trait_name ^ " method name (" ^ fwd_back_kind ^ "): "
      ^ fun_name
  | TraitSelfClauseId -> "trait_self_clause"

(** We might not check for collisions for some specific ids (ex.: field names) *)
let allow_collisions (id : id) : bool =
  match id with
  | FieldId _ | TraitItemClauseId _ | TraitParentClauseId _ | TraitItemId _ ->
      !Config.record_fields_short_names
  | _ -> false

let ctx_add (is_opaque : bool) (id : id) (name : string) (ctx : extraction_ctx)
    : extraction_ctx =
  (* The id_to_string function to print nice debugging messages if there are
   * collisions *)
  let id_to_string (id : id) : string = id_to_string id ctx in
  (* We do not use the same name map if we allow/disallow collisions.
     We notably use it for field names: some backends like Lean can use the
     type information to disambiguate field projections.

     Remark: what we do is actually subtle. Taking the example of fields:
     - we allow fields from different ADT definitions to collide
     - we do *not* allow field names to collide with other names
     For instance, we don't allow naming a field "let". We enforce this by
     not checking collision between ids for which we permit collisions (ex.:
     between fields), but still checking collisions between those ids and the
     others (ex.: fields and keywords).
  *)
  if allow_collisions id then (
    assert (not is_opaque);
    (* Check with the other ids *)
    names_map_check_collision id_to_string id name ctx.names_map;
    {
      ctx with
      unsafe_names_map = unsafe_names_map_add id name ctx.unsafe_names_map;
    })
  else
    (* Remark: we don't check that there are no collisions with the unsafe ids.
       Importantly, we don't want some safe ids like keywords to clash with
       unsafe ids like fields names. For this, we leverage the fact that we register
       keywords *first*, then unsafe ids (meaning the clash will be detected with
       the check in the other branch of the if ... then ... else ..., and we do
       have to check for all possible collisions, which may be slightly too
       restrictive).

       TODO: this is a bit hacky, we might want to improve the way we detect
       clashes by being more precise. Overall, there is only an issue with
       field names which are allowed to clash with each other.
    *)
    let names_map =
      names_map_add id_to_string is_opaque id name ctx.names_map
    in
    { ctx with names_map }

(** [with_opaque_pre]: if [true] and the definition is opaque, add the opaque prefix *)
let ctx_get (with_opaque_pre : bool) (id : id) (ctx : extraction_ctx) : string =
  (* We do not use the same name map if we allow/disallow collisions *)
  if allow_collisions id then IdMap.find id ctx.unsafe_names_map.id_to_name
  else
    match IdMap.find_opt id ctx.names_map.id_to_name with
    | Some s ->
        let is_opaque = IdSet.mem id ctx.names_map.opaque_ids in
        if with_opaque_pre && is_opaque then ctx.fmt.opaque_pre () ^ s else s
    | None ->
        log#serror ("Could not find: " ^ id_to_string id ctx);
        raise Not_found

let ctx_get_global (with_opaque_pre : bool) (id : A.GlobalDeclId.id)
    (ctx : extraction_ctx) : string =
  ctx_get with_opaque_pre (GlobalId id) ctx

let ctx_get_function (with_opaque_pre : bool) (id : fun_id)
    (ctx : extraction_ctx) : string =
  ctx_get with_opaque_pre (FunId id) ctx

let ctx_get_local_function (with_opaque_pre : bool) (id : A.FunDeclId.id)
    (lp : LoopId.id option) (rg : RegionGroupId.id option)
    (ctx : extraction_ctx) : string =
  ctx_get_function with_opaque_pre (FromLlbc (FunId (Regular id), lp, rg)) ctx

let ctx_get_type (with_opaque_pre : bool) (id : type_id) (ctx : extraction_ctx)
    : string =
  assert (id <> Tuple);
  ctx_get with_opaque_pre (TypeId id) ctx

let ctx_get_local_type (with_opaque_pre : bool) (id : TypeDeclId.id)
    (ctx : extraction_ctx) : string =
  ctx_get_type with_opaque_pre (AdtId id) ctx

let ctx_get_assumed_type (id : assumed_ty) (ctx : extraction_ctx) : string =
  (* In practice, the assumed types are opaque. However, assumed types
     are never grouped in the opaque module, meaning we never need to
     prefix them: we thus consider them as non-opaque with regards to the
     names map.
  *)
  let is_opaque = false in
  ctx_get_type is_opaque (Assumed id) ctx

let ctx_get_trait_self_clause (ctx : extraction_ctx) : string =
  let with_opaque_pre = false in
  ctx_get with_opaque_pre TraitSelfClauseId ctx

let ctx_get_trait_decl (with_opaque_pre : bool) (id : trait_decl_id)
    (ctx : extraction_ctx) : string =
  ctx_get with_opaque_pre (TraitDeclId id) ctx

let ctx_get_trait_impl (with_opaque_pre : bool) (id : trait_impl_id)
    (ctx : extraction_ctx) : string =
  ctx_get with_opaque_pre (TraitImplId id) ctx

let ctx_get_trait_item (id : trait_decl_id) (item_name : string)
    (ctx : extraction_ctx) : string =
  let is_opaque = false in
  ctx_get is_opaque (TraitItemId (id, item_name)) ctx

let ctx_get_trait_const (id : trait_decl_id) (item_name : string)
    (ctx : extraction_ctx) : string =
  ctx_get_trait_item id item_name ctx

let ctx_get_trait_type (id : trait_decl_id) (item_name : string)
    (ctx : extraction_ctx) : string =
  ctx_get_trait_item id item_name ctx

let ctx_get_trait_method (id : trait_decl_id) (item_name : string)
    (rg_id : T.RegionGroupId.id option) (ctx : extraction_ctx) : string =
  let with_opaque_pre = false in
  ctx_get with_opaque_pre (TraitMethodId (id, item_name, rg_id)) ctx

let ctx_get_trait_parent_clause (id : trait_decl_id) (clause : trait_clause_id)
    (ctx : extraction_ctx) : string =
  let with_opaque_pre = false in
  ctx_get with_opaque_pre (TraitParentClauseId (id, clause)) ctx

let ctx_get_trait_item_clause (id : trait_decl_id) (item : string)
    (clause : trait_clause_id) (ctx : extraction_ctx) : string =
  let with_opaque_pre = false in
  ctx_get with_opaque_pre (TraitItemClauseId (id, item, clause)) ctx

let ctx_get_var (id : VarId.id) (ctx : extraction_ctx) : string =
  let is_opaque = false in
  ctx_get is_opaque (VarId id) ctx

let ctx_get_type_var (id : TypeVarId.id) (ctx : extraction_ctx) : string =
  let is_opaque = false in
  ctx_get is_opaque (TypeVarId id) ctx

let ctx_get_const_generic_var (id : ConstGenericVarId.id) (ctx : extraction_ctx)
    : string =
  let is_opaque = false in
  ctx_get is_opaque (ConstGenericVarId id) ctx

let ctx_get_local_trait_clause (id : TraitClauseId.id) (ctx : extraction_ctx) :
    string =
  let is_opaque = false in
  ctx_get is_opaque (LocalTraitClauseId id) ctx

let ctx_get_field (type_id : type_id) (field_id : FieldId.id)
    (ctx : extraction_ctx) : string =
  let is_opaque = false in
  ctx_get is_opaque (FieldId (type_id, field_id)) ctx

let ctx_get_struct (with_opaque_pre : bool) (def_id : type_id)
    (ctx : extraction_ctx) : string =
  ctx_get with_opaque_pre (StructId def_id) ctx

let ctx_get_variant (def_id : type_id) (variant_id : VariantId.id)
    (ctx : extraction_ctx) : string =
  let is_opaque = false in
  ctx_get is_opaque (VariantId (def_id, variant_id)) ctx

let ctx_get_decreases_proof (def_id : A.FunDeclId.id)
    (loop_id : LoopId.id option) (ctx : extraction_ctx) : string =
  let is_opaque = false in
  ctx_get is_opaque (DecreasesProofId (Regular def_id, loop_id)) ctx

let ctx_get_termination_measure (def_id : A.FunDeclId.id)
    (loop_id : LoopId.id option) (ctx : extraction_ctx) : string =
  let is_opaque = false in
  ctx_get is_opaque (TerminationMeasureId (Regular def_id, loop_id)) ctx

(** Generate a unique type variable name and add it to the context *)
let ctx_add_type_var (basename : string) (id : TypeVarId.id)
    (ctx : extraction_ctx) : extraction_ctx * string =
  let is_opaque = false in
  let name = ctx.fmt.type_var_basename ctx.names_map.names_set basename in
  let name =
    basename_to_unique ctx.names_map.names_set ctx.fmt.append_index name
  in
  let ctx = ctx_add is_opaque (TypeVarId id) name ctx in
  (ctx, name)

(** Generate a unique const generic variable name and add it to the context *)
let ctx_add_const_generic_var (basename : string) (id : ConstGenericVarId.id)
    (ctx : extraction_ctx) : extraction_ctx * string =
  let is_opaque = false in
  let name =
    ctx.fmt.const_generic_var_basename ctx.names_map.names_set basename
  in
  let name =
    basename_to_unique ctx.names_map.names_set ctx.fmt.append_index name
  in
  let ctx = ctx_add is_opaque (ConstGenericVarId id) name ctx in
  (ctx, name)

(** See {!ctx_add_type_var} *)
let ctx_add_type_vars (vars : (string * TypeVarId.id) list)
    (ctx : extraction_ctx) : extraction_ctx * string list =
  List.fold_left_map
    (fun ctx (name, id) -> ctx_add_type_var name id ctx)
    ctx vars

(** Generate a unique variable name and add it to the context *)
let ctx_add_var (basename : string) (id : VarId.id) (ctx : extraction_ctx) :
    extraction_ctx * string =
  let is_opaque = false in
  let name =
    basename_to_unique ctx.names_map.names_set ctx.fmt.append_index basename
  in
  let ctx = ctx_add is_opaque (VarId id) name ctx in
  (ctx, name)

(** Generate a unique variable name for the trait self clause and add it to the context *)
let ctx_add_trait_self_clause (ctx : extraction_ctx) : extraction_ctx * string =
  let is_opaque = false in
  let basename = ctx.fmt.trait_self_clause_basename in
  let name =
    basename_to_unique ctx.names_map.names_set ctx.fmt.append_index basename
  in
  let ctx = ctx_add is_opaque TraitSelfClauseId name ctx in
  (ctx, name)

(** Generate a unique trait clause name and add it to the context *)
let ctx_add_local_trait_clause (basename : string) (id : TraitClauseId.id)
    (ctx : extraction_ctx) : extraction_ctx * string =
  let is_opaque = false in
  let name =
    basename_to_unique ctx.names_map.names_set ctx.fmt.append_index basename
  in
  let ctx = ctx_add is_opaque (LocalTraitClauseId id) name ctx in
  (ctx, name)

(** See {!ctx_add_var} *)
let ctx_add_vars (vars : var list) (ctx : extraction_ctx) :
    extraction_ctx * string list =
  List.fold_left_map
    (fun ctx (v : var) ->
      let name = ctx.fmt.var_basename ctx.names_map.names_set v.basename v.ty in
      ctx_add_var name v.id ctx)
    ctx vars

let ctx_add_type_params (vars : type_var list) (ctx : extraction_ctx) :
    extraction_ctx * string list =
  List.fold_left_map
    (fun ctx (var : type_var) -> ctx_add_type_var var.name var.index ctx)
    ctx vars

let ctx_add_const_generic_params (vars : const_generic_var list)
    (ctx : extraction_ctx) : extraction_ctx * string list =
  List.fold_left_map
    (fun ctx (var : const_generic_var) ->
      ctx_add_const_generic_var var.name var.index ctx)
    ctx vars

let ctx_add_local_trait_clauses (clauses : trait_clause list)
    (ctx : extraction_ctx) : extraction_ctx * string list =
  List.fold_left_map
    (fun ctx (c : trait_clause) ->
      let basename = ctx.fmt.trait_clause_basename ctx.names_map.names_set c in
      ctx_add_local_trait_clause basename c.clause_id ctx)
    ctx clauses

(** Returns the lists of names for:
    - the type variables
    - the const generic variables
    - the trait clauses
  *)
let ctx_add_generic_params (generics : generic_params) (ctx : extraction_ctx) :
    extraction_ctx * string list * string list * string list =
  let { types; const_generics; trait_clauses } = generics in
  let ctx, tys = ctx_add_type_params types ctx in
  let ctx, cgs = ctx_add_const_generic_params const_generics ctx in
  let ctx, tcs = ctx_add_local_trait_clauses trait_clauses ctx in
  (ctx, tys, cgs, tcs)

let ctx_add_type_decl_struct (def : type_decl) (ctx : extraction_ctx) :
    extraction_ctx * string =
  assert (match def.kind with Struct _ -> true | _ -> false);
  let is_opaque = false in
  let cons_name = ctx.fmt.struct_constructor def.name in
  let ctx = ctx_add is_opaque (StructId (AdtId def.def_id)) cons_name ctx in
  (ctx, cons_name)

let ctx_add_type_decl (def : type_decl) (ctx : extraction_ctx) : extraction_ctx
    =
  let is_opaque = def.kind = Opaque in
  let def_name = ctx.fmt.type_name def.name in
  let ctx = ctx_add is_opaque (TypeId (AdtId def.def_id)) def_name ctx in
  ctx

let ctx_add_field (def : type_decl) (field_id : FieldId.id) (field : field)
    (ctx : extraction_ctx) : extraction_ctx * string =
  let is_opaque = false in
  let name = ctx.fmt.field_name def.name field_id field.field_name in
  let ctx = ctx_add is_opaque (FieldId (AdtId def.def_id, field_id)) name ctx in
  (ctx, name)

let ctx_add_fields (def : type_decl) (fields : (FieldId.id * field) list)
    (ctx : extraction_ctx) : extraction_ctx * string list =
  List.fold_left_map
    (fun ctx (vid, v) -> ctx_add_field def vid v ctx)
    ctx fields

let ctx_add_variant (def : type_decl) (variant_id : VariantId.id)
    (variant : variant) (ctx : extraction_ctx) : extraction_ctx * string =
  let is_opaque = false in
  let name = ctx.fmt.variant_name def.name variant.variant_name in
  (* Add the type name prefix for Lean *)
  let name =
    if !Config.backend = Lean then
      let type_name = ctx.fmt.type_name def.name in
      type_name ^ "." ^ name
    else name
  in
  let ctx =
    ctx_add is_opaque (VariantId (AdtId def.def_id, variant_id)) name ctx
  in
  (ctx, name)

let ctx_add_variants (def : type_decl)
    (variants : (VariantId.id * variant) list) (ctx : extraction_ctx) :
    extraction_ctx * string list =
  List.fold_left_map
    (fun ctx (vid, v) -> ctx_add_variant def vid v ctx)
    ctx variants

let ctx_add_struct (def : type_decl) (ctx : extraction_ctx) :
    extraction_ctx * string =
  assert (match def.kind with Struct _ -> true | _ -> false);
  let is_opaque = false in
  let name = ctx.fmt.struct_constructor def.name in
  let ctx = ctx_add is_opaque (StructId (AdtId def.def_id)) name ctx in
  (ctx, name)

let ctx_add_decreases_proof (def : fun_decl) (ctx : extraction_ctx) :
    extraction_ctx =
  let is_opaque = false in
  let name =
    ctx.fmt.decreases_proof_name def.def_id def.basename def.num_loops
      def.loop_id
  in
  ctx_add is_opaque
    (DecreasesProofId (Regular def.def_id, def.loop_id))
    name ctx

let ctx_add_termination_measure (def : fun_decl) (ctx : extraction_ctx) :
    extraction_ctx =
  let is_opaque = false in
  let name =
    ctx.fmt.termination_measure_name def.def_id def.basename def.num_loops
      def.loop_id
  in
  ctx_add is_opaque
    (TerminationMeasureId (Regular def.def_id, def.loop_id))
    name ctx

let ctx_add_global_decl_and_body (def : A.global_decl) (ctx : extraction_ctx) :
    extraction_ctx =
  (* TODO: update once the body id can be an option *)
  let is_opaque = false in
  let decl = GlobalId def.def_id in

  (* Check if the global corresponds to an assumed global that we should map
     to a custom definition in our standard library (for instance, happens
     with "core::num::usize::MAX") *)
  let sname = name_to_simple_name def.name in
  match SimpleNameMap.find_opt sname assumed_globals_map with
  | Some name ->
      (* Yes: register the custom binding *)
      ctx_add is_opaque decl name ctx
  | None ->
      (* Not the case: "standard" registration *)
      let name = ctx.fmt.global_name def.name in
      let body = FunId (FromLlbc (FunId (Regular def.body_id), None, None)) in
      let ctx = ctx_add is_opaque decl (name ^ "_c") ctx in
      let ctx = ctx_add is_opaque body (name ^ "_body") ctx in
      ctx

let ctx_compute_fun_name (trans_group : pure_fun_translation) (def : fun_decl)
    (ctx : extraction_ctx) : string =
  (* Lookup the LLBC def to compute the region group information *)
  let def_id = def.def_id in
  let llbc_def =
    A.FunDeclId.Map.find def_id ctx.trans_ctx.fun_context.fun_decls
  in
  let sg = llbc_def.signature in
  let num_rgs = List.length sg.regions_hierarchy in
  let { keep_fwd; fwd = _; backs } = trans_group in
  let num_backs = List.length backs in
  let rg_info =
    match def.back_id with
    | None -> None
    | Some rg_id ->
        let rg = T.RegionGroupId.nth sg.regions_hierarchy rg_id in
        let region_names =
          List.map
            (fun rid -> (T.RegionVarId.nth sg.generics.regions rid).name)
            rg.regions
        in
        Some { id = rg_id; region_names }
  in
  (* Add the function name *)
  ctx.fmt.fun_name def.basename def.num_loops def.loop_id num_rgs rg_info
    (keep_fwd, num_backs)

let ctx_add_fun_decl (trans_group : pure_fun_translation) (def : fun_decl)
    (ctx : extraction_ctx) : extraction_ctx =
  (* Sanity check: the function should not be a global body - those are handled
   * separately *)
  assert (not def.is_global_decl_body);
  (* Lookup the LLBC def to compute the region group information *)
  let def_id = def.def_id in
  let { keep_fwd; fwd = _; backs } = trans_group in
  let num_backs = List.length backs in
  let is_opaque = def.body = None in
  (* Add the function name *)
  let def_name = ctx_compute_fun_name trans_group def ctx in
  let fun_id = (Pure.FunId (Regular def_id), def.loop_id, def.back_id) in
  let ctx = ctx_add is_opaque (FunId (FromLlbc fun_id)) def_name ctx in
  (* Add the name info *)
  {
    ctx with
    fun_name_info =
      PureUtils.RegularFunIdMap.add fun_id { keep_fwd; num_backs }
        ctx.fun_name_info;
  }

let ctx_add_trait_decl (d : trait_decl) (ctx : extraction_ctx) : extraction_ctx
    =
  let is_opaque = false in
  let name = ctx.fmt.trait_decl_name d in
  ctx_add is_opaque (TraitDeclId d.def_id) name ctx

let ctx_add_trait_impl (d : trait_impl) (ctx : extraction_ctx) : extraction_ctx
    =
  let is_opaque = false in
  let name = ctx.fmt.trait_impl_name d in
  ctx_add is_opaque (TraitImplId d.def_id) name ctx

let ctx_add_trait_const (d : trait_decl) (item : string) (ctx : extraction_ctx)
    : extraction_ctx =
  let is_opaque = false in
  let name = ctx.fmt.trait_const_name d item in
  ctx_add is_opaque (TraitItemId (d.def_id, item)) name ctx

let ctx_add_trait_type (d : trait_decl) (item : string) (ctx : extraction_ctx) :
    extraction_ctx =
  let is_opaque = false in
  let name = ctx.fmt.trait_type_name d item in
  ctx_add is_opaque (TraitItemId (d.def_id, item)) name ctx

let ctx_add_trait_method (d : trait_decl) (item_name : string) (f : fun_decl)
    (ctx : extraction_ctx) : extraction_ctx =
  (* We do something special: we use the base name but remove everything
         but the crate (because [get_name] removes it) and the last ident.
         This allows us to reuse the [ctx_compute_fun_decl] function.
  *)
  let basename : name =
    match (f.basename : name) with
    | Ident crate :: name -> Ident crate :: [ Collections.List.last name ]
    | _ -> raise (Failure "Unexpected")
  in
  let f = { f with basename } in
  let trans = A.FunDeclId.Map.find f.def_id ctx.trans_funs in
  let name = ctx_compute_fun_name trans f ctx in
  let is_opaque = false in
  ctx_add is_opaque (TraitMethodId (d.def_id, item_name, f.back_id)) name ctx

let ctx_add_trait_parent_clause (d : trait_decl) (clause : trait_clause)
    (ctx : extraction_ctx) : extraction_ctx =
  let is_opaque = false in
  let name = ctx.fmt.trait_parent_clause_name d clause in
  ctx_add is_opaque (TraitParentClauseId (d.def_id, clause.clause_id)) name ctx

let ctx_add_trait_type_clause (d : trait_decl) (item : string)
    (clause : trait_clause) (ctx : extraction_ctx) : extraction_ctx =
  let is_opaque = false in
  let name = ctx.fmt.trait_type_clause_name d item clause in
  ctx_add is_opaque
    (TraitItemClauseId (d.def_id, item, clause.clause_id))
    name ctx

type names_map_init = {
  keywords : string list;
  assumed_adts : (assumed_ty * string) list;
  assumed_structs : (assumed_ty * string) list;
  assumed_variants : (assumed_ty * VariantId.id * string) list;
  assumed_llbc_functions :
    (A.assumed_fun_id * RegionGroupId.id option * string) list;
  assumed_pure_functions : (pure_assumed_fun_id * string) list;
}

(** Initialize a names map with a proper set of keywords/names coming from the
    target language/prover. *)
let initialize_names_map (fmt : formatter) (init : names_map_init) : names_map =
  let int_names = List.map fmt.int_name T.all_int_types in
  let keywords =
    List.concat
      [
        [ fmt.bool_name; fmt.char_name; fmt.str_name ]; int_names; init.keywords;
      ]
  in
  let names_set = StringSet.of_list keywords in
  let name_to_id =
    StringMap.of_list (List.map (fun x -> (x, UnknownId)) keywords)
  in
  let opaque_ids = IdSet.empty in
  (* We fist initialize [id_to_name] as empty, because the id of a keyword is [UnknownId].
   * Also note that we don't need this mapping for keywords: we insert keywords only
   * to check collisions. *)
  let id_to_name = IdMap.empty in
  let nm = { id_to_name; name_to_id; names_set; opaque_ids } in
  (* For debugging - we are creating bindings for assumed types and functions, so
   * it is ok if we simply use the "show" function (those aren't simply identified
   * by numbers) *)
  let id_to_string = show_id in
  (* Then we add:
   * - the assumed types
   * - the assumed struct constructors
   * - the assumed variants
   * - the assumed functions
   *)
  let nm =
    List.fold_left
      (fun nm (type_id, name) ->
        names_map_add_assumed_type id_to_string type_id name nm)
      nm init.assumed_adts
  in
  let nm =
    List.fold_left
      (fun nm (type_id, name) ->
        names_map_add_assumed_struct id_to_string type_id name nm)
      nm init.assumed_structs
  in
  let nm =
    List.fold_left
      (fun nm (type_id, variant_id, name) ->
        names_map_add_assumed_variant id_to_string type_id variant_id name nm)
      nm init.assumed_variants
  in
  let assumed_functions =
    List.map
      (fun (fid, rg, name) ->
        (FromLlbc (Pure.FunId (A.Assumed fid), None, rg), name))
      init.assumed_llbc_functions
    @ List.map (fun (fid, name) -> (Pure fid, name)) init.assumed_pure_functions
  in
  let nm =
    (* In practice, the assumed function are opaque. However, assumed functions
       are never grouped in the opaque module, meaning we never need to
       prefix them: we thus consider them as non-opaque with regards to the
       names map.
    *)
    let is_opaque = false in
    List.fold_left
      (fun nm (fid, name) ->
        names_map_add_function id_to_string is_opaque fid name nm)
      nm assumed_functions
  in
  (* Return *)
  nm

let compute_type_decl_name (fmt : formatter) (def : type_decl) : string =
  fmt.type_name def.name

(** Helper function: generate a suffix for a function name, i.e., generates
    a suffix like "_loop", "loop1", etc. to append to a function name.
 *)
let default_fun_loop_suffix (num_loops : int) (loop_id : LoopId.id option) :
    string =
  match loop_id with
  | None -> ""
  | Some loop_id ->
      (* If this is for a loop, generally speaking, we append the loop index.
         If this function admits only one loop, we omit it. *)
      if num_loops = 1 then "_loop" else "_loop" ^ LoopId.to_string loop_id

(** A helper function: generates a function suffix from a region group
    information.
    TODO: move all those helpers.
*)
let default_fun_suffix (num_loops : int) (loop_id : LoopId.id option)
    (num_region_groups : int) (rg : region_group_info option)
    ((keep_fwd, num_backs) : bool * int) : string =
  let lp_suff = default_fun_loop_suffix num_loops loop_id in

  (* There are several cases:
     - [rg] is [Some]: this is a forward function:
       - we add "_fwd"
     - [rg] is [None]: this is a backward function:
       - this function has one extracted backward function:
         - if the forward function has been filtered, we add "_fwd_back":
           the forward function is useless, so the unique backward function
           takes its place, in a way
         - otherwise we add "_back"
       - this function has several backward functions: we add "_back" and an
         additional suffix to identify the precise backward function
     Note that we always add a suffix (in case there are no region groups,
     we could not add the "_fwd" suffix) to prevent name clashes between
     definitions (in particular between type and function definitions).
  *)
  let rg_suff =
    (* TODO: make all the backends match what is done for Lean *)
    match rg with
    | None -> (
        match !Config.backend with
        | FStar | Coq | HOL4 -> "_fwd"
        | Lean ->
            (* In order to avoid name conflicts:
             * - if the forward is eliminated, we add the suffix "_fwd" (it won't be used)
             * - otherwise, no suffix (because the backward functions will have a suffix)
             *)
            if num_backs = 1 && not keep_fwd then "_fwd" else "")
    | Some rg ->
        assert (num_region_groups > 0 && num_backs > 0);
        if num_backs = 1 then
          (* Exactly one backward function *)
          match !Config.backend with
          | FStar | Coq | HOL4 -> if not keep_fwd then "_fwd_back" else "_back"
          | Lean -> if not keep_fwd then "" else "_back"
        else if
          (* Several region groups/backward functions:
             - if all the regions in the group have names, we use those names
             - otherwise we use an index
          *)
          List.for_all Option.is_some rg.region_names
        then
          (* Concatenate the region names *)
          "_back" ^ String.concat "" (List.map Option.get rg.region_names)
        else (* Use the region index *)
          "_back" ^ RegionGroupId.to_string rg.id
  in
  lp_suff ^ rg_suff