package catala

  1. Overview
  2. Docs
Compiler and library for the literate programming language for tax code specification

Install

dune-project
 Dependency

Authors

Maintainers

Sources

1.0.0-alpha.tar.gz
md5=2615968670ac21b1d00386a9b04b3843
sha512=eff292fdd75012f26ce7b17020f5a8374eef37cd4dd6ba60338dfbe89fbcad3443d1b409e44c182b740da9f58dff7e76dcb8ddefe47f9b2b160666d1c6930143

doc/src/catala.driver/driver.ml.html

Source file driver.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
(* This file is part of the Catala compiler, a specification language for tax
   and social benefits computation rules. Copyright (C) 2020 Inria,
   contributors: Denis Merigoux <denis.merigoux@inria.fr>, Emile Rolley
   <emile.rolley@tuta.io>, Louis Gesbert <louis.gesbert@inria.fr>

   Licensed under the Apache License, Version 2.0 (the "License"); you may not
   use this file except in compliance with the License. You may obtain a copy of
   the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   License for the specific language governing permissions and limitations under
   the License. *)

open Catala_utils
open Shared_ast

(** Associates a file extension with its corresponding
    {!type: Global.backend_lang} string representation. *)
let extensions = [".catala_fr", "fr"; ".catala_en", "en"; ".catala_pl", "pl"]

let modname_of_file f =
  String.capitalize_ascii
    (String.to_id Filename.(basename (remove_extension f)))

let load_modules
    options
    includes
    ~stdlib
    ?(more_includes = [])
    ?(allow_notmodules = false)
    program :
    ModuleName.t Ident.Map.t
    * (Surface.Ast.module_content * ModuleName.t Ident.Map.t) ModuleName.Map.t =
  let stdlib_root_module lang =
    let lang = if Global.has_localised_stdlib lang then lang else Global.En in
    "Stdlib_" ^ Cli.language_code lang
  in
  if stdlib <> None || program.Surface.Ast.program_used_modules <> [] then
    Message.debug "Loading module interfaces...";
  (* Recurse into program modules, looking up files in [using] and loading
     them *)
  let stdlib_includes =
    match stdlib with
    | Some dir -> File.Tree.build (options.Global.path_rewrite dir)
    | None -> File.Tree.empty
  in
  let stdlib_use file =
    let pos = Pos.from_info file 0 0 0 0 in
    let lang = Cli.file_lang file in
    {
      Surface.Ast.mod_use_name = stdlib_root_module lang, pos;
      Surface.Ast.mod_use_alias = "Stdlib", pos;
    }
  in
  let includes =
    List.map options.Global.path_rewrite includes @ more_includes
    |> List.map File.Tree.build
    |> List.fold_left File.Tree.union File.Tree.empty
  in
  let err_req_pos chain =
    List.map (fun mpos -> "Module required from", mpos) chain
  in
  let find_module in_stdlib req_chain (mname, mpos) =
    let required_from_file = Pos.get_file mpos in
    let includes =
      if in_stdlib then stdlib_includes
      else
        File.Tree.union includes
          (File.Tree.build (File.dirname required_from_file))
    in
    match
      List.filter_map
        (fun (ext, _) -> File.Tree.lookup includes (mname ^ ext))
        extensions
    with
    | [] ->
      if in_stdlib then
        Message.error
          "The standard library module @{<magenta>%s@}@ could@ not@ be@ found@ \
           at@ %a,@ please@ check@ your@ Catala@ installation.@ You can use \
           the command-line flag @{<yellow>--stdlib=DIR@}@ to@ specify@ a@ \
           non-standard@ location."
          mname File.format
          (options.Global.path_rewrite (Option.get stdlib))
      else
        Message.error
          ~extra_pos:(err_req_pos (mpos :: req_chain))
          "Required module not found: @{<blue>%s@}" mname
    | [f] -> f
    | ms ->
      Message.error
        ~extra_pos:(err_req_pos (mpos :: req_chain))
        "Required module @{<blue>%s@} matches multiple files:@;<1 2>%a" mname
        (Format.pp_print_list ~pp_sep:Format.pp_print_space File.format)
        ms
  in
  let rec aux is_stdlib req_chain seen uses :
      (ModuleName.t * Surface.Ast.module_content * ModuleName.t Ident.Map.t)
      option
      File.Map.t
      * ModuleName.t Ident.Map.t =
    List.fold_left
      (fun (seen, use_map) use ->
        let f = find_module is_stdlib req_chain use.Surface.Ast.mod_use_name in
        match File.Map.find_opt f seen with
        | Some (Some (modname, _, _)) ->
          ( seen,
            Ident.Map.add
              (Mark.remove use.Surface.Ast.mod_use_alias)
              modname use_map )
        | Some None ->
          Message.error
            ~extra_pos:
              (err_req_pos (Mark.get use.Surface.Ast.mod_use_name :: req_chain))
            "Circular module dependency"
        | None ->
          let default_module_name =
            if allow_notmodules then
              (* This preserves the filename capitalisation, which corresponds
                 to the convention for files related to not-module compilation
                 artifacts and is used by [depends] below *)
              Some Filename.(basename (remove_extension f))
            else None
          in
          let module_content =
            if options.Global.whole_program then
              Surface.Parser_driver.load_interface_and_code ?default_module_name
                (Global.FileName f)
            else
              Surface.Parser_driver.load_interface ?default_module_name
                (Global.FileName f)
          in
          let modname =
            ModuleName.fresh
              module_content.Surface.Ast.module_modname.module_name
          in
          let seen = File.Map.add f None seen in
          let seen, file_use_map =
            aux is_stdlib
              (Mark.get use.Surface.Ast.mod_use_name :: req_chain)
              seen module_content.Surface.Ast.module_submodules
          in
          ( File.Map.add f (Some (modname, module_content, file_use_map)) seen,
            Ident.Map.add
              (Mark.remove use.Surface.Ast.mod_use_alias)
              modname use_map ))
      (seen, Ident.Map.empty) uses
  in
  let file =
    match program.Surface.Ast.program_module with
    | Some m -> Pos.get_file (Mark.get m.module_name)
    | None -> List.hd program.Surface.Ast.program_source_files
  in
  let modules_map file_map =
    File.Map.fold
      (fun _ info acc ->
        match info with
        | None -> acc
        | Some (mname, intf, use_map) ->
          ModuleName.Map.add mname (intf, use_map) acc)
      file_map ModuleName.Map.empty
  in
  let stdlib_files, stdlib_uses =
    if stdlib = None then File.Map.empty, Ident.Map.empty
    else
      let stdlib_files, stdlib_use_map =
        aux true [Pos.from_info file 0 0 0 0] File.Map.empty [stdlib_use file]
      in
      let stdlib_modules = modules_map stdlib_files in
      let _, (_, stdlib_uses) =
        (* Uses from the stdlib are "flattened" to the parent module, but can
           still be overriden *)
        ModuleName.Map.choose stdlib_modules
      in
      ( stdlib_files,
        Ident.Map.union (fun _ _ m -> Some m) stdlib_uses stdlib_use_map )
  in
  let file_module_map, root_uses =
    aux false [] stdlib_files program.Surface.Ast.program_used_modules
  in
  let file_module_map =
    File.Map.mapi
      (fun file ->
        Option.map (fun (mname, intf, use_map) ->
            ( mname,
              intf,
              if
                File.Map.mem file stdlib_files
                || intf.Surface.Ast.module_modname.module_external
              then use_map
              else Ident.Map.union (fun _ _ m -> Some m) stdlib_uses use_map )))
      file_module_map
  in
  ( Ident.Map.union (fun _ _ m -> Some m) stdlib_uses root_uses,
    modules_map file_module_map )

