2020-04-16 18:47:35 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
2021-05-27 19:56:47 +03:00
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria,
|
|
|
|
contributors: Denis Merigoux <denis.merigoux@inria.fr>, Emile Rolley
|
|
|
|
<emile.rolley@tuta.io>
|
2020-03-08 03:52:31 +03:00
|
|
|
|
|
|
|
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. *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2022-01-02 16:53:51 +03:00
|
|
|
(** Associates a file extension with its corresponding {!type: Cli.backend_lang}
|
2021-05-24 17:24:45 +03:00
|
|
|
string representation. *)
|
2021-05-26 18:44:57 +03:00
|
|
|
let extensions = [".catala_fr", "fr"; ".catala_en", "en"; ".catala_pl", "pl"]
|
2021-05-24 17:24:45 +03:00
|
|
|
|
2023-04-18 11:31:44 +03:00
|
|
|
let get_scope_uid
|
|
|
|
(options : Cli.options)
|
|
|
|
(backend : Plugin.t Cli.backend_option)
|
|
|
|
(ctxt : Desugared.Name_resolution.context) =
|
|
|
|
match options.ex_scope, backend with
|
|
|
|
| None, `Interpret ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error "No scope was provided for execution."
|
2023-04-18 11:31:44 +03:00
|
|
|
| None, _ ->
|
|
|
|
let _, scope =
|
|
|
|
try
|
2023-04-18 15:39:38 +03:00
|
|
|
Shared_ast.Ident.Map.filter_map
|
2023-04-18 11:31:44 +03:00
|
|
|
(fun _ -> function
|
|
|
|
| Desugared.Name_resolution.TScope (uid, _) -> Some uid
|
|
|
|
| _ -> None)
|
|
|
|
ctxt.typedefs
|
2023-04-18 15:39:38 +03:00
|
|
|
|> Shared_ast.Ident.Map.choose
|
2023-04-18 11:31:44 +03:00
|
|
|
with Not_found ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error "There isn't any scope inside the program."
|
2023-04-18 11:31:44 +03:00
|
|
|
in
|
|
|
|
scope
|
|
|
|
| Some name, _ -> (
|
2023-04-18 15:39:38 +03:00
|
|
|
match Shared_ast.Ident.Map.find_opt name ctxt.typedefs with
|
2023-04-18 11:31:44 +03:00
|
|
|
| Some (Desugared.Name_resolution.TScope (uid, _)) -> uid
|
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2023-06-07 19:15:14 +03:00
|
|
|
"There is no scope @{<yellow>\"%s\"@} inside the program." name)
|
2023-04-18 11:31:44 +03:00
|
|
|
|
|
|
|
let get_variable_uid
|
|
|
|
(options : Cli.options)
|
|
|
|
(backend : Plugin.t Cli.backend_option)
|
|
|
|
(ctxt : Desugared.Name_resolution.context)
|
|
|
|
(scope_uid : Shared_ast.ScopeName.t) =
|
|
|
|
match options.ex_variable, backend with
|
|
|
|
| None, `Exceptions ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2023-04-18 11:31:44 +03:00
|
|
|
"Please specify a variable with the -v option to print its exception \
|
|
|
|
tree."
|
|
|
|
| None, _ -> None
|
|
|
|
| Some name, _ -> (
|
|
|
|
(* Sometimes the variable selected is of the form [a.b]*)
|
|
|
|
let first_part, second_part =
|
|
|
|
match
|
|
|
|
Re.(
|
|
|
|
exec_opt
|
|
|
|
(compile
|
|
|
|
@@ whole_string
|
|
|
|
@@ seq [group (rep1 (compl [char '.'])); char '.'; group (rep1 any)]
|
|
|
|
)
|
|
|
|
name)
|
|
|
|
with
|
|
|
|
| None -> name, None
|
|
|
|
| Some groups -> Re.Group.get groups 1, Some (Re.Group.get groups 2)
|
|
|
|
in
|
|
|
|
match
|
2023-04-18 15:39:38 +03:00
|
|
|
Shared_ast.Ident.Map.find_opt first_part
|
2023-04-18 11:31:44 +03:00
|
|
|
(Shared_ast.ScopeName.Map.find scope_uid ctxt.scopes).var_idmap
|
|
|
|
with
|
|
|
|
| None ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2023-06-07 19:15:14 +03:00
|
|
|
"Variable @{<yellow>\"%s\"@} not found inside scope @{<yellow>\"%a\"@}"
|
2023-06-07 19:10:50 +03:00
|
|
|
name Shared_ast.ScopeName.format_t scope_uid
|
2023-04-18 11:31:44 +03:00
|
|
|
| Some
|
|
|
|
(Desugared.Name_resolution.SubScope (subscope_var_name, subscope_name))
|
|
|
|
-> (
|
|
|
|
match second_part with
|
|
|
|
| None ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2023-06-07 19:15:14 +03:00
|
|
|
"Subscope @{<yellow>\"%a\"@} of scope @{<yellow>\"%a\"@} cannot be \
|
|
|
|
selected by itself, please add \".<var>\" where <var> is a subscope \
|
|
|
|
variable."
|
2023-06-07 19:10:50 +03:00
|
|
|
Shared_ast.SubScopeName.format_t subscope_var_name
|
|
|
|
Shared_ast.ScopeName.format_t scope_uid
|
2023-04-18 11:31:44 +03:00
|
|
|
| Some second_part -> (
|
|
|
|
match
|
2023-04-18 15:39:38 +03:00
|
|
|
Shared_ast.Ident.Map.find_opt second_part
|
2023-04-18 11:31:44 +03:00
|
|
|
(Shared_ast.ScopeName.Map.find subscope_name ctxt.scopes).var_idmap
|
|
|
|
with
|
|
|
|
| Some (Desugared.Name_resolution.ScopeVar v) ->
|
|
|
|
Some
|
|
|
|
(Desugared.Ast.ScopeDef.SubScopeVar
|
|
|
|
(subscope_var_name, v, Pos.no_pos))
|
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2023-06-07 19:15:14 +03:00
|
|
|
"Var @{<yellow>\"%s\"@} of subscope @{<yellow>\"%a\"@} in scope \
|
|
|
|
@{<yellow>\"%a\"@} does not exist, please check your command line \
|
|
|
|
arguments."
|
|
|
|
second_part Shared_ast.SubScopeName.format_t subscope_var_name
|
2023-06-07 19:10:50 +03:00
|
|
|
Shared_ast.ScopeName.format_t scope_uid))
|
2023-04-18 11:31:44 +03:00
|
|
|
| Some (Desugared.Name_resolution.ScopeVar v) ->
|
|
|
|
Some
|
|
|
|
(Desugared.Ast.ScopeDef.Var
|
|
|
|
( v,
|
|
|
|
Option.map
|
|
|
|
(fun second_part ->
|
|
|
|
let var_sig = Shared_ast.ScopeVar.Map.find v ctxt.var_typs in
|
|
|
|
match
|
2023-04-18 15:39:38 +03:00
|
|
|
Shared_ast.Ident.Map.find_opt second_part
|
2023-04-18 11:31:44 +03:00
|
|
|
var_sig.var_sig_states_idmap
|
|
|
|
with
|
|
|
|
| Some state -> state
|
|
|
|
| None ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2023-06-07 19:15:14 +03:00
|
|
|
"State @{<yellow>\"%s\"@} is not found for variable \
|
|
|
|
@{<yellow>\"%s\"@} of scope @{<yellow>\"%a\"@}"
|
|
|
|
second_part first_part Shared_ast.ScopeName.format_t
|
|
|
|
scope_uid)
|
2023-04-18 11:31:44 +03:00
|
|
|
second_part )))
|
|
|
|
|
2021-04-22 12:57:50 +03:00
|
|
|
(** Entry function for the executable. Returns a negative number in case of
|
2022-03-16 13:44:34 +03:00
|
|
|
error. Usage: [driver source_file options]*)
|
2022-03-04 21:25:06 +03:00
|
|
|
let driver source_file (options : Cli.options) : int =
|
2020-08-07 16:29:52 +03:00
|
|
|
try
|
2022-03-04 20:32:03 +03:00
|
|
|
List.iter
|
|
|
|
(fun d ->
|
|
|
|
match Sys.is_directory d with
|
|
|
|
| true -> Plugin.load_dir d
|
|
|
|
| false -> ()
|
|
|
|
| exception Sys_error _ -> ())
|
|
|
|
options.plugins_dirs;
|
2022-03-04 21:25:06 +03:00
|
|
|
Cli.set_option_globals options;
|
2023-04-21 15:51:15 +03:00
|
|
|
if options.debug then Printexc.record_backtrace true;
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Reading files...";
|
2021-05-24 14:10:53 +03:00
|
|
|
let filename = ref "" in
|
2022-03-04 21:25:06 +03:00
|
|
|
(match source_file with
|
|
|
|
| Pos.FileName f -> filename := f
|
|
|
|
| Contents c -> Cli.contents := c);
|
2021-05-24 16:41:59 +03:00
|
|
|
let l =
|
2022-03-04 21:25:06 +03:00
|
|
|
match options.language with
|
2021-05-24 16:41:59 +03:00
|
|
|
| Some l -> l
|
2021-05-24 17:24:45 +03:00
|
|
|
| None -> (
|
2021-05-24 16:41:59 +03:00
|
|
|
(* Try to infer the language from the intput file extension. *)
|
2021-05-24 22:02:04 +03:00
|
|
|
let ext = Filename.extension !filename in
|
|
|
|
if ext = "" then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2022-03-08 15:04:27 +03:00
|
|
|
"No file extension found for the file '%s'. (Try to add one or to \
|
|
|
|
specify the -l flag)"
|
|
|
|
!filename;
|
2021-05-24 17:24:45 +03:00
|
|
|
try List.assoc ext extensions with Not_found -> ext)
|
2021-05-24 16:41:59 +03:00
|
|
|
in
|
|
|
|
let language =
|
2023-03-13 15:40:10 +03:00
|
|
|
try List.assoc l Cli.languages
|
2021-05-24 17:24:45 +03:00
|
|
|
with Not_found ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2022-03-08 15:04:27 +03:00
|
|
|
"The selected language (%s) is not supported by Catala" l
|
2020-08-07 16:29:52 +03:00
|
|
|
in
|
2021-05-26 22:18:18 +03:00
|
|
|
Cli.locale_lang := language;
|
2022-03-04 21:25:06 +03:00
|
|
|
let backend = options.backend in
|
2020-08-07 16:29:52 +03:00
|
|
|
let backend =
|
2022-03-04 20:32:03 +03:00
|
|
|
match Cli.backend_option_of_string backend with
|
|
|
|
| #Cli.backend_option_builtin as backend -> backend
|
|
|
|
| `Plugin s -> (
|
|
|
|
try `Plugin (Plugin.find s)
|
|
|
|
with Not_found ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2023-01-17 15:46:21 +03:00
|
|
|
"The selected backend (%s) is not supported by Catala, nor was a \
|
|
|
|
plugin by this name found under %a"
|
|
|
|
backend
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun ppf () -> Format.fprintf ppf "@ or @ ")
|
|
|
|
(fun ppf dir ->
|
|
|
|
Format.pp_print_string ppf
|
|
|
|
(try Unix.readlink dir with _ -> dir)))
|
|
|
|
options.plugins_dirs)
|
2022-03-08 18:12:25 +03:00
|
|
|
in
|
2021-05-29 15:15:23 +03:00
|
|
|
let prgm =
|
|
|
|
Surface.Parser_driver.parse_top_level_file source_file language
|
2020-08-07 16:29:52 +03:00
|
|
|
in
|
2021-05-29 15:15:23 +03:00
|
|
|
let prgm = Surface.Fill_positions.fill_pos_with_legislative_info prgm in
|
2022-07-29 14:39:33 +03:00
|
|
|
let get_output ?ext =
|
|
|
|
File.get_out_channel ~source_file ~output_file:options.output_file ?ext
|
2022-05-31 17:24:37 +03:00
|
|
|
in
|
2022-07-29 14:39:33 +03:00
|
|
|
let get_output_format ?ext =
|
|
|
|
File.get_formatter_of_out_channel ~source_file
|
|
|
|
~output_file:options.output_file ?ext
|
2022-05-31 17:24:37 +03:00
|
|
|
in
|
2022-03-04 20:32:03 +03:00
|
|
|
(match backend with
|
|
|
|
| `Makefile ->
|
2020-08-07 16:29:52 +03:00
|
|
|
let backend_extensions_list = [".tex"] in
|
2020-12-26 19:37:41 +03:00
|
|
|
let source_file =
|
|
|
|
match source_file with
|
|
|
|
| FileName f -> f
|
|
|
|
| Contents _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2020-12-26 19:37:41 +03:00
|
|
|
"The Makefile backend does not work if the input is not a file"
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-05-31 17:24:37 +03:00
|
|
|
let output_file, with_output = get_output ~ext:".d" () in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Writing list of dependencies to %s..."
|
2022-05-31 17:24:37 +03:00
|
|
|
(Option.value ~default:"stdout" output_file);
|
|
|
|
with_output
|
|
|
|
@@ fun oc ->
|
2020-08-07 16:29:52 +03:00
|
|
|
Printf.fprintf oc "%s:\\\n%s\n%s:"
|
|
|
|
(String.concat "\\\n"
|
2022-05-31 17:24:37 +03:00
|
|
|
(Option.value ~default:"stdout" output_file
|
2021-05-29 15:15:23 +03:00
|
|
|
:: List.map
|
|
|
|
(fun ext -> Filename.remove_extension source_file ^ ext)
|
|
|
|
backend_extensions_list))
|
|
|
|
(String.concat "\\\n" prgm.program_source_files)
|
2022-03-04 20:32:03 +03:00
|
|
|
(String.concat "\\\n" prgm.program_source_files)
|
|
|
|
| (`Latex | `Html) as backend ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Weaving literate program into %s"
|
2022-03-04 20:32:03 +03:00
|
|
|
(match backend with `Latex -> "LaTeX" | `Html -> "HTML");
|
2022-05-31 17:24:37 +03:00
|
|
|
let output_file, with_output =
|
|
|
|
get_output_format ()
|
|
|
|
~ext:(match backend with `Latex -> ".tex" | `Html -> ".html")
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-05-31 17:24:37 +03:00
|
|
|
with_output (fun fmt ->
|
2022-05-18 16:10:59 +03:00
|
|
|
let weave_output =
|
|
|
|
match backend with
|
2022-05-26 20:05:06 +03:00
|
|
|
| `Latex ->
|
|
|
|
Literate.Latex.ast_to_latex language
|
|
|
|
~print_only_law:options.print_only_law
|
|
|
|
| `Html ->
|
|
|
|
Literate.Html.ast_to_html language
|
|
|
|
~print_only_law:options.print_only_law
|
2022-05-18 16:10:59 +03:00
|
|
|
in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Writing to %s"
|
2022-05-31 17:24:37 +03:00
|
|
|
(Option.value ~default:"stdout" output_file);
|
2022-05-18 16:10:59 +03:00
|
|
|
if options.wrap_weaved_output then
|
|
|
|
match backend with
|
2022-03-04 20:32:03 +03:00
|
|
|
| `Latex ->
|
2022-05-18 16:10:59 +03:00
|
|
|
Literate.Latex.wrap_latex prgm.Surface.Ast.program_source_files
|
|
|
|
language fmt (fun fmt -> weave_output fmt prgm)
|
2022-03-04 20:32:03 +03:00
|
|
|
| `Html ->
|
2022-05-18 16:10:59 +03:00
|
|
|
Literate.Html.wrap_html prgm.Surface.Ast.program_source_files
|
|
|
|
language fmt (fun fmt -> weave_output fmt prgm)
|
2022-03-04 20:32:03 +03:00
|
|
|
else weave_output fmt prgm)
|
2023-04-05 16:38:46 +03:00
|
|
|
| ( `Interpret | `Interpret_Lcalc | `Typecheck | `OCaml | `Python | `Scalc
|
2023-04-07 13:39:26 +03:00
|
|
|
| `Lcalc | `Dcalc | `Scopelang | `Exceptions | `Proof | `Plugin _ ) as
|
|
|
|
backend -> (
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Name resolution...";
|
2022-11-07 15:50:28 +03:00
|
|
|
let ctxt = Desugared.Name_resolution.form_context prgm in
|
2023-04-18 11:31:44 +03:00
|
|
|
let scope_uid = get_scope_uid options backend ctxt in
|
2023-04-07 13:39:26 +03:00
|
|
|
(* This uid is a Desugared identifier *)
|
2023-04-18 11:31:44 +03:00
|
|
|
let variable_uid = get_variable_uid options backend ctxt scope_uid in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Desugaring...";
|
2022-11-07 15:50:28 +03:00
|
|
|
let prgm = Desugared.From_surface.translate_program ctxt prgm in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Disambiguating...";
|
2022-11-28 18:23:27 +03:00
|
|
|
let prgm = Desugared.Disambiguate.program prgm in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Linting...";
|
2023-03-30 16:48:39 +03:00
|
|
|
Desugared.Linting.lint_program prgm;
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Collecting rules...";
|
2023-04-18 12:06:58 +03:00
|
|
|
let exceptions_graphs =
|
|
|
|
Scopelang.From_desugared.build_exceptions_graph prgm
|
|
|
|
in
|
|
|
|
let prgm =
|
|
|
|
Scopelang.From_desugared.translate_program prgm exceptions_graphs
|
2023-04-07 13:39:26 +03:00
|
|
|
in
|
2021-01-28 02:28:28 +03:00
|
|
|
match backend with
|
2023-04-07 13:39:26 +03:00
|
|
|
| `Exceptions ->
|
|
|
|
let variable_uid =
|
|
|
|
match variable_uid with
|
|
|
|
| Some variable_uid -> variable_uid
|
|
|
|
| None ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2023-04-07 13:39:26 +03:00
|
|
|
"Please provide a scope variable to analyze with the -v option."
|
|
|
|
in
|
2023-04-07 17:35:09 +03:00
|
|
|
Desugared.Print.print_exceptions_graph scope_uid variable_uid
|
2023-04-18 11:31:44 +03:00
|
|
|
(Desugared.Ast.ScopeDef.Map.find variable_uid exceptions_graphs)
|
2022-03-04 20:32:03 +03:00
|
|
|
| `Scopelang ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let _output_file, with_output = get_output_format () in
|
|
|
|
with_output
|
|
|
|
@@ fun fmt ->
|
2022-03-04 20:32:03 +03:00
|
|
|
if Option.is_some options.ex_scope then
|
|
|
|
Format.fprintf fmt "%a\n"
|
2022-08-25 13:09:51 +03:00
|
|
|
(Scopelang.Print.scope prgm.program_ctx ~debug:options.debug)
|
2022-11-21 12:12:45 +03:00
|
|
|
( scope_uid,
|
|
|
|
Shared_ast.ScopeName.Map.find scope_uid prgm.program_scopes )
|
2022-03-04 20:32:03 +03:00
|
|
|
else
|
|
|
|
Format.fprintf fmt "%a\n"
|
2022-08-25 13:09:51 +03:00
|
|
|
(Scopelang.Print.program ~debug:options.debug)
|
2022-03-04 20:32:03 +03:00
|
|
|
prgm
|
2023-04-05 16:38:46 +03:00
|
|
|
| ( `Interpret | `Interpret_Lcalc | `Typecheck | `OCaml | `Python | `Scalc
|
2023-04-14 12:32:49 +03:00
|
|
|
| `Lcalc | `Dcalc | `Proof | `Plugin _ ) as backend -> (
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Typechecking...";
|
2022-09-27 19:45:22 +03:00
|
|
|
let type_ordering =
|
|
|
|
Scopelang.Dependency.check_type_cycles prgm.program_ctx.ctx_structs
|
|
|
|
prgm.program_ctx.ctx_enums
|
2021-12-10 00:59:39 +03:00
|
|
|
in
|
2022-09-28 13:27:47 +03:00
|
|
|
let prgm = Scopelang.Ast.type_program prgm in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Translating to default calculus...";
|
2022-11-07 15:50:28 +03:00
|
|
|
let prgm = Dcalc.From_scopelang.translate_program prgm in
|
2022-01-07 20:36:56 +03:00
|
|
|
let prgm =
|
2022-03-04 21:25:06 +03:00
|
|
|
if options.optimize then begin
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Optimizing default calculus...";
|
2023-04-21 12:56:07 +03:00
|
|
|
Shared_ast.Optimizations.optimize_program prgm
|
2022-01-07 20:36:56 +03:00
|
|
|
end
|
|
|
|
else prgm
|
|
|
|
in
|
2023-06-13 12:27:45 +03:00
|
|
|
(* Message.emit_debug (Format.asprintf "Typechecking results :@\n%a"
|
2023-01-05 20:56:19 +03:00
|
|
|
(Print.typ prgm.decl_ctx) typ); *)
|
2022-03-04 20:32:03 +03:00
|
|
|
match backend with
|
2022-10-05 12:46:18 +03:00
|
|
|
| `Typecheck ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Typechecking again...";
|
2023-01-05 20:56:19 +03:00
|
|
|
let _ =
|
2023-03-28 10:38:47 +03:00
|
|
|
try Shared_ast.Typing.program prgm ~leave_unresolved:false
|
2023-06-13 12:27:45 +03:00
|
|
|
with Message.CompilerError error_content ->
|
2023-05-30 16:41:49 +03:00
|
|
|
raise
|
2023-06-15 11:51:31 +03:00
|
|
|
(Message.CompilerError (Message.to_internal_error error_content))
|
2023-01-05 20:56:19 +03:00
|
|
|
in
|
2022-10-05 12:46:18 +03:00
|
|
|
(* That's it! *)
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_result "Typechecking successful!"
|
2022-03-04 20:32:03 +03:00
|
|
|
| `Dcalc ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let _output_file, with_output = get_output_format () in
|
|
|
|
with_output
|
|
|
|
@@ fun fmt ->
|
2022-03-04 20:32:03 +03:00
|
|
|
if Option.is_some options.ex_scope then
|
|
|
|
Format.fprintf fmt "%a\n"
|
2023-04-07 12:26:10 +03:00
|
|
|
(Shared_ast.Print.scope ~debug:options.debug prgm.decl_ctx)
|
2022-03-04 20:32:03 +03:00
|
|
|
( scope_uid,
|
|
|
|
Option.get
|
2022-08-16 18:09:26 +03:00
|
|
|
(Shared_ast.Scope.fold_left ~init:None
|
2023-01-23 14:19:36 +03:00
|
|
|
~f:(fun acc def _ ->
|
|
|
|
match def with
|
|
|
|
| ScopeDef (name, body)
|
|
|
|
when Shared_ast.ScopeName.equal name scope_uid ->
|
|
|
|
Some body
|
|
|
|
| _ -> acc)
|
2023-02-13 17:00:23 +03:00
|
|
|
prgm.code_items) )
|
2022-03-04 20:32:03 +03:00
|
|
|
else
|
2022-05-31 19:38:14 +03:00
|
|
|
let prgrm_dcalc_expr =
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
Shared_ast.Expr.unbox (Shared_ast.Program.to_expr prgm scope_uid)
|
2022-05-31 19:38:14 +03:00
|
|
|
in
|
2022-03-04 20:32:03 +03:00
|
|
|
Format.fprintf fmt "%a\n"
|
2023-05-02 17:33:23 +03:00
|
|
|
(Shared_ast.Print.expr ~debug:options.debug ())
|
2022-03-04 20:32:03 +03:00
|
|
|
prgrm_dcalc_expr
|
2023-04-05 16:38:46 +03:00
|
|
|
| ( `Interpret | `OCaml | `Python | `Scalc | `Lcalc | `Proof | `Plugin _
|
2023-04-14 12:32:49 +03:00
|
|
|
| `Interpret_Lcalc ) as backend -> (
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Typechecking again...";
|
2022-09-28 13:27:47 +03:00
|
|
|
let prgm =
|
2022-12-13 18:06:36 +03:00
|
|
|
try Shared_ast.Typing.program ~leave_unresolved:false prgm
|
2023-06-13 12:27:45 +03:00
|
|
|
with Message.CompilerError error_content ->
|
2023-05-30 16:41:49 +03:00
|
|
|
raise
|
2023-06-15 11:51:31 +03:00
|
|
|
(Message.CompilerError (Message.to_internal_error error_content))
|
2022-09-28 13:27:47 +03:00
|
|
|
in
|
2023-04-14 12:32:49 +03:00
|
|
|
if !Cli.check_invariants_flag then (
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Checking invariants...";
|
2023-04-14 13:01:16 +03:00
|
|
|
let result = Dcalc.Invariants.check_all_invariants prgm in
|
2023-04-18 16:45:30 +03:00
|
|
|
if not result then
|
|
|
|
raise
|
2023-06-13 12:27:45 +03:00
|
|
|
(Message.raise_internal_error
|
2023-05-30 16:41:49 +03:00
|
|
|
"Some Dcalc invariants are invalid"));
|
2023-04-14 12:32:49 +03:00
|
|
|
match backend with
|
|
|
|
| `Proof ->
|
|
|
|
let vcs =
|
|
|
|
Verification.Conditions.generate_verification_conditions prgm
|
|
|
|
(match options.ex_scope with
|
|
|
|
| None -> None
|
|
|
|
| Some _ -> Some scope_uid)
|
|
|
|
in
|
|
|
|
|
|
|
|
Verification.Solver.solve_vc prgm.decl_ctx vcs
|
2022-03-04 20:32:03 +03:00
|
|
|
| `Interpret ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Starting interpretation (dcalc)...";
|
2022-03-04 20:32:03 +03:00
|
|
|
let results =
|
2023-04-05 17:35:12 +03:00
|
|
|
Shared_ast.Interpreter.interpret_program_dcalc prgm scope_uid
|
2022-03-04 20:32:03 +03:00
|
|
|
in
|
|
|
|
let results =
|
|
|
|
List.sort
|
|
|
|
(fun ((v1, _), _) ((v2, _), _) -> String.compare v1 v2)
|
|
|
|
results
|
|
|
|
in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "End of interpretation";
|
|
|
|
Message.emit_result "Computation successful!%s"
|
2022-03-04 20:32:03 +03:00
|
|
|
(if List.length results > 0 then " Results:" else "");
|
|
|
|
List.iter
|
|
|
|
(fun ((var, _), result) ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_result "@[<hov 2>%s@ =@ %a@]" var
|
2023-05-02 17:33:23 +03:00
|
|
|
(Shared_ast.Print.expr ~debug:options.debug ())
|
2022-03-04 20:32:03 +03:00
|
|
|
result)
|
|
|
|
results
|
2023-04-13 22:49:16 +03:00
|
|
|
| `Plugin (Plugin.Dcalc p) ->
|
|
|
|
let output_file, _ = get_output_format ~ext:p.Plugin.extension () in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Compiling program through backend \"%s\"..."
|
2023-04-13 22:49:16 +03:00
|
|
|
p.Plugin.name;
|
2023-04-14 17:56:57 +03:00
|
|
|
p.Plugin.apply ~source_file ~output_file
|
|
|
|
~scope:
|
|
|
|
(match options.ex_scope with
|
|
|
|
| None -> None
|
|
|
|
| Some _ -> Some scope_uid)
|
2023-04-13 22:49:16 +03:00
|
|
|
(Shared_ast.Program.untype prgm)
|
|
|
|
type_ordering
|
2023-04-05 16:38:46 +03:00
|
|
|
| (`OCaml | `Interpret_Lcalc | `Python | `Lcalc | `Scalc | `Plugin _)
|
|
|
|
as backend -> (
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Compiling program into lambda calculus...";
|
2022-03-04 20:32:03 +03:00
|
|
|
let prgm =
|
2023-04-21 15:51:15 +03:00
|
|
|
if options.trace && options.avoid_exceptions then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2023-04-21 15:51:15 +03:00
|
|
|
"Option --avoid_exceptions is not compatible with option \
|
|
|
|
--trace";
|
2022-03-04 20:32:03 +03:00
|
|
|
if options.avoid_exceptions then
|
2023-03-28 10:55:01 +03:00
|
|
|
Shared_ast.Program.untype
|
|
|
|
@@ Lcalc.Compile_without_exceptions.translate_program prgm
|
2023-03-16 19:14:33 +03:00
|
|
|
else
|
|
|
|
Shared_ast.Program.untype
|
|
|
|
@@ Lcalc.Compile_with_exceptions.translate_program prgm
|
2022-03-04 20:32:03 +03:00
|
|
|
in
|
|
|
|
let prgm =
|
|
|
|
if options.optimize then begin
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Optimizing lambda calculus...";
|
2023-04-21 13:32:09 +03:00
|
|
|
Shared_ast.Optimizations.optimize_program prgm
|
2022-03-04 20:32:03 +03:00
|
|
|
end
|
2022-08-16 18:09:26 +03:00
|
|
|
else Shared_ast.Program.untype prgm
|
2022-03-04 20:32:03 +03:00
|
|
|
in
|
|
|
|
let prgm =
|
|
|
|
if options.closure_conversion then (
|
2022-12-06 17:59:08 +03:00
|
|
|
if not options.avoid_exceptions then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_error
|
2022-12-13 19:21:37 +03:00
|
|
|
"Option --avoid_exceptions must be enabled for \
|
|
|
|
--closure_conversion";
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Performing closure conversion...";
|
2022-03-04 20:32:03 +03:00
|
|
|
let prgm = Lcalc.Closure_conversion.closure_conversion prgm in
|
|
|
|
let prgm = Bindlib.unbox prgm in
|
2022-12-07 12:55:26 +03:00
|
|
|
let prgm =
|
|
|
|
if options.optimize then (
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Optimizing lambda calculus...";
|
2023-04-21 13:32:09 +03:00
|
|
|
Shared_ast.Optimizations.optimize_program prgm)
|
2022-12-07 12:55:26 +03:00
|
|
|
else prgm
|
|
|
|
in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Retyping lambda calculus...";
|
2022-12-07 13:00:01 +03:00
|
|
|
let prgm =
|
2022-12-13 18:06:36 +03:00
|
|
|
Shared_ast.Program.untype
|
|
|
|
(Shared_ast.Typing.program ~leave_unresolved:true prgm)
|
2022-12-07 13:00:01 +03:00
|
|
|
in
|
2022-03-04 20:32:03 +03:00
|
|
|
prgm)
|
|
|
|
else prgm
|
|
|
|
in
|
|
|
|
match backend with
|
|
|
|
| `Lcalc ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let _output_file, with_output = get_output_format () in
|
|
|
|
with_output
|
|
|
|
@@ fun fmt ->
|
2022-05-18 16:10:59 +03:00
|
|
|
if Option.is_some options.ex_scope then
|
|
|
|
Format.fprintf fmt "%a\n"
|
2023-04-07 12:26:10 +03:00
|
|
|
(Shared_ast.Print.scope ~debug:options.debug prgm.decl_ctx)
|
2022-11-17 19:13:35 +03:00
|
|
|
(scope_uid, Shared_ast.Program.get_scope_body prgm scope_uid)
|
2022-05-18 16:10:59 +03:00
|
|
|
else
|
2022-07-22 16:49:57 +03:00
|
|
|
Format.fprintf fmt "%a\n"
|
2023-04-07 12:57:14 +03:00
|
|
|
(Shared_ast.Print.program ~debug:options.debug)
|
|
|
|
prgm
|
2023-04-05 16:38:46 +03:00
|
|
|
| `Interpret_Lcalc ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Starting interpretation (lcalc)...";
|
2023-04-05 16:38:46 +03:00
|
|
|
let results =
|
2023-04-05 17:35:12 +03:00
|
|
|
Shared_ast.Interpreter.interpret_program_lcalc prgm scope_uid
|
2023-04-05 16:38:46 +03:00
|
|
|
in
|
|
|
|
let results =
|
|
|
|
List.sort
|
|
|
|
(fun ((v1, _), _) ((v2, _), _) -> String.compare v1 v2)
|
|
|
|
results
|
|
|
|
in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "End of interpretation";
|
|
|
|
Message.emit_result "Computation successful!%s"
|
2023-04-05 16:38:46 +03:00
|
|
|
(if List.length results > 0 then " Results:" else "");
|
|
|
|
List.iter
|
|
|
|
(fun ((var, _), result) ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_result "@[<hov 2>%s@ =@ %a@]" var
|
2023-05-02 17:33:23 +03:00
|
|
|
(Shared_ast.Print.expr ~debug:options.debug ())
|
2023-04-05 16:38:46 +03:00
|
|
|
result)
|
|
|
|
results
|
2022-03-04 20:32:03 +03:00
|
|
|
| (`OCaml | `Python | `Scalc | `Plugin _) as backend -> (
|
|
|
|
match backend with
|
|
|
|
| `OCaml ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let output_file, with_output =
|
|
|
|
get_output_format ~ext:".ml" ()
|
|
|
|
in
|
|
|
|
with_output
|
|
|
|
@@ fun fmt ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Compiling program into OCaml...";
|
|
|
|
Message.emit_debug "Writing to %s..."
|
2022-05-31 17:24:37 +03:00
|
|
|
(Option.value ~default:"stdout" output_file);
|
2022-03-04 20:32:03 +03:00
|
|
|
Lcalc.To_ocaml.format_program fmt prgm type_ordering
|
2023-04-13 22:49:16 +03:00
|
|
|
| `Plugin (Plugin.Dcalc _) -> assert false
|
2022-03-04 20:32:03 +03:00
|
|
|
| `Plugin (Plugin.Lcalc p) ->
|
2022-08-01 11:28:38 +03:00
|
|
|
let output_file, _ =
|
|
|
|
get_output_format ~ext:p.Plugin.extension ()
|
|
|
|
in
|
2023-06-15 11:51:31 +03:00
|
|
|
Message.emit_debug "Compiling program through backend \"%s\"..."
|
|
|
|
p.Plugin.name;
|
2023-04-14 17:56:57 +03:00
|
|
|
p.Plugin.apply ~source_file ~output_file
|
|
|
|
~scope:
|
|
|
|
(match options.ex_scope with
|
|
|
|
| None -> None
|
|
|
|
| Some _ -> Some scope_uid)
|
2022-07-29 14:40:43 +03:00
|
|
|
prgm type_ordering
|
2022-03-04 20:32:03 +03:00
|
|
|
| (`Python | `Scalc | `Plugin (Plugin.Scalc _)) as backend -> (
|
2023-02-17 21:47:15 +03:00
|
|
|
let prgm = Scalc.From_lcalc.translate_program prgm in
|
2022-03-04 20:32:03 +03:00
|
|
|
match backend with
|
|
|
|
| `Scalc ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let _output_file, with_output = get_output_format () in
|
|
|
|
with_output
|
|
|
|
@@ fun fmt ->
|
2022-03-04 20:32:03 +03:00
|
|
|
if Option.is_some options.ex_scope then
|
|
|
|
Format.fprintf fmt "%a\n"
|
2023-02-13 17:00:23 +03:00
|
|
|
(Scalc.Print.format_item ~debug:options.debug
|
2022-03-04 20:32:03 +03:00
|
|
|
prgm.decl_ctx)
|
|
|
|
(List.find
|
2023-02-13 17:00:23 +03:00
|
|
|
(function
|
|
|
|
| Scalc.Ast.SScope { scope_body_name; _ } ->
|
|
|
|
scope_body_name = scope_uid
|
|
|
|
| _ -> false)
|
|
|
|
prgm.code_items)
|
2023-02-10 20:56:40 +03:00
|
|
|
else Scalc.Print.format_program prgm.decl_ctx fmt prgm
|
2022-03-04 20:32:03 +03:00
|
|
|
| `Python ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let output_file, with_output =
|
|
|
|
get_output_format ~ext:".py" ()
|
|
|
|
in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Compiling program into Python...";
|
|
|
|
Message.emit_debug "Writing to %s..."
|
2022-05-31 17:24:37 +03:00
|
|
|
(Option.value ~default:"stdout" output_file);
|
|
|
|
with_output
|
|
|
|
@@ fun fmt ->
|
2022-03-04 20:32:03 +03:00
|
|
|
Scalc.To_python.format_program fmt prgm type_ordering
|
2023-04-13 22:49:16 +03:00
|
|
|
| `Plugin (Plugin.Dcalc _ | Plugin.Lcalc _) -> assert false
|
2022-03-04 20:32:03 +03:00
|
|
|
| `Plugin (Plugin.Scalc p) ->
|
2022-05-31 17:24:37 +03:00
|
|
|
let output_file, _ = get_output ~ext:p.Plugin.extension () in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug
|
2023-05-30 17:08:25 +03:00
|
|
|
"Compiling program through backend \"%s\"..." p.Plugin.name;
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_debug "Writing to %s..."
|
2022-05-31 17:24:37 +03:00
|
|
|
(Option.value ~default:"stdout" output_file);
|
2022-07-29 14:39:33 +03:00
|
|
|
p.Plugin.apply ~source_file ~output_file
|
2023-04-14 17:56:57 +03:00
|
|
|
~scope:
|
|
|
|
(match options.ex_scope with
|
|
|
|
| None -> None
|
|
|
|
| Some _ -> Some scope_uid)
|
|
|
|
prgm type_ordering)))))));
|
2022-03-04 20:32:03 +03:00
|
|
|
0
|
2022-02-01 17:41:53 +03:00
|
|
|
with
|
2023-06-13 12:27:45 +03:00
|
|
|
| Message.CompilerError content ->
|
2022-07-05 11:29:24 +03:00
|
|
|
let bt = Printexc.get_raw_backtrace () in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_content content Error;
|
2022-07-05 11:29:24 +03:00
|
|
|
if Printexc.backtrace_status () then Printexc.print_raw_backtrace stderr bt;
|
2023-05-04 19:28:24 +03:00
|
|
|
-1
|
2022-02-01 17:41:53 +03:00
|
|
|
| Sys_error msg ->
|
2022-07-05 11:29:24 +03:00
|
|
|
let bt = Printexc.get_raw_backtrace () in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_content
|
|
|
|
(Message.Content.of_string ("System error: " ^ msg))
|
2023-05-30 17:22:05 +03:00
|
|
|
Error;
|
2022-07-05 11:29:24 +03:00
|
|
|
if Printexc.backtrace_status () then Printexc.print_raw_backtrace stderr bt;
|
2022-02-01 17:41:53 +03:00
|
|
|
-1
|
2020-03-08 03:52:31 +03:00
|
|
|
|
2020-12-26 19:37:41 +03:00
|
|
|
let main () =
|
2023-03-14 18:57:14 +03:00
|
|
|
if
|
|
|
|
Array.length Sys.argv >= 2
|
|
|
|
&& String.lowercase_ascii Sys.argv.(1) = "pygmentize"
|
|
|
|
then Literate.Pygmentize.exec ();
|
2022-02-21 16:49:58 +03:00
|
|
|
let return_code =
|
2022-05-04 18:37:03 +03:00
|
|
|
Cmdliner.Cmd.eval'
|
|
|
|
(Cmdliner.Cmd.v Cli.info (Cli.catala_t (fun f -> driver (FileName f))))
|
2022-02-21 16:49:58 +03:00
|
|
|
in
|
2022-05-04 18:37:03 +03:00
|
|
|
exit return_code
|
2022-03-04 20:32:03 +03:00
|
|
|
|
|
|
|
(* Export module PluginAPI, hide parent module Plugin *)
|
|
|
|
module Plugin = Plugin.PluginAPI
|