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:
|
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
|
|
|
|
|
|
|
|
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. *)
|
|
|
|
|
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
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
type marked_typ = typ Marked.pos
|
2022-07-11 12:34:01 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
and typ =
|
2021-02-12 19:20:14 +03:00
|
|
|
| TLit of typ_lit
|
2022-05-31 19:38:14 +03:00
|
|
|
| TTuple of marked_typ list * StructName.t option
|
|
|
|
| TEnum of marked_typ list * EnumName.t
|
|
|
|
| TArrow of marked_typ * marked_typ
|
|
|
|
| TArray of marked_typ
|
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
|
|
|
|
(** During code generation, we need to know the type of the variable being
|
|
|
|
logged for embedding *)
|
|
|
|
| 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
|
2022-04-29 22:18:15 +03:00
|
|
|
| RoundDecimal
|
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-05-31 19:38:14 +03:00
|
|
|
(** Contains some structures used for type inference *)
|
|
|
|
module Infer : sig
|
|
|
|
module Any : Utils.Uid.Id with type info = unit
|
|
|
|
|
|
|
|
type unionfind_typ = typ Marked.pos UnionFind.elem
|
|
|
|
(** We do not reuse {!type: typ} because we have to include a new [TAny]
|
|
|
|
variant. Indeed, error terms can have any type and this has to be captured
|
|
|
|
by the type sytem. *)
|
2022-07-11 12:34:01 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
and typ =
|
|
|
|
| TLit of typ_lit
|
|
|
|
| TArrow of unionfind_typ * unionfind_typ
|
|
|
|
| TTuple of unionfind_typ list * StructName.t option
|
|
|
|
| TEnum of unionfind_typ list * EnumName.t
|
|
|
|
| TArray of unionfind_typ
|
|
|
|
| TAny of Any.t
|
|
|
|
|
|
|
|
val typ_to_ast : unionfind_typ -> marked_typ
|
2022-06-23 15:04:51 +03:00
|
|
|
val ast_to_typ : marked_typ -> unionfind_typ
|
2022-05-31 19:38:14 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
type untyped = { pos : Pos.t } [@@unboxed]
|
2022-07-19 16:19:06 +03:00
|
|
|
type typed = { pos : Pos.t; ty : marked_typ }
|
|
|
|
type inferring = { pos : Pos.t; uf : Infer.unionfind_typ }
|
2022-05-31 19:38:14 +03:00
|
|
|
|
|
|
|
(** The generic type of AST markings. Using a GADT allows functions to be
|
|
|
|
polymorphic in the marking, but still do transformations on types when
|
|
|
|
appropriate *)
|
2022-07-19 16:19:06 +03:00
|
|
|
type _ mark =
|
|
|
|
| Untyped : untyped -> untyped mark
|
|
|
|
| Typed : typed -> typed mark
|
|
|
|
| Inferring : inferring -> inferring mark
|
2022-05-31 19:38:14 +03:00
|
|
|
|
|
|
|
type ('a, 'm) marked = ('a, 'm mark) Marked.t
|
|
|
|
|
|
|
|
type 'm marked_expr = ('m expr, 'm) marked
|
|
|
|
|
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-05-31 19:38:14 +03:00
|
|
|
and 'm expr =
|
|
|
|
| EVar of 'm expr Bindlib.var
|
|
|
|
| ETuple of 'm marked_expr list * StructName.t option
|
2021-02-12 19:20:14 +03:00
|
|
|
(** The [MarkedString.info] is the former struct field name*)
|
2022-05-31 19:38:14 +03:00
|
|
|
| ETupleAccess of 'm marked_expr * int * StructName.t option * marked_typ list
|
2021-02-12 19:20:14 +03:00
|
|
|
(** The [MarkedString.info] is the former struct field name *)
|
2022-05-31 19:38:14 +03:00
|
|
|
| EInj of 'm marked_expr * int * EnumName.t * marked_typ list
|
2021-02-12 19:20:14 +03:00
|
|
|
(** The [MarkedString.info] is the former enum case name *)
|
2022-05-31 19:38:14 +03:00
|
|
|
| EMatch of 'm marked_expr * 'm marked_expr list * EnumName.t
|
2021-02-12 19:20:14 +03:00
|
|
|
(** The [MarkedString.info] is the former enum case name *)
|
2022-05-31 19:38:14 +03:00
|
|
|
| EArray of 'm marked_expr list
|
2021-02-11 20:48:59 +03:00
|
|
|
| ELit of lit
|
2022-05-31 19:38:14 +03:00
|
|
|
| EAbs of
|
|
|
|
(('m expr, 'm marked_expr) Bindlib.mbinder[@opaque]) * marked_typ list
|
|
|
|
| EApp of 'm marked_expr * 'm marked_expr list
|
|
|
|
| EAssert of 'm marked_expr
|
2021-02-11 20:48:59 +03:00
|
|
|
| EOp of operator
|
2022-05-31 19:38:14 +03:00
|
|
|
| EDefault of 'm marked_expr list * 'm marked_expr * 'm marked_expr
|
|
|
|
| EIfThenElse of 'm marked_expr * 'm marked_expr * 'm marked_expr
|
|
|
|
| ErrorOnEmpty of 'm marked_expr
|
|
|
|
|
|
|
|
(** {3 Expression annotations ([Marked.t])} *)
|
2022-02-04 14:33:26 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
type typed_expr = typed marked_expr
|
|
|
|
type struct_ctx = (StructFieldName.t * marked_typ) list StructMap.t
|
|
|
|
type enum_ctx = (EnumConstructor.t * marked_typ) list EnumMap.t
|
2021-02-12 19:20:14 +03:00
|
|
|
type decl_ctx = { ctx_enums : enum_ctx; ctx_structs : struct_ctx }
|
2022-05-31 19:38:14 +03:00
|
|
|
type 'm binder = ('m expr, 'm marked_expr) Bindlib.binder
|
2021-12-09 01:56:03 +03:00
|
|
|
|
2021-12-09 20:42:36 +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
|
|
|
|
(** [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
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
type ('expr, 'm) scope_let = {
|
2021-12-09 01:56:03 +03:00
|
|
|
scope_let_kind : scope_let_kind;
|
2022-05-31 19:38:14 +03:00
|
|
|
scope_let_typ : marked_typ;
|
|
|
|
scope_let_expr : ('expr, 'm) marked;
|
|
|
|
scope_let_next : ('expr, ('expr, 'm) scope_body_expr) Bindlib.binder;
|
|
|
|
scope_let_pos : Pos.t;
|
2021-12-09 01:56:03 +03:00
|
|
|
}
|
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. *)
|
2022-04-02 13:29:43 +03:00
|
|
|
|
2021-12-09 20:42:36 +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-05-31 19:38:14 +03:00
|
|
|
and ('expr, 'm) scope_body_expr =
|
|
|
|
| Result of ('expr, 'm) marked
|
|
|
|
| ScopeLet of ('expr, 'm) scope_let
|
2021-12-09 01:56:03 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
type ('expr, 'm) scope_body = {
|
2021-12-10 18:30:36 +03:00
|
|
|
scope_body_input_struct : StructName.t;
|
|
|
|
scope_body_output_struct : StructName.t;
|
2022-05-31 19:38:14 +03:00
|
|
|
scope_body_expr : ('expr, ('expr, 'm) scope_body_expr) Bindlib.binder;
|
2021-12-10 00:59:39 +03:00
|
|
|
}
|
2021-12-09 20:42:36 +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-05-31 19:38:14 +03:00
|
|
|
type ('expr, 'm) scope_def = {
|
2022-04-02 13:29:43 +03:00
|
|
|
scope_name : ScopeName.t;
|
2022-05-31 19:38:14 +03:00
|
|
|
scope_body : ('expr, 'm) scope_body;
|
|
|
|
scope_next : ('expr, ('expr, 'm) scopes) Bindlib.binder;
|
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. *)
|
2022-05-31 19:38:14 +03:00
|
|
|
and ('expr, 'm) scopes = Nil | ScopeDef of ('expr, 'm) scope_def
|
2022-04-02 13:29:43 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
type 'm program = { decl_ctx : decl_ctx; scopes : ('m expr, 'm) scopes }
|
2022-04-02 13:29:43 +03:00
|
|
|
|
2022-02-02 12:30:39 +03:00
|
|
|
(** {1 Helpers} *)
|
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
(** {2 Manipulation of marks} *)
|
|
|
|
|
|
|
|
val no_mark : 'm mark -> 'm mark
|
2022-06-23 15:06:11 +03:00
|
|
|
val mark_pos : 'm mark -> Pos.t
|
2022-05-31 19:38:14 +03:00
|
|
|
val pos : ('a, 'm) marked -> Pos.t
|
2022-07-19 16:19:06 +03:00
|
|
|
val ty : ('a, typed) marked -> marked_typ
|
|
|
|
val with_ty : marked_typ -> ('a, 'm) marked -> ('a, typed) marked
|
|
|
|
|
|
|
|
(** All the following functions will resolve the types if called on an
|
|
|
|
[Inferring] type *)
|
2022-07-11 12:34:01 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val map_mark :
|
2022-07-19 16:19:06 +03:00
|
|
|
(Pos.t -> Pos.t) -> (marked_typ -> marked_typ) -> 'm mark -> 'm mark
|
2022-07-11 12:34:01 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val map_mark2 :
|
|
|
|
(Pos.t -> Pos.t -> Pos.t) ->
|
2022-07-19 16:19:06 +03:00
|
|
|
(typed -> typed -> marked_typ) ->
|
2022-05-31 19:38:14 +03:00
|
|
|
'm mark ->
|
|
|
|
'm mark ->
|
2022-06-23 15:04:51 +03:00
|
|
|
'm mark
|
2022-07-11 12:34:01 +03:00
|
|
|
|
2022-06-23 15:04:51 +03:00
|
|
|
val fold_marks :
|
2022-07-19 16:19:06 +03:00
|
|
|
(Pos.t list -> Pos.t) -> (typed list -> marked_typ) -> 'm mark list -> 'm mark
|
2022-07-11 12:34:01 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val get_scope_body_mark : ('expr, 'm) scope_body -> 'm mark
|
2022-07-06 17:56:16 +03:00
|
|
|
val untype_expr : 'm marked_expr -> untyped marked_expr
|
2022-07-13 13:16:29 +03:00
|
|
|
val untype_program : 'm program -> untyped program
|
2022-05-31 19:38:14 +03:00
|
|
|
|
|
|
|
(** {2 Boxed constructors} *)
|
2022-04-06 10:35:07 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val evar : 'm expr Bindlib.var -> 'm mark -> 'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
|
|
|
val etuple :
|
2022-05-31 19:38:14 +03:00
|
|
|
'm marked_expr Bindlib.box list ->
|
2022-04-06 10:35:07 +03:00
|
|
|
StructName.t option ->
|
2022-05-31 19:38:14 +03:00
|
|
|
'm mark ->
|
|
|
|
'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
|
|
|
val etupleaccess :
|
2022-05-31 19:38:14 +03:00
|
|
|
'm marked_expr Bindlib.box ->
|
2022-04-06 10:35:07 +03:00
|
|
|
int ->
|
|
|
|
StructName.t option ->
|
2022-05-31 19:38:14 +03:00
|
|
|
marked_typ list ->
|
|
|
|
'm mark ->
|
|
|
|
'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
|
|
|
val einj :
|
2022-05-31 19:38:14 +03:00
|
|
|
'm marked_expr Bindlib.box ->
|
2022-04-06 10:35:07 +03:00
|
|
|
int ->
|
|
|
|
EnumName.t ->
|
2022-05-31 19:38:14 +03:00
|
|
|
marked_typ list ->
|
|
|
|
'm mark ->
|
|
|
|
'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
|
|
|
val ematch :
|
2022-05-31 19:38:14 +03:00
|
|
|
'm marked_expr Bindlib.box ->
|
|
|
|
'm marked_expr Bindlib.box list ->
|
2022-04-06 10:35:07 +03:00
|
|
|
EnumName.t ->
|
2022-05-31 19:38:14 +03:00
|
|
|
'm mark ->
|
|
|
|
'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val earray :
|
|
|
|
'm marked_expr Bindlib.box list -> 'm mark -> 'm marked_expr Bindlib.box
|
2022-07-11 12:34:01 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val elit : lit -> 'm mark -> 'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
|
|
|
val eabs :
|
2022-05-31 19:38:14 +03:00
|
|
|
('m expr, 'm marked_expr) Bindlib.mbinder Bindlib.box ->
|
|
|
|
marked_typ list ->
|
|
|
|
'm mark ->
|
|
|
|
'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
|
|
|
val eapp :
|
2022-05-31 19:38:14 +03:00
|
|
|
'm marked_expr Bindlib.box ->
|
|
|
|
'm marked_expr Bindlib.box list ->
|
|
|
|
'm mark ->
|
|
|
|
'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val eassert :
|
|
|
|
'm marked_expr Bindlib.box -> 'm mark -> 'm marked_expr Bindlib.box
|
2022-07-11 12:34:01 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val eop : operator -> 'm mark -> 'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
|
|
|
val edefault :
|
2022-05-31 19:38:14 +03:00
|
|
|
'm marked_expr Bindlib.box list ->
|
|
|
|
'm marked_expr Bindlib.box ->
|
|
|
|
'm marked_expr Bindlib.box ->
|
|
|
|
'm mark ->
|
|
|
|
'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
|
|
|
val eifthenelse :
|
2022-05-31 19:38:14 +03:00
|
|
|
'm marked_expr Bindlib.box ->
|
|
|
|
'm marked_expr Bindlib.box ->
|
|
|
|
'm marked_expr Bindlib.box ->
|
|
|
|
'm mark ->
|
|
|
|
'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
|
|
|
val eerroronempty :
|
2022-05-31 19:38:14 +03:00
|
|
|
'm marked_expr Bindlib.box -> 'm mark -> 'm marked_expr Bindlib.box
|
2022-04-06 10:35:07 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
type ('expr, 'm) box_expr_sig =
|
|
|
|
('expr, 'm) marked -> ('expr, 'm) marked Bindlib.box
|
2022-04-15 13:16:22 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val box_expr : ('m expr, 'm) box_expr_sig
|
2022-04-15 13:16:22 +03:00
|
|
|
|
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. *)
|
|
|
|
|
2022-04-12 12:51:33 +03:00
|
|
|
val map_expr :
|
|
|
|
'a ->
|
2022-07-06 17:56:16 +03:00
|
|
|
f:('a -> 'm1 marked_expr -> 'm2 marked_expr Bindlib.box) ->
|
|
|
|
('m1 expr, 'm2 mark) Marked.t ->
|
|
|
|
'm2 marked_expr Bindlib.box
|
2022-04-12 12:51:33 +03:00
|
|
|
(** 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 =
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark e with
|
2022-04-12 12:51:33 +03:00
|
|
|
| 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. *)
|
|
|
|
|
2022-07-06 17:56:16 +03:00
|
|
|
val map_expr_top_down :
|
|
|
|
f:('m1 marked_expr -> ('m1 expr, 'm2 mark) Marked.t) ->
|
|
|
|
'm1 marked_expr ->
|
|
|
|
'm2 marked_expr Bindlib.box
|
|
|
|
(** Recursively applies [f] to the nodes of the expression tree. The type
|
|
|
|
returned by [f] is hybrid since the mark at top-level has been rewritten,
|
|
|
|
but not yet the marks in the subtrees. *)
|
|
|
|
|
|
|
|
val map_expr_marks :
|
|
|
|
f:('m1 mark -> 'm2 mark) -> 'm1 marked_expr -> 'm2 marked_expr
|
|
|
|
|
2022-04-15 13:16:22 +03:00
|
|
|
val fold_left_scope_lets :
|
2022-05-31 19:38:14 +03:00
|
|
|
f:('a -> ('expr, 'm) scope_let -> 'expr Bindlib.var -> 'a) ->
|
2022-04-26 13:07:25 +03:00
|
|
|
init:'a ->
|
2022-05-31 19:38:14 +03:00
|
|
|
('expr, 'm) scope_body_expr ->
|
2022-04-26 13:07:25 +03:00
|
|
|
'a
|
|
|
|
(** Usage:
|
|
|
|
[fold_left_scope_lets ~f:(fun acc scope_let scope_let_var -> ...) ~init scope_lets],
|
|
|
|
where [scope_let_var] is the variable bound to the scope let in the next
|
|
|
|
scope lets to be examined. *)
|
2022-04-02 13:29:43 +03:00
|
|
|
|
2022-04-15 13:16:22 +03:00
|
|
|
val fold_right_scope_lets :
|
2022-05-31 19:38:14 +03:00
|
|
|
f:(('expr1, 'm1) scope_let -> 'expr1 Bindlib.var -> 'a -> 'a) ->
|
|
|
|
init:(('expr1, 'm1) marked -> 'a) ->
|
|
|
|
('expr1, 'm1) scope_body_expr ->
|
2022-04-15 13:16:22 +03:00
|
|
|
'a
|
2022-04-26 13:07:25 +03:00
|
|
|
(** Usage:
|
|
|
|
[fold_right_scope_lets ~f:(fun scope_let scope_let_var acc -> ...) ~init scope_lets],
|
|
|
|
where [scope_let_var] is the variable bound to the scope let in the next
|
|
|
|
scope lets to be examined (which are before in the program order). *)
|
2022-04-15 13:16:22 +03:00
|
|
|
|
2022-04-26 13:07:25 +03:00
|
|
|
val map_exprs_in_scope_lets :
|
2022-05-31 19:38:14 +03:00
|
|
|
f:(('expr1, 'm1) marked -> ('expr2, 'm2) marked Bindlib.box) ->
|
|
|
|
varf:('expr1 Bindlib.var -> 'expr2 Bindlib.var) ->
|
|
|
|
('expr1, 'm1) scope_body_expr ->
|
|
|
|
('expr2, 'm2) scope_body_expr Bindlib.box
|
2022-04-15 13:16:22 +03:00
|
|
|
|
|
|
|
val fold_left_scope_defs :
|
2022-05-31 19:38:14 +03:00
|
|
|
f:('a -> ('expr1, 'm1) scope_def -> 'expr1 Bindlib.var -> 'a) ->
|
2022-04-26 13:07:25 +03:00
|
|
|
init:'a ->
|
2022-05-31 19:38:14 +03:00
|
|
|
('expr1, 'm1) scopes ->
|
2022-04-26 13:07:25 +03:00
|
|
|
'a
|
|
|
|
(** Usage:
|
|
|
|
[fold_left_scope_defs ~f:(fun acc scope_def scope_var -> ...) ~init scope_def],
|
|
|
|
where [scope_var] is the variable bound to the scope in the next scopes to
|
|
|
|
be examined. *)
|
2022-04-02 13:29:43 +03:00
|
|
|
|
2022-04-15 13:16:22 +03:00
|
|
|
val fold_right_scope_defs :
|
2022-05-31 19:38:14 +03:00
|
|
|
f:(('expr1, 'm1) scope_def -> 'expr1 Bindlib.var -> 'a -> 'a) ->
|
2022-04-15 13:16:22 +03:00
|
|
|
init:'a ->
|
2022-05-31 19:38:14 +03:00
|
|
|
('expr1, 'm1) scopes ->
|
2022-04-15 13:16:22 +03:00
|
|
|
'a
|
2022-04-26 13:07:25 +03:00
|
|
|
(** Usage:
|
|
|
|
[fold_right_scope_defs ~f:(fun scope_def scope_var acc -> ...) ~init scope_def],
|
|
|
|
where [scope_var] is the variable bound to the scope in the next scopes to
|
|
|
|
be examined (which are before in the program order). *)
|
2022-04-15 13:16:22 +03:00
|
|
|
|
|
|
|
val map_scope_defs :
|
2022-05-31 19:38:14 +03:00
|
|
|
f:(('expr, 'm) scope_def -> ('expr, 'm) scope_def Bindlib.box) ->
|
|
|
|
('expr, 'm) scopes ->
|
|
|
|
('expr, 'm) scopes Bindlib.box
|
2022-04-25 11:19:04 +03:00
|
|
|
|
|
|
|
val map_exprs_in_scopes :
|
2022-05-31 19:38:14 +03:00
|
|
|
f:(('expr1, 'm1) marked -> ('expr2, 'm2) marked Bindlib.box) ->
|
|
|
|
varf:('expr1 Bindlib.var -> 'expr2 Bindlib.var) ->
|
|
|
|
('expr1, 'm1) scopes ->
|
|
|
|
('expr2, 'm2) scopes Bindlib.box
|
2022-04-25 11:19:04 +03:00
|
|
|
(** This is the main map visitor for all the expressions inside all the scopes
|
|
|
|
of the program. *)
|
2022-04-02 13:29:43 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
(** {2 Variables} *)
|
|
|
|
|
|
|
|
type 'm var = 'm expr Bindlib.var
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val new_var : string -> 'm var
|
|
|
|
|
2022-07-06 17:56:16 +03:00
|
|
|
val translate_var : 'm1 var -> 'm2 var
|
|
|
|
(** used to convert between e.g. [untyped expr var] into a [typed expr var] *)
|
|
|
|
|
2021-02-12 19:20:14 +03:00
|
|
|
module Var : sig
|
2022-05-31 19:38:14 +03:00
|
|
|
type t
|
2021-02-12 20:16:06 +03:00
|
|
|
|
2022-06-23 15:06:11 +03:00
|
|
|
val t : 'm expr Bindlib.var -> t
|
2022-07-13 13:28:08 +03:00
|
|
|
(** Hides the marking type parameter annotation behind an existential type so
|
|
|
|
that variables can be stored in non-polymorphic sets and maps *)
|
|
|
|
|
2022-06-23 15:06:11 +03:00
|
|
|
val get : t -> 'm expr Bindlib.var
|
2022-07-13 13:28:08 +03:00
|
|
|
(** Be careful with this, it breaks the type abstraction by casting the
|
|
|
|
existential type annotation. See [!Bindlib.copy_var] for more detail. *)
|
|
|
|
|
2021-02-12 19:20:14 +03:00
|
|
|
val compare : t -> t -> int
|
2022-07-06 11:08:27 +03:00
|
|
|
val eq : t -> t -> bool
|
2021-02-12 19:20:14 +03:00
|
|
|
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-07-19 22:41:55 +03:00
|
|
|
val free_vars_expr : 'm marked_expr -> VarSet.t
|
|
|
|
val free_vars_scope_body_expr : ('m expr, 'm) scope_body_expr -> VarSet.t
|
|
|
|
val free_vars_scope_body : ('m expr, 'm) scope_body -> VarSet.t
|
|
|
|
val free_vars_scopes : ('m expr, 'm) scopes -> VarSet.t
|
2022-02-04 11:24:51 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
(* type vars = expr Bindlib.mvar *)
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-06-23 15:06:11 +03:00
|
|
|
val make_var : ('m var, 'm) marked -> 'm marked_expr Bindlib.box
|
|
|
|
|
|
|
|
(** {2 Boxed term constructors} *)
|
2022-04-12 18:54:00 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
type ('e, 'm) make_abs_sig =
|
|
|
|
'e Bindlib.mvar ->
|
|
|
|
('e, 'm) marked Bindlib.box ->
|
|
|
|
marked_typ list ->
|
|
|
|
'm mark ->
|
|
|
|
('e, 'm) marked Bindlib.box
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val make_abs : ('m expr, 'm) make_abs_sig
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val make_app :
|
2022-05-31 19:38:14 +03:00
|
|
|
'm marked_expr Bindlib.box ->
|
|
|
|
'm marked_expr Bindlib.box list ->
|
|
|
|
'm mark ->
|
|
|
|
'm marked_expr Bindlib.box
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
type ('expr, 'm) make_let_in_sig =
|
|
|
|
'expr Bindlib.var ->
|
|
|
|
marked_typ ->
|
|
|
|
('expr, 'm) marked Bindlib.box ->
|
|
|
|
('expr, 'm) marked Bindlib.box ->
|
2021-10-28 16:24:39 +03:00
|
|
|
Pos.t ->
|
2022-05-31 19:38:14 +03:00
|
|
|
('expr, 'm) marked Bindlib.box
|
|
|
|
|
|
|
|
val make_let_in : ('m expr, 'm) make_let_in_sig
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-02-02 12:30:39 +03:00
|
|
|
(**{2 Other}*)
|
|
|
|
|
2022-06-23 15:04:51 +03:00
|
|
|
val empty_thunked_term : 'm mark -> 'm marked_expr
|
2022-05-31 19:38:14 +03:00
|
|
|
val is_value : 'm marked_expr -> bool
|
2022-02-02 12:30:39 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val equal_exprs : 'm marked_expr -> 'm marked_expr -> bool
|
2022-03-08 19:35:08 +03:00
|
|
|
(** 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
|
|
|
|
2021-12-10 18:30:36 +03:00
|
|
|
val build_whole_scope_expr :
|
2022-05-31 19:38:14 +03:00
|
|
|
box_expr:('expr, 'm) box_expr_sig ->
|
|
|
|
make_abs:('expr, 'm) make_abs_sig ->
|
|
|
|
make_let_in:('expr, 'm) make_let_in_sig ->
|
2022-04-12 18:54:00 +03:00
|
|
|
decl_ctx ->
|
2022-05-31 19:38:14 +03:00
|
|
|
('expr, 'm) scope_body ->
|
|
|
|
'm mark ->
|
|
|
|
('expr, 'm) marked Bindlib.box
|
2021-12-10 18:30:36 +03:00
|
|
|
(** 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-04-12 18:54:00 +03:00
|
|
|
type 'expr scope_name_or_var =
|
|
|
|
| ScopeName of ScopeName.t
|
|
|
|
| ScopeVar of 'expr Bindlib.var
|
|
|
|
|
|
|
|
val unfold_scopes :
|
2022-05-31 19:38:14 +03:00
|
|
|
box_expr:('expr, 'm) box_expr_sig ->
|
|
|
|
make_abs:('expr, 'm) make_abs_sig ->
|
|
|
|
make_let_in:('expr, 'm) make_let_in_sig ->
|
2022-04-12 18:54:00 +03:00
|
|
|
decl_ctx ->
|
2022-05-31 19:38:14 +03:00
|
|
|
('expr, 'm) scopes ->
|
|
|
|
'm mark ->
|
2022-04-12 18:54:00 +03:00
|
|
|
'expr scope_name_or_var ->
|
2022-05-31 19:38:14 +03:00
|
|
|
('expr, 'm) marked Bindlib.box
|
2022-04-12 18:54:00 +03:00
|
|
|
|
2021-12-10 19:55:24 +03:00
|
|
|
val build_whole_program_expr :
|
2022-05-31 19:38:14 +03:00
|
|
|
'm program -> ScopeName.t -> 'm marked_expr Bindlib.box
|
2021-12-10 19:55:24 +03:00
|
|
|
(** 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
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val expr_size : 'm marked_expr -> int
|
2022-01-07 20:36:56 +03:00
|
|
|
(** Used by the optimizer to know when to stop *)
|
2022-04-12 12:14:39 +03:00
|
|
|
|
2022-05-31 19:38:14 +03:00
|
|
|
val remove_logging_calls : 'm marked_expr -> 'm marked_expr Bindlib.box
|
2022-04-12 12:14:39 +03:00
|
|
|
(** Removes all calls to [Log] unary operators in the AST, replacing them by
|
|
|
|
their argument. *)
|
2022-07-05 11:29:02 +03:00
|
|
|
|
|
|
|
val build_scope_typ_from_sig :
|
|
|
|
decl_ctx -> StructName.t -> StructName.t -> Pos.t -> typ Marked.pos
|
|
|
|
(** [build_scope_typ_from_sig ctx in_struct out_struct pos] builds the arrow
|
|
|
|
type for the specified scope *)
|