2020-11-23 12:44:06 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
2023-04-05 15:57:02 +03:00
|
|
|
Denis Merigoux <denis.merigoux@inria.fr>, Emile Rolley
|
|
|
|
<emile.rolley@tuta.io>, Alain Delaët <alain.delaet--tixeuil@inria.Fr>, Louis
|
|
|
|
Gesbert <louis.gesbert@inria.fr>
|
2020-11-23 12:44:06 +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. *)
|
|
|
|
|
2020-12-14 20:09:38 +03:00
|
|
|
(** Reference interpreter for the default calculus *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2023-03-19 22:04:02 +03:00
|
|
|
open Definitions
|
2023-03-24 16:54:06 +03:00
|
|
|
open Op
|
2022-07-19 20:17:02 +03:00
|
|
|
module Runtime = Runtime_ocaml.Runtime
|
2020-11-23 12:44:06 +03:00
|
|
|
|
2020-12-14 20:09:38 +03:00
|
|
|
(** {1 Helpers} *)
|
|
|
|
|
2023-03-20 17:17:09 +03:00
|
|
|
let is_empty_error : type a. (a, 'm) gexpr -> bool =
|
2023-05-17 16:44:57 +03:00
|
|
|
fun e -> match Mark.remove e with EEmptyError -> true | _ -> false
|
2020-11-23 12:44:06 +03:00
|
|
|
|
2023-04-05 15:57:02 +03:00
|
|
|
(** [e' = propagate_empty_error e f] return [EEmptyError] if [e] is
|
|
|
|
[EEmptyError], else it apply [f] on not-empty term [e]. *)
|
|
|
|
let propagate_empty_error :
|
2023-03-27 17:55:52 +03:00
|
|
|
type a. (a, 'm) gexpr -> ((a, 'm) gexpr -> (a, 'm) gexpr) -> (a, 'm) gexpr =
|
|
|
|
fun e f -> match e with (EEmptyError, _) as e -> e | e -> f e
|
|
|
|
|
2023-04-05 15:57:02 +03:00
|
|
|
(** [e' = propagate_empty_error_list elist f] return [EEmptyError] if one lement
|
|
|
|
of [es] is [EEmptyError], else it apply [f] on not-empty term list [elist]. *)
|
|
|
|
let propagate_empty_error_list elist f =
|
2023-03-27 17:55:52 +03:00
|
|
|
let rec aux acc = function
|
|
|
|
| [] -> f (List.rev acc)
|
2023-04-05 15:57:02 +03:00
|
|
|
| e :: r -> propagate_empty_error e (fun e -> aux (e :: acc) r)
|
2023-03-27 17:55:52 +03:00
|
|
|
in
|
|
|
|
aux [] elist
|
|
|
|
|
2023-04-06 17:35:28 +03:00
|
|
|
(* TODO: we should provide a generic way to print logs, that work across the
|
|
|
|
different backends: python, ocaml, javascript, and interpreter *)
|
|
|
|
|
2023-06-08 14:34:54 +03:00
|
|
|
let indent_str = ref ""
|
|
|
|
|
2020-12-14 20:09:38 +03:00
|
|
|
(** {1 Evaluation} *)
|
2023-10-11 10:37:36 +03:00
|
|
|
let print_log lang entry infos pos e =
|
2023-06-28 16:57:52 +03:00
|
|
|
if Cli.globals.trace then
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
match entry with
|
|
|
|
| VarDef _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_log "%s%a %a: @{<green>%s@}" !indent_str Print.log_entry
|
2023-06-08 14:34:54 +03:00
|
|
|
entry Print.uid_list infos
|
2023-10-11 10:37:36 +03:00
|
|
|
(Message.unformat (fun ppf ->
|
|
|
|
(if Cli.globals.debug then Print.expr ~debug:true ()
|
|
|
|
else Print.UserFacing.expr lang)
|
|
|
|
ppf e))
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
| PosRecordIfTrueBool -> (
|
2023-05-17 16:44:57 +03:00
|
|
|
match pos <> Pos.no_pos, Mark.remove e with
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
| true, ELit (LBool true) ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_log "%s@[<v>%a@{<green>Definition applied@}:@,%a@]"
|
2023-06-08 14:34:54 +03:00
|
|
|
!indent_str Print.log_entry entry Pos.format_loc_text pos
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
| _ -> ())
|
|
|
|
| BeginCall ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_log "%s%a %a" !indent_str Print.log_entry entry
|
2023-06-08 14:34:54 +03:00
|
|
|
Print.uid_list infos;
|
|
|
|
indent_str := !indent_str ^ " "
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
| EndCall ->
|
2023-06-08 14:34:54 +03:00
|
|
|
indent_str := String.sub !indent_str 0 (String.length !indent_str - 2);
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.emit_log "%s%a %a" !indent_str Print.log_entry entry
|
2023-06-08 14:34:54 +03:00
|
|
|
Print.uid_list infos
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
|
2023-03-20 17:17:09 +03:00
|
|
|
exception CatalaException of except
|
|
|
|
|
2022-12-13 13:51:22 +03:00
|
|
|
(* Todo: this should be handled early when resolving overloads. Here we have
|
|
|
|
proper structural equality, but the OCaml backend for example uses the
|
|
|
|
builtin equality function instead of this. *)
|
2023-09-22 19:01:25 +03:00
|
|
|
let handle_eq evaluate_operator pos lang e1 e2 =
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
let open Runtime.Oper in
|
|
|
|
match e1, e2 with
|
|
|
|
| ELit LUnit, ELit LUnit -> true
|
|
|
|
| ELit (LBool b1), ELit (LBool b2) -> not (o_xor b1 b2)
|
|
|
|
| ELit (LInt x1), ELit (LInt x2) -> o_eq_int_int x1 x2
|
|
|
|
| ELit (LRat x1), ELit (LRat x2) -> o_eq_rat_rat x1 x2
|
|
|
|
| ELit (LMoney x1), ELit (LMoney x2) -> o_eq_mon_mon x1 x2
|
|
|
|
| ELit (LDuration x1), ELit (LDuration x2) -> o_eq_dur_dur x1 x2
|
|
|
|
| ELit (LDate x1), ELit (LDate x2) -> o_eq_dat_dat x1 x2
|
|
|
|
| EArray es1, EArray es2 -> (
|
|
|
|
try
|
|
|
|
List.for_all2
|
|
|
|
(fun e1 e2 ->
|
2023-09-22 19:01:25 +03:00
|
|
|
match Mark.remove (evaluate_operator Eq pos lang [e1; e2]) with
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
| ELit (LBool b) -> b
|
|
|
|
| _ -> assert false
|
|
|
|
(* should not happen *))
|
|
|
|
es1 es2
|
|
|
|
with Invalid_argument _ -> false)
|
|
|
|
| EStruct { fields = es1; name = s1 }, EStruct { fields = es2; name = s2 } ->
|
|
|
|
StructName.equal s1 s2
|
|
|
|
&& StructField.Map.equal
|
|
|
|
(fun e1 e2 ->
|
2023-09-22 19:01:25 +03:00
|
|
|
match Mark.remove (evaluate_operator Eq pos lang [e1; e2]) with
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
| ELit (LBool b) -> b
|
|
|
|
| _ -> assert false
|
|
|
|
(* should not happen *))
|
|
|
|
es1 es2
|
|
|
|
| ( EInj { e = e1; cons = i1; name = en1 },
|
|
|
|
EInj { e = e2; cons = i2; name = en2 } ) -> (
|
|
|
|
try
|
|
|
|
EnumName.equal en1 en2
|
|
|
|
&& EnumConstructor.equal i1 i2
|
|
|
|
&&
|
2023-09-22 19:01:25 +03:00
|
|
|
match Mark.remove (evaluate_operator Eq pos lang [e1; e2]) with
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
| ELit (LBool b) -> b
|
|
|
|
| _ -> assert false
|
|
|
|
(* should not happen *)
|
|
|
|
with Invalid_argument _ -> false)
|
|
|
|
| _, _ -> false (* comparing anything else return false *)
|
|
|
|
|
2022-12-20 15:10:41 +03:00
|
|
|
(* Call-by-value: the arguments are expected to be already evaluated here *)
|
2023-03-27 17:55:52 +03:00
|
|
|
let rec evaluate_operator
|
|
|
|
evaluate_expr
|
2023-04-04 12:10:59 +03:00
|
|
|
(op : < overloaded : no ; .. > operator)
|
2023-03-27 17:55:52 +03:00
|
|
|
m
|
2023-09-22 19:01:25 +03:00
|
|
|
lang
|
2023-03-27 17:55:52 +03:00
|
|
|
args =
|
|
|
|
let pos = Expr.mark_pos m in
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
let protect f x y =
|
|
|
|
let get_binop_args_pos = function
|
2023-03-19 22:04:02 +03:00
|
|
|
| (arg0 :: arg1 :: _ : ('t, 'm) gexpr list) ->
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
[None, Expr.pos arg0; None, Expr.pos arg1]
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
|
|
|
try f x y with
|
|
|
|
| Division_by_zero ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2021-05-29 19:56:20 +03:00
|
|
|
[
|
2022-06-23 15:06:11 +03:00
|
|
|
Some "The division operator:", pos;
|
2022-08-12 23:42:39 +03:00
|
|
|
Some "The null denominator:", Expr.pos (List.nth args 1);
|
2021-05-29 19:56:20 +03:00
|
|
|
]
|
2022-03-08 15:04:27 +03:00
|
|
|
"division by zero at runtime"
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
| Runtime.UncomparableDurations ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error (get_binop_args_pos args)
|
2021-05-29 21:17:22 +03:00
|
|
|
"Cannot compare together durations that cannot be converted to a \
|
|
|
|
precise number of days"
|
|
|
|
in
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
let err () =
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2023-06-15 18:47:58 +03:00
|
|
|
([
|
|
|
|
( Some
|
|
|
|
(Format.asprintf "Operator (value %a):"
|
|
|
|
(Print.operator ~debug:true)
|
|
|
|
op),
|
|
|
|
pos );
|
|
|
|
]
|
2021-03-05 21:16:56 +03:00
|
|
|
@ List.mapi
|
|
|
|
(fun i arg ->
|
|
|
|
( Some
|
2022-03-08 16:55:48 +03:00
|
|
|
(Format.asprintf "Argument n°%d, value %a" (i + 1)
|
2023-09-22 19:01:25 +03:00
|
|
|
(Print.UserFacing.expr lang)
|
|
|
|
arg),
|
2022-08-12 23:42:39 +03:00
|
|
|
Expr.pos arg ))
|
2022-03-08 15:04:27 +03:00
|
|
|
args)
|
2023-04-15 18:07:24 +03:00
|
|
|
"Operator %a applied to the wrong arguments\n\
|
|
|
|
(should not happen if the term was well-typed)%a"
|
|
|
|
(Print.operator ~debug:true)
|
|
|
|
op Expr.format
|
2023-12-18 12:25:00 +03:00
|
|
|
(EAppOp { op; tys = []; args }, m)
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
in
|
2023-04-05 15:57:02 +03:00
|
|
|
propagate_empty_error_list args
|
2023-03-27 17:55:52 +03:00
|
|
|
@@ fun args ->
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
let open Runtime.Oper in
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.add m
|
2023-03-27 17:55:52 +03:00
|
|
|
@@
|
|
|
|
match op, args with
|
|
|
|
| Length, [(EArray es, _)] ->
|
|
|
|
ELit (LInt (Runtime.integer_of_int (List.length es)))
|
|
|
|
| Log (entry, infos), [e'] ->
|
2023-10-11 10:37:36 +03:00
|
|
|
print_log lang entry infos pos e';
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove e'
|
2023-06-15 17:52:36 +03:00
|
|
|
| (FromClosureEnv | ToClosureEnv), [e'] ->
|
|
|
|
(* [FromClosureEnv] and [ToClosureEnv] are just there to bypass the need for
|
|
|
|
existential types when typing code after closure conversion. There are
|
|
|
|
effectively no-ops. *)
|
|
|
|
Mark.remove e'
|
|
|
|
| (ToClosureEnv | FromClosureEnv), _ -> err ()
|
2023-03-27 17:55:52 +03:00
|
|
|
| Eq, [(e1, _); (e2, _)] ->
|
2023-09-22 19:01:25 +03:00
|
|
|
ELit (LBool (handle_eq (evaluate_operator evaluate_expr) m lang e1 e2))
|
2023-03-27 17:55:52 +03:00
|
|
|
| Map, [f; (EArray es, _)] ->
|
|
|
|
EArray
|
|
|
|
(List.map
|
2023-12-18 12:25:00 +03:00
|
|
|
(fun e' ->
|
|
|
|
evaluate_expr
|
|
|
|
(Mark.copy e'
|
|
|
|
(EApp { f; args = [e']; tys = [Expr.maybe_ty (Mark.get e')] })))
|
2023-03-27 17:55:52 +03:00
|
|
|
es)
|
2024-01-24 17:36:51 +03:00
|
|
|
| Map2, [f; (EArray es1, _); (EArray es2, _)] ->
|
|
|
|
EArray
|
|
|
|
(List.map2
|
|
|
|
(fun e1 e2 ->
|
|
|
|
evaluate_expr
|
|
|
|
(Mark.add m
|
|
|
|
(EApp { f; args = [e1; e2]; tys = [Expr.maybe_ty (Mark.get e1); Expr.maybe_ty (Mark.get e2)] })))
|
|
|
|
es1 es2)
|
2023-05-17 16:44:57 +03:00
|
|
|
| Reduce, [_; default; (EArray [], _)] -> Mark.remove default
|
2023-03-27 17:55:52 +03:00
|
|
|
| Reduce, [f; _; (EArray (x0 :: xn), _)] ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove
|
2023-03-27 17:55:52 +03:00
|
|
|
(List.fold_left
|
|
|
|
(fun acc x ->
|
2023-12-18 12:25:00 +03:00
|
|
|
evaluate_expr
|
|
|
|
(Mark.copy f
|
|
|
|
(EApp
|
|
|
|
{
|
|
|
|
f;
|
|
|
|
args = [acc; x];
|
|
|
|
tys =
|
|
|
|
[
|
|
|
|
Expr.maybe_ty (Mark.get acc); Expr.maybe_ty (Mark.get x);
|
|
|
|
];
|
|
|
|
})))
|
2023-03-27 17:55:52 +03:00
|
|
|
x0 xn)
|
|
|
|
| Concat, [(EArray es1, _); (EArray es2, _)] -> EArray (es1 @ es2)
|
|
|
|
| Filter, [f; (EArray es, _)] ->
|
|
|
|
EArray
|
|
|
|
(List.filter
|
|
|
|
(fun e' ->
|
2023-12-18 12:25:00 +03:00
|
|
|
match
|
|
|
|
evaluate_expr
|
|
|
|
(Mark.copy e'
|
|
|
|
(EApp { f; args = [e']; tys = [Expr.maybe_ty (Mark.get e')] }))
|
|
|
|
with
|
2023-03-27 17:55:52 +03:00
|
|
|
| ELit (LBool b), _ -> b
|
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error
|
2023-03-27 17:55:52 +03:00
|
|
|
(Expr.pos (List.nth args 0))
|
|
|
|
"This predicate evaluated to something else than a boolean \
|
|
|
|
(should not happen if the term was well-typed)")
|
|
|
|
es)
|
|
|
|
| Fold, [f; init; (EArray es, _)] ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove
|
2023-03-27 17:55:52 +03:00
|
|
|
(List.fold_left
|
|
|
|
(fun acc e' ->
|
2023-12-18 12:25:00 +03:00
|
|
|
evaluate_expr
|
|
|
|
(Mark.copy e'
|
|
|
|
(EApp
|
|
|
|
{
|
|
|
|
f;
|
|
|
|
args = [acc; e'];
|
|
|
|
tys =
|
|
|
|
[
|
|
|
|
Expr.maybe_ty (Mark.get acc);
|
|
|
|
Expr.maybe_ty (Mark.get e');
|
|
|
|
];
|
|
|
|
})))
|
2023-03-27 17:55:52 +03:00
|
|
|
init es)
|
2024-01-24 17:36:51 +03:00
|
|
|
| (Length | Log _ | Eq | Map | Map2 | Concat | Filter | Fold | Reduce), _ -> err ()
|
2023-03-27 17:55:52 +03:00
|
|
|
| Not, [(ELit (LBool b), _)] -> ELit (LBool (o_not b))
|
|
|
|
| GetDay, [(ELit (LDate d), _)] -> ELit (LInt (o_getDay d))
|
|
|
|
| GetMonth, [(ELit (LDate d), _)] -> ELit (LInt (o_getMonth d))
|
|
|
|
| GetYear, [(ELit (LDate d), _)] -> ELit (LInt (o_getYear d))
|
|
|
|
| FirstDayOfMonth, [(ELit (LDate d), _)] -> ELit (LDate (o_firstDayOfMonth d))
|
|
|
|
| LastDayOfMonth, [(ELit (LDate d), _)] -> ELit (LDate (o_lastDayOfMonth d))
|
|
|
|
| And, [(ELit (LBool b1), _); (ELit (LBool b2), _)] ->
|
|
|
|
ELit (LBool (o_and b1 b2))
|
|
|
|
| Or, [(ELit (LBool b1), _); (ELit (LBool b2), _)] ->
|
|
|
|
ELit (LBool (o_or b1 b2))
|
|
|
|
| Xor, [(ELit (LBool b1), _); (ELit (LBool b2), _)] ->
|
|
|
|
ELit (LBool (o_xor b1 b2))
|
|
|
|
| ( ( Not | GetDay | GetMonth | GetYear | FirstDayOfMonth | LastDayOfMonth
|
|
|
|
| And | Or | Xor ),
|
|
|
|
_ ) ->
|
|
|
|
err ()
|
|
|
|
| Minus_int, [(ELit (LInt x), _)] -> ELit (LInt (o_minus_int x))
|
|
|
|
| Minus_rat, [(ELit (LRat x), _)] -> ELit (LRat (o_minus_rat x))
|
|
|
|
| Minus_mon, [(ELit (LMoney x), _)] -> ELit (LMoney (o_minus_mon x))
|
|
|
|
| Minus_dur, [(ELit (LDuration x), _)] -> ELit (LDuration (o_minus_dur x))
|
|
|
|
| ToRat_int, [(ELit (LInt i), _)] -> ELit (LRat (o_torat_int i))
|
|
|
|
| ToRat_mon, [(ELit (LMoney i), _)] -> ELit (LRat (o_torat_mon i))
|
|
|
|
| ToMoney_rat, [(ELit (LRat i), _)] -> ELit (LMoney (o_tomoney_rat i))
|
|
|
|
| Round_mon, [(ELit (LMoney m), _)] -> ELit (LMoney (o_round_mon m))
|
|
|
|
| Round_rat, [(ELit (LRat m), _)] -> ELit (LRat (o_round_rat m))
|
|
|
|
| Add_int_int, [(ELit (LInt x), _); (ELit (LInt y), _)] ->
|
|
|
|
ELit (LInt (o_add_int_int x y))
|
|
|
|
| Add_rat_rat, [(ELit (LRat x), _); (ELit (LRat y), _)] ->
|
|
|
|
ELit (LRat (o_add_rat_rat x y))
|
|
|
|
| Add_mon_mon, [(ELit (LMoney x), _); (ELit (LMoney y), _)] ->
|
|
|
|
ELit (LMoney (o_add_mon_mon x y))
|
|
|
|
| Add_dat_dur r, [(ELit (LDate x), _); (ELit (LDuration y), _)] ->
|
|
|
|
ELit (LDate (o_add_dat_dur r x y))
|
|
|
|
| Add_dur_dur, [(ELit (LDuration x), _); (ELit (LDuration y), _)] ->
|
|
|
|
ELit (LDuration (o_add_dur_dur x y))
|
|
|
|
| Sub_int_int, [(ELit (LInt x), _); (ELit (LInt y), _)] ->
|
|
|
|
ELit (LInt (o_sub_int_int x y))
|
|
|
|
| Sub_rat_rat, [(ELit (LRat x), _); (ELit (LRat y), _)] ->
|
|
|
|
ELit (LRat (o_sub_rat_rat x y))
|
|
|
|
| Sub_mon_mon, [(ELit (LMoney x), _); (ELit (LMoney y), _)] ->
|
|
|
|
ELit (LMoney (o_sub_mon_mon x y))
|
|
|
|
| Sub_dat_dat, [(ELit (LDate x), _); (ELit (LDate y), _)] ->
|
|
|
|
ELit (LDuration (o_sub_dat_dat x y))
|
|
|
|
| Sub_dat_dur, [(ELit (LDate x), _); (ELit (LDuration y), _)] ->
|
|
|
|
ELit (LDate (o_sub_dat_dur x y))
|
|
|
|
| Sub_dur_dur, [(ELit (LDuration x), _); (ELit (LDuration y), _)] ->
|
|
|
|
ELit (LDuration (o_sub_dur_dur x y))
|
|
|
|
| Mult_int_int, [(ELit (LInt x), _); (ELit (LInt y), _)] ->
|
|
|
|
ELit (LInt (o_mult_int_int x y))
|
|
|
|
| Mult_rat_rat, [(ELit (LRat x), _); (ELit (LRat y), _)] ->
|
|
|
|
ELit (LRat (o_mult_rat_rat x y))
|
|
|
|
| Mult_mon_rat, [(ELit (LMoney x), _); (ELit (LRat y), _)] ->
|
|
|
|
ELit (LMoney (o_mult_mon_rat x y))
|
|
|
|
| Mult_dur_int, [(ELit (LDuration x), _); (ELit (LInt y), _)] ->
|
|
|
|
ELit (LDuration (o_mult_dur_int x y))
|
|
|
|
| Div_int_int, [(ELit (LInt x), _); (ELit (LInt y), _)] ->
|
|
|
|
ELit (LRat (protect o_div_int_int x y))
|
|
|
|
| Div_rat_rat, [(ELit (LRat x), _); (ELit (LRat y), _)] ->
|
|
|
|
ELit (LRat (protect o_div_rat_rat x y))
|
|
|
|
| Div_mon_mon, [(ELit (LMoney x), _); (ELit (LMoney y), _)] ->
|
|
|
|
ELit (LRat (protect o_div_mon_mon x y))
|
|
|
|
| Div_mon_rat, [(ELit (LMoney x), _); (ELit (LRat y), _)] ->
|
|
|
|
ELit (LMoney (protect o_div_mon_rat x y))
|
|
|
|
| Div_dur_dur, [(ELit (LDuration x), _); (ELit (LDuration y), _)] ->
|
|
|
|
ELit (LRat (protect o_div_dur_dur x y))
|
|
|
|
| Lt_int_int, [(ELit (LInt x), _); (ELit (LInt y), _)] ->
|
|
|
|
ELit (LBool (o_lt_int_int x y))
|
|
|
|
| Lt_rat_rat, [(ELit (LRat x), _); (ELit (LRat y), _)] ->
|
|
|
|
ELit (LBool (o_lt_rat_rat x y))
|
|
|
|
| Lt_mon_mon, [(ELit (LMoney x), _); (ELit (LMoney y), _)] ->
|
|
|
|
ELit (LBool (o_lt_mon_mon x y))
|
|
|
|
| Lt_dat_dat, [(ELit (LDate x), _); (ELit (LDate y), _)] ->
|
|
|
|
ELit (LBool (o_lt_dat_dat x y))
|
|
|
|
| Lt_dur_dur, [(ELit (LDuration x), _); (ELit (LDuration y), _)] ->
|
|
|
|
ELit (LBool (protect o_lt_dur_dur x y))
|
|
|
|
| Lte_int_int, [(ELit (LInt x), _); (ELit (LInt y), _)] ->
|
|
|
|
ELit (LBool (o_lte_int_int x y))
|
|
|
|
| Lte_rat_rat, [(ELit (LRat x), _); (ELit (LRat y), _)] ->
|
|
|
|
ELit (LBool (o_lte_rat_rat x y))
|
|
|
|
| Lte_mon_mon, [(ELit (LMoney x), _); (ELit (LMoney y), _)] ->
|
|
|
|
ELit (LBool (o_lte_mon_mon x y))
|
|
|
|
| Lte_dat_dat, [(ELit (LDate x), _); (ELit (LDate y), _)] ->
|
|
|
|
ELit (LBool (o_lte_dat_dat x y))
|
|
|
|
| Lte_dur_dur, [(ELit (LDuration x), _); (ELit (LDuration y), _)] ->
|
|
|
|
ELit (LBool (protect o_lte_dur_dur x y))
|
|
|
|
| Gt_int_int, [(ELit (LInt x), _); (ELit (LInt y), _)] ->
|
|
|
|
ELit (LBool (o_gt_int_int x y))
|
|
|
|
| Gt_rat_rat, [(ELit (LRat x), _); (ELit (LRat y), _)] ->
|
|
|
|
ELit (LBool (o_gt_rat_rat x y))
|
|
|
|
| Gt_mon_mon, [(ELit (LMoney x), _); (ELit (LMoney y), _)] ->
|
|
|
|
ELit (LBool (o_gt_mon_mon x y))
|
|
|
|
| Gt_dat_dat, [(ELit (LDate x), _); (ELit (LDate y), _)] ->
|
|
|
|
ELit (LBool (o_gt_dat_dat x y))
|
|
|
|
| Gt_dur_dur, [(ELit (LDuration x), _); (ELit (LDuration y), _)] ->
|
|
|
|
ELit (LBool (protect o_gt_dur_dur x y))
|
|
|
|
| Gte_int_int, [(ELit (LInt x), _); (ELit (LInt y), _)] ->
|
|
|
|
ELit (LBool (o_gte_int_int x y))
|
|
|
|
| Gte_rat_rat, [(ELit (LRat x), _); (ELit (LRat y), _)] ->
|
|
|
|
ELit (LBool (o_gte_rat_rat x y))
|
|
|
|
| Gte_mon_mon, [(ELit (LMoney x), _); (ELit (LMoney y), _)] ->
|
|
|
|
ELit (LBool (o_gte_mon_mon x y))
|
|
|
|
| Gte_dat_dat, [(ELit (LDate x), _); (ELit (LDate y), _)] ->
|
|
|
|
ELit (LBool (o_gte_dat_dat x y))
|
|
|
|
| Gte_dur_dur, [(ELit (LDuration x), _); (ELit (LDuration y), _)] ->
|
|
|
|
ELit (LBool (protect o_gte_dur_dur x y))
|
|
|
|
| Eq_int_int, [(ELit (LInt x), _); (ELit (LInt y), _)] ->
|
|
|
|
ELit (LBool (o_eq_int_int x y))
|
|
|
|
| Eq_rat_rat, [(ELit (LRat x), _); (ELit (LRat y), _)] ->
|
|
|
|
ELit (LBool (o_eq_rat_rat x y))
|
|
|
|
| Eq_mon_mon, [(ELit (LMoney x), _); (ELit (LMoney y), _)] ->
|
|
|
|
ELit (LBool (o_eq_mon_mon x y))
|
|
|
|
| Eq_dat_dat, [(ELit (LDate x), _); (ELit (LDate y), _)] ->
|
|
|
|
ELit (LBool (o_eq_dat_dat x y))
|
|
|
|
| Eq_dur_dur, [(ELit (LDuration x), _); (ELit (LDuration y), _)] ->
|
|
|
|
ELit (LBool (protect o_eq_dur_dur x y))
|
2023-11-28 17:00:41 +03:00
|
|
|
| HandleDefault, [(EArray excepts, _); just; cons] -> (
|
|
|
|
(* This case is for lcalc with exceptions: we rely OCaml exception handling
|
|
|
|
here *)
|
|
|
|
match
|
|
|
|
List.filter_map
|
|
|
|
(fun e ->
|
|
|
|
try Some (evaluate_expr (Expr.unthunk_term_nobox e m))
|
|
|
|
with CatalaException EmptyError -> None)
|
|
|
|
excepts
|
|
|
|
with
|
|
|
|
| [] -> (
|
|
|
|
let just = evaluate_expr (Expr.unthunk_term_nobox just m) in
|
|
|
|
match Mark.remove just with
|
|
|
|
| ELit (LBool true) ->
|
|
|
|
Mark.remove
|
|
|
|
(evaluate_expr (Expr.unthunk_term_nobox cons (Mark.get cons)))
|
|
|
|
| ELit (LBool false) -> raise (CatalaException EmptyError)
|
|
|
|
| _ ->
|
|
|
|
Message.raise_spanned_error pos
|
|
|
|
"Default justification has not been reduced to a boolean at \
|
|
|
|
evaluation (should not happen if the term was well-typed@\n\
|
|
|
|
%a@."
|
|
|
|
Expr.format just)
|
|
|
|
| [e] -> Mark.remove e
|
|
|
|
| es ->
|
|
|
|
Message.raise_multispanned_error
|
|
|
|
(List.map
|
|
|
|
(fun except ->
|
|
|
|
Some "This consequence has a valid justification:", Expr.pos except)
|
|
|
|
es)
|
|
|
|
"There is a conflict between multiple valid consequences for assigning \
|
|
|
|
the same variable.")
|
2023-04-11 18:46:41 +03:00
|
|
|
| HandleDefaultOpt, [(EArray exps, _); justification; conclusion] -> (
|
2023-04-11 15:09:57 +03:00
|
|
|
let valid_exceptions =
|
|
|
|
ListLabels.filter exps ~f:(function
|
2023-05-17 14:26:47 +03:00
|
|
|
| EInj { name; cons; _ }, _ when EnumName.equal name Expr.option_enum ->
|
|
|
|
EnumConstructor.equal cons Expr.some_constr
|
2023-04-11 15:09:57 +03:00
|
|
|
| _ -> err ())
|
|
|
|
in
|
|
|
|
match valid_exceptions with
|
|
|
|
| [] -> (
|
2023-11-15 13:01:45 +03:00
|
|
|
let e = evaluate_expr (Expr.unthunk_term_nobox justification m) in
|
|
|
|
match Mark.remove e with
|
|
|
|
| ELit (LBool true) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove (evaluate_expr (Expr.unthunk_term_nobox conclusion m))
|
2023-11-15 13:01:45 +03:00
|
|
|
| ELit (LBool false) ->
|
2023-04-11 15:09:57 +03:00
|
|
|
EInj
|
|
|
|
{
|
2023-05-17 14:26:47 +03:00
|
|
|
name = Expr.option_enum;
|
|
|
|
cons = Expr.none_constr;
|
2023-11-15 13:01:45 +03:00
|
|
|
e = Mark.copy justification (ELit LUnit);
|
2023-04-11 15:09:57 +03:00
|
|
|
}
|
|
|
|
| EInj { name; cons; e }
|
2023-05-17 14:26:47 +03:00
|
|
|
when EnumName.equal name Expr.option_enum
|
|
|
|
&& EnumConstructor.equal cons Expr.none_constr ->
|
2023-04-11 15:09:57 +03:00
|
|
|
EInj
|
|
|
|
{
|
2023-05-17 14:26:47 +03:00
|
|
|
name = Expr.option_enum;
|
|
|
|
cons = Expr.none_constr;
|
2023-05-17 16:44:57 +03:00
|
|
|
e = Mark.copy e (ELit LUnit);
|
2023-04-11 15:09:57 +03:00
|
|
|
}
|
|
|
|
| _ -> err ())
|
|
|
|
| [((EInj { cons; name; _ } as e), _)]
|
2023-05-17 14:26:47 +03:00
|
|
|
when EnumName.equal name Expr.option_enum
|
|
|
|
&& EnumConstructor.equal cons Expr.some_constr ->
|
2023-04-11 15:09:57 +03:00
|
|
|
e
|
|
|
|
| [_] -> err ()
|
|
|
|
| _ -> raise (CatalaException ConflictError))
|
2023-03-27 17:55:52 +03:00
|
|
|
| ( ( Minus_int | Minus_rat | Minus_mon | Minus_dur | ToRat_int | ToRat_mon
|
|
|
|
| ToMoney_rat | Round_rat | Round_mon | Add_int_int | Add_rat_rat
|
|
|
|
| Add_mon_mon | Add_dat_dur _ | Add_dur_dur | Sub_int_int | Sub_rat_rat
|
|
|
|
| Sub_mon_mon | Sub_dat_dat | Sub_dat_dur | Sub_dur_dur | Mult_int_int
|
|
|
|
| Mult_rat_rat | Mult_mon_rat | Mult_dur_int | Div_int_int | Div_rat_rat
|
|
|
|
| Div_mon_mon | Div_mon_rat | Div_dur_dur | Lt_int_int | Lt_rat_rat
|
|
|
|
| Lt_mon_mon | Lt_dat_dat | Lt_dur_dur | Lte_int_int | Lte_rat_rat
|
|
|
|
| Lte_mon_mon | Lte_dat_dat | Lte_dur_dur | Gt_int_int | Gt_rat_rat
|
|
|
|
| Gt_mon_mon | Gt_dat_dat | Gt_dur_dur | Gte_int_int | Gte_rat_rat
|
|
|
|
| Gte_mon_mon | Gte_dat_dat | Gte_dur_dur | Eq_int_int | Eq_rat_rat
|
2023-11-28 17:00:41 +03:00
|
|
|
| Eq_mon_mon | Eq_dat_dat | Eq_dur_dur | HandleDefault | HandleDefaultOpt
|
|
|
|
),
|
2023-03-27 17:55:52 +03:00
|
|
|
_ ) ->
|
|
|
|
err ()
|
2023-03-24 16:54:06 +03:00
|
|
|
|
2023-04-19 19:26:50 +03:00
|
|
|
(* /S\ dark magic here. This relies both on internals of [Lcalc.to_ocaml] *and*
|
|
|
|
of the OCaml runtime *)
|
|
|
|
let rec runtime_to_val :
|
2023-10-12 15:41:57 +03:00
|
|
|
type d e.
|
2023-10-13 17:16:45 +03:00
|
|
|
(decl_ctx ->
|
|
|
|
((d, e, _) interpr_kind, 'm) gexpr ->
|
|
|
|
((d, e, _) interpr_kind, 'm) gexpr) ->
|
2023-04-19 19:26:50 +03:00
|
|
|
decl_ctx ->
|
|
|
|
'm mark ->
|
|
|
|
typ ->
|
|
|
|
Obj.t ->
|
2023-10-13 17:16:45 +03:00
|
|
|
(((d, e, yes) interpr_kind as 'a), 'm) gexpr =
|
2023-04-19 19:26:50 +03:00
|
|
|
fun eval_expr ctx m ty o ->
|
|
|
|
let m = Expr.map_ty (fun _ -> ty) m in
|
|
|
|
match Mark.remove ty with
|
|
|
|
| TLit TBool -> ELit (LBool (Obj.obj o)), m
|
|
|
|
| TLit TUnit -> ELit LUnit, m
|
|
|
|
| TLit TInt -> ELit (LInt (Obj.obj o)), m
|
|
|
|
| TLit TRat -> ELit (LRat (Obj.obj o)), m
|
|
|
|
| TLit TMoney -> ELit (LMoney (Obj.obj o)), m
|
|
|
|
| TLit TDate -> ELit (LDate (Obj.obj o)), m
|
|
|
|
| TLit TDuration -> ELit (LDuration (Obj.obj o)), m
|
|
|
|
| TTuple ts ->
|
|
|
|
( ETuple
|
|
|
|
(List.map2
|
|
|
|
(runtime_to_val eval_expr ctx m)
|
|
|
|
ts
|
|
|
|
(Array.to_list (Obj.obj o))),
|
|
|
|
m )
|
|
|
|
| TStruct name ->
|
|
|
|
StructName.Map.find name ctx.ctx_structs
|
|
|
|
|> StructField.Map.to_seq
|
|
|
|
|> Seq.map2
|
|
|
|
(fun o (fld, ty) -> fld, runtime_to_val eval_expr ctx m ty o)
|
|
|
|
(Array.to_seq (Obj.obj o))
|
|
|
|
|> StructField.Map.of_seq
|
|
|
|
|> fun fields -> EStruct { name; fields }, m
|
|
|
|
| TEnum name ->
|
|
|
|
(* we only use non-constant constructors of arity 1, which allows us to
|
|
|
|
always use the tag directly (ordered as declared in the constr map), and
|
|
|
|
the field 0 *)
|
2023-08-30 18:49:29 +03:00
|
|
|
let cons_map = EnumName.Map.find name ctx.ctx_enums in
|
2023-04-19 19:26:50 +03:00
|
|
|
let cons, ty =
|
|
|
|
List.nth
|
2023-08-10 17:52:39 +03:00
|
|
|
(EnumConstructor.Map.bindings cons_map)
|
2023-04-19 19:26:50 +03:00
|
|
|
(Obj.tag o - Obj.first_non_constant_constructor_tag)
|
|
|
|
in
|
|
|
|
let e = runtime_to_val eval_expr ctx m ty (Obj.field o 0) in
|
|
|
|
EInj { name; cons; e }, m
|
|
|
|
| TOption _ty -> assert false
|
2023-06-20 11:56:23 +03:00
|
|
|
| TClosureEnv -> assert false
|
2023-04-19 19:26:50 +03:00
|
|
|
| TArray ty ->
|
|
|
|
( EArray
|
|
|
|
(List.map
|
|
|
|
(runtime_to_val eval_expr ctx m ty)
|
|
|
|
(Array.to_list (Obj.obj o))),
|
|
|
|
m )
|
|
|
|
| TArrow (targs, tret) -> ECustom { obj = o; targs; tret }, m
|
2023-11-03 12:29:32 +03:00
|
|
|
| TDefault ty -> runtime_to_val eval_expr ctx m ty o
|
2023-04-19 19:26:50 +03:00
|
|
|
| TAny -> assert false
|
|
|
|
|
|
|
|
and val_to_runtime :
|
2023-10-12 15:41:57 +03:00
|
|
|
type d e.
|
2023-10-13 17:16:45 +03:00
|
|
|
(decl_ctx ->
|
|
|
|
((d, e, _) interpr_kind, 'm) gexpr ->
|
|
|
|
((d, e, _) interpr_kind, 'm) gexpr) ->
|
2023-04-19 19:26:50 +03:00
|
|
|
decl_ctx ->
|
|
|
|
typ ->
|
2023-10-13 17:16:45 +03:00
|
|
|
((d, e, _) interpr_kind, 'm) gexpr ->
|
2023-04-19 19:26:50 +03:00
|
|
|
Obj.t =
|
|
|
|
fun eval_expr ctx ty v ->
|
|
|
|
match Mark.remove ty, Mark.remove v with
|
2023-10-09 13:47:02 +03:00
|
|
|
| _, EEmptyError -> raise Runtime.EmptyError
|
2023-04-19 19:26:50 +03:00
|
|
|
| TLit TBool, ELit (LBool b) -> Obj.repr b
|
|
|
|
| TLit TUnit, ELit LUnit -> Obj.repr ()
|
|
|
|
| TLit TInt, ELit (LInt i) -> Obj.repr i
|
|
|
|
| TLit TRat, ELit (LRat r) -> Obj.repr r
|
|
|
|
| TLit TMoney, ELit (LMoney m) -> Obj.repr m
|
|
|
|
| TLit TDate, ELit (LDate t) -> Obj.repr t
|
|
|
|
| TLit TDuration, ELit (LDuration d) -> Obj.repr d
|
|
|
|
| TTuple ts, ETuple es ->
|
|
|
|
List.map2 (val_to_runtime eval_expr ctx) ts es |> Array.of_list |> Obj.repr
|
|
|
|
| TStruct name1, EStruct { name; fields } ->
|
|
|
|
assert (StructName.equal name name1);
|
|
|
|
let fld_tys = StructName.Map.find name ctx.ctx_structs in
|
|
|
|
Seq.map2
|
|
|
|
(fun (_, ty) (_, v) -> val_to_runtime eval_expr ctx ty v)
|
|
|
|
(StructField.Map.to_seq fld_tys)
|
|
|
|
(StructField.Map.to_seq fields)
|
|
|
|
|> Array.of_seq
|
|
|
|
|> Obj.repr
|
|
|
|
| TEnum name1, EInj { name; cons; e } ->
|
|
|
|
assert (EnumName.equal name name1);
|
2023-08-30 18:49:29 +03:00
|
|
|
let cons_map = EnumName.Map.find name ctx.ctx_enums in
|
2023-04-19 19:26:50 +03:00
|
|
|
let rec find_tag n = function
|
|
|
|
| [] -> assert false
|
|
|
|
| (c, ty) :: _ when EnumConstructor.equal c cons -> n, ty
|
|
|
|
| _ :: r -> find_tag (n + 1) r
|
|
|
|
in
|
|
|
|
let tag, ty =
|
|
|
|
find_tag Obj.first_non_constant_constructor_tag
|
2023-08-10 17:52:39 +03:00
|
|
|
(EnumConstructor.Map.bindings cons_map)
|
2023-04-19 19:26:50 +03:00
|
|
|
in
|
|
|
|
let o = Obj.with_tag tag (Obj.repr (Some ())) in
|
|
|
|
Obj.set_field o 0 (val_to_runtime eval_expr ctx ty e);
|
|
|
|
o
|
|
|
|
| TOption _ty, _ -> assert false
|
|
|
|
| TArray ty, EArray es ->
|
|
|
|
Array.of_list (List.map (val_to_runtime eval_expr ctx ty) es) |> Obj.repr
|
|
|
|
| TArrow (targs, tret), _ ->
|
|
|
|
let m = Mark.get v in
|
|
|
|
(* we want stg like [fun args -> val_to_runtime (eval_expr ctx (EApp (v,
|
|
|
|
args)))] but in curried form *)
|
|
|
|
let rec curry acc = function
|
|
|
|
| [] ->
|
|
|
|
let args = List.rev acc in
|
2023-12-18 12:25:00 +03:00
|
|
|
let tys = List.map (fun a -> Expr.maybe_ty (Mark.get a)) args in
|
2023-04-19 19:26:50 +03:00
|
|
|
val_to_runtime eval_expr ctx tret
|
2023-12-18 12:25:00 +03:00
|
|
|
(try eval_expr ctx (EApp { f = v; args; tys }, m)
|
2023-12-07 17:09:18 +03:00
|
|
|
with CatalaException EmptyError -> raise Runtime.EmptyError)
|
2023-04-19 19:26:50 +03:00
|
|
|
| targ :: targs ->
|
|
|
|
Obj.repr (fun x ->
|
|
|
|
curry (runtime_to_val eval_expr ctx m targ x :: acc) targs)
|
|
|
|
in
|
|
|
|
curry [] targs
|
2023-11-07 17:46:19 +03:00
|
|
|
| TDefault ty, _ -> val_to_runtime eval_expr ctx ty v
|
2023-10-09 13:47:02 +03:00
|
|
|
| _ ->
|
|
|
|
Message.raise_internal_error
|
2023-10-12 15:41:57 +03:00
|
|
|
"Could not convert value of type %a to runtime: %a" (Print.typ ctx) ty
|
2023-10-09 13:47:02 +03:00
|
|
|
Expr.format v
|
2023-04-19 19:26:50 +03:00
|
|
|
|
2023-03-27 17:55:52 +03:00
|
|
|
let rec evaluate_expr :
|
2023-04-19 19:26:50 +03:00
|
|
|
type d e.
|
2023-09-22 19:01:25 +03:00
|
|
|
decl_ctx ->
|
|
|
|
Cli.backend_lang ->
|
2023-10-13 17:16:45 +03:00
|
|
|
((d, e, yes) interpr_kind, 't) gexpr ->
|
|
|
|
((d, e, yes) interpr_kind, 't) gexpr =
|
2023-09-22 19:01:25 +03:00
|
|
|
fun ctx lang e ->
|
2023-05-17 16:44:57 +03:00
|
|
|
let m = Mark.get e in
|
2023-03-27 17:55:52 +03:00
|
|
|
let pos = Expr.mark_pos m in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2023-03-24 16:54:06 +03:00
|
|
|
| EVar _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error pos
|
2023-03-27 17:55:52 +03:00
|
|
|
"free variable found at evaluation (should not happen if term was \
|
|
|
|
well-typed)"
|
2023-08-30 18:49:29 +03:00
|
|
|
| EExternal { name } ->
|
|
|
|
let path =
|
|
|
|
match Mark.remove name with
|
|
|
|
| External_value td -> TopdefName.path td
|
|
|
|
| External_scope s -> ScopeName.path s
|
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
let ty =
|
|
|
|
try
|
|
|
|
match Mark.remove name with
|
|
|
|
| External_value name -> TopdefName.Map.find name ctx.ctx_topdefs
|
|
|
|
| External_scope name ->
|
|
|
|
let scope_info = ScopeName.Map.find name ctx.ctx_scopes in
|
|
|
|
( TArrow
|
|
|
|
( [TStruct scope_info.in_struct_name, pos],
|
|
|
|
(TStruct scope_info.out_struct_name, pos) ),
|
|
|
|
pos )
|
2023-08-15 17:57:52 +03:00
|
|
|
with TopdefName.Map.Not_found _ | ScopeName.Map.Not_found _ ->
|
2023-08-30 18:49:29 +03:00
|
|
|
Message.raise_spanned_error pos "Reference to %a could not be resolved"
|
2023-08-10 17:52:39 +03:00
|
|
|
Print.external_ref name
|
|
|
|
in
|
|
|
|
let runtime_path =
|
2023-08-30 18:49:29 +03:00
|
|
|
( List.map ModuleName.to_string path,
|
2023-08-10 17:52:39 +03:00
|
|
|
match Mark.remove name with
|
|
|
|
| External_value name -> Mark.remove (TopdefName.get_info name)
|
|
|
|
| External_scope name -> Mark.remove (ScopeName.get_info name) )
|
|
|
|
(* we have the guarantee that the two cases won't collide because they
|
|
|
|
have different capitalisation rules inherited from the input *)
|
|
|
|
in
|
|
|
|
let o = Runtime.lookup_value runtime_path in
|
2023-09-22 19:01:25 +03:00
|
|
|
runtime_to_val (fun ctx -> evaluate_expr ctx lang) ctx m ty o
|
2023-12-18 12:25:00 +03:00
|
|
|
| EApp { f = e1; args; _ } -> (
|
2023-09-22 19:01:25 +03:00
|
|
|
let e1 = evaluate_expr ctx lang e1 in
|
|
|
|
let args = List.map (evaluate_expr ctx lang) args in
|
2023-04-05 15:57:02 +03:00
|
|
|
propagate_empty_error e1
|
2023-03-27 17:55:52 +03:00
|
|
|
@@ fun e1 ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e1 with
|
2023-03-27 17:55:52 +03:00
|
|
|
| EAbs { binder; _ } ->
|
|
|
|
if Bindlib.mbinder_arity binder = List.length args then
|
2023-09-22 19:01:25 +03:00
|
|
|
evaluate_expr ctx lang
|
2023-05-17 16:44:57 +03:00
|
|
|
(Bindlib.msubst binder (Array.of_list (List.map Mark.remove args)))
|
2023-03-27 17:55:52 +03:00
|
|
|
else
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error pos
|
2023-03-27 17:55:52 +03:00
|
|
|
"wrong function call, expected %d arguments, got %d"
|
|
|
|
(Bindlib.mbinder_arity binder)
|
|
|
|
(List.length args)
|
2023-04-19 19:26:50 +03:00
|
|
|
| ECustom { obj; targs; tret } ->
|
|
|
|
(* Applies the arguments one by one to the curried form *)
|
|
|
|
List.fold_left2
|
|
|
|
(fun fobj targ arg ->
|
|
|
|
(Obj.obj fobj : Obj.t -> Obj.t)
|
2023-09-22 19:01:25 +03:00
|
|
|
(val_to_runtime (fun ctx -> evaluate_expr ctx lang) ctx targ arg))
|
2023-04-19 19:26:50 +03:00
|
|
|
obj targs args
|
|
|
|
|> Obj.obj
|
2023-09-22 19:01:25 +03:00
|
|
|
|> fun o ->
|
|
|
|
runtime_to_val (fun ctx -> evaluate_expr ctx lang) ctx m tret o
|
2023-03-27 17:55:52 +03:00
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error pos
|
2023-03-27 17:55:52 +03:00
|
|
|
"function has not been reduced to a lambda at evaluation (should not \
|
|
|
|
happen if the term was well-typed")
|
2023-12-18 12:25:00 +03:00
|
|
|
| EAppOp { op; args; _ } ->
|
|
|
|
let args = List.map (evaluate_expr ctx lang) args in
|
|
|
|
evaluate_operator (evaluate_expr ctx lang) op m lang args
|
2023-04-19 19:26:50 +03:00
|
|
|
| EAbs { binder; tys } -> Expr.unbox (Expr.eabs (Bindlib.box binder) tys m)
|
|
|
|
| ELit _ as e -> Mark.add m e
|
|
|
|
(* | EAbs _ as e -> Marked.mark m e (* these are values *) *)
|
2023-03-27 17:55:52 +03:00
|
|
|
| EStruct { fields = es; name } ->
|
|
|
|
let fields, es = List.split (StructField.Map.bindings es) in
|
2023-09-22 19:01:25 +03:00
|
|
|
let es = List.map (evaluate_expr ctx lang) es in
|
2023-04-05 15:57:02 +03:00
|
|
|
propagate_empty_error_list es
|
2023-03-27 17:55:52 +03:00
|
|
|
@@ fun es ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.add m
|
2023-03-27 17:55:52 +03:00
|
|
|
(EStruct
|
|
|
|
{
|
|
|
|
fields =
|
|
|
|
StructField.Map.of_seq
|
|
|
|
(Seq.zip (List.to_seq fields) (List.to_seq es));
|
|
|
|
name;
|
|
|
|
})
|
|
|
|
| EStructAccess { e; name = s; field } -> (
|
2023-09-22 19:01:25 +03:00
|
|
|
propagate_empty_error (evaluate_expr ctx lang e)
|
2023-03-27 17:55:52 +03:00
|
|
|
@@ fun e ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2023-03-27 17:55:52 +03:00
|
|
|
| EStruct { fields = es; name } -> (
|
|
|
|
if not (StructName.equal s name) then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2023-03-27 17:55:52 +03:00
|
|
|
[None, pos; None, Expr.pos e]
|
|
|
|
"Error during struct access: not the same structs (should not happen \
|
|
|
|
if the term was well-typed)";
|
|
|
|
match StructField.Map.find_opt field es with
|
|
|
|
| Some e' -> e'
|
|
|
|
| None ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2023-03-27 17:55:52 +03:00
|
|
|
"Invalid field access %a in struct %a (should not happen if the term \
|
|
|
|
was well-typed)"
|
2023-07-12 12:48:46 +03:00
|
|
|
StructField.format field StructName.format s)
|
2023-03-27 17:55:52 +03:00
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2023-03-27 17:55:52 +03:00
|
|
|
"The expression %a should be a struct %a but is not (should not happen \
|
|
|
|
if the term was well-typed)"
|
2023-09-22 19:01:25 +03:00
|
|
|
(Print.UserFacing.expr lang)
|
|
|
|
e StructName.format s)
|
|
|
|
| ETuple es -> Mark.add m (ETuple (List.map (evaluate_expr ctx lang) es))
|
2023-03-27 17:55:52 +03:00
|
|
|
| ETupleAccess { e = e1; index; size } -> (
|
2023-09-22 19:01:25 +03:00
|
|
|
match evaluate_expr ctx lang e1 with
|
2023-03-27 17:55:52 +03:00
|
|
|
| ETuple es, _ when List.length es = size -> List.nth es index
|
|
|
|
| e ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2023-03-27 17:55:52 +03:00
|
|
|
"The expression %a was expected to be a tuple of size %d (should not \
|
|
|
|
happen if the term was well-typed)"
|
2023-09-22 19:01:25 +03:00
|
|
|
(Print.UserFacing.expr lang)
|
|
|
|
e size)
|
2023-03-27 17:55:52 +03:00
|
|
|
| EInj { e; name; cons } ->
|
2023-09-22 19:01:25 +03:00
|
|
|
propagate_empty_error (evaluate_expr ctx lang e)
|
2023-05-17 16:44:57 +03:00
|
|
|
@@ fun e -> Mark.add m (EInj { e; name; cons })
|
2023-03-27 17:55:52 +03:00
|
|
|
| EMatch { e; cases; name } -> (
|
2023-09-22 19:01:25 +03:00
|
|
|
propagate_empty_error (evaluate_expr ctx lang e)
|
2023-03-27 17:55:52 +03:00
|
|
|
@@ fun e ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2023-03-27 17:55:52 +03:00
|
|
|
| EInj { e = e1; cons; name = name' } ->
|
|
|
|
if not (EnumName.equal name name') then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2023-03-27 17:55:52 +03:00
|
|
|
[None, Expr.pos e; None, Expr.pos e1]
|
|
|
|
"Error during match: two different enums found (should not happen if \
|
|
|
|
the term was well-typed)";
|
|
|
|
let es_n =
|
|
|
|
match EnumConstructor.Map.find_opt cons cases with
|
|
|
|
| Some es_n -> es_n
|
2023-03-20 17:17:09 +03:00
|
|
|
| None ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2023-03-27 17:55:52 +03:00
|
|
|
"sum type index error (should not happen if the term was \
|
|
|
|
well-typed)"
|
|
|
|
in
|
2023-12-18 12:25:00 +03:00
|
|
|
let ty =
|
|
|
|
EnumConstructor.Map.find cons (EnumName.Map.find name ctx.ctx_enums)
|
|
|
|
in
|
|
|
|
let new_e = Mark.add m (EApp { f = es_n; args = [e1]; tys = [ty] }) in
|
2023-09-22 19:01:25 +03:00
|
|
|
evaluate_expr ctx lang new_e
|
2023-03-27 17:55:52 +03:00
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2023-03-27 17:55:52 +03:00
|
|
|
"Expected a term having a sum type as an argument to a match (should \
|
|
|
|
not happen if the term was well-typed")
|
|
|
|
| EIfThenElse { cond; etrue; efalse } -> (
|
2023-09-22 19:01:25 +03:00
|
|
|
propagate_empty_error (evaluate_expr ctx lang cond)
|
2023-03-27 17:55:52 +03:00
|
|
|
@@ fun cond ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove cond with
|
2023-09-22 19:01:25 +03:00
|
|
|
| ELit (LBool true) -> evaluate_expr ctx lang etrue
|
|
|
|
| ELit (LBool false) -> evaluate_expr ctx lang efalse
|
2023-03-27 17:55:52 +03:00
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos cond)
|
2023-03-27 17:55:52 +03:00
|
|
|
"Expected a boolean literal for the result of this condition (should \
|
|
|
|
not happen if the term was well-typed)")
|
|
|
|
| EArray es ->
|
2023-09-22 19:01:25 +03:00
|
|
|
propagate_empty_error_list (List.map (evaluate_expr ctx lang) es)
|
2023-05-17 16:44:57 +03:00
|
|
|
@@ fun es -> Mark.add m (EArray es)
|
2023-03-27 17:55:52 +03:00
|
|
|
| EAssert e' ->
|
2023-09-22 19:01:25 +03:00
|
|
|
propagate_empty_error (evaluate_expr ctx lang e') (fun e ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
|
|
|
| ELit (LBool true) -> Mark.add m (ELit LUnit)
|
2023-09-19 15:43:47 +03:00
|
|
|
| ELit (LBool false) ->
|
|
|
|
Message.raise_spanned_error (Expr.pos e') "Assertion failed:@\n%a"
|
2023-09-22 19:04:48 +03:00
|
|
|
(Print.UserFacing.expr lang)
|
2023-09-22 19:01:25 +03:00
|
|
|
(partially_evaluate_expr_for_assertion_failure_message ctx lang
|
2023-09-19 15:43:47 +03:00
|
|
|
(Expr.skip_wrappers e'))
|
2023-03-20 17:17:09 +03:00
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e')
|
2023-03-27 17:55:52 +03:00
|
|
|
"Expected a boolean literal for the result of this assertion \
|
|
|
|
(should not happen if the term was well-typed)")
|
2023-04-19 19:26:50 +03:00
|
|
|
| ECustom _ -> e
|
2023-05-17 16:44:57 +03:00
|
|
|
| EEmptyError -> Mark.copy e EEmptyError
|
2023-04-04 13:52:26 +03:00
|
|
|
| EErrorOnEmpty e' -> (
|
2023-09-22 19:01:25 +03:00
|
|
|
match evaluate_expr ctx lang e' with
|
2023-04-04 13:52:26 +03:00
|
|
|
| EEmptyError, _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e')
|
2023-04-04 13:52:26 +03:00
|
|
|
"This variable evaluated to an empty term (no rule that defined it \
|
2023-11-08 20:25:50 +03:00
|
|
|
applied in this situation): %a"
|
|
|
|
Expr.format e
|
2023-04-04 13:52:26 +03:00
|
|
|
| e -> e)
|
|
|
|
| EDefault { excepts; just; cons } -> (
|
2023-09-22 19:01:25 +03:00
|
|
|
let excepts = List.map (evaluate_expr ctx lang) excepts in
|
2023-04-04 13:52:26 +03:00
|
|
|
let empty_count = List.length (List.filter is_empty_error excepts) in
|
|
|
|
match List.length excepts - empty_count with
|
|
|
|
| 0 -> (
|
2023-09-22 19:01:25 +03:00
|
|
|
let just = evaluate_expr ctx lang just in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove just with
|
2023-09-22 19:01:25 +03:00
|
|
|
| ELit (LBool true) -> evaluate_expr ctx lang cons
|
2023-05-17 16:44:57 +03:00
|
|
|
| ELit (LBool false) -> Mark.copy e EEmptyError
|
2023-04-04 13:52:26 +03:00
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2023-04-04 13:52:26 +03:00
|
|
|
"Default justification has not been reduced to a boolean at \
|
|
|
|
evaluation (should not happen if the term was well-typed")
|
|
|
|
| 1 -> List.find (fun sub -> not (is_empty_error sub)) excepts
|
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2023-04-04 13:52:26 +03:00
|
|
|
(List.map
|
|
|
|
(fun except ->
|
|
|
|
Some "This consequence has a valid justification:", Expr.pos except)
|
|
|
|
(List.filter (fun sub -> not (is_empty_error sub)) excepts))
|
|
|
|
"There is a conflict between multiple valid consequences for assigning \
|
|
|
|
the same variable.")
|
2023-11-08 20:25:50 +03:00
|
|
|
| EPureDefault e -> evaluate_expr ctx lang e
|
2023-04-04 13:52:26 +03:00
|
|
|
| ERaise exn -> raise (CatalaException exn)
|
|
|
|
| ECatch { body; exn; handler } -> (
|
2023-09-22 19:01:25 +03:00
|
|
|
try evaluate_expr ctx lang body
|
2023-04-04 13:52:26 +03:00
|
|
|
with CatalaException caught when Expr.equal_except caught exn ->
|
2023-09-22 19:01:25 +03:00
|
|
|
evaluate_expr ctx lang handler)
|
2023-04-04 13:52:26 +03:00
|
|
|
| _ -> .
|
2020-11-26 18:22:08 +03:00
|
|
|
|
2023-09-19 15:43:47 +03:00
|
|
|
and partially_evaluate_expr_for_assertion_failure_message :
|
|
|
|
type d e.
|
2023-09-22 19:01:25 +03:00
|
|
|
decl_ctx ->
|
|
|
|
Cli.backend_lang ->
|
2023-10-13 17:16:45 +03:00
|
|
|
((d, e, yes) interpr_kind, 't) gexpr ->
|
|
|
|
((d, e, yes) interpr_kind, 't) gexpr =
|
2023-09-22 19:01:25 +03:00
|
|
|
fun ctx lang e ->
|
2023-09-19 15:48:21 +03:00
|
|
|
(* Here we want to print an expression that explains why an assertion has
|
|
|
|
failed. Since assertions have type [bool] and are usually constructed with
|
|
|
|
comparisons and logical operators, we leave those unevaluated at the top of
|
|
|
|
the AST while evaluating everything below. This makes for a good error
|
|
|
|
message. *)
|
2023-09-19 15:43:47 +03:00
|
|
|
match Mark.remove e with
|
2023-12-18 12:25:00 +03:00
|
|
|
| EAppOp
|
|
|
|
{
|
|
|
|
args = [e1; e2];
|
|
|
|
tys;
|
|
|
|
op =
|
|
|
|
( And | Or | Xor | Eq | Lt_int_int | Lt_rat_rat | Lt_mon_mon
|
|
|
|
| Lt_dat_dat | Lt_dur_dur | Lte_int_int | Lte_rat_rat | Lte_mon_mon
|
|
|
|
| Lte_dat_dat | Lte_dur_dur | Gt_int_int | Gt_rat_rat | Gt_mon_mon
|
|
|
|
| Gt_dat_dat | Gt_dur_dur | Gte_int_int | Gte_rat_rat | Gte_mon_mon
|
|
|
|
| Gte_dat_dat | Gte_dur_dur | Eq_int_int | Eq_rat_rat | Eq_mon_mon
|
|
|
|
| Eq_dur_dur | Eq_dat_dat ) as op;
|
|
|
|
} ->
|
|
|
|
( EAppOp
|
2023-09-19 15:43:47 +03:00
|
|
|
{
|
2023-12-18 12:25:00 +03:00
|
|
|
op;
|
|
|
|
tys;
|
2023-09-19 15:43:47 +03:00
|
|
|
args =
|
|
|
|
[
|
2023-09-22 19:01:25 +03:00
|
|
|
partially_evaluate_expr_for_assertion_failure_message ctx lang e1;
|
|
|
|
partially_evaluate_expr_for_assertion_failure_message ctx lang e2;
|
2023-09-19 15:43:47 +03:00
|
|
|
];
|
|
|
|
},
|
|
|
|
Mark.get e )
|
2023-09-22 19:01:25 +03:00
|
|
|
| _ -> evaluate_expr ctx lang e
|
2023-09-19 15:43:47 +03:00
|
|
|
|
2023-12-18 13:27:49 +03:00
|
|
|
(* Typing shenanigan to add custom terms to the AST type. *)
|
2023-04-19 19:26:50 +03:00
|
|
|
let addcustom e =
|
|
|
|
let rec f :
|
|
|
|
type c d e.
|
2023-10-13 17:16:45 +03:00
|
|
|
((d, e, c) interpr_kind, 't) gexpr ->
|
|
|
|
((d, e, yes) interpr_kind, 't) gexpr boxed = function
|
2023-04-19 19:26:50 +03:00
|
|
|
| (ECustom _, _) as e -> Expr.map ~f e
|
2023-12-18 12:25:00 +03:00
|
|
|
| EAppOp { op; tys; args }, m ->
|
|
|
|
Expr.eappop ~tys ~args:(List.map f args) ~op:(Operator.translate op) m
|
2023-04-19 19:26:50 +03:00
|
|
|
| (EDefault _, _) as e -> Expr.map ~f e
|
2023-11-08 20:25:50 +03:00
|
|
|
| (EPureDefault _, _) as e -> Expr.map ~f e
|
2023-04-19 19:26:50 +03:00
|
|
|
| (EEmptyError, _) as e -> Expr.map ~f e
|
|
|
|
| (EErrorOnEmpty _, _) as e -> Expr.map ~f e
|
|
|
|
| (ECatch _, _) as e -> Expr.map ~f e
|
|
|
|
| (ERaise _, _) as e -> Expr.map ~f e
|
|
|
|
| ( ( EAssert _ | ELit _ | EApp _ | EArray _ | EVar _ | EExternal _ | EAbs _
|
|
|
|
| EIfThenElse _ | ETuple _ | ETupleAccess _ | EInj _ | EStruct _
|
|
|
|
| EStructAccess _ | EMatch _ ),
|
|
|
|
_ ) as e ->
|
|
|
|
Expr.map ~f e
|
|
|
|
| _ -> .
|
|
|
|
in
|
2023-12-18 13:27:49 +03:00
|
|
|
let open struct
|
|
|
|
external id :
|
|
|
|
(('d, 'e, 'c) interpr_kind, 't) gexpr ->
|
|
|
|
(('d, 'e, yes) interpr_kind, 't) gexpr = "%identity"
|
|
|
|
end in
|
|
|
|
if false then Expr.unbox (f e)
|
|
|
|
(* We keep the implementation as a typing proof, but bypass the AST
|
|
|
|
traversal for performance. Note that it's not completely 1-1 since the
|
|
|
|
traversal would do a reboxing of all bound variables *)
|
|
|
|
else id e
|
2023-04-19 19:26:50 +03:00
|
|
|
|
|
|
|
let delcustom e =
|
|
|
|
let rec f :
|
|
|
|
type c d e.
|
2023-10-13 17:16:45 +03:00
|
|
|
((d, e, c) interpr_kind, 't) gexpr ->
|
|
|
|
((d, e, no) interpr_kind, 't) gexpr boxed = function
|
2023-04-19 19:26:50 +03:00
|
|
|
| ECustom _, _ -> invalid_arg "Custom term remaining in evaluated term"
|
2023-12-18 12:25:00 +03:00
|
|
|
| EAppOp { op; args; tys }, m ->
|
|
|
|
Expr.eappop ~tys ~args:(List.map f args) ~op:(Operator.translate op) m
|
2023-04-19 19:26:50 +03:00
|
|
|
| (EDefault _, _) as e -> Expr.map ~f e
|
2023-11-08 20:25:50 +03:00
|
|
|
| (EPureDefault _, _) as e -> Expr.map ~f e
|
2023-04-19 19:26:50 +03:00
|
|
|
| (EEmptyError, _) as e -> Expr.map ~f e
|
|
|
|
| (EErrorOnEmpty _, _) as e -> Expr.map ~f e
|
|
|
|
| (ECatch _, _) as e -> Expr.map ~f e
|
|
|
|
| (ERaise _, _) as e -> Expr.map ~f e
|
|
|
|
| ( ( EAssert _ | ELit _ | EApp _ | EArray _ | EVar _ | EExternal _ | EAbs _
|
|
|
|
| EIfThenElse _ | ETuple _ | ETupleAccess _ | EInj _ | EStruct _
|
|
|
|
| EStructAccess _ | EMatch _ ),
|
|
|
|
_ ) as e ->
|
|
|
|
Expr.map ~f e
|
|
|
|
| _ -> .
|
|
|
|
in
|
2023-12-18 13:27:49 +03:00
|
|
|
(* /!\ don't be tempted to use the same trick here, the function does one
|
|
|
|
thing: validate at runtime that the term does not contain [ECustom]
|
|
|
|
nodes. *)
|
2023-04-19 19:26:50 +03:00
|
|
|
Expr.unbox (f e)
|
|
|
|
|
2023-04-05 17:35:12 +03:00
|
|
|
let interpret_program_lcalc p s : (Uid.MarkedString.info * ('a, 'm) gexpr) list
|
|
|
|
=
|
|
|
|
let e = Expr.unbox @@ Program.to_expr p s in
|
|
|
|
let ctx = p.decl_ctx in
|
2023-09-22 19:01:25 +03:00
|
|
|
match evaluate_expr ctx p.lang (addcustom e) with
|
2023-04-05 17:35:12 +03:00
|
|
|
| (EAbs { tys = [((TStruct s_in, _) as _targs)]; _ }, mark_e) as e -> begin
|
|
|
|
(* At this point, the interpreter seeks to execute the scope but does not
|
|
|
|
have a way to retrieve input values from the command line. [taus] contain
|
|
|
|
the types of the scope arguments. For [context] arguments, we can provide
|
|
|
|
an empty thunked term. But for [input] arguments of another type, we
|
|
|
|
cannot provide anything so we have to fail. *)
|
|
|
|
let taus = StructName.Map.find s_in ctx.ctx_structs in
|
|
|
|
let application_term =
|
2023-11-28 15:34:01 +03:00
|
|
|
let pos = Expr.mark_pos mark_e in
|
2023-04-05 17:35:12 +03:00
|
|
|
StructField.Map.map
|
|
|
|
(fun ty ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove ty with
|
2023-11-28 15:34:01 +03:00
|
|
|
| TArrow (ty_in, ((TDefault _, _) as ty_out)) ->
|
|
|
|
(* Context args may return an option if avoid_exceptions is off *)
|
|
|
|
Expr.make_abs
|
|
|
|
(Array.of_list @@ List.map (fun _ -> Var.make "_") ty_in)
|
2023-11-28 17:00:41 +03:00
|
|
|
(Expr.eraise EmptyError (Expr.with_ty mark_e ty_out))
|
2023-11-28 15:34:01 +03:00
|
|
|
ty_in (Expr.mark_pos mark_e)
|
2023-11-15 16:08:58 +03:00
|
|
|
| TArrow (ty_in, (TOption _, _)) ->
|
2023-11-28 15:34:01 +03:00
|
|
|
(* ... or an option if it is on *)
|
2023-11-15 16:08:58 +03:00
|
|
|
Expr.make_abs
|
|
|
|
(Array.of_list @@ List.map (fun _ -> Var.make "_") ty_in)
|
|
|
|
(Expr.einj ~e:(Expr.elit LUnit mark_e) ~cons:Expr.none_constr
|
|
|
|
~name:Expr.option_enum mark_e
|
|
|
|
: (_, _) boxed_gexpr)
|
2023-11-28 15:34:01 +03:00
|
|
|
ty_in pos
|
|
|
|
| TTuple ((TArrow (ty_in, (TOption _, _)), _) :: _) ->
|
|
|
|
(* ... or a closure if closure conversion is enabled *)
|
|
|
|
Expr.make_tuple
|
|
|
|
[
|
|
|
|
Expr.make_abs
|
|
|
|
(Array.of_list @@ List.map (fun _ -> Var.make "_") ty_in)
|
|
|
|
(Expr.einj ~e:(Expr.elit LUnit mark_e) ~cons:Expr.none_constr
|
|
|
|
~name:Expr.option_enum mark_e)
|
|
|
|
ty_in (Expr.mark_pos mark_e);
|
2023-12-18 12:25:00 +03:00
|
|
|
Expr.eappop ~op:Operator.ToClosureEnv
|
|
|
|
~args:[Expr.etuple [] mark_e]
|
|
|
|
~tys:[TClosureEnv, pos]
|
2023-11-28 15:34:01 +03:00
|
|
|
mark_e;
|
|
|
|
]
|
|
|
|
mark_e
|
2023-04-05 17:35:12 +03:00
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Mark.get ty)
|
2023-04-05 17:35:12 +03:00
|
|
|
"This scope needs input arguments to be executed. But the Catala \
|
|
|
|
built-in interpreter does not have a way to retrieve input \
|
|
|
|
values from the command line, so it cannot execute this scope. \
|
2023-08-17 01:46:16 +03:00
|
|
|
Please create another scope that provides the input arguments \
|
2023-11-28 15:34:01 +03:00
|
|
|
to this one and execute it instead."
|
|
|
|
Print.typ_debug ty)
|
2023-04-05 17:35:12 +03:00
|
|
|
taus
|
|
|
|
in
|
|
|
|
let to_interpret =
|
|
|
|
Expr.make_app (Expr.box e)
|
2023-08-10 17:52:39 +03:00
|
|
|
[Expr.estruct ~name:s_in ~fields:application_term mark_e]
|
2023-12-18 12:25:00 +03:00
|
|
|
[TStruct s_in, Expr.pos e]
|
2023-04-05 17:35:12 +03:00
|
|
|
(Expr.pos e)
|
|
|
|
in
|
2023-09-22 19:01:25 +03:00
|
|
|
match Mark.remove (evaluate_expr ctx p.lang (Expr.unbox to_interpret)) with
|
2023-04-05 17:35:12 +03:00
|
|
|
| EStruct { fields; _ } ->
|
|
|
|
List.map
|
2023-10-13 17:16:45 +03:00
|
|
|
(fun (fld, e) -> StructField.get_info fld, e)
|
2023-04-05 17:35:12 +03:00
|
|
|
(StructField.Map.bindings fields)
|
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2023-04-05 17:35:12 +03:00
|
|
|
"The interpretation of a program should always yield a struct \
|
|
|
|
corresponding to the scope variables"
|
|
|
|
end
|
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2023-04-05 17:35:12 +03:00
|
|
|
"The interpreter can only interpret terms starting with functions having \
|
|
|
|
thunked arguments"
|
|
|
|
|
|
|
|
(** {1 API} *)
|
|
|
|
let interpret_program_dcalc p s : (Uid.MarkedString.info * ('a, 'm) gexpr) list
|
|
|
|
=
|
|
|
|
let ctx = p.decl_ctx in
|
|
|
|
let e = Expr.unbox (Program.to_expr p s) in
|
2023-09-22 19:01:25 +03:00
|
|
|
match evaluate_expr p.decl_ctx p.lang (addcustom e) with
|
2022-11-17 19:13:35 +03:00
|
|
|
| (EAbs { tys = [((TStruct s_in, _) as _targs)]; _ }, mark_e) as e -> begin
|
2022-08-06 14:35:17 +03:00
|
|
|
(* At this point, the interpreter seeks to execute the scope but does not
|
|
|
|
have a way to retrieve input values from the command line. [taus] contain
|
2022-08-23 16:23:52 +03:00
|
|
|
the types of the scope arguments. For [context] arguments, we can provide
|
|
|
|
an empty thunked term. But for [input] arguments of another type, we
|
|
|
|
cannot provide anything so we have to fail. *)
|
2022-11-21 12:12:45 +03:00
|
|
|
let taus = StructName.Map.find s_in ctx.ctx_structs in
|
2022-06-23 15:06:11 +03:00
|
|
|
let application_term =
|
2022-11-21 12:12:45 +03:00
|
|
|
StructField.Map.map
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun ty ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove ty with
|
2022-12-02 18:42:29 +03:00
|
|
|
| TArrow (ty_in, ty_out) ->
|
|
|
|
Expr.make_abs
|
2023-02-20 19:58:29 +03:00
|
|
|
(Array.of_list @@ List.map (fun _ -> Var.make "_") ty_in)
|
2023-03-30 19:53:07 +03:00
|
|
|
(Bindlib.box EEmptyError, Expr.with_ty mark_e ty_out)
|
2023-02-20 19:58:29 +03:00
|
|
|
ty_in (Expr.mark_pos mark_e)
|
2022-08-06 14:35:17 +03:00
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Mark.get ty)
|
2022-08-06 14:35:17 +03:00
|
|
|
"This scope needs input arguments to be executed. But the Catala \
|
|
|
|
built-in interpreter does not have a way to retrieve input \
|
|
|
|
values from the command line, so it cannot execute this scope. \
|
2023-08-17 01:46:16 +03:00
|
|
|
Please create another scope that provides the input arguments \
|
|
|
|
to this one and execute it instead. ")
|
2022-06-23 15:06:11 +03:00
|
|
|
taus
|
|
|
|
in
|
2021-02-01 17:57:19 +03:00
|
|
|
let to_interpret =
|
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
|
|
|
Expr.make_app (Expr.box e)
|
2023-08-10 17:52:39 +03:00
|
|
|
[Expr.estruct ~name:s_in ~fields:application_term mark_e]
|
2023-12-18 12:25:00 +03:00
|
|
|
[TStruct s_in, Expr.pos e]
|
2022-10-03 18:07:06 +03:00
|
|
|
(Expr.pos e)
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2023-09-22 19:01:25 +03:00
|
|
|
match Mark.remove (evaluate_expr ctx p.lang (Expr.unbox to_interpret)) with
|
2022-11-17 19:13:35 +03:00
|
|
|
| EStruct { fields; _ } ->
|
|
|
|
List.map
|
2023-10-13 17:16:45 +03:00
|
|
|
(fun (fld, e) -> StructField.get_info fld, e)
|
2022-11-21 12:12:45 +03:00
|
|
|
(StructField.Map.bindings fields)
|
2020-11-26 18:22:08 +03:00
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2021-01-29 18:32:04 +03:00
|
|
|
"The interpretation of a program should always yield a struct \
|
2022-06-23 15:06:11 +03:00
|
|
|
corresponding to the scope variables"
|
|
|
|
end
|
2022-05-12 16:10:55 +03:00
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2020-11-26 18:22:08 +03:00
|
|
|
"The interpreter can only interpret terms starting with functions having \
|
|
|
|
thunked arguments"
|
2023-06-28 16:57:52 +03:00
|
|
|
|
2023-09-19 19:22:43 +03:00
|
|
|
(* Evaluation may introduce intermediate custom terms ([ECustom], pointers to
|
|
|
|
external functions), straying away from the DCalc and LCalc ASTS. [addcustom]
|
|
|
|
and [delcustom] are needed to expand and shrink the type of the terms to
|
|
|
|
reflect that. *)
|
2023-10-13 17:16:45 +03:00
|
|
|
let evaluate_expr ctx lang e = evaluate_expr ctx lang (addcustom e)
|
2023-09-19 19:22:43 +03:00
|
|
|
|
2023-09-27 12:17:26 +03:00
|
|
|
let load_runtime_modules prg =
|
2023-09-22 16:37:58 +03:00
|
|
|
let load m =
|
2023-09-27 12:17:26 +03:00
|
|
|
let obj_file =
|
2023-09-22 16:37:58 +03:00
|
|
|
Dynlink.adapt_filename
|
|
|
|
File.(
|
2023-12-01 01:53:38 +03:00
|
|
|
Pos.get_file (Mark.get (ModuleName.get_info m))
|
|
|
|
/../ ModuleName.to_string m
|
|
|
|
^ ".cmo")
|
2023-09-22 16:37:58 +03:00
|
|
|
in
|
2023-09-27 12:17:26 +03:00
|
|
|
if not (Sys.file_exists obj_file) then
|
2023-09-22 16:37:58 +03:00
|
|
|
Message.raise_spanned_error
|
|
|
|
~span_msg:(fun ppf -> Format.pp_print_string ppf "Module defined here")
|
2023-11-20 18:01:06 +03:00
|
|
|
(Mark.get (ModuleName.get_info m))
|
2023-09-27 12:17:26 +03:00
|
|
|
"Compiled OCaml object %a not found. Make sure it has been suitably \
|
|
|
|
compiled."
|
|
|
|
File.format obj_file
|
|
|
|
else
|
|
|
|
try Dynlink.loadfile obj_file
|
|
|
|
with Dynlink.Error dl_err ->
|
|
|
|
Message.raise_error
|
|
|
|
"Error loading compiled module from %a:@;<1 2>@[<hov>%a@]" File.format
|
|
|
|
obj_file Format.pp_print_text
|
|
|
|
(Dynlink.error_message dl_err)
|
2023-09-22 16:37:58 +03:00
|
|
|
in
|
2023-11-20 18:01:06 +03:00
|
|
|
let modules_list_topo =
|
|
|
|
let rec aux acc (M mtree) =
|
|
|
|
ModuleName.Map.fold
|
|
|
|
(fun mname sub acc ->
|
2023-12-01 01:53:38 +03:00
|
|
|
if List.exists (ModuleName.equal mname) acc then acc
|
|
|
|
else mname :: aux acc sub)
|
2023-11-20 18:01:06 +03:00
|
|
|
mtree acc
|
|
|
|
in
|
|
|
|
List.rev (aux [] prg.decl_ctx.ctx_modules)
|
2023-09-22 16:37:58 +03:00
|
|
|
in
|
2023-11-20 18:01:06 +03:00
|
|
|
if modules_list_topo <> [] then
|
2023-09-26 12:42:46 +03:00
|
|
|
Message.emit_debug "Loading shared modules... %a"
|
2023-11-20 18:01:06 +03:00
|
|
|
(Format.pp_print_list ~pp_sep:Format.pp_print_space ModuleName.format)
|
|
|
|
modules_list_topo;
|
|
|
|
List.iter load modules_list_topo
|