summaryrefslogtreecommitdiff
path: root/src/PureMicroPasses.ml
blob: 9ddc71ab7949320183fa5df8eea9355644a3eec2 (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
(** The following module defines micro-passes which operate on the pure AST *)

open Pure
open PureUtils
open TranslateCore
module V = Values

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

type config = {
  decompose_monadic_let_bindings : bool;
      (** Some provers like F* don't support the decomposition of return values
          in monadic let-bindings:
          ```
          // NOT supported in F*
          let (x, y) <-- f ();
          ...
          ```

          In such situations, we might want to introduce an intermediate
          assignment:
          ```
          let tmp <-- f ();
          let (x, y) = tmp in
          ...
          ```
       *)
  unfold_monadic_let_bindings : bool;
      (** Controls the unfolding of monadic let-bindings to explicit matches:
          
          `y <-- f x; ...`

          becomes:
          
          `match f x with | Failure -> Failure | Return y -> ...`

          
          This is useful when extracting to F*: the support for monadic
          definitions is not super powerful.
          Note that when [undolf_monadic_let_bindings] is true, setting
          [decompose_monadic_let_bindings] to true and only makes the code
          more verbose.
       *)
  filter_useless_monadic_calls : bool;
      (** Controls whether we try to filter the calls to monadic functions
          (which can fail) when their outputs are not used.
          
          See the comments for [expression_contains_child_call_in_all_paths]
          for additional explanations.
          
          TODO: rename to [filter_useless_monadic_calls]
       *)
  filter_useless_functions : bool;
      (** If [filter_useless_monadic_calls] is activated, some functions
          become useless: if this option is true, we don't extract them.

          The calls to functions which always get filtered are:
          - the forward functions with unit return value
          - the backward functions which don't output anything (backward
            functions coming from rust functions with no mutable borrows
            as input values - note that if a function doesn't take mutable
            borrows as inputs, it can't return mutable borrows; we actually
            dynamically check for that).
       *)
  use_state_monad : bool;  (** TODO: remove *)
}
(** A configuration to control the application of the passes *)

(** Small utility.

    We sometimes have to insert new fresh variables in a function body, in which
    case we need to make their indices greater than the indices of all the variables
    in the body.
    TODO: things would be simpler if we used a better representation of the
    variables indices...
 *)
let get_body_min_var_counter (body : fun_body) : VarId.generator =
  (* Find the max id in the input variables - some of them may have been
   * filtered from the body *)
  let min_input_id =
    List.fold_left
      (fun id (var : var) -> VarId.max id var.id)
      VarId.zero body.inputs
  in
  let obj =
    object
      inherit [_] reduce_expression

      method zero _ = min_input_id

      method plus id0 id1 _ = VarId.max (id0 ()) (id1 ())
      (* Get the maximum *)

      method! visit_var _ v _ = v.id
      (** For the lvalues *)

      method! visit_place _ p _ = p.var
      (** For the rvalues *)
    end
  in
  (* Find the max counter in the body *)
  let id = obj#visit_expression () body.body.e () in
  VarId.generator_from_incr_id id

type pn_ctx = {
  pure_vars : string VarId.Map.t;
      (** Information about the pure variables used in the synthesized program *)
  llbc_vars : string V.VarId.Map.t;
      (** Information about the LLBC variables used in the original program *)
}
(** "pretty-name context": see [compute_pretty_names] *)

(** This function computes pretty names for the variables in the pure AST. It
    relies on the "meta"-place information in the AST to generate naming
    constraints, and then uses those to compute the names.
    
    The way it works is as follows:
    - we only modify the names of the unnamed variables
    - whenever we see an rvalue/lvalue which is exactly an unnamed variable,
      and this value is linked to some meta-place information which contains
      a name and an empty path, we consider we should use this name
    - we try to propagate naming constraints on the pure variables use in the
      synthesized programs, and also on the LLBC variables from the original
      program (information about the LLBC variables is stored in the meta-places)
      
      
    Something important is that, for every variable we find, the name of this
    variable can be influenced by the information we find *below* in the AST.

    For instance, the following situations happen:
    
    - let's say we evaluate:
      ```
      match (ls : List<T>) {
        List::Cons(x, hd) => {
          ...
        }
      }
      ```
      
      Actually, in MIR, we get:
      ```
      tmp := discriminant(ls);
      switch tmp {
        0 => {
          x := (ls as Cons).0; // (i)
          hd := (ls as Cons).1; // (ii)
          ...
        }
      }
      ```
      If `ls` maps to a symbolic value `s0` upon evaluating the match in symbolic
      mode, we expand this value upon evaluating `tmp = discriminant(ls)`.
      However, at this point, we don't know which should be the names of
      the symbolic values we introduce for the fields of `Cons`!

      Let's imagine we have (for the `Cons` branch): `s0 ~~> Cons s1 s2`.
      The assigments at (i) and (ii) lead to the following binding in the
      evaluation context:
      ```
      x -> s1
      hd -> s2
      ```
      
      When generating the symbolic AST, we save as meta-information that we
      assign `s1` to the place `x` and `s2` to the place `hd`. This way,
      we learn we can use the names `x` and `hd` for the variables which are
      introduced by the match:
      ```
      match ls with
      | Cons x hd -> ...
      | ...
      ```
   - Assignments:
     `let x [@mplace=lp] = v [@mplace = rp} in ...`
     
     We propagate naming information across the assignments. This is important
     because many reassignments using temporary, anonymous variables are
     introduced during desugaring.
   
   - Given back values (introduced by backward functions):
     Let's say we have the following Rust code:
     ```
     let py = id(&mut x);
     *py = 2;
     assert!(x == 2);
     ```
     
     After desugaring, we get the following MIR:
     ```
     ^0 = &mut x; // anonymous variable
     py = id(move ^0);
     *py += 2;
     assert!(x == 2);
     ```
     
     We want this to be translated as:
     ```
     let py = id_fwd x in
     let py1 = py + 2 in
     let x1 = id_back x py1 in // <-- x1 is "given back": doesn't appear in the original MIR
     assert(x1 = 2);
     ```

     We want to notice that the value given back by `id_back` is given back for "x",
     so we should use "x" as the basename (hence the resulting name "x1"). However,
     this is non-trivial, because after desugaring the input argument given to `id`
     is not `&mut x` but `move ^0` (i.e., it comes from a temporary, anonymous
     variable). For this reason, we use the meta-place "&mut x" as the meta-place
     for the given back value (this is done during the synthesis), and propagate
     naming information *also* on the LLBC variables (which are referenced by the
     meta-places).

     This way, because of `^0 = &mut x`, we can propagate the name "x" to the place
     `^0`, then to the given back variable across the function call.
   
 *)
