2021-02-12 19:20: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:
|
|
|
|
Denis Merigoux <denis.merigoux@inria.fr>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
License for the specific language governing permissions and limitations under
|
|
|
|
the License. *)
|
|
|
|
|
|
|
|
(** Abstract syntax tree of the scope language *)
|
|
|
|
|
|
|
|
open Utils
|
|
|
|
|
|
|
|
(** {1 Identifiers} *)
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
module ScopeName = Dcalc.Ast.ScopeName
|
2021-02-12 19:20:14 +03:00
|
|
|
module ScopeNameSet : Set.S with type elt = ScopeName.t
|
|
|
|
module ScopeMap : Map.S with type key = ScopeName.t
|
|
|
|
module SubScopeName : Uid.Id with type info = Uid.MarkedString.info
|
|
|
|
module SubScopeNameSet : Set.S with type elt = SubScopeName.t
|
|
|
|
module SubScopeMap : Map.S with type key = SubScopeName.t
|
|
|
|
module ScopeVar : Uid.Id with type info = Uid.MarkedString.info
|
|
|
|
module ScopeVarSet : Set.S with type elt = ScopeVar.t
|
|
|
|
module ScopeVarMap : Map.S with type key = ScopeVar.t
|
2021-02-11 20:48:59 +03:00
|
|
|
module StructName = Dcalc.Ast.StructName
|
|
|
|
module StructMap = Dcalc.Ast.StructMap
|
|
|
|
module StructFieldName = Dcalc.Ast.StructFieldName
|
2021-02-12 19:20:14 +03:00
|
|
|
module StructFieldMap : Map.S with type key = StructFieldName.t
|
|
|
|
|
2022-03-01 22:41:01 +03:00
|
|
|
module StructFieldMapLift : sig
|
|
|
|
val lift_box :
|
|
|
|
'a Bindlib.box StructFieldMap.t -> 'a StructFieldMap.t Bindlib.box
|
|
|
|
end
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
module EnumName = Dcalc.Ast.EnumName
|
|
|
|
module EnumMap = Dcalc.Ast.EnumMap
|
|
|
|
module EnumConstructor = Dcalc.Ast.EnumConstructor
|
2021-02-12 19:20:14 +03:00
|
|
|
module EnumConstructorMap : Map.S with type key = EnumConstructor.t
|
|
|
|
|
2022-03-01 22:41:01 +03:00
|
|
|
module EnumConstructorMapLift : sig
|
|
|
|
val lift_box :
|
|
|
|
'a Bindlib.box EnumConstructorMap.t -> 'a EnumConstructorMap.t Bindlib.box
|
|
|
|
end
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type location =
|
2021-02-12 19:20:14 +03:00
|
|
|
| ScopeVar of ScopeVar.t Pos.marked
|
|
|
|
| SubScopeVar of
|
|
|
|
ScopeName.t * SubScopeName.t Pos.marked * ScopeVar.t Pos.marked
|
|
|
|
|
|
|
|
module LocationSet : Set.S with type elt = location Pos.marked
|
|
|
|
|
|
|
|
(** {1 Abstract syntax tree} *)
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type typ =
|
2021-02-12 19:20:14 +03:00
|
|
|
| TLit of Dcalc.Ast.typ_lit
|
2021-02-11 20:48:59 +03:00
|
|
|
| TStruct of StructName.t
|
|
|
|
| TEnum of EnumName.t
|
2021-02-12 19:20:14 +03:00
|
|
|
| TArrow of typ Pos.marked * typ Pos.marked
|
2021-02-11 20:48:59 +03:00
|
|
|
| TArray of typ
|
|
|
|
| TAny
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-05-25 15:41:04 +03:00
|
|
|
module Typ : Set.OrderedType with type t = typ
|
|
|
|
|
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*)
|
2021-02-11 20:48:59 +03:00
|
|
|
type expr =
|
2021-02-12 19:20:14 +03:00
|
|
|
| ELocation of location
|
|
|
|
| EVar of expr Bindlib.var Pos.marked
|
|
|
|
| EStruct of StructName.t * expr Pos.marked StructFieldMap.t
|
|
|
|
| EStructAccess of expr Pos.marked * StructFieldName.t * StructName.t
|
|
|
|
| EEnumInj of expr Pos.marked * EnumConstructor.t * EnumName.t
|
|
|
|
| EMatch of
|
|
|
|
expr Pos.marked * EnumName.t * expr Pos.marked EnumConstructorMap.t
|
2021-02-11 20:48:59 +03:00
|
|
|
| ELit of Dcalc.Ast.lit
|
2021-04-03 12:49:13 +03:00
|
|
|
| EAbs of
|
|
|
|
(expr, expr Pos.marked) Bindlib.mbinder Pos.marked * typ Pos.marked list
|
2021-02-12 19:20:14 +03:00
|
|
|
| EApp of expr Pos.marked * expr Pos.marked list
|
2021-02-11 20:48:59 +03:00
|
|
|
| EOp of Dcalc.Ast.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
|
|
|
|
| EArray of expr Pos.marked list
|
2021-04-03 16:07:49 +03:00
|
|
|
| ErrorOnEmpty of expr Pos.marked
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-05-25 15:41:04 +03:00
|
|
|
module Expr : Set.OrderedType with type t = expr
|
|
|
|
module ExprMap : Map.S with type key = expr
|
|
|
|
|
2021-02-12 19:20:14 +03:00
|
|
|
val locations_used : expr Pos.marked -> LocationSet.t
|
|
|
|
|
2022-02-14 12:29:17 +03:00
|
|
|
(** This type characterizes the three levels of visibility for a given scope
|
|
|
|
variable with regards to the scope's input and possible redefinitions inside
|
|
|
|
the scope.. *)
|
2022-02-07 12:30:36 +03:00
|
|
|
type io_input =
|
2022-02-14 12:29:17 +03:00
|
|
|
| NoInput
|
|
|
|
(** For an internal variable defined only in the scope, and does not
|
|
|
|
appear in the input. *)
|
|
|
|
| OnlyInput
|
|
|
|
(** For variables that should not be redefined in the scope, because they
|
|
|
|
appear in the input. *)
|
|
|
|
| Reentrant
|
|
|
|
(** For variables defined in the scope that can also be redefined by the
|
|
|
|
caller as they appear in the input. *)
|
2022-02-07 12:30:36 +03:00
|
|
|
|
|
|
|
type io = {
|
2022-02-14 12:29:17 +03:00
|
|
|
io_output : bool Pos.marked;
|
|
|
|
(** [true] is present in the output of the scope. *)
|
|
|
|
io_input : io_input Pos.marked;
|
2022-01-28 19:31:31 +03:00
|
|
|
}
|
2022-02-14 12:29:17 +03:00
|
|
|
(** Characterization of the input/output status of a scope variable. *)
|
2022-01-28 19:31:31 +03:00
|
|
|
|
2022-02-04 16:34:25 +03:00
|
|
|
type rule =
|
2022-02-10 12:09:58 +03:00
|
|
|
| Definition of location Pos.marked * typ Pos.marked * io * expr Pos.marked
|
2022-02-04 16:34:25 +03:00
|
|
|
| Assertion of expr Pos.marked
|
|
|
|
| Call of ScopeName.t * SubScopeName.t
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type scope_decl = {
|
|
|
|
scope_decl_name : ScopeName.t;
|
2022-02-07 12:30:36 +03:00
|
|
|
scope_sig : (typ Pos.marked * io) ScopeVarMap.t;
|
2021-02-11 20:48:59 +03:00
|
|
|
scope_decl_rules : rule list;
|
|
|
|
}
|
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
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type program = {
|
|
|
|
program_scopes : scope_decl ScopeMap.t;
|
|
|
|
program_enums : enum_ctx;
|
|
|
|
program_structs : struct_ctx;
|
|
|
|
}
|
2021-02-12 19:20:14 +03:00
|
|
|
|
|
|
|
(** {1 Variable helpers} *)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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 19:20:14 +03:00
|
|
|
vars ->
|
|
|
|
expr Pos.marked Bindlib.box ->
|
|
|
|
Pos.t ->
|
|
|
|
typ Pos.marked list ->
|
|
|
|
Pos.t ->
|
|
|
|
expr Pos.marked Bindlib.box
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val make_app :
|
2021-02-12 19:20:14 +03:00
|
|
|
expr Pos.marked Bindlib.box ->
|
|
|
|
expr Pos.marked Bindlib.box list ->
|
|
|
|
Pos.t ->
|
|
|
|
expr Pos.marked Bindlib.box
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val make_let_in :
|
2021-02-12 19:20:14 +03:00
|
|
|
Var.t ->
|
|
|
|
typ Pos.marked ->
|
|
|
|
expr Pos.marked Bindlib.box ->
|
|
|
|
expr Pos.marked Bindlib.box ->
|
|
|
|
expr Pos.marked Bindlib.box
|
2022-05-25 15:47:23 +03:00
|
|
|
|
|
|
|
val make_default :
|
|
|
|
?pos:Pos.t ->
|
|
|
|
expr Pos.marked list ->
|
|
|
|
expr Pos.marked ->
|
|
|
|
expr Pos.marked ->
|
|
|
|
expr Pos.marked
|
|
|
|
(** [make_default ?pos exceptions just cons] builds the equivalent to an
|
|
|
|
[EDefault] term, while avoiding redundant nested constructions. The position
|
|
|
|
is extracted from [just] by default. *)
|