2022-03-08 17:03:14 +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:
|
2022-04-04 19:06:40 +03:00
|
|
|
Denis Merigoux <denis.merigoux@inria.fr>, Alain Delaët-Tixeuil
|
|
|
|
<alain.delaet--tixeuil@inria.fr>
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-03-08 17:03:14 +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
|
2021-02-12 19:20:14 +03:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
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
|
2021-02-12 19:20:14 +03:00
|
|
|
the License. *)
|
|
|
|
|
2022-01-19 12:54:16 +03:00
|
|
|
(** Abstract syntax tree of the default calculus intermediate representation *)
|
|
|
|
|
2021-02-12 19:20:14 +03:00
|
|
|
open Utils
|
|
|
|
module ScopeName : Uid.Id with type info = Uid.MarkedString.info
|
|
|
|
module StructName : Uid.Id with type info = Uid.MarkedString.info
|
|
|
|
module StructFieldName : Uid.Id with type info = Uid.MarkedString.info
|
|
|
|
module StructMap : Map.S with type key = StructName.t
|
|
|
|
module EnumName : Uid.Id with type info = Uid.MarkedString.info
|
|
|
|
module EnumConstructor : Uid.Id with type info = Uid.MarkedString.info
|
|
|
|
module EnumMap : Map.S with type key = EnumName.t
|
|
|
|
|
|
|
|
(** Abstract syntax tree for the default calculus *)
|
|
|
|
|
|
|
|
(** {1 Abstract syntax tree} *)
|
|
|
|
|
2022-02-04 14:33:26 +03:00
|
|
|
type typ_lit = TBool | TUnit | TInt | TRat | TMoney | TDate | TDuration
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type typ =
|
2021-02-12 19:20:14 +03:00
|
|
|
| TLit of typ_lit
|
|
|
|
| TTuple of typ Pos.marked list * StructName.t option
|
|
|
|
| TEnum of typ Pos.marked list * EnumName.t
|
|
|
|
| TArrow of typ Pos.marked * typ Pos.marked
|
|
|
|
| TArray of typ Pos.marked
|
2021-02-11 20:48:59 +03:00
|
|
|
| TAny
|
2022-02-04 14:33:26 +03:00
|
|
|
|
2021-03-05 21:16:56 +03:00
|
|
|
type date = Runtime.date
|
|
|
|
type duration = Runtime.duration
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type lit =
|
2021-02-12 19:20:14 +03:00
|
|
|
| LBool of bool
|
2021-02-11 20:48:59 +03:00
|
|
|
| LEmptyError
|
2021-03-05 21:16:56 +03:00
|
|
|
| LInt of Runtime.integer
|
|
|
|
| LRat of Runtime.decimal
|
|
|
|
| LMoney of Runtime.money
|
2021-02-11 20:48:59 +03:00
|
|
|
| LUnit
|
|
|
|
| LDate of date
|
|
|
|
| LDuration of duration
|
2021-02-12 19:20:14 +03:00
|
|
|
|
|
|
|
type op_kind =
|
|
|
|
| KInt
|
|
|
|
| KRat
|
|
|
|
| KMoney
|
|
|
|
| KDate
|
2021-07-08 17:27:46 +03:00
|
|
|
| KDuration (** All ops don't have a KDate and KDuration. *)
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type ternop = Fold
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type binop =
|
2021-02-12 19:20:14 +03:00
|
|
|
| And
|
2021-02-11 20:48:59 +03:00
|
|
|
| Or
|
2021-03-16 20:34:59 +03:00
|
|
|
| Xor
|
2021-02-11 20:48:59 +03:00
|
|
|
| Add of op_kind
|
|
|
|
| Sub of op_kind
|
|
|
|
| Mult of op_kind
|
|
|
|
| Div of op_kind
|
|
|
|
| Lt of op_kind
|
|
|
|
| Lte of op_kind
|
|
|
|
| Gt of op_kind
|
|
|
|
| Gte of op_kind
|
|
|
|
| Eq
|
|
|
|
| Neq
|
|
|
|
| Map
|
2021-07-08 17:27:46 +03:00
|
|
|
| Concat
|
2021-02-11 20:48:59 +03:00
|
|
|
| Filter
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2021-04-05 20:06:32 +03:00
|
|
|
type log_entry =
|
|
|
|
| VarDef of typ
|
2022-03-08 17:03:14 +03:00
|
|
|
(** During code generation, we need to know the type of the variable being
|
|
|
|
logged for embedding *)
|
2021-04-05 20:06:32 +03:00
|
|
|
| BeginCall
|
|
|
|
| EndCall
|
|
|
|
| PosRecordIfTrueBool
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type unop =
|
2021-02-12 19:20:14 +03:00
|
|
|
| Not
|
2021-02-11 20:48:59 +03:00
|
|
|
| Minus of op_kind
|
|
|
|
| Log of log_entry * Utils.Uid.MarkedString.info list
|
|
|
|
| Length
|
|
|
|
| IntToRat
|
|
|
|
| GetDay
|
|
|
|
| GetMonth
|
|
|
|
| GetYear
|
2022-03-17 14:30:14 +03:00
|
|
|
| RoundMoney
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-02-04 14:33:26 +03:00
|
|
|
type operator = Ternop of ternop | Binop of binop | Unop of unop
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** The expressions use the {{:https://lepigre.fr/ocaml-bindlib/} Bindlib}
|
|
|
|
library, based on higher-order abstract syntax*)
|
2021-02-11 20:48:59 +03:00
|
|
|
type expr =
|
2021-02-12 19:20:14 +03:00
|
|
|
| EVar of expr Bindlib.var Pos.marked
|
|
|
|
| ETuple of expr Pos.marked list * StructName.t option
|
|
|
|
(** The [MarkedString.info] is the former struct field name*)
|
2022-03-08 17:03:14 +03:00
|
|
|
| ETupleAccess of
|
|
|
|
expr Pos.marked * int * StructName.t option * typ Pos.marked list
|
2021-02-12 19:20:14 +03:00
|
|
|
(** The [MarkedString.info] is the former struct field name *)
|
|
|
|
| EInj of expr Pos.marked * int * EnumName.t * typ Pos.marked list
|
|
|
|
(** The [MarkedString.info] is the former enum case name *)
|
|
|
|
| EMatch of expr Pos.marked * expr Pos.marked list * EnumName.t
|
|
|
|
(** The [MarkedString.info] is the former enum case name *)
|
|
|
|
| EArray of expr Pos.marked list
|
2021-02-11 20:48:59 +03:00
|
|
|
| ELit of lit
|
2022-03-08 17:03:14 +03:00
|
|
|
| EAbs of
|
|
|
|
((expr, expr Pos.marked) Bindlib.mbinder[@opaque]) Pos.marked
|
|
|
|
* typ Pos.marked list
|
2021-02-12 19:20:14 +03:00
|
|
|
| EApp of expr Pos.marked * expr Pos.marked list
|
|
|
|
| EAssert of expr Pos.marked
|
2021-02-11 20:48:59 +03:00
|
|
|
| EOp of operator
|
2021-02-12 19:20:14 +03:00
|
|
|
| EDefault of expr Pos.marked list * expr Pos.marked * expr Pos.marked
|
|
|
|
| EIfThenElse of expr Pos.marked * expr Pos.marked * expr Pos.marked
|
2021-04-03 16:07:49 +03:00
|
|
|
| ErrorOnEmpty of expr Pos.marked
|
2022-02-04 14:33:26 +03:00
|
|
|
|
2021-02-12 19:20:14 +03:00
|
|
|
type struct_ctx = (StructFieldName.t * typ Pos.marked) list StructMap.t
|
|
|
|
type enum_ctx = (EnumConstructor.t * typ Pos.marked) list EnumMap.t
|
|
|
|
type decl_ctx = { ctx_enums : enum_ctx; ctx_structs : struct_ctx }
|
2021-12-09 01:56:03 +03:00
|
|
|
type binder = (expr, expr Pos.marked) Bindlib.binder
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** This kind annotation signals that the let-binding respects a structural
|
|
|
|
invariant. These invariants concern the shape of the expression in the
|
|
|
|
let-binding, and are documented below. *)
|
2021-12-09 01:56:03 +03:00
|
|
|
type scope_let_kind =
|
2021-12-09 20:42:36 +03:00
|
|
|
| DestructuringInputStruct (** [let x = input.field]*)
|
|
|
|
| ScopeVarDefinition (** [let x = error_on_empty e]*)
|
2022-02-09 20:06:03 +03:00
|
|
|
| SubScopeVarDefinition
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [let s.x = fun _ -> e] or [let s.x = error_on_empty e] for input-only
|
|
|
|
subscope variables. *)
|
2021-12-09 20:42:36 +03:00
|
|
|
| CallingSubScope (** [let result = s ({ x = s.x; y = s.x; ...}) ]*)
|
|
|
|
| DestructuringSubScopeResults (** [let s.x = result.x ]**)
|
|
|
|
| Assertion (** [let _ = assert e]*)
|
2021-12-09 01:56:03 +03:00
|
|
|
|
|
|
|
type scope_let = {
|
|
|
|
scope_let_kind : scope_let_kind;
|
2022-04-02 13:29:43 +03:00
|
|
|
scope_let_typ : typ Utils.Pos.marked;
|
|
|
|
scope_let_expr : expr Utils.Pos.marked;
|
|
|
|
scope_let_next : (expr, scope_body_expr) Bindlib.binder;
|
|
|
|
scope_let_pos : Utils.Pos.t;
|
2021-12-09 01:56:03 +03:00
|
|
|
}
|
2022-04-02 13:29:43 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** A scope let-binding has all the information necessary to make a proper
|
|
|
|
let-binding expression, plus an annotation for the kind of the let-binding
|
|
|
|
that comes from the compilation of a {!module: Scopelang.Ast} statement. *)
|
2022-04-02 13:29:43 +03:00
|
|
|
and scope_body_expr = Result of expr Utils.Pos.marked | ScopeLet of scope_let
|
2021-12-09 01:56:03 +03:00
|
|
|
|
2021-12-10 00:59:39 +03:00
|
|
|
type scope_body = {
|
2021-12-10 18:30:36 +03:00
|
|
|
scope_body_input_struct : StructName.t;
|
|
|
|
scope_body_output_struct : StructName.t;
|
2022-04-02 13:29:43 +03:00
|
|
|
scope_body_expr : (expr, scope_body_expr) Bindlib.binder;
|
2021-12-10 00:59:39 +03:00
|
|
|
}
|
2022-03-08 17:03:14 +03:00
|
|
|
(** Instead of being a single expression, we give a little more ad-hoc structure
|
|
|
|
to the scope body by decomposing it in an ordered list of let-bindings, and
|
2022-04-02 13:29:43 +03:00
|
|
|
a result expression that uses the let-binded variables. The first binder is
|
|
|
|
the argument of type [scope_body_input_struct]. *)
|
2021-12-09 01:56:03 +03:00
|
|
|
|
2022-04-02 13:29:43 +03:00
|
|
|
type scope_def = {
|
|
|
|
scope_name : ScopeName.t;
|
|
|
|
scope_body : scope_body;
|
|
|
|
scope_next : (expr, scopes) Bindlib.binder;
|
2022-03-08 17:03:14 +03:00
|
|
|
}
|
2021-12-09 01:56:03 +03:00
|
|
|
|
2022-04-02 13:29:43 +03:00
|
|
|
(** Finally, we do the same transformation for the whole program for the kinded
|
|
|
|
lets. This permit us to use bindlib variables for scopes names. *)
|
|
|
|
and scopes = Nil | ScopeDef of scope_def
|
|
|
|
|
|
|
|
type program = { decl_ctx : decl_ctx; scopes : scopes }
|
|
|
|
|
2022-02-02 12:30:39 +03:00
|
|
|
(** {1 Helpers} *)
|
|
|
|
|
2022-04-02 13:29:43 +03:00
|
|
|
(**{2 Program traversal}*)
|
|
|
|
|
|
|
|
(** Be careful when using these traversal functions, as the bound variables they
|
|
|
|
open will be different at each traversal. *)
|
|
|
|
|
|
|
|
val fold_scope_lets :
|
|
|
|
f:('a -> scope_let -> 'a) -> init:'a -> scope_body_expr -> 'a
|
|
|
|
|
|
|
|
val fold_scope_defs : f:('a -> scope_def -> 'a) -> init:'a -> scopes -> 'a
|
|
|
|
|
2022-02-02 12:30:39 +03:00
|
|
|
(** {2 Variables}*)
|
2021-02-12 19:20:14 +03:00
|
|
|
|
|
|
|
module Var : sig
|
|
|
|
type t = expr Bindlib.var
|
2021-02-12 20:16:06 +03:00
|
|
|
|
2021-02-12 19:20:14 +03:00
|
|
|
val make : string Pos.marked -> t
|
|
|
|
val compare : t -> t -> int
|
|
|
|
end
|
|
|
|
|
|
|
|
module VarMap : Map.S with type key = Var.t
|
2022-04-02 13:29:43 +03:00
|
|
|
module VarSet : Set.S with type elt = Var.t
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-04-02 13:29:43 +03:00
|
|
|
val free_vars_expr : expr Pos.marked -> VarSet.t
|
|
|
|
val free_vars_scope_body_expr : scope_body_expr -> VarSet.t
|
|
|
|
val free_vars_scope_body : scope_body -> VarSet.t
|
|
|
|
val free_vars_scopes : scopes -> VarSet.t
|
2022-02-04 11:24:51 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type vars = expr Bindlib.mvar
|
2021-02-12 19:20:14 +03:00
|
|
|
|
|
|
|
val make_var : Var.t Pos.marked -> expr Pos.marked Bindlib.box
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val make_abs :
|
2021-02-12 20:16:06 +03:00
|
|
|
vars ->
|
|
|
|
expr Pos.marked Bindlib.box ->
|
|
|
|
Pos.t ->
|
|
|
|
typ Pos.marked list ->
|
|
|
|
Pos.t ->
|
2021-02-12 19:20:14 +03:00
|
|
|
expr Pos.marked Bindlib.box
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val make_app :
|
2021-02-12 20:16:06 +03:00
|
|
|
expr Pos.marked Bindlib.box ->
|
|
|
|
expr Pos.marked Bindlib.box list ->
|
|
|
|
Pos.t ->
|
2021-02-12 19:20:14 +03:00
|
|
|
expr Pos.marked Bindlib.box
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val make_let_in :
|
2021-02-12 20:16:06 +03:00
|
|
|
Var.t ->
|
|
|
|
typ Pos.marked ->
|
|
|
|
expr Pos.marked Bindlib.box ->
|
|
|
|
expr Pos.marked Bindlib.box ->
|
2021-10-28 16:24:39 +03:00
|
|
|
Pos.t ->
|
2021-02-12 19:20:14 +03:00
|
|
|
expr Pos.marked Bindlib.box
|
|
|
|
|
2022-02-02 12:30:39 +03:00
|
|
|
(**{2 Other}*)
|
|
|
|
|
2022-01-28 19:31:31 +03:00
|
|
|
val empty_thunked_term : expr Pos.marked
|
2022-02-02 12:30:39 +03:00
|
|
|
val is_value : expr Pos.marked -> bool
|
|
|
|
|
2022-03-08 19:35:08 +03:00
|
|
|
val equal_exprs : expr Pos.marked -> expr Pos.marked -> bool
|
|
|
|
(** Determines if two expressions are equal, omitting their position information *)
|
|
|
|
|
2021-12-10 18:30:36 +03:00
|
|
|
(** {1 AST manipulation helpers}*)
|
2021-12-10 00:59:39 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
val build_whole_scope_expr :
|
|
|
|
decl_ctx -> scope_body -> Pos.t -> expr Pos.marked Bindlib.box
|
|
|
|
(** Usage: [build_whole_scope_expr ctx body scope_position] where
|
|
|
|
[scope_position] corresponds to the line of the scope declaration for
|
|
|
|
instance. *)
|
2021-12-10 00:59:39 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
val build_whole_program_expr :
|
|
|
|
program -> ScopeName.t -> expr Pos.marked Bindlib.box
|
|
|
|
(** Usage: [build_whole_program_expr program main_scope] builds an expression
|
|
|
|
corresponding to the main program and returning the main scope as a
|
|
|
|
function. *)
|
2022-01-07 20:36:56 +03:00
|
|
|
|
|
|
|
val expr_size : expr Pos.marked -> int
|
|
|
|
(** Used by the optimizer to know when to stop *)
|