module Passes = struct
  (* Each pass takes only its cli options, then calls upon its dependent passes
     (forwarding their options as needed) *)

  let debug_pass_name s =
    Message.debug "@{<bold;magenta>=@} @{<bold>%s@} @{<bold;magenta>=@}"
      (String.uppercase_ascii s)

  let surface options : Surface.Ast.program =
    debug_pass_name "surface";
    Surface.Parser_driver.parse_top_level_file options.Global.input_src

  let desugared options ~includes ~stdlib :
      Desugared.Ast.program * Desugared.Name_resolution.context =
    let prg = surface options in
    let mod_uses, modules = load_modules options includes ~stdlib prg in
    debug_pass_name "desugared";
    Message.debug "Name resolution...";
    let ctx = Desugared.Name_resolution.form_context (prg, mod_uses) modules in
    Message.debug "Desugaring...";
    let modules = ModuleName.Map.map fst modules in
    let prg = Desugared.From_surface.translate_program ctx modules prg in
    Message.debug "Disambiguating...";
    let prg = Desugared.Disambiguate.program prg in
    Message.debug "Linting...";
    Desugared.Linting.lint_program prg;
    prg, ctx

  let scopelang options ~includes ~stdlib : untyped Scopelang.Ast.program =
    let prg, _ = desugared options ~includes ~stdlib in
    debug_pass_name "scopelang";
    let exceptions_graphs =
      Scopelang.From_desugared.build_exceptions_graph prg
    in
    let prg =
      Scopelang.From_desugared.translate_program prg exceptions_graphs
    in
    prg

  let dcalc :
      type ty.
      Global.options ->
      includes:Global.raw_file list ->
      stdlib:Global.raw_file option ->
      optimize:bool ->
      check_invariants:bool ->
      autotest:bool ->
      typed:ty mark ->
      ty Dcalc.Ast.program * TypeIdent.t list =
   fun options ~includes ~stdlib ~optimize ~check_invariants ~autotest ~typed ->
    let prg = scopelang options ~includes ~stdlib in
    debug_pass_name "dcalc";
    let type_ordering =
      Scopelang.Dependency.check_type_cycles prg.program_ctx.ctx_structs
        prg.program_ctx.ctx_enums
    in
    let (prg : ty Scopelang.Ast.program) =
      match typed with
      | Typed _ ->
        Message.debug "Typechecking...";
        Scopelang.Ast.type_program prg
      | Untyped _ -> prg
      | Custom _ -> invalid_arg "Driver.Passes.dcalc"
    in
    Message.debug "Translating to default calculus...";
    let prg = Dcalc.From_scopelang.translate_program prg in
    let prg =
      if autotest then (
        Interpreter.load_runtime_modules
          ~hashf:Hash.(finalise ~monomorphize_types:false)
          prg;
        Dcalc.Autotest.program prg)
      else prg
    in
    let prg =
      if optimize then begin
        Message.debug "Optimizing default calculus...";
        Optimizations.optimize_program prg
      end
      else prg
    in
    let (prg : ty Dcalc.Ast.program) =
      match typed with
      | Typed _ ->
        Message.debug "Typechecking again...";
        Typing.program ~internal_check:true prg
      | Untyped _ -> prg
      | Custom _ -> assert false
    in
    if check_invariants then (
      Message.debug "Checking invariants...";
      match typed with
      | Typed _ ->
        if Dcalc.Invariants.check_all_invariants prg then
          Message.result "All invariant checks passed"
        else
          raise
            (Message.error ~internal:true "Some Dcalc invariants are invalid")
      | _ -> Message.error "--check-invariants cannot be used with --no-typing");
    prg, type_ordering

  let lcalc
      (type ty)
      options
      ~includes
      ~stdlib
      ~optimize
      ~check_invariants
      ~autotest
      ~(typed : ty mark)
      ~closure_conversion
      ~keep_special_ops
      ~monomorphize_types
      ~expand_ops
      ~renaming :
      typed Lcalc.Ast.program * TypeIdent.t list * Renaming.context option =
    let prg, type_ordering =
      dcalc options ~includes ~stdlib ~optimize ~check_invariants ~autotest
        ~typed
    in
    debug_pass_name "lcalc";
    let prg =
      match typed with
      | Untyped _ -> Lcalc.From_dcalc.translate_program prg
      | Typed _ -> Lcalc.From_dcalc.translate_program prg
      | Custom _ -> invalid_arg "Driver.Passes.lcalc"
    in
    let prg = if expand_ops then Lcalc.Expand_op.program prg else prg in
    let prg =
      if optimize then begin
        Message.debug "Optimizing lambda calculus...";
        Optimizations.optimize_program prg
      end
      else prg
    in
    let prg =
      if not closure_conversion then (
        Message.debug "Retyping lambda calculus...";
        let prg = Typing.program ~internal_check:true prg in
        if expand_ops then Lcalc.Expand_op.program prg else prg)
      else (
        Message.debug "Performing closure conversion...";
        let prg =
          Lcalc.Closure_conversion.closure_conversion ~keep_special_ops prg
        in
        let prg =
          if optimize then (
            Message.debug "Optimizing lambda calculus...";
            Optimizations.optimize_program prg)
          else prg
        in
        Message.debug "Retyping lambda calculus...";
        Typing.program ~internal_check:true ~assume_op_types:true prg)
    in
    let prg, type_ordering =
      if monomorphize_types then (
        Message.debug "Monomorphizing types...";
        let prg, type_ordering = Lcalc.Monomorphize.program prg in
        Message.debug "Retyping lambda calculus...";
        let prg =
          Typing.program ~assume_op_types:true ~internal_check:true prg
        in
        prg, type_ordering)
      else prg, type_ordering
    in
    match renaming with
    | None -> prg, type_ordering, None
    | Some renaming ->
      Message.debug "Renaming idents...";
      let prg, ren_ctx = Renaming.apply renaming prg in
      let type_ordering =
        let open TypeIdent in
        List.map
          (function
            | Struct s -> Struct (Renaming.struct_name ren_ctx s)
            | Enum e -> Enum (Renaming.enum_name ren_ctx e))
          type_ordering
      in
      prg, type_ordering, Some ren_ctx

  let scalc
      options
      ~includes
      ~stdlib
      ~optimize
      ~check_invariants
      ~autotest
      ~closure_conversion
      ~keep_special_ops
      ~dead_value_assignment
      ~no_struct_literals
      ~keep_module_names
      ~monomorphize_types
      ~expand_ops
      ~renaming : Scalc.Ast.program * TypeIdent.t list * Renaming.context =
    let prg, type_ordering, renaming_context =
      lcalc options ~includes ~stdlib ~optimize ~check_invariants ~autotest
        ~typed:Expr.typed ~closure_conversion ~keep_special_ops
        ~monomorphize_types ~expand_ops ~renaming
    in
    let renaming_context =
      match renaming_context with
      | None -> Renaming.(get_ctx default_config)
      | Some r -> r
    in
    debug_pass_name "scalc";
    ( Scalc.From_lcalc.translate_program
        ~config:
          {
            keep_special_ops;
            dead_value_assignment;
            no_struct_literals;
            keep_module_names;
            renaming_context;
          }
        prg,
      type_ordering,
      renaming_context )
