catala/compiler/dcalc/ast.mli

365 lines
11 KiB
OCaml
Raw Normal View History

(* 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
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
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
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
| TAny
2022-02-04 14:33:26 +03:00
type date = Runtime.date
type duration = Runtime.duration
2021-02-12 19:20:14 +03:00
type lit =
2021-02-12 19:20:14 +03:00
| LBool of bool
| LEmptyError
| LInt of Runtime.integer
| LRat of Runtime.decimal
| LMoney of Runtime.money
| LUnit
| LDate of date
| LDuration of duration
2021-02-12 19:20:14 +03:00
type op_kind =
| KInt
| KRat
| KMoney
| KDate
| KDuration (** All ops don't have a KDate and KDuration. *)
2021-02-12 19:20:14 +03:00
type ternop = Fold
2021-02-12 19:20:14 +03:00
type binop =
2021-02-12 19:20:14 +03:00
| And
| Or
2021-03-16 20:34:59 +03:00
| Xor
| 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
| Concat
| Filter
2021-02-12 19:20:14 +03:00
2021-04-05 20:06:32 +03:00
type log_entry =
| VarDef of typ
(** 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
type unop =
2021-02-12 19:20:14 +03:00
| Not
| 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
(** The expressions use the {{:https://lepigre.fr/ocaml-bindlib/} Bindlib}
library, based on higher-order abstract syntax*)
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*)
| 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
| ELit of lit
| 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
| 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
| 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 }
type binder = (expr, expr Pos.marked) Bindlib.binder
(** 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. *)
type scope_let_kind =
2021-12-09 20:42:36 +03:00
| DestructuringInputStruct (** [let x = input.field]*)
| ScopeVarDefinition (** [let x = error_on_empty e]*)
| SubScopeVarDefinition
(** [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]*)
2022-04-12 11:53:07 +03:00
type 'expr scope_let = {
scope_let_kind : scope_let_kind;
scope_let_typ : typ Utils.Pos.marked;
2022-04-12 11:53:07 +03:00
scope_let_expr : 'expr Utils.Pos.marked;
scope_let_next : ('expr, 'expr scope_body_expr) Bindlib.binder;
scope_let_pos : Utils.Pos.t;
}
2022-04-12 11:53:07 +03:00
(** This type is parametrized by the expression type so it can be reused in
later intermediate representations. *)
(** 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-12 11:53:07 +03:00
and 'expr scope_body_expr =
| Result of 'expr Utils.Pos.marked
| ScopeLet of 'expr scope_let
2022-04-12 11:53:07 +03:00
type 'expr scope_body = {
scope_body_input_struct : StructName.t;
scope_body_output_struct : StructName.t;
2022-04-12 11:53:07 +03:00
scope_body_expr : ('expr, 'expr scope_body_expr) Bindlib.binder;
2021-12-10 00:59:39 +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
a result expression that uses the let-binded variables. The first binder is
the argument of type [scope_body_input_struct]. *)
2022-04-12 11:53:07 +03:00
type 'expr scope_def = {
scope_name : ScopeName.t;
2022-04-12 11:53:07 +03:00
scope_body : 'expr scope_body;
scope_next : (expr, 'expr scopes) Bindlib.binder;
}
(** Finally, we do the same transformation for the whole program for the kinded
lets. This permit us to use bindlib variables for scopes names. *)
2022-04-12 11:53:07 +03:00
and 'a scopes = Nil | ScopeDef of 'a scope_def
2022-04-12 11:53:07 +03:00
type program = { decl_ctx : decl_ctx; scopes : expr scopes }
(** {1 Helpers} *)
2022-04-06 10:35:07 +03:00
(** {2 Boxed constructors}*)
val evar : expr Bindlib.var -> Pos.t -> expr Pos.marked Bindlib.box
val etuple :
expr Pos.marked Bindlib.box list ->
StructName.t option ->
Pos.t ->
expr Pos.marked Bindlib.box
val etupleaccess :
expr Pos.marked Bindlib.box ->
int ->
StructName.t option ->
typ Pos.marked list ->
Pos.t ->
expr Pos.marked Bindlib.box
val einj :
expr Pos.marked Bindlib.box ->
int ->
EnumName.t ->
typ Pos.marked list ->
Pos.t ->
expr Pos.marked Bindlib.box
val ematch :
expr Pos.marked Bindlib.box ->
expr Pos.marked Bindlib.box list ->
EnumName.t ->
Pos.t ->
expr Pos.marked Bindlib.box
val earray :
expr Pos.marked Bindlib.box list -> Pos.t -> expr Pos.marked Bindlib.box
val elit : lit -> Pos.t -> expr Pos.marked Bindlib.box
val eabs :
(expr, expr Pos.marked) Bindlib.mbinder Bindlib.box ->
Pos.t ->
typ Pos.marked list ->
Pos.t ->
expr Pos.marked Bindlib.box
val eapp :
expr Pos.marked Bindlib.box ->
expr Pos.marked Bindlib.box list ->
Pos.t ->
expr Pos.marked Bindlib.box
val eassert :
expr Pos.marked Bindlib.box -> Pos.t -> expr Pos.marked Bindlib.box
val eop : operator -> Pos.t -> expr Pos.marked Bindlib.box
val edefault :
expr Pos.marked Bindlib.box list ->
expr Pos.marked Bindlib.box ->
expr Pos.marked Bindlib.box ->
Pos.t ->
expr Pos.marked Bindlib.box
val eifthenelse :
expr Pos.marked Bindlib.box ->
expr Pos.marked Bindlib.box ->
expr Pos.marked Bindlib.box ->
Pos.t ->
expr Pos.marked Bindlib.box
val eerroronempty :
expr Pos.marked Bindlib.box -> Pos.t -> expr Pos.marked Bindlib.box
(**{2 Program traversal}*)
(** Be careful when using these traversal functions, as the bound variables they
open will be different at each traversal. *)
2022-04-12 12:51:33 +03:00
val map_expr :
'a ->
f:('a -> expr Pos.marked -> expr Pos.marked Bindlib.box) ->
expr Pos.marked ->
expr Pos.marked Bindlib.box
(** If you want to apply a map transform to an expression, you can save up
writing a painful match over all the cases of the AST. For instance, if you
want to remove all errors on empty, you can write
{[
let remove_error_empty =
let rec f () e =
match Pos.unmark e with
| ErrorOnEmpty e1 -> map_expr () f e1
| _ -> map_expr () f e
in
f () e
]}
The first argument of map_expr is an optional context that you can carry
around during your map traversal. *)
val fold_scope_lets :
2022-04-12 11:53:07 +03:00
f:('a -> 'expr scope_let -> 'a) -> init:'a -> 'expr scope_body_expr -> 'a
2022-04-12 11:53:07 +03:00
val fold_scope_defs :
f:('a -> 'expr scope_def -> 'a) -> init:'a -> 'expr scopes -> 'a
(** {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
module VarSet : Set.S with type elt = Var.t
2021-02-12 19:20:14 +03:00
val free_vars_expr : expr Pos.marked -> VarSet.t
2022-04-12 11:53:07 +03:00
val free_vars_scope_body_expr : expr scope_body_expr -> VarSet.t
val free_vars_scope_body : expr scope_body -> VarSet.t
val free_vars_scopes : expr scopes -> VarSet.t
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
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
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
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 ->
Pos.t ->
2021-02-12 19:20:14 +03:00
expr Pos.marked Bindlib.box
(**{2 Other}*)
val empty_thunked_term : expr Pos.marked
val is_value : expr Pos.marked -> bool
val equal_exprs : expr Pos.marked -> expr Pos.marked -> bool
(** Determines if two expressions are equal, omitting their position information *)
(** {1 AST manipulation helpers}*)
2021-12-10 00:59:39 +03:00
val build_whole_scope_expr :
2022-04-12 11:53:07 +03:00
decl_ctx -> expr 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
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 *)
2022-04-12 12:14:39 +03:00
val remove_logging_calls : expr Pos.marked -> expr Pos.marked Bindlib.box
(** Removes all calls to [Log] unary operators in the AST, replacing them by
their argument. *)