let compute_pretty_names (def : fun_decl) : fun_decl =
  (* Small helpers *)
  (* 
   * When we do branchings, we need to merge (the constraints saved in) the
   * contexts returned by the different branches.
   *
   * Note that by doing so, some mappings from var id to name
   * in one context may be overriden by the ones in the other context.
   *
   * This should be ok because:
   * - generally, the overriden variables should have been introduced *inside*
   *   the branches, in which case we don't care
   * - or they were introduced before, in which case the naming should generally
   *   be consistent? In the worse case, it isn't, but it leads only to less
   *   readable code, not to unsoundness. This case should be pretty rare,
   *   also.
   *)
  let merge_ctxs (ctx0 : pn_ctx) (ctx1 : pn_ctx) : pn_ctx =
    let pure_vars =
      VarId.Map.fold
        (fun id name ctx -> VarId.Map.add id name ctx)
        ctx0.pure_vars ctx1.pure_vars
    in
    let llbc_vars =
      V.VarId.Map.fold
        (fun id name ctx -> V.VarId.Map.add id name ctx)
        ctx0.llbc_vars ctx1.llbc_vars
    in
    { pure_vars; llbc_vars }
  in
  let empty_ctx =
    { pure_vars = VarId.Map.empty; llbc_vars = V.VarId.Map.empty }
  in
  let merge_ctxs_ls (ctxs : pn_ctx list) : pn_ctx =
    List.fold_left (fun ctx0 ctx1 -> merge_ctxs ctx0 ctx1) empty_ctx ctxs
  in

  (*
   * The way we do is as follows:
   * - we explore the expressions
   * - we register the variables introduced by the let-bindings
   * - we use the naming information we find (through the variables and the
   *   meta-places) to update our context (i.e., maps from variable ids to
   *   names)
   * - we use this information to update the names of the variables used in the
   *   expressions
   *)

  (* Register a variable for constraints propagation - used when an variable is
   * introduced (left-hand side of a left binding) *)
  let register_var (ctx : pn_ctx) (v : var) : pn_ctx =
    assert (not (VarId.Map.mem v.id ctx.pure_vars));
    match v.basename with
    | None -> ctx
    | Some name ->
        let pure_vars = VarId.Map.add v.id name ctx.pure_vars in
        { ctx with pure_vars }
  in
  (* Update a variable - used to update an expression after we computed constraints *)
  let update_var (ctx : pn_ctx) (v : var) (mp : mplace option) : var =
    match v.basename with
    | Some _ -> v
    | None -> (
        match VarId.Map.find_opt v.id ctx.pure_vars with
        | Some basename -> { v with basename = Some basename }
        | None ->
            if Option.is_some mp then
              match
                V.VarId.Map.find_opt (Option.get mp).var_id ctx.llbc_vars
              with
              | None -> v
              | Some basename -> { v with basename = Some basename }
            else v)
  in
  (* Update an lvalue - used to update an expression after we computed constraints *)
  let update_typed_lvalue ctx (lv : typed_lvalue) : typed_lvalue =
    let obj =
      object
        inherit [_] map_typed_lvalue

        method! visit_Var _ v mp = Var (update_var ctx v mp, mp)
      end
    in
    obj#visit_typed_lvalue () lv
  in

  (* Register an mplace the first time we find one *)
  let register_mplace (mp : mplace) (ctx : pn_ctx) : pn_ctx =
    match (V.VarId.Map.find_opt mp.var_id ctx.llbc_vars, mp.name) with
    | None, Some name ->
        let llbc_vars = V.VarId.Map.add mp.var_id name ctx.llbc_vars in
        { ctx with llbc_vars }
    | _ -> ctx
  in

  (* Register the fact that [name] can be used for the pure variable identified
   * by [var_id] (will add this name in the map if the variable is anonymous) *)
  let add_pure_var_constraint (var_id : VarId.id) (name : string) (ctx : pn_ctx)
      : pn_ctx =
    let pure_vars =
      if VarId.Map.mem var_id ctx.pure_vars then ctx.pure_vars
      else VarId.Map.add var_id name ctx.pure_vars
    in
    { ctx with pure_vars }
  in
  (* Similar to [add_pure_var_constraint], but for LLBC variables *)
  let add_llbc_var_constraint (var_id : V.VarId.id) (name : string)
      (ctx : pn_ctx) : pn_ctx =
    let llbc_vars =
      if V.VarId.Map.mem var_id ctx.llbc_vars then ctx.llbc_vars
      else V.VarId.Map.add var_id name ctx.llbc_vars
    in
    { ctx with llbc_vars }
  in
  (* Add a constraint: given a variable id and an associated meta-place, try to
   * extract naming information from the meta-place and save it *)
  let add_constraint (mp : mplace) (var_id : VarId.id) (ctx : pn_ctx) : pn_ctx =
    (* Register the place *)
    let ctx = register_mplace mp ctx in
    (* Update the variable name *)
    match (mp.name, mp.projection) with
    | Some name, [] ->
        (* Check if the variable already has a name - if not: insert the new name *)
        let ctx = add_pure_var_constraint var_id name ctx in
        let ctx = add_llbc_var_constraint mp.var_id name ctx in
        ctx
    | _ -> ctx
  in
  (* Specific case of constraint on rvalues *)
  let add_right_constraint (mp : mplace) (rv : typed_rvalue) (ctx : pn_ctx) :
      pn_ctx =
    (* Register the place *)
    let ctx = register_mplace mp ctx in
    (* Add the constraint *)
    match rv.value with RvPlace p -> add_constraint mp p.var ctx | _ -> ctx
  in
  let add_opt_right_constraint (mp : mplace option) (rv : typed_rvalue)
      (ctx : pn_ctx) : pn_ctx =
    match mp with None -> ctx | Some mp -> add_right_constraint mp rv ctx
  in
  (* Specific case of constraint on left values *)
  let add_left_constraint (lv : typed_lvalue) (ctx : pn_ctx) : pn_ctx =
    let obj =
      object (self)
        inherit [_] reduce_typed_lvalue

        method zero _ = empty_ctx

        method plus ctx0 ctx1 _ = merge_ctxs (ctx0 ()) (ctx1 ())

        method! visit_Var _ v mp () =
          (* Register the variable *)
          let ctx = register_var (self#zero ()) v in
          (* Register the mplace information if there is such information *)
          match mp with Some mp -> add_constraint mp v.id ctx | None -> ctx
      end
    in
    let ctx1 = obj#visit_typed_lvalue () lv () in
    merge_ctxs ctx ctx1
  in

  (* This is used to propagate constraint information about places in case of
   * variable reassignments: we try to propagate the information from the
   * rvalue to the left *)
  let add_left_right_constraint (lv : typed_lvalue) (re : texpression)
      (ctx : pn_ctx) : pn_ctx =
    (* We propagate constraints across variable reassignments: `^0 = x`,
     * if the destination doesn't have naming information *)
    match lv.value with
    | LvVar (Var (({ id = _; basename = None; ty = _ } as lvar), lmp)) -> (
        if
          (* Check that there is not already a name for teh variable *)
          VarId.Map.mem lvar.id ctx.pure_vars
        then ctx
        else
          (* We ignore the left meta-place information: it should have been taken
           * care of by [add_left_constraint]. We try to use the right meta-place
           * information *)
          let add (name : string) (ctx : pn_ctx) : pn_ctx =
            (* Add the constraint for the pure variable *)
            let ctx = add_pure_var_constraint lvar.id name ctx in
            (* Add the constraint for the LLBC variable *)
            match lmp with
            | None -> ctx
            | Some lmp -> add_llbc_var_constraint lmp.var_id name ctx
          in
          match re.e with
          | Value (rv, rmp) ->
              (* We try to use the right-place information *)
              let ctx =
                match rmp with
                | Some { var_id; name; projection = [] } -> (
                    if Option.is_some name then add (Option.get name) ctx
                    else
                      match V.VarId.Map.find_opt var_id ctx.llbc_vars with
                      | None -> ctx
                      | Some name -> add name ctx)
                | _ -> ctx
              in
              (* We try to use the rvalue information *)
              let ctx =
                match rv with
                | { value = RvPlace { var = rvar_id; projection = [] }; ty = _ }
                  -> (
                    match VarId.Map.find_opt rvar_id ctx.pure_vars with
                    | None -> ctx
                    | Some name -> add name ctx)
                | _ -> ctx
              in
              ctx
          | _ -> ctx)
    | _ -> ctx
  in

  (* *)
  let rec update_texpression (e : texpression) (ctx : pn_ctx) :
      pn_ctx * texpression =
    let ty = e.ty in
    let ctx, e =
      match e.e with
      | Value (v, mp) -> update_value v mp ctx
      | App (app, arg) ->
          let ctx, app = update_texpression app ctx in
          let ctx, arg = update_texpression arg ctx in
          let e = App (app, arg) in
          (ctx, e)
      | Abs (x, e) -> update_abs x e ctx
      | Func _ -> (* nothing to do *) (ctx, e.e)
      | Let (monadic, lb, re, e) -> update_let monadic lb re e ctx
      | Switch (scrut, body) -> update_switch_body scrut body ctx
      | Meta (meta, e) -> update_meta meta e ctx
    in
    (ctx, { e; ty })
  (* *)
  and update_value (v : typed_rvalue) (mp : mplace option) (ctx : pn_ctx) :
      pn_ctx * expression =
    let ctx = add_opt_right_constraint mp v ctx in
    (ctx, Value (v, mp))
  (* *)
  and update_abs (x : typed_lvalue) (e : texpression) (ctx : pn_ctx) :
      pn_ctx * expression =
    (* We first add the left-constraint *)
    let ctx = add_left_constraint x ctx in
    (* Update the expression, and add additional constraints *)
    let ctx, e = update_texpression e ctx in
    (* Update the abstracted value *)
    let x = update_typed_lvalue ctx x in
    (* Put together *)
    (ctx, Abs (x, e))
  (* *)
  and update_let (monadic : bool) (lv : typed_lvalue) (re : texpression)
      (e : texpression) (ctx : pn_ctx) : pn_ctx * expression =
    (* We first add the left-constraint *)
    let ctx = add_left_constraint lv ctx in
    (* Then we try to propagate the right-constraints to the left, in case
     * the left constraints didn't give naming information *)
    let ctx = add_left_right_constraint lv re ctx in
    let ctx, re = update_texpression re ctx in
    let ctx, e = update_texpression e ctx in
    let lv = update_typed_lvalue ctx lv in
    (ctx, Let (monadic, lv, re, e))
  (* *)
  and update_switch_body (scrut : texpression) (body : switch_body)
      (ctx : pn_ctx) : pn_ctx * expression =
    let ctx, scrut = update_texpression scrut ctx in

    let ctx, body =
      match body with
      | If (e_true, e_false) ->
          let ctx1, e_true = update_texpression e_true ctx in
          let ctx2, e_false = update_texpression e_false ctx in
          let ctx = merge_ctxs ctx1 ctx2 in
          (ctx, If (e_true, e_false))
      | Match branches ->
          let ctx_branches_ls =
            List.map
              (fun br ->
                let ctx = add_left_constraint br.pat ctx in
                let ctx, branch = update_texpression br.branch ctx in
                let pat = update_typed_lvalue ctx br.pat in
                (ctx, { pat; branch }))
              branches
          in
          let ctxs, branches = List.split ctx_branches_ls in
          let ctx = merge_ctxs_ls ctxs in
          (ctx, Match branches)
    in
    (ctx, Switch (scrut, body))
  (* *)
  and update_meta (meta : meta) (e : texpression) (ctx : pn_ctx) :
      pn_ctx * expression =
    match meta with
    | Assignment (mp, rvalue, rmp) ->
        let ctx = add_right_constraint mp rvalue ctx in
        let ctx =
          match (mp.projection, rmp) with
          | [], Some { var_id; name; projection = [] } -> (
              let name =
                match name with
                | Some name -> Some name
                | None -> V.VarId.Map.find_opt var_id ctx.llbc_vars
              in
              match name with
              | None -> ctx
              | Some name -> add_llbc_var_constraint mp.var_id name ctx)
          | _ -> ctx
        in
        let ctx, e = update_texpression e ctx in
        (ctx, e.e)
  in

  let body =
    match def.body with
    | None -> None
    | Some body ->
        let input_names =
          List.filter_map
            (fun (v : var) ->
              match v.basename with
              | None -> None
              | Some name -> Some (v.id, name))
            body.inputs
        in
        let ctx =
          {
            pure_vars = VarId.Map.of_list input_names;
            llbc_vars = V.VarId.Map.empty;
          }
        in
        let _, body_exp = update_texpression body.body ctx in
        Some { body with body = body_exp }
  in
  { def with body }

(** Remove the meta-information *)
let remove_meta (def : fun_decl) : fun_decl =
  let obj =
    object
      inherit [_] map_expression as super

      method! visit_Meta env _ e = super#visit_expression env e.e
    end
  in
  match def.body with
  | None -> def
  | Some body ->
      let body = { body with body = obj#visit_texpression () body.body } in
      { def with body = Some body }

(** Inline the useless variable (re-)assignments:

    A lot of intermediate variable assignments are introduced through the
    compilation to MIR and by the translation itself (and the variable used
    on the left is often unnamed).

    Note that many of them are just variable "reassignments": `let x = y in ...`.
    Some others come from ??
    
    TODO: how do we call that when we introduce intermediate variable assignments
    for the arguments of a function call?

    [inline_named]: if `true`, inline all the assignments of the form
    `let VAR = VAR in ...`, otherwise inline only the ones where the variable
    on the left is anonymous.
    
    [inline_pure]: if `true`, inline all the pure assignments where the variable
    on the left is anonymous, but the assignments where the r-expression is
    a non-primitive function call (i.e.: inline the binops, ADT constructions,
    etc.).

    TODO: we have a smallish issue which is that rvalues should be merged with
    expressions... For now, this forces us to substitute whenever we can, but
    leave the let-bindings where they are, and eliminated them in a subsequent
    pass (if they are useless).
 *)
let inline_useless_var_reassignments (inline_named : bool) (inline_pure : bool)
    (def : fun_decl) : fun_decl =
  let obj =
    object (self)
      inherit [_] map_expression as super

      method! visit_Let env monadic lv re e =
        (* In order to filter, we need to check first that:
         * - the let-binding is not monadic
         * - the left-value is a variable
         *)
        match (monadic, lv.value) with
        | false, LvVar (Var (lv_var, _)) ->
            (* We can filter if: *)
            let filter = false in
            (* 1. Either:
             *   - the left variable is unnamed or [inline_named] is true
             *   - the right-expression is a variable
             *)
            let filter =
              match (inline_named, lv_var.basename) with
              | true, _ | _, None -> is_var re
              | _ -> filter
            in
            (* 2. Or:
             *   - the left variable is an unnamed variable
             *   - the right-expression is a value or a primitive function call
             *)
            let filter =
              if inline_pure then
                match re.e with
                | Value _ -> true
                | App _ -> (
                    (* Application: decompose, and check that function call *)
                    match opt_destruct_function_call re with
                    | Some (func, _) -> (
                        match func.func with
                        | Regular _ -> false
                        | Unop _ | Binop _ -> true)
                    | _ -> false)
                | _ -> filter
              else false
            in

            (* Update the environment and continue the exploration *)
            let re = self#visit_texpression env re in
            (* TODO: once rvalues and expressions are merged, filter the
             * let-binding (note that for now we leave it, expect it to
             * become useless, and wait for a subsequent pass to filter it) *)
            (* let env = add_subst lv_var.id re env in *)
            let env = if filter then VarId.Map.add lv_var.id re env else env in
            let e = self#visit_texpression env e in
            Let (monadic, lv, re, e)
        | _ -> super#visit_Let env monadic lv re e
      (** Visit the let-bindings to filter the useless ones (and update
          the substitution map while doing so *)

      method! visit_Value env v mp =
        (* Check if we need to substitute *)
        match v.value with
        | RvPlace p -> (
            match VarId.Map.find_opt p.var env with
            | None -> (* No substitution *) super#visit_Value env v mp
            | Some ne ->
                (* Substitute - note that we need to reexplore, because
                 * there may be stacked substitutions, if we have:
                 * var0 --> var1
                 * var1 --> var2.
                 *
                 * Also: we can always substitute if we substitute with
                 * a variable. If we substitute with a value we need to
                 * check that the path is empty.
                 * TODO: actually do a projection *)
                if is_var ne then
                  let var = as_var ne in
                  let p = { p with var } in
                  let nv = { v with value = RvPlace p } in
                  self#visit_Value env nv mp
                else if p.projection = [] then self#visit_expression env ne.e
                else super#visit_Value env v mp)
        | _ -> (* No substitution *) super#visit_Value env v mp
      (** Visit the values, to substitute them if possible *)

      method! visit_RvPlace env p =
        if p.projection = [] then
          match VarId.Map.find_opt p.var env with
          | None -> (* No substitution *) super#visit_RvPlace env p
          | Some ne -> (
              (* Substitute if the new expression is a value *)
              match ne.e with
              | Value (nv, _) -> self#visit_rvalue env nv.value
              | _ -> (* Not a value *) super#visit_RvPlace env p)
        else (* TODO: project *)
          super#visit_RvPlace env p
      (** Visit the places used as rvalues, to substitute them if possible *)
    end
  in
  match def.body with
  | None -> def
  | Some body ->
      let body =
        { body with body = obj#visit_texpression VarId.Map.empty body.body }
      in
      { def with body = Some body }

(** Given a forward or backward function call, is there, for every execution
    path, a child backward function called later with exactly the same input
    list prefix? We use this to filter useless function calls: if there are
    such child calls, we can remove this one (in case its outputs are not
    used).
    We do this check because we can't simply remove function calls whose
    outputs are not used, as they might fail. However, if a function fails,
    its children backward functions then fail on the same inputs (ignoring
    the additional inputs those receive).
    
    For instance, if we have:
    ```
    fn f<'a>(x : &'a mut T);
    ```
    
    We often have  things like this in the synthesized code:
    ```
    _ <-- f x;
    ...
    nx <-- f@back'a x y;
    ...
    ```

    In this situation, we can remove the call `f x`.
 *)
let expression_contains_child_call_in_all_paths (ctx : trans_ctx) (func0 : func)
    (args0 : texpression list) (e : texpression) : bool =
  let check_call (func1 : func) (args1 : texpression list) : bool =
    (* Check the func_ids, to see if call1's function is a child of call0's function *)
    match (func0.func, func1.func) with
    | Regular (id0, rg_id0), Regular (id1, rg_id1) ->
        (* Both are "regular" calls: check if they come from the same rust function *)
        if id0 = id1 then
          (* Same rust functions: check the regions hierarchy *)
          let call1_is_child =
            match (rg_id0, rg_id1) with
            | None, _ ->
                (* The function used in call0 is the forward function: the one
                 * used in call1 is necessarily a child *)
                true
            | Some _, None ->
                (* Opposite of previous case *)
                false
            | Some rg_id0, Some rg_id1 ->
                if rg_id0 = rg_id1 then true
                else
                  (* We need to use the regions hierarchy *)
                  (* First, lookup the signature of the LLBC function *)
                  let sg =
                    LlbcAstUtils.lookup_fun_sig id0 ctx.fun_context.fun_decls
                  in
                  (* Compute the set of ancestors of the function in call1 *)
                  let call1_ancestors =
                    LlbcAstUtils.list_parent_region_groups sg rg_id1
                  in
                  (* Check if the function used in call0 is inside *)
                  T.RegionGroupId.Set.mem rg_id0 call1_ancestors
          in
          (* If call1 is a child, then we need to check if the input arguments
           * used in call0 are a prefix of the input arguments used in call1
           * (note call1 being a child, it will likely consume strictly more
           * given back values).
           * *)
          if call1_is_child then
            let call1_args =
              Collections.List.prefix (List.length args0) args1
            in
            let args = List.combine args0 call1_args in
            (* Note that the input values are expressions, *which may contain
             * meta-values* (which we need to ignore). We only consider the
             * case where both expressions are actually values. *)
            let input_eq (v0, v1) =
              match (v0.e, v1.e) with
              | Value (v0, _), Value (v1, _) -> v0 = v1
              | _ -> false
            in
            (* Compare the input types and the prefix of the input arguments *)
            func0.type_params = func1.type_params && List.for_all input_eq args
          else (* Not a child *)
            false
        else (* Not the same function *)
          false
    | _ -> false
  in

  let visitor =
    object (self)
      inherit [_] reduce_expression

      method zero _ = false

      method plus b0 b1 _ = b0 () && b1 ()

      method! visit_texpression env e =
        match e.e with
        | Value (_, _) -> fun _ -> false
        | Let (_, _, re, e) -> (
            match opt_destruct_function_call re with
            | None -> fun () -> self#visit_texpression env e ()
            | Some (func1, args1) ->
                let call_is_child = check_call func1 args1 in
                if call_is_child then fun () -> true
                else fun () -> self#visit_texpression env e ())
        | App _ -> (
            fun () ->
              match opt_destruct_function_call e with
              | Some (func1, args1) -> check_call func1 args1
              | None -> false)
        | Abs (_, e) -> self#visit_texpression env e
        | Func _ -> fun () -> false
        | Meta (_, e) -> self#visit_texpression env e
        | Switch (_, body) -> self#visit_switch_body env body

      method! visit_switch_body env body =
        match body with
        | If (e1, e2) ->
            fun () ->
              self#visit_texpression env e1 ()
              && self#visit_texpression env e2 ()
        | Match branches ->
            fun () ->
              List.for_all
                (fun br -> self#visit_texpression env br.branch ())
                branches
    end
  in
  visitor#visit_texpression () e ()

(** Filter the useless assignments (removes the useless variables, filters
    the function calls) *)
let filter_useless (filter_monadic_calls : bool) (ctx : trans_ctx)
    (def : fun_decl) : fun_decl =
  (* We first need a transformation on *left-values*, which filters the useless
   * variables and tells us whether the value contains any variable which has
   * not been replaced by `_` (in which case we need to keep the assignment,
   * etc.).
   * 
   * This is implemented as a map-reduce.
   *
   * Returns: ( filtered_left_value, *all_dummies* )
   *
   * `all_dummies`:
   * If the returned boolean is true, it means that all the variables appearing
   * in the filtered left-value are *dummies* (meaning that if this left-value
   * appears at the left of a let-binding, this binding might potentially be
   * removed).
   *)
  let lv_visitor =
    object
      inherit [_] mapreduce_typed_lvalue

      method zero _ = true

      method plus b0 b1 _ = b0 () && b1 ()

      method! visit_var_or_dummy env v =
        match v with
        | Dummy -> (Dummy, fun _ -> true)
        | Var (v, mp) ->
            if VarId.Set.mem v.id env then (Var (v, mp), fun _ -> false)
            else (Dummy, fun _ -> true)
    end
  in
  let filter_typed_lvalue (used_vars : VarId.Set.t) (lv : typed_lvalue) :
      typed_lvalue * bool =
    let lv, all_dummies = lv_visitor#visit_typed_lvalue used_vars lv in
    (lv, all_dummies ())
  in

  (* We then implement the transformation on *expressions* through a mapreduce.
   * Note that the transformation is bottom-up.
   * The map filters the useless assignments, the reduce computes the set of
   * used variables.
   *)
  let expr_visitor =
    object (self)
      inherit [_] mapreduce_expression as super

      method zero _ = VarId.Set.empty

      method plus s0 s1 _ = VarId.Set.union (s0 ()) (s1 ())

      method! visit_place _ p = (p, fun _ -> VarId.Set.singleton p.var)
      (** Whenever we visit a place, we need to register the used variable *)

      method! visit_expression env e =
        match e with
        | Value (_, _) | App _ | Func _ | Switch (_, _) | Meta (_, _) | Abs _ ->
            super#visit_expression env e
        | Let (monadic, lv, re, e) ->
            (* Compute the set of values used in the next expression *)
            let e, used = self#visit_texpression env e in
            let used = used () in
            (* Filter the left values *)
            let lv, all_dummies = filter_typed_lvalue used lv in
            (* Small utility - called if we can't filter the let-binding *)
            let dont_filter () =
              let re, used_re = self#visit_texpression env re in
              let used = VarId.Set.union used (used_re ()) in
              (Let (monadic, lv, re, e), fun _ -> used)
            in
            (* Potentially filter the let-binding *)
            if all_dummies then
              if not monadic then
                (* Not a monadic let-binding: simple case *)
                (e.e, fun _ -> used)
              else
                (* Monadic let-binding: trickier.
                 * We can filter if the right-expression is a function call,
                 * under some conditions. *)
                match (filter_monadic_calls, opt_destruct_function_call re) with
                | true, Some (func, args) ->
                    (* We need to check if there is a child call - see
                     * the comments for:
                     * [expression_contains_child_call_in_all_paths] *)
                    let has_child_call =
                      expression_contains_child_call_in_all_paths ctx func args
                        e
                    in
                    if has_child_call then (* Filter *)
                      (e.e, fun _ -> used)
                    else (* No child call: don't filter *)
                      dont_filter ()
                | _ ->
                    (* Not a call or not allowed to filter: we can't filter *)
                    dont_filter ()
            else (* There are used variables: don't filter *)
              dont_filter ()
    end
  in
  (* We filter only inside of transparent (i.e., non-opaque) definitions *)
  match def.body with
  | None -> def
  | Some body ->
      (* Visit the body *)
      let body_exp, used_vars = expr_visitor#visit_texpression () body.body in
      (* Visit the parameters - TODO: update: we can filter only if the definition
       * is not recursive (otherwise it might mess up with the decrease clauses:
       * the decrease clauses uses all the inputs given to the function, if some
       * inputs are replaced by '_' we can't give it to the function used in the
       * decreases clause).
       * For now we deactivate the filtering. *)
      let used_vars = used_vars () in
      let inputs_lvs =
        if false then
          List.map
            (fun lv -> fst (filter_typed_lvalue used_vars lv))
            body.inputs_lvs
        else body.inputs_lvs
      in
      (* Return *)
      let body = { body with body = body_exp; inputs_lvs } in
      { def with body = Some body }

(** Return `None` if the function is a backward function with no outputs (so
    that we eliminate the definition which is useless).

    Note that the calls to such functions are filtered when translating from
    symbolic to pure. Here, we remove the definitions altogether, because they
    are now useless
  *)
let filter_if_backward_with_no_outputs (config : config) (def : fun_decl) :
    fun_decl option =
  let return_ty =
    if config.use_state_monad then
      mk_arrow mk_state_ty
        (mk_result_ty (mk_simpl_tuple_ty [ mk_state_ty; unit_ty ]))
    else mk_result_ty (mk_simpl_tuple_ty [ unit_ty ])
  in
  if
    config.filter_useless_functions && Option.is_some def.back_id
    && def.signature.outputs = [ return_ty ]
  then None
  else Some def

(** Return `false` if the forward function is useless and should be filtered.

    - a forward function with no output (comes from a Rust function with
      unit return type)
    - the function has mutable borrows as inputs (which is materialized
      by the fact we generated backward functions which were not filtered).

    In such situation, every call to the Rust function will be translated to:
    - a call to the forward function which returns nothing
    - calls to the backward functions
    As a failing backward function implies the forward function also fails,
    we can filter the calls to the forward function, which thus becomes
    useless.
    In such situation, we can remove the forward function definition
    altogether.
  *)
let keep_forward (config : config) (trans : pure_fun_translation) : bool =
  let fwd, backs = trans in
  (* Note that at this point, the output types are no longer seen as tuples:
   * they should be lists of length 1. *)
  if
    config.filter_useless_functions
    && fwd.signature.outputs = [ mk_result_ty unit_ty ]
    && backs <> []
  then false
  else true

(** Convert the unit variables to `()` if they are used as right-values or
    `_` if they are used as left values in patterns. *)
let unit_vars_to_unit (def : fun_decl) : fun_decl =
  (* The map visitor *)
  let obj =
    object
      inherit [_] map_expression as super

      method! visit_var_or_dummy _ v =
        match v with
        | Dummy -> Dummy
        | Var (v, mp) -> if v.ty = unit_ty then Dummy else Var (v, mp)
      (** Replace in lvalues *)

      method! visit_typed_rvalue env rv =
        if rv.ty = unit_ty then unit_rvalue else super#visit_typed_rvalue env rv
      (** Replace in rvalues *)
    end
  in
  (* Update the body *)
  match def.body with
  | None -> def
  | Some body ->
      let body_exp = obj#visit_texpression () body.body in
      (* Update the input parameters *)
      let inputs_lvs = List.map (obj#visit_typed_lvalue ()) body.inputs_lvs in
      (* Return *)
      let body = Some { body with body = body_exp; inputs_lvs } in
      { def with body }

(** Eliminate the box functions like `Box::new`, `Box::deref`, etc. Most of them
    are translated to identity, and `Box::free` is translated to `()`.

    Note that the box types have already been eliminated during the translation
    from symbolic to pure.
    The reason why we don't eliminate the box functions at the same time is
    that we would need to eliminate them in two different places: when translating
    function calls, and when translating end abstractions. Here, we can do
    something simpler, in one micro-pass.
 *)
let eliminate_box_functions (_ctx : trans_ctx) (def : fun_decl) : fun_decl =
  (* The map visitor *)
  let obj =
    object
      inherit [_] map_expression as super

      method! visit_texpression env e =
        match opt_destruct_function_call e with
        | Some (func, args) -> (
            match func.func with
            | Regular (A.Assumed aid, rg_id) -> (
                (* Below, when dealing with the arguments: we consider the very
                 * general case, where functions could be boxed (meaning we
                 * could have: `box_new f x`)
                 * *)
                match (aid, rg_id) with
                | A.BoxNew, _ ->
                    assert (rg_id = None);
                    let arg, args = Collections.List.pop args in
                    mk_apps arg args
                | A.BoxDeref, None ->
                    (* `Box::deref` forward is the identity *)
                    let arg, args = Collections.List.pop args in
                    mk_apps arg args
                | A.BoxDeref, Some _ ->
                    (* `Box::deref` backward is `()` (doesn't give back anything) *)
                    assert (args = []);
                    mk_value_expression unit_rvalue None
                | A.BoxDerefMut, None ->
                    (* `Box::deref_mut` forward is the identity *)
                    let arg, args = Collections.List.pop args in
                    mk_apps arg args
                | A.BoxDerefMut, Some _ ->
                    (* `Box::deref_mut` back is almost the identity:
                     * let box_deref_mut (x_init : t) (x_back : t) : t = x_back
                     * *)
                    let arg, args =
                      match args with
                      | _ :: given_back :: args -> (given_back, args)
                      | _ -> failwith "Unreachable"
                    in
                    mk_apps arg args
                | A.BoxFree, _ ->
                    assert (args = []);
                    mk_value_expression unit_rvalue None
                | ( ( A.Replace | A.VecNew | A.VecPush | A.VecInsert | A.VecLen
                    | A.VecIndex | A.VecIndexMut ),
                    _ ) ->
                    super#visit_texpression env e)
            | _ -> super#visit_texpression env e)
        | _ -> super#visit_texpression env e
    end
  in
  (* Update the body *)
  match def.body with
  | None -> def
  | Some body ->
      let body = Some { body with body = obj#visit_texpression () body.body } in
      { def with body }

(** Decompose the monadic let-bindings.

    See the explanations in [config].
 *)
let decompose_monadic_let_bindings (_ctx : trans_ctx) (def : fun_decl) :
    fun_decl =
  match def.body with
  | None -> def
  | Some body ->
      (* Set up the var id generator *)
      let cnt = get_body_min_var_counter body in
      let _, fresh_id = VarId.mk_stateful_generator cnt in
      (* It is a very simple map *)
      let obj =
        object (self)
          inherit [_] map_expression as super

          method! visit_Let env monadic lv re next_e =
            if not monadic then super#visit_Let env monadic lv re next_e
            else
              (* If monadic, we need to check if the left-value is a variable:
               * - if yes, don't decompose
               * - if not, make the decomposition in two steps
               *)
              match lv.value with
              | LvVar _ ->
                  (* Variable: nothing to do *)
                  super#visit_Let env monadic lv re next_e
              | _ ->
                  (* Not a variable: decompose *)
                  (* Introduce a temporary variable to receive the value of the
                   * monadic binding *)
                  let vid = fresh_id () in
                  let tmp : var = { id = vid; basename = None; ty = lv.ty } in
                  let ltmp = mk_typed_lvalue_from_var tmp None in
                  let rtmp = mk_typed_rvalue_from_var tmp in
                  let rtmp = mk_value_expression rtmp None in
                  (* Visit the next expression *)
                  let next_e = self#visit_texpression env next_e in
                  (* Create the let-bindings *)
                  (mk_let true ltmp re (mk_let false lv rtmp next_e)).e
        end
      in
      (* Update the body *)
      let body = Some { body with body = obj#visit_texpression () body.body } in
      (* Return *)
      { def with body }

(** Unfold the monadic let-bindings to explicit matches. *)
let unfold_monadic_let_bindings (config : config) (_ctx : trans_ctx)
    (def : fun_decl) : fun_decl =
  match def.body with
  | None -> def
  | Some body ->
      (* We may need to introduce fresh variables for the state *)
      let fresh_var_id =
        let var_cnt = get_body_min_var_counter body in
        let _, fresh_var_id = VarId.mk_stateful_generator var_cnt in
        fresh_var_id
      in
      let fresh_state_var () =
        let id = fresh_var_id () in
        { id; basename = Some "st"; ty = mk_state_ty }
      in
      (* It is a very simple map *)
      let obj =
        object (_self)
          inherit [_] map_expression as super

          method! visit_Switch env scrut switch_body =
            (* We transform the switches the following way (if their branches
             * are stateful):
             * ```
             * match x with
             * | Pati -> branchi
             *
             *     ~~>
             *
             * fun st ->
             * match x with
             * | Pati -> branchi st
             * ```
             *
             * The reason is that after unfolding the monadic lets, we often
             * have this: `(match x with | ...) st`, and we want to "push" the
             * `st` variable inside.
             *)
            let sb_ty = get_switch_body_ty switch_body in
            if Option.is_some (opt_destruct_state_monad_result sb_ty) then
              (* Generate a fresh state variable *)
              let state_var = fresh_state_var () in
              let state_value = mk_typed_rvalue_from_var state_var in
              let state_value = mk_value_expression state_value None in
              let state_lvar = mk_typed_lvalue_from_var state_var None in
              (* Apply in all the branches and reconstruct the switch *)
              let mk_app e = mk_app e state_value in
              let switch_body = map_switch_body_branches mk_app switch_body in
              let e = mk_switch scrut switch_body in
              let e = mk_abs state_lvar e in
              (* Introduce the lambda and continue
               * Rk.: we will revisit the switch, but won't loop because its
               * type has now changed (the `state -> ...` disappeared) *)
              super#visit_Abs env state_lvar e
            else super#visit_Switch env scrut switch_body

          method! visit_Let env monadic lv re e =
            (* For now, we do the following transformation:
             * ```
             * x <-- re; e
             *
             *     ~~>
             *
             * (fun st ->
             * match re st with
             * | Return (st', x) -> e st'
             * | Fail err -> Fail err)
             * ```
             *
             * We rely on the simplification pass which comes later to normalize
             * away expressions like `(fun x -> e) y`.
             * 
             * TODO: fix the use of state-error monads (with the bakward functions,
             * we apply some updates twice...
             * It would be better if symbolic to pure generated code of the
             * following shape:
             * `(st1, x) <-- e st0`
             * Then, this micro-pass would only expand the monadic let-bindings
             * (we wouldn't need to introduce state variables).
             * *)
            (* TODO: we should use a monad "kind" instead of a boolean *)
            if not monadic then super#visit_Let env monadic lv re e
            else
              (* We don't do the same thing if we use a state-error monad or simply
               * an error monad.
               * Note that some functions always live in the error monad (arithmetic
               * operations, for instance).
               *)
              (* TODO: this information should be computed in SymbolicToPure and
               * store in an enum ("monadic" should be an enum, not a bool). *)
              let re_uses_state =
                Option.is_some (opt_destruct_state_monad_result re.ty)
              in
              if re_uses_state then (
                let e0 = e in
                (* Create a fresh state variable *)
                let state_var = fresh_state_var () in
                (* The type of `e` is: `state -> e_no_arrow_ty` *)
                let _, e_no_arrow_ty = destruct_arrow e.ty in
                let e_no_monad_ty = destruct_result e_no_arrow_ty in
                let _, re_no_arrow_ty = destruct_arrow re.ty in
                let re_no_monad_ty = destruct_result re_no_arrow_ty in
                (* Add the state argument on the right-expression *)
                let re =
                  let state_value = mk_typed_rvalue_from_var state_var in
                  let state_value = mk_value_expression state_value None in
                  mk_app re state_value
                in
                (* Create the match *)
                let fail_pat = mk_result_fail_lvalue re_no_monad_ty in
                let fail_value = mk_result_fail_rvalue e_no_monad_ty in
                let fail_branch =
                  {
                    pat = fail_pat;
                    branch = mk_value_expression fail_value None;
                  }
                in
                (* The `Success` branch introduces a fresh state variable *)
                let pat_state_var = fresh_state_var () in
                let pat_state_lvalue =
                  mk_typed_lvalue_from_var pat_state_var None
                in
                let success_pat =
                  mk_result_return_lvalue
                    (mk_simpl_tuple_lvalue [ pat_state_lvalue; lv ])
                in
                let pat_state_rvalue = mk_typed_rvalue_from_var pat_state_var in
                let pat_state_rvalue =
                  mk_value_expression pat_state_rvalue None
                in
                (* TODO: write a utility to create matches (and perform
                 * type-checking, etc.) *)
                let success_branch =
                  { pat = success_pat; branch = mk_app e pat_state_rvalue }
                in
                let switch_body = Match [ fail_branch; success_branch ] in
                let e = Switch (re, switch_body) in
                let e = { e; ty = e_no_arrow_ty } in
                (* Add the lambda to introduce the state variable *)
                let e = mk_abs (mk_typed_lvalue_from_var state_var None) e in
                (* Sanity check *)
                assert (e0.ty = e.ty);
                assert (fail_branch.branch.ty = success_branch.branch.ty);
                (* Continue *)
                super#visit_expression env e.e)
              else
                let re_ty = Option.get (opt_destruct_result re.ty) in
                assert (lv.ty = re_ty);
                let fail_pat = mk_result_fail_lvalue lv.ty in
                let fail_value = mk_result_fail_rvalue e.ty in
                let fail_branch =
                  {
                    pat = fail_pat;
                    branch = mk_value_expression fail_value None;
                  }
                in
                let success_pat = mk_result_return_lvalue lv in
                let success_branch = { pat = success_pat; branch = e } in
                let switch_body = Match [ fail_branch; success_branch ] in
                let e = Switch (re, switch_body) in
                (* Continue *)
                super#visit_expression env e
        end
      in
      (* Update the body: add *)
      let body, signature =
        let state_var = fresh_state_var () in
        (* First, unfold the expressions inside the body *)
        let body_e = obj#visit_texpression () body.body in
        (* Then, add a "state" input variable if necessary: *)
        if config.use_state_monad then
          (* - in the body *)
          let state_rvalue = mk_typed_rvalue_from_var state_var in
          let body_e = mk_app body_e (mk_value_expression state_rvalue None) in
          (* - in the signature *)
          let sg = def.signature in
          (* Input types *)
          let sg_inputs = sg.inputs @ [ mk_state_ty ] in
          (* Output types *)
          let sg_outputs = Collections.List.to_cons_nil sg.outputs in
          let _, sg_outputs = dest_arrow_ty sg_outputs in
          let sg_outputs = [ sg_outputs ] in
          let sg = { sg with inputs = sg_inputs; outputs = sg_outputs } in
          (* Input list *)
          let inputs = body.inputs @ [ state_var ] in
          let input_lv = mk_typed_lvalue_from_var state_var None in
          let inputs_lvs = body.inputs_lvs @ [ input_lv ] in
          let body = { body = body_e; inputs; inputs_lvs } in
          (body, sg)
        else ({ body with body = body_e }, def.signature)
      in
      (* Return *)
      { def with body = Some body; signature }

(** Apply all the micro-passes to a function.

    Will return `None` if the function is a backward function with no outputs.

    [ctx]: used only for printing.
 *)
let apply_passes_to_def (config : config) (ctx : trans_ctx) (def : fun_decl) :
    fun_decl option =
  (* Debug *)
  log#ldebug
    (lazy
      ("PureMicroPasses.apply_passes_to_def: "
      ^ Print.fun_name_to_string def.basename
      ^ " ("
      ^ Print.option_to_string T.RegionGroupId.to_string def.back_id
      ^ ")"));

  (* First, find names for the variables which are unnamed *)
  let def = compute_pretty_names def in
  log#ldebug
    (lazy ("compute_pretty_name:\n\n" ^ fun_decl_to_string ctx def ^ "\n"));

  (* TODO: we might want to leverage more the assignment meta-data, for
   * aggregates for instance. *)

  (* TODO: reorder the branches of the matches/switches *)

  (* The meta-information is now useless: remove it.
   * Rk.: some passes below use the fact that we removed the meta-data
   * (otherwise we would have to "unmeta" expressions before matching) *)
  let def = remove_meta def in
  log#ldebug (lazy ("remove_meta:\n\n" ^ fun_decl_to_string ctx def ^ "\n"));

  (* Remove the backward functions with no outputs.
   * Note that the calls to those functions should already have been removed,
   * when translating from symbolic to pure. Here, we remove the definitions
   * altogether, because they are now useless *)
  let def = filter_if_backward_with_no_outputs config def in

  match def with
  | None -> None
  | Some def ->
      (* Convert the unit variables to `()` if they are used as right-values or
       * `_` if they are used as left values. *)
      let def = unit_vars_to_unit def in
      log#ldebug
        (lazy ("unit_vars_to_unit:\n\n" ^ fun_decl_to_string ctx def ^ "\n"));

      (* Inline the useless variable reassignments *)
      let inline_named_vars = true in
      let inline_pure = true in
      let def =
        inline_useless_var_reassignments inline_named_vars inline_pure def
      in
      log#ldebug
        (lazy
          ("inline_useless_var_assignments:\n\n" ^ fun_decl_to_string ctx def
         ^ "\n"));

      (* Eliminate the box functions - note that the "box" types were eliminated
       * during the symbolic to pure phase: see the comments for [eliminate_box_functions] *)
      let def = eliminate_box_functions ctx def in
      log#ldebug
        (lazy
          ("eliminate_box_functions:\n\n" ^ fun_decl_to_string ctx def ^ "\n"));

      (* Filter the useless variables, assignments, function calls, etc. *)
      let def = filter_useless config.filter_useless_monadic_calls ctx def in
      log#ldebug
        (lazy ("filter_useless:\n\n" ^ fun_decl_to_string ctx def ^ "\n"));

      (* Decompose the monadic let-bindings - F* specific
       * TODO: remove? With the state-error monad, it is becoming completely
       * ad-hoc. *)
      let def =
        if config.decompose_monadic_let_bindings then (
          (* TODO: we haven't updated the code to handle the state-error monad *)
          assert (not config.use_state_monad);
          let def = decompose_monadic_let_bindings ctx def in
          log#ldebug
            (lazy
              ("decompose_monadic_let_bindings:\n\n"
             ^ fun_decl_to_string ctx def ^ "\n"));
          def)
        else (
          log#ldebug
            (lazy
              "ignoring decompose_monadic_let_bindings due to the configuration\n");
          def)
      in

      (* Unfold the monadic let-bindings *)
      let def =
        if config.unfold_monadic_let_bindings then (
          let def = unfold_monadic_let_bindings config ctx def in
          log#ldebug
            (lazy
              ("unfold_monadic_let_bindings:\n\n" ^ fun_decl_to_string ctx def
             ^ "\n"));
          def)
        else (
          log#ldebug
            (lazy
              "ignoring unfold_monadic_let_bindings due to the configuration\n");
          def)
      in

      (* We are done *)
      Some def

(** Return the forward/backward translations on which we applied the micro-passes.

    Also returns a boolean indicating whether the forward function should be kept
    or not (because useful/useless - `true` means we need to keep the forward
    function).
    Note that we don't "filter" the forward function and return a boolean instead,
    because this function contains useful information to extract the backward
    functions: keeping it is not necessary but more convenient.
 *)
let apply_passes_to_pure_fun_translation (config : config) (ctx : trans_ctx)
    (trans : pure_fun_translation) : bool * pure_fun_translation =
  (* Apply the passes to the individual functions *)
  let forward, backwards = trans in
  let forward = Option.get (apply_passes_to_def config ctx forward) in
  let backwards = List.filter_map (apply_passes_to_def config ctx) backwards in
  let trans = (forward, backwards) in
  (* Compute whether we need to filter the forward function or not *)
  (keep_forward config trans, trans)