end

module Commands = struct
  open Cmdliner

  let fix_trace options =
    if options.Global.trace <> None then (
      Message.warning "%a" Format.pp_print_text
        "Trace printing is not compatible with closure conversion or the C or \
         Java backends at the moment and has been disabled.";
      Global.enforce_options ~trace:None ())
    else options

  let get_scope_uid (ctx : decl_ctx) (scope : string) : ScopeName.t =
    if String.contains scope '.' then
      Message.error
        "Bad scope argument @{<yellow>%s@}: only references to the top-level \
         module are allowed"
        scope;
    try Ident.Map.find scope ctx.ctx_scope_index
    with Ident.Map.Not_found _ ->
      Message.error "There is no scope \"@{<yellow>%s@}\" inside the program."
        scope
        ~suggestion:(Ident.Map.keys ctx.ctx_scope_index)

  let get_scopelist_uids prg (scopes : string list) : ScopeName.t list =
    match scopes with
    | _ :: _ -> List.map (get_scope_uid prg.decl_ctx) scopes
    | [] ->
      let exports = BoundList.last prg.code_items in
      let test_scopes =
        List.filter_map
          (function KTest scope, _ -> Some scope | _ -> None)
          exports
      in
      if test_scopes = [] then
        Message.warning
          "The program defines no test scopes.@ Please specify option \
           @{<yellow>--scope@} to explicit what to execute@ or@ mark@ scopes@ \
           in@ the@ program@ with@ the@ @{<cyan>#[test]@}@ attribute.@ The \
           program defines the following scopes:@ @[<hv 4>%a@]"
          (Format.pp_print_list ~pp_sep:Format.pp_print_space ScopeName.format)
          (List.filter_map
             (function KScope n, _ -> Some n | _ -> None)
             exports)
      else
        Message.debug "Will execute the following test scopes:@ %a"
          (Format.pp_print_list ~pp_sep:Format.pp_print_space ScopeName.format)
          test_scopes;
      test_scopes

  let get_variable_uid
      (ctxt : Desugared.Name_resolution.context)
      (scope_uid : ScopeName.t)
      (variable : string) : Desugared.Ast.ScopeDef.t =
    (* Sometimes the variable selected is of the form [a.b] *)
    let first_part, second_part =
      match String.index_opt variable '.' with
      | Some i ->
        ( String.sub variable 0 i,
          Some (String.sub variable i (String.length variable - i)) )
      | None -> variable, None
    in
    match
      Ident.Map.find_opt first_part
        (ScopeName.Map.find scope_uid ctxt.scopes).var_idmap
    with
    | None ->
      Message.error
        "Variable @{<yellow>\"%s\"@} not found inside scope @{<yellow>\"%a\"@}"
        variable ScopeName.format scope_uid
    | Some (ScopeVar v | SubScope (v, _)) ->
      let state =
        second_part
        |> Option.map
           @@ fun id ->
           let var_sig = ScopeVar.Map.find v ctxt.var_typs in
           match Ident.Map.find_opt id var_sig.var_sig_states_idmap with
           | Some state -> state
           | None ->
             Message.error
               "State @{<yellow>\"%s\"@} is not found for variable \
                @{<yellow>\"%s\"@} of scope @{<yellow>\"%a\"@}"
               id first_part ScopeName.format scope_uid
      in
      (v, Pos.void), Desugared.Ast.ScopeDef.Var state

  let get_output ?ext options output_file =
    let output_file = Option.map options.Global.path_rewrite output_file in
    File.get_main_out_channel ~source_file:options.Global.input_src ~output_file
      ?ext ()

  let get_output_format options output_file =
    let output_file = Option.map options.Global.path_rewrite output_file in
    fun ?ext f ->
      let output_file, with_output =
        File.get_main_out_formatter ~source_file:options.Global.input_src
          ~output_file ?ext ()
      in
      Message.debug "Writing to %s" (Option.value ~default:"stdout" output_file);
      with_output (fun ppf -> f output_file ppf)

  let makefile options output =
    let prg = Passes.surface options in
    let backend_extensions_list = [".tex"] in
    let source_file = Global.input_src_file options.Global.input_src in
    let output_file, with_output = get_output options ~ext:"d" output in
    Message.debug "Writing list of dependencies to %s..."
      (Option.value ~default:"stdout" output_file);
    with_output
    @@ fun oc ->
    Printf.fprintf oc "%s:\\\n%s\n%s:"
      (String.concat "\\\n"
         (Option.value ~default:"stdout" output_file
         :: List.map
              (fun ext -> Filename.remove_extension source_file ^ ext)
              backend_extensions_list))
      (String.concat "\\\n" prg.Surface.Ast.program_source_files)
      (String.concat "\\\n" prg.Surface.Ast.program_source_files)

  let makefile_cmd =
    Cmd.v
      (Cmd.info "makefile" ~man:Cli.man_base
         ~doc:
           "Generates a Makefile-compatible list of the file dependencies of a \
            Catala program.")
      Term.(const makefile $ Cli.Flags.Global.options $ Cli.Flags.output)

  let html options output print_only_law wrap_weaved_output =
    let prg = Passes.surface options in
    Message.debug "Weaving literate program into HTML";
    get_output_format options ~ext:"html" output
    @@ fun output_file fmt ->
    let language =
      Cli.file_lang (Global.input_src_file options.Global.input_src)
    in
    let weave_output = Literate.Html.ast_to_html language ~print_only_law in
    Message.debug "Writing to %s" (Option.value ~default:"stdout" output_file);
    if wrap_weaved_output then
      Literate.Html.wrap_html prg.Surface.Ast.program_source_files language fmt
        (fun fmt -> weave_output fmt prg)
    else weave_output fmt prg

  let html_cmd =
    Cmd.v
      (Cmd.info "html" ~man:Cli.man_base
         ~doc:
           "Weaves an HTML literate programming output of the Catala program.")
      Term.(
        const html
        $ Cli.Flags.Global.options
        $ Cli.Flags.output
        $ Cli.Flags.print_only_law
        $ Cli.Flags.wrap_weaved_output)

  let latex options output print_only_law wrap_weaved_output extra_files =
    let prg = Passes.surface options in
    let prg_annex =
      List.map
        (fun f -> Surface.Parser_driver.parse_top_level_file (FileName f))
        extra_files
    in
    Message.debug "Weaving literate program into LaTeX";
    get_output_format options ~ext:"tex" output
    @@ fun _output_file fmt ->
    let language =
      Cli.file_lang (Global.input_src_file options.Global.input_src)
    in
    let weave_output = Literate.Latex.ast_to_latex language ~print_only_law in
    let weave fmt =
      weave_output fmt prg;
      List.iter
        (fun p ->
          Format.fprintf fmt "@,\\newpage@,";
          weave_output fmt p)
        prg_annex
    in
    if wrap_weaved_output then
      Literate.Latex.wrap_latex
        (List.flatten
           (List.map
              (fun p -> p.Surface.Ast.program_source_files)
              (prg :: prg_annex)))
        language fmt weave
    else weave fmt

  let latex_cmd =
    Cmd.v
      (Cmd.info "latex" ~man:Cli.man_base
         ~doc:
           "Weaves a LaTeX literate programming output of the Catala program.")
      Term.(
        const latex
        $ Cli.Flags.Global.options
        $ Cli.Flags.output
        $ Cli.Flags.print_only_law
        $ Cli.Flags.wrap_weaved_output
        $ Cli.Flags.extra_files)

  let exceptions options includes stdlib ex_scope ex_variable =
    let prg, ctxt = Passes.desugared options ~includes ~stdlib in
    Passes.debug_pass_name "scopelang";
    let exceptions_graphs =
      Scopelang.From_desugared.build_exceptions_graph prg
    in
    let scope_uid = get_scope_uid prg.program_ctx ex_scope in
    let variable_uid = get_variable_uid ctxt scope_uid ex_variable in
    Desugared.Print.exceptions_graph scope_uid variable_uid
      (Desugared.Ast.ScopeDef.Map.find variable_uid exceptions_graphs)

  let exceptions_cmd =
    Cmd.v
      (Cmd.info "exceptions" ~man:Cli.man_base
         ~doc:
           "Prints the exception tree for the definitions of a particular \
            variable, for debugging purposes. Use the $(b,-s) option to select \
            the scope and the $(b,-v) option to select the variable. Use \
            foo.bar to access state bar of variable foo or variable bar of \
            subscope foo.")
      Term.(
        const exceptions
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.ex_scope
        $ Cli.Flags.ex_variable)

  let dependency_graph options includes stdlib =
    let prg_desugared, _ctxt = Passes.desugared options ~includes ~stdlib in
    let exceptions_graphs =
      Scopelang.From_desugared.build_exceptions_graph prg_desugared
    in
    let intra_scope_dependency_graphs =
      ScopeName.Map.map
        (fun s ->
          Desugared.Dependency.build_scope_dependencies s
          |> Desugared.Dependency.scope_dependencies_to_json)
        prg_desugared.program_root.module_scopes
    in
    let prg_scopelang =
      Scopelang.From_desugared.translate_program prg_desugared exceptions_graphs
    in
    let types_dependency_graph =
      Scopelang.Dependency.build_type_graph
        prg_scopelang.program_ctx.ctx_structs
        prg_scopelang.program_ctx.ctx_enums
      |> Scopelang.Dependency.type_dependencies_graph_to_json
    in
    let inter_scope_dependencies =
      Scopelang.Dependency.build_program_dep_graph prg_scopelang
      |> Scopelang.Dependency.inter_scope_dependencies_graph_to_json
    in
    Passes.debug_pass_name "scopelang";
    let json_output =
      `Assoc
        [
          ( "intra_scopes",
            `Assoc
              (List.map
                 (fun (s_name, g) -> ScopeName.to_string s_name, g)
                 (ScopeName.Map.bindings intra_scope_dependency_graphs)) );
          "inter_scopes", inter_scope_dependencies;
          "types", types_dependency_graph;
        ]
    in
    Yojson.Safe.to_channel stdout json_output;
    print_newline ()

  let dependency_graph_cmd =
    Cmd.v
      (Cmd.info "dependency-graph" ~man:Cli.man_base
         ~doc:
           "Prints the inter-scope dependency graph (which scope calls which \
            scope), as well as the intra-scope dependency graphs (which scope \
            variable uses which scope variable), and the type dependency graph \
            (which type uses which type), in JSON format.")
      Term.(
        const dependency_graph
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir)

  let scopelang options includes stdlib output ex_scopes =
    let prg = Passes.scopelang options ~includes ~stdlib in
    get_output_format options output
    @@ fun _ fmt ->
    match ex_scopes with
    | _ :: _ ->
      List.iter
        (fun scope ->
          let scope_uid = get_scope_uid prg.program_ctx scope in
          Scopelang.Print.scope ~debug:options.Global.debug fmt
            (scope_uid, ScopeName.Map.find scope_uid prg.program_scopes);
          Format.pp_print_newline fmt ())
        ex_scopes
    | [] ->
      Scopelang.Print.program ~debug:options.Global.debug fmt prg;
      Format.pp_print_newline fmt ()

  let scopelang_cmd =
    Cmd.v
      (Cmd.info "scopelang" ~man:Cli.man_base ~docs:Cli.s_debug
         ~doc:
           "Prints a debugging verbatim of the scope language intermediate \
            representation of the Catala program. Use the $(b,-s) option to \
            restrict the output to a particular scope.")
      Term.(
        const scopelang
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.output
        $ Cli.Flags.ex_scopes)

  let typecheck options check_invariants includes stdlib quiet =
    let prg = Passes.scopelang options ~includes ~stdlib in
    Message.debug "Typechecking...";
    let _type_ordering =
      Scopelang.Dependency.check_type_cycles prg.program_ctx.ctx_structs
        prg.program_ctx.ctx_enums
    in
    let prg = Scopelang.Ast.type_program prg in
    Message.debug "Translating to default calculus...";
    (* Strictly type-checking could stop here, but we also want this pass to
       check full name-resolution and cycle detection. These are checked during
       translation to dcalc so we run it here and drop the result. *)
    let prg = Dcalc.From_scopelang.translate_program prg in

    (* Additionally, we might want to check the invariants. *)
    if check_invariants then (
      let prg = Shared_ast.Typing.program prg in
      Message.debug "Checking invariants...";
      if Dcalc.Invariants.check_all_invariants prg then
        if quiet then () else Message.result "All invariant checks passed"
      else
        raise (Message.error ~internal:true "Some Dcalc invariants are invalid"));
    if not quiet then Message.result "Typechecking successful!"

  let typecheck_cmd =
    Cmd.v
      (Cmd.info "typecheck" ~man:Cli.man_base
         ~doc:"Parses and typechecks a Catala program, without interpreting it.")
      Term.(
        const typecheck
        $ Cli.Flags.Global.options
        $ Cli.Flags.check_invariants
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.quiet)

  let dcalc
      typed
      options
      includes
      stdlib
      output
      optimize
      ex_scopes
      check_invariants
      autotest =
    let prg, _ =
      Passes.dcalc options ~includes ~stdlib ~optimize ~check_invariants
        ~autotest ~typed
    in
    get_output_format options output
    @@ fun _ fmt ->
    match ex_scopes with
    | [] ->
      Print.program ~debug:options.Global.debug fmt prg;
      Format.pp_print_newline fmt ()
    | scopes ->
      List.iter
        (fun scope ->
          let scope_uid = get_scope_uid prg.decl_ctx scope in
          Print.scope ~debug:options.Global.debug fmt
            ( scope,
              BoundList.find
                ~f:(function
                  | ScopeDef (name, body) when ScopeName.equal name scope_uid ->
                    Some body
                  | _ -> None)
                prg.code_items );
          Format.pp_print_newline fmt ())
        scopes

  let dcalc_cmd =
    let f no_typing =
      if no_typing then dcalc Expr.untyped else dcalc Expr.typed
    in
    Cmd.v
      (Cmd.info "dcalc" ~man:Cli.man_base ~docs:Cli.s_debug
         ~doc:
           "Prints a debugging verbatim of the default calculus intermediate \
            representation of the Catala program. Use the $(b,-s) option to \
            restrict the output to a particular scope.")
      Term.(
        const f
        $ Cli.Flags.no_typing
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.output
        $ Cli.Flags.optimize
        $ Cli.Flags.ex_scopes
        $ Cli.Flags.check_invariants
        $ Cli.Flags.autotest)

  let proof
      options
      includes
      stdlib
      optimize
      ex_scope_opt
      check_invariants
      disable_counterexamples =
    let prg, _ =
      Passes.dcalc options ~includes ~stdlib ~optimize ~check_invariants
        ~autotest:false ~typed:Expr.typed
    in
    Verification.Globals.setup ~optimize ~disable_counterexamples;
    let vcs =
      Verification.Conditions.generate_verification_conditions prg
        (Option.map (get_scope_uid prg.decl_ctx) ex_scope_opt)
    in
    Verification.Solver.solve_vc prg.decl_ctx vcs

  let proof_cmd =
    Cmd.v
      (Cmd.info "proof" ~man:Cli.man_base
         ~doc:
           "Generates and proves verification conditions about the \
            well-behaved execution of the Catala program.")
      Term.(
        const proof
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.optimize
        $ Cli.Flags.ex_scope_opt
        $ Cli.Flags.check_invariants
        $ Cli.Flags.disable_counterexamples)

  let print_interpretation_results
      options
      ?(quiet = false)
      interpreter
      prg
      scope_uid =
    try
      Message.debug "Starting interpretation...";
      let results = interpreter prg scope_uid in
      Message.debug "End of interpretation";
      let results =
        List.sort
          (fun ((v1, _), _) ((v2, _), _) -> String.compare v1 v2)
          results
      in
      let language =
        Cli.file_lang (Global.input_src_file options.Global.input_src)
      in
      if quiet then
        (* Caution: this output is parsed by Clerk *)
        Format.fprintf (Message.std_ppf ()) "%a: @{<green>passed@}@."
          ScopeName.format scope_uid
      else if results = [] then Message.result "Computation successful!"
      else
        Message.results
          ~title:(ScopeName.to_string scope_uid)
          (List.map
             (fun ((var, _), result) ppf ->
               Format.fprintf ppf "@[<hov 2>%s@ =@ %a@]" var
                 (if options.Global.debug then Print.expr ~debug:false ()
                  else Print.UserFacing.value language)
                 result)
             results);
      true
    with
    | Message.CompilerError content ->
      Message.Content.emit content Error;
      if quiet then
        Format.fprintf (Message.std_ppf ()) "%a: @{<red>failed@}@."
          ScopeName.format scope_uid;
      false
    | Message.CompilerErrors contents ->
      Message.Content.emit_n contents Error;
      if quiet then
        Format.fprintf (Message.std_ppf ()) "%a: @{<red>failed@}@."
          ScopeName.format scope_uid;
      false

  let interpret_dcalc
      typed
      options
      includes
      stdlib
      optimize
      check_invariants
      quiet
      ex_scopes =
    let prg, _ =
      Passes.dcalc options ~includes ~stdlib ~optimize ~check_invariants
        ~autotest:false ~typed
    in
    Interpreter.load_runtime_modules
      ~hashf:Hash.(finalise ~monomorphize_types:false)
      prg;
    let success =
      List.fold_left
        (fun success scope ->
          print_interpretation_results ~quiet options
            Interpreter.interpret_program_dcalc prg scope
          && success)
        true
        (get_scopelist_uids prg ex_scopes)
    in
    if not success then raise (Cli.Exit_with 123)

  let lcalc
      typed
      options
      includes
      stdlib
      output
      optimize
      check_invariants
      autotest
      closure_conversion
      keep_special_ops
      monomorphize_types
      expand_ops
      ex_scopes =
    let options = if closure_conversion then fix_trace options else options in
    let prg, _, _ =
      Passes.lcalc options ~includes ~stdlib ~optimize ~check_invariants
        ~autotest ~closure_conversion ~keep_special_ops ~typed
        ~monomorphize_types ~expand_ops ~renaming:(Some Renaming.default)
    in
    get_output_format options output
    @@ fun _ fmt ->
    match ex_scopes with
    | _ :: _ as scopes ->
      List.iter
        (fun scope ->
          let scope_uid = get_scope_uid prg.decl_ctx scope in
          Print.scope ~debug:options.Global.debug fmt
            (scope, Program.get_scope_body prg scope_uid);
          Format.pp_print_newline fmt ())
        scopes
    | [] ->
      Print.program ~debug:options.Global.debug fmt prg;
      Format.pp_print_newline fmt ()

  let lcalc_cmd =
    let f no_typing =
      if no_typing then lcalc Expr.untyped else lcalc Expr.typed
    in
    Cmd.v
      (Cmd.info "lcalc" ~man:Cli.man_base ~docs:Cli.s_debug
         ~doc:
           "Prints a debugging verbatim of the lambda calculus intermediate \
            representation of the Catala program. Use the $(b,-s) option to \
            restrict the output to a particular scope.")
      Term.(
        const f
        $ Cli.Flags.no_typing
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.output
        $ Cli.Flags.optimize
        $ Cli.Flags.check_invariants
        $ Cli.Flags.autotest
        $ Cli.Flags.closure_conversion
        $ Cli.Flags.keep_special_ops
        $ Cli.Flags.monomorphize_types
        $ Cli.Flags.expand_ops
        $ Cli.Flags.ex_scopes)

  let interpret_lcalc
      typed
      closure_conversion
      keep_special_ops
      monomorphize_types
      expand_ops
      options
      includes
      stdlib
      optimize
      check_invariants
      quiet
      ex_scopes =
    let options = if closure_conversion then fix_trace options else options in
    let prg, _, _ =
      Passes.lcalc options ~includes ~stdlib ~optimize ~check_invariants
        ~autotest:false ~closure_conversion ~keep_special_ops
        ~monomorphize_types ~typed ~expand_ops ~renaming:None
    in
    Interpreter.load_runtime_modules
      ~hashf:(Hash.finalise ~monomorphize_types)
      prg;
    let success =
      List.fold_left
        (fun success scope ->
          print_interpretation_results ~quiet options
            Interpreter.interpret_program_lcalc prg scope
          && success)
        true
        (get_scopelist_uids prg ex_scopes)
    in
    if not success then raise (Cli.Exit_with 123)

  let interpret_cmd =
    let f
        lcalc
        closure_conversion
        keep_special_ops
        monomorphize_types
        expand_ops
        no_typing =
      if not lcalc then
        if closure_conversion || monomorphize_types then
          Message.error
            "The flags @{<bold>--closure-conversion@} and \
             @{<bold>--monomorphize-types@} only make sense with the \
             @{<bold>--lcalc@} option"
        else if no_typing then interpret_dcalc Expr.untyped
        else interpret_dcalc Expr.typed
      else if no_typing then
        interpret_lcalc Expr.untyped closure_conversion keep_special_ops
          monomorphize_types expand_ops
      else
        interpret_lcalc Expr.typed closure_conversion keep_special_ops
          monomorphize_types expand_ops
    in
    Cmd.v
      (Cmd.info "interpret" ~man:Cli.man_base
         ~doc:
           "Runs the interpreter on the Catala program, executing the scopes \
            specified with the $(b,-s) option, or the scopes marked as \
            $(i,#[test]) if absent.")
      Term.(
        const f
        $ Cli.Flags.lcalc
        $ Cli.Flags.closure_conversion
        $ Cli.Flags.monomorphize_types
        $ Cli.Flags.keep_special_ops
        $ Cli.Flags.expand_ops
        $ Cli.Flags.no_typing
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.optimize
        $ Cli.Flags.check_invariants
        $ Cli.Flags.quiet
        $ Cli.Flags.ex_scopes)

  let ocaml
      options
      includes
      stdlib
      output
      optimize
      check_invariants
      autotest
      closure_conversion =
    let options = if closure_conversion then fix_trace options else options in
    let prg, type_ordering, _ =
      Passes.lcalc options ~includes ~stdlib ~optimize ~check_invariants
        ~autotest ~typed:Expr.typed ~closure_conversion ~keep_special_ops:true
        ~monomorphize_types:false ~expand_ops:true
        ~renaming:(Some Lcalc.To_ocaml.renaming)
    in
    Message.debug "Compiling program into OCaml...";
    get_output_format options output
      ~ext:(if Global.options.gen_external then "template.ml" else "ml")
    @@ fun output_file fmt ->
    let hashf = Hash.finalise ~monomorphize_types:false in
    Lcalc.To_ocaml.format_program output_file fmt prg ~hashf type_ordering

  let ocaml_cmd =
    Cmd.v
      (Cmd.info "ocaml" ~man:Cli.man_base
         ~doc:"Generates an OCaml translation of the Catala program.")
      Term.(
        const ocaml
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.output
        $ Cli.Flags.optimize
        $ Cli.Flags.check_invariants
        $ Cli.Flags.autotest
        $ Cli.Flags.closure_conversion)

  let scalc
      options
      includes
      stdlib
      output
      optimize
      check_invariants
      autotest
      closure_conversion
      keep_special_ops
      dead_value_assignment
      no_struct_literals
      monomorphize_types
      expand_ops
      ex_scope_opt =
    let options = if closure_conversion then fix_trace options else options in
    let prg, _, _ =
      Passes.scalc options ~includes ~stdlib ~optimize ~check_invariants
        ~autotest ~closure_conversion ~keep_special_ops ~dead_value_assignment
        ~no_struct_literals ~keep_module_names:false ~monomorphize_types
        ~expand_ops ~renaming:(Some Renaming.default)
    in
    get_output_format options output
    @@ fun _ fmt ->
    match ex_scope_opt with
    | Some scope ->
      let scope_uid = get_scope_uid prg.ctx.decl_ctx scope in
      Scalc.Print.format_item ~debug:options.Global.debug prg.ctx.decl_ctx fmt
        (List.find
           (function
             | Scalc.Ast.SScope { scope_body_name; _ } ->
               scope_body_name = scope_uid
             | _ -> false)
           prg.code_items);
      Format.pp_print_newline fmt ()
    | None -> Scalc.Print.format_program fmt prg

  let scalc_cmd =
    Cmd.v
      (Cmd.info "scalc" ~man:Cli.man_base ~docs:Cli.s_debug
         ~doc:
           "Prints a debugging verbatim of the statement calculus intermediate \
            representation of the Catala program. Use the $(b,-s) option to \
            restrict the output to a particular scope.")
      Term.(
        const scalc
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.output
        $ Cli.Flags.optimize
        $ Cli.Flags.check_invariants
        $ Cli.Flags.autotest
        $ Cli.Flags.closure_conversion
        $ Cli.Flags.keep_special_ops
        $ Cli.Flags.dead_value_assignment
        $ Cli.Flags.no_struct_literals
        $ Cli.Flags.monomorphize_types
        $ Cli.Flags.expand_ops
        $ Cli.Flags.ex_scope_opt)

  let python
      options
      includes
      stdlib
      output
      optimize
      check_invariants
      autotest
      closure_conversion =
    let options = if closure_conversion then fix_trace options else options in
    let prg, type_ordering, _ren_ctx =
      Passes.scalc options ~includes ~stdlib ~optimize ~check_invariants
        ~autotest ~closure_conversion ~keep_special_ops:false
        ~dead_value_assignment:true ~no_struct_literals:false
        ~keep_module_names:false ~monomorphize_types:false ~expand_ops:false
        ~renaming:(Some Scalc.To_python.renaming)
    in
    Message.debug "Compiling program into Python...";
    get_output_format options output
      ~ext:(if Global.options.gen_external then "template.py" else "py")
    @@ fun output_file fmt ->
    Scalc.To_python.format_program output_file fmt prg type_ordering

  let python_cmd =
    Cmd.v
      (Cmd.info "python" ~man:Cli.man_base
         ~doc:"Generates a Python translation of the Catala program.")
      Term.(
        const python
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.output
        $ Cli.Flags.optimize
        $ Cli.Flags.check_invariants
        $ Cli.Flags.autotest
        $ Cli.Flags.closure_conversion)

  let java
      options
      includes
      stdlib
      (output : Global.raw_file option)
      optimize
      check_invariants
      autotest
      closure_conversion =
    let options = fix_trace options in
    let prg, _type_ordering, _ren_ctx =
      Passes.scalc options ~includes ~stdlib ~optimize ~check_invariants
        ~autotest ~closure_conversion ~keep_special_ops:false
        ~dead_value_assignment:true ~no_struct_literals:false
        ~keep_module_names:true ~monomorphize_types:false ~expand_ops:false
        ~renaming:(Some Scalc.To_java.renaming)
    in
    Message.debug "Compiling program into Java...";
    get_output_format options output
      ~ext:(if Global.options.gen_external then "template.java" else "java")
    @@ fun output_file ppf ->
    let class_name =
      match output_file, options.Global.input_src with
      | Some file, _
      | None, (FileName (file : File.t) | Contents (_, (file : File.t))) ->
        let name = Filename.(remove_extension file |> basename) in
        if Global.options.gen_external then
          String.capitalize_ascii (Filename.remove_extension name)
        else name
      | None, Stdin _ -> "AnonymousClass"
    in
    Scalc.To_java.format_program ~class_name output_file ppf prg

  let java_cmd =
    Cmd.v
      (Cmd.info "java" ~man:Cli.man_base
         ~doc:"Generates a Java translation of the Catala program.")
      Term.(
        const java
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.output
        $ Cli.Flags.optimize
        $ Cli.Flags.check_invariants
        $ Cli.Flags.autotest
        $ Cli.Flags.closure_conversion)

  let c options includes stdlib output optimize check_invariants autotest =
    let options = fix_trace options in
    let prg, type_ordering, _ren_ctx =
      Passes.scalc options ~includes ~stdlib ~optimize ~check_invariants
        ~autotest ~closure_conversion:true ~keep_special_ops:false
        ~dead_value_assignment:false ~no_struct_literals:true
        ~keep_module_names:false ~monomorphize_types:false ~expand_ops:true
        ~renaming:(Some Scalc.To_c.renaming)
    in
    Message.debug "Compiling program into C...";
    get_output_format options output
      ~ext:(if Global.options.gen_external then "template.c" else "c")
    @@ fun output_file ppf ->
    Scalc.To_c.format_program output_file ppf prg type_ordering

  let c_cmd =
    Cmd.v
      (Cmd.info "c" ~man:Cli.man_base
         ~doc:"Generates an C translation of the Catala program.")
      Term.(
        const c
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.output
        $ Cli.Flags.optimize
        $ Cli.Flags.check_invariants
        $ Cli.Flags.autotest)

  let depends options includes stdlib prefix subdir extension extra_files =
    let file = Global.input_src_file options.Global.input_src in
    let more_includes = List.map Filename.dirname (file :: extra_files) in
    let prg =
      Surface.Ast.
        {
          program_module = None;
          program_items = [];
          program_source_files = [file];
          program_used_modules =
            List.map
              (fun f ->
                let name = modname_of_file f in
                {
                  mod_use_name = name, Pos.void;
                  mod_use_alias = name, Pos.void;
                })
              (file :: extra_files);
          program_lang = Cli.file_lang file;
        }
    in
    let mod_uses, modules =
      load_modules options includes ~stdlib ~more_includes
        ~allow_notmodules:true prg
    in
    let d_ctx =
      Desugared.Name_resolution.form_context (prg, mod_uses) modules
    in
    let modules = ModuleName.Map.map fst modules in
    let prg = Desugared.From_surface.translate_program d_ctx modules prg in
    let modules_list_topo =
      Program.modules_to_list prg.program_ctx.ctx_modules
    in
    Format.open_hbox ();
    Format.pp_print_list ~pp_sep:Format.pp_print_space
      (fun ppf (m, _) ->
        let f = Pos.get_file (Mark.get (ModuleName.get_info m)) in
        let f =
          match prefix with
          | None -> f
          | Some pfx ->
            if not (Filename.is_relative f) then (
              Message.warning
                "Not adding prefix to %s, which is an absolute path" f;
              f)
            else File.(pfx / f)
        in
        let f =
          match subdir with
          | None -> f
          | Some d -> File.(dirname f / d / basename f)
        in
        let f = File.clean_path f in
        if extension = [] then Format.pp_print_string ppf f
        else
          Format.pp_print_list ~pp_sep:Format.pp_print_space
            (fun ppf ext ->
              let base = File.(dirname f / ModuleName.to_string m) in
              Format.pp_print_string ppf base;
              if ext <> "" then (
                Format.(
                  pp_print_char ppf '.';
                  pp_print_string ppf ext)))
            ppf extension)
      Format.std_formatter modules_list_topo;
    Format.close_box ();
    Format.print_newline ()

  let depends_cmd =
    Cmd.v
      (Cmd.info "depends" ~man:Cli.man_base
         ~doc:
           "Lists the dependencies of the given catala files, in linking \
            order. This includes recursive dependencies and is useful for \
            linking an application in a target language. The space-separated \
            list is printed to stdout. The names are printed as expected of \
            module identifiers, $(i,i.e.) capitalized.\n\
            NOTE: the files specified are also included in the returned list.")
      Term.(
        const depends
        $ Cli.Flags.Global.options
        $ Cli.Flags.include_dirs
        $ Cli.Flags.stdlib_dir
        $ Cli.Flags.prefix
        $ Cli.Flags.subdir
        $ Cli.Flags.extension
        $ Cli.Flags.extra_files)

  let pygmentize_cmd =
    Cmd.v
      (Cmd.info "pygmentize" ~man:Cli.man_base
         ~doc:
           "This special command is a wrapper around the $(b,pygmentize) \
            command that enables support for colorising Catala code.")
      Term.(
        const (fun _ ->
            assert false
            (* Not really a catala command, this is handled preemptively at
               startup *))
        $ Cli.Flags.Global.options)

  let commands =
    [
      interpret_cmd;
      typecheck_cmd;
      proof_cmd;
      ocaml_cmd;
      python_cmd;
      java_cmd;
      c_cmd;
      latex_cmd;
      html_cmd;
      makefile_cmd;
      scopelang_cmd;
      dcalc_cmd;
      lcalc_cmd;
      scalc_cmd;
      exceptions_cmd;
      dependency_graph_cmd;
      depends_cmd;
      pygmentize_cmd;
    ]
