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 =
|
2022-05-30 12:20:48 +03:00
|
|
|
| ScopeVar of ScopeVar.t Marked.pos
|
2021-02-12 19:20:14 +03:00
|
|
|
| SubScopeVar of
|
2022-05-30 12:20:48 +03:00
|
|
|
ScopeName.t * SubScopeName.t Marked.pos * ScopeVar.t Marked.pos
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
module LocationSet : Set.S with type elt = location Marked.pos
|
2021-02-12 19:20:14 +03:00
|
|
|
|
|
|
|
(** {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
|
2022-05-30 12:20:48 +03:00
|
|
|
| TArrow of typ Marked.pos * typ Marked.pos
|
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
|
|
|
|
|
2022-06-03 17:40:03 +03:00
|
|
|
type marked_expr = expr Marked.pos
|
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*)
|
2022-06-03 17:40:03 +03:00
|
|
|
|
|
|
|
and expr =
|
2021-02-12 19:20:14 +03:00
|
|
|
| ELocation of location
|
2022-06-03 17:40:03 +03:00
|
|
|
| EVar of expr Bindlib.var
|
|
|
|
| EStruct of StructName.t * marked_expr StructFieldMap.t
|
|
|
|
| EStructAccess of marked_expr * StructFieldName.t * StructName.t
|
|
|
|
| EEnumInj of marked_expr * EnumConstructor.t * EnumName.t
|
|
|
|
| EMatch of marked_expr * EnumName.t * marked_expr EnumConstructorMap.t
|
2021-02-11 20:48:59 +03:00
|
|
|
| ELit of Dcalc.Ast.lit
|
2022-06-03 17:40:03 +03:00
|
|
|
| EAbs of (expr, marked_expr) Bindlib.mbinder * typ Marked.pos list
|
|
|
|
| EApp of marked_expr * marked_expr list
|
2021-02-11 20:48:59 +03:00
|
|
|
| EOp of Dcalc.Ast.operator
|
2022-06-03 17:40:03 +03:00
|
|
|
| EDefault of marked_expr list * marked_expr * marked_expr
|
|
|
|
| EIfThenElse of marked_expr * marked_expr * marked_expr
|
|
|
|
| EArray of marked_expr list
|
|
|
|
| ErrorOnEmpty of marked_expr
|
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
|
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
val locations_used : expr Marked.pos -> LocationSet.t
|
2021-02-12 19:20:14 +03:00
|
|
|
|
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-05-30 12:20:48 +03:00
|
|
|
io_output : bool Marked.pos;
|
2022-02-14 12:29:17 +03:00
|
|
|
(** [true] is present in the output of the scope. *)
|
2022-05-30 12:20:48 +03:00
|
|
|
io_input : io_input Marked.pos;
|
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-05-30 12:20:48 +03:00
|
|
|
| Definition of location Marked.pos * typ Marked.pos * io * expr Marked.pos
|
|
|
|
| Assertion of expr Marked.pos
|
2022-02-04 16:34:25 +03:00
|
|
|
| Call of ScopeName.t * SubScopeName.t
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type scope_decl = {
|
|
|
|
scope_decl_name : ScopeName.t;
|
2022-05-30 12:20:48 +03:00
|
|
|
scope_sig : (typ Marked.pos * io) ScopeVarMap.t;
|
2021-02-11 20:48:59 +03:00
|
|
|
scope_decl_rules : rule list;
|
|
|
|
}
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
type struct_ctx = (StructFieldName.t * typ Marked.pos) list StructMap.t
|
|
|
|
type enum_ctx = (EnumConstructor.t * typ Marked.pos) list EnumMap.t
|
2021-02-12 19:20:14 +03:00
|
|
|
|
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
|
|
|
|
2022-06-03 17:40:03 +03:00
|
|
|
val make : string -> t
|
2021-02-12 19:20:14 +03:00
|
|
|
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
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
val make_var : Var.t Marked.pos -> expr Marked.pos Bindlib.box
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val make_abs :
|
2021-02-12 19:20:14 +03:00
|
|
|
vars ->
|
2022-05-30 12:20:48 +03:00
|
|
|
expr Marked.pos Bindlib.box ->
|
|
|
|
typ Marked.pos list ->
|
2021-02-12 19:20:14 +03:00
|
|
|
Pos.t ->
|
2022-05-30 12:20:48 +03:00
|
|
|
expr Marked.pos Bindlib.box
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val make_app :
|
2022-05-30 12:20:48 +03:00
|
|
|
expr Marked.pos Bindlib.box ->
|
|
|
|
expr Marked.pos Bindlib.box list ->
|
2021-02-12 19:20:14 +03:00
|
|
|
Pos.t ->
|
2022-05-30 12:20:48 +03:00
|
|
|
expr Marked.pos Bindlib.box
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val make_let_in :
|
2021-02-12 19:20:14 +03:00
|
|
|
Var.t ->
|
2022-05-30 12:20:48 +03:00
|
|
|
typ Marked.pos ->
|
|
|
|
expr Marked.pos Bindlib.box ->
|
|
|
|
expr Marked.pos Bindlib.box ->
|
|
|
|
expr Marked.pos Bindlib.box
|
2022-05-25 15:47:23 +03:00
|
|
|
|
|
|
|
val make_default :
|
|
|
|
?pos:Pos.t ->
|
2022-05-30 12:20:48 +03:00
|
|
|
expr Marked.pos list ->
|
|
|
|
expr Marked.pos ->
|
|
|
|
expr Marked.pos ->
|
|
|
|
expr Marked.pos
|
2022-05-25 15:54:59 +03:00
|
|
|
(** [make_default ?pos exceptions just cons] builds a term semantically
|
|
|
|
equivalent to [<exceptions | just :- cons>] (the [EDefault] constructor)
|
|
|
|
while avoiding redundant nested constructions. The position is extracted
|
|
|
|
from [just] by default.
|
|
|
|
|
|
|
|
Note that, due to the simplifications taking place, the result might not be
|
|
|
|
of the form [EDefault]:
|
|
|
|
|
|
|
|
- [<true :- x>] is rewritten as [x]
|
|
|
|
- [<ex | true :- def>], when [def] is a default term [<j :- c>] without
|
|
|
|
exceptions, is collapsed into [<ex | def>]
|
|
|
|
- [<ex | false :- _>], when [ex] is a single exception, is rewritten as [ex] *)
|