end

let raise_help cmdname cmds =
  let plugins = Plugin.names () in
  let cmds = List.filter (fun name -> not (List.mem name plugins)) cmds in
  Message.error
    "One of the following commands was expected:@;\
     <1 4>@[<v>@{<bold;blue>%a@}@]%a@\n\
     Run `@{<bold>%s --help@}' or `@{<bold>%s COMMAND --help@}' for details."
    (Format.pp_print_list Format.pp_print_string)
    (List.sort String.compare cmds)
    (fun ppf -> function
      | [] -> ()
      | plugins ->
        Format.fprintf ppf
          "@\n\
           Or one of the following installed plugins:@;\
           <1 4>@[<v>@{<blue>%a@}@]"
          (Format.pp_print_list Format.pp_print_string)
          plugins)
    plugins cmdname cmdname

let catala_t extra_commands =
  let open Cmdliner in
  let default =
    Term.(const raise_help $ main_name $ choice_names $ Cli.Flags.Global.flags)
  in
  Cmd.group ~default Cli.info (Commands.commands @ extra_commands)

let main () =
  let argv = Array.copy Sys.argv in
  (* Our command names (first argument) are case-insensitive *)
  if Array.length argv >= 2 then argv.(1) <- String.lowercase_ascii argv.(1);
  (* Pygmentize is a specific exec subcommand that doesn't go through
     cmdliner *)
  if Array.length Sys.argv >= 2 && argv.(1) = "pygmentize" then
    Literate.Pygmentize.exec ();
  (* Peek to load plugins before the command-line is parsed proper (plugins add
     their own commands) *)
  let plugins =
    let plugins_dirs =
      match
        Cmdliner.Cmd.eval_peek_opts ~argv Cli.Flags.Global.flags
          ~version_opt:true
      with
      | Some opts, _ -> opts.Global.plugins_dirs
      | None, _ -> []
    in
    Passes.debug_pass_name "init";
    List.iter
      (fun d ->
        if d = "" then ()
        else
          match Sys.is_directory d with
          | true -> Plugin.load_dir d
          | false -> Message.debug "Could not read plugin directory %s" d
          | exception Sys_error _ ->
            Message.debug "Could not read plugin directory %s" d)
      plugins_dirs;
    Dynlink.allow_only
      (List.filter (( <> ) "Driver__Plugin") (Dynlink.all_units ()));
    (* From here on, no plugin registration is allowed. However, the interpreter
       may yet use Dynlink to load external modules. - TODO: This used to allow
       only "Runtime_ocaml__Runtime", but forbidding external Catala modules to
       use the OCaml Stdlib was a bit much. We should examine how to re-add some
       more filtering here without being too restrictive. *)
    Plugin.list ()
  in
  let command = catala_t plugins in
  let open Cmdliner in
  let[@inline] exit_with_error excode fcontent =
    let bt = Printexc.get_raw_backtrace () in
    Message.Content.emit (fcontent ()) Error;
    if Global.options.debug then Printexc.print_raw_backtrace stderr bt;
    exit excode
  in
  match Cmd.eval_value ~catch:false ~argv command with
  | Ok _ -> exit Cmd.Exit.ok
  | Error e ->
    if e = `Term then Plugin.print_failures ();
    exit Cmd.Exit.cli_error
  | exception Cli.Exit_with n -> exit n
  | exception Message.CompilerError content ->
    exit_with_error Cmd.Exit.some_error @@ fun () -> content
  | exception Message.CompilerErrors contents ->
    let bt = Printexc.get_raw_backtrace () in
    Message.Content.emit_n contents Error;
    if Global.options.debug then Printexc.print_raw_backtrace stderr bt;
    exit Cmd.Exit.some_error
  | exception Failure msg ->
    exit_with_error Cmd.Exit.some_error
    @@ fun () -> Message.Content.of_string msg
  | exception Sys_error msg ->
    exit_with_error Cmd.Exit.internal_error
    @@ fun () -> Message.Content.of_string ("System error: " ^ msg)
  | exception e ->
    exit_with_error Cmd.Exit.internal_error
    @@ fun () ->
    Message.Content.of_string ("Unexpected error: " ^ Printexc.to_string e)

(* Export module PluginAPI, hide parent module Plugin *)
module Plugin = struct
  let register name ?man ?doc term =
    let name = String.lowercase_ascii name in
    let info = Cmdliner.Cmd.info name ?man ?doc ~docs:Cli.s_plugins in
    Plugin.register info term

  let register_subcommands name ?man ?doc cmds =
    let name = String.lowercase_ascii name in
    let info = Cmdliner.Cmd.info name ?man ?doc ~docs:Cli.s_plugins in
    Plugin.register_subcommands info cmds

  let register_attribute = Plugin.register_attribute
end