2022-07-26 22:58:04 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020-2022 Inria,
|
|
|
|
contributor: Denis Merigoux <denis.merigoux@inria.fr>, Alain Delaët-Tixeuil
|
|
|
|
<alain.delaet--tixeuil@inria.fr>, Louis Gesbert <louis.gesbert@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. *)
|
|
|
|
|
2022-08-12 18:59:49 +03:00
|
|
|
(** This module defines generic types for types, literals and expressions shared
|
|
|
|
through several of the different ASTs. *)
|
|
|
|
|
|
|
|
(* Doesn't define values, so OK to have without an mli *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-07-26 22:58:04 +03:00
|
|
|
module Runtime = Runtime_ocaml.Runtime
|
2022-11-21 12:11:51 +03:00
|
|
|
module ScopeName = Uid.Gen ()
|
2023-01-12 13:12:48 +03:00
|
|
|
module TopdefName = Uid.Gen ()
|
2022-11-21 12:11:51 +03:00
|
|
|
module StructName = Uid.Gen ()
|
|
|
|
module StructField = Uid.Gen ()
|
|
|
|
module EnumName = Uid.Gen ()
|
|
|
|
module EnumConstructor = Uid.Gen ()
|
2022-07-26 22:58:04 +03:00
|
|
|
|
2022-11-07 15:50:28 +03:00
|
|
|
(** Only used by surface *)
|
|
|
|
|
2022-11-21 12:11:51 +03:00
|
|
|
module RuleName = Uid.Gen ()
|
|
|
|
module LabelName = Uid.Gen ()
|
2022-11-07 15:50:28 +03:00
|
|
|
|
2022-11-22 22:57:59 +03:00
|
|
|
(** Used for unresolved structs/maps in desugared *)
|
|
|
|
|
|
|
|
module IdentName = String
|
|
|
|
|
2022-08-17 18:14:29 +03:00
|
|
|
(** Only used by desugared/scopelang *)
|
|
|
|
|
2022-11-21 12:11:51 +03:00
|
|
|
module ScopeVar = Uid.Gen ()
|
|
|
|
module SubScopeName = Uid.Gen ()
|
|
|
|
module StateName = Uid.Gen ()
|
2022-08-25 13:09:51 +03:00
|
|
|
|
2022-07-26 22:58:04 +03:00
|
|
|
(** {1 Abstract syntax tree} *)
|
|
|
|
|
2022-11-22 22:57:59 +03:00
|
|
|
(** Define a common base type for the expressions in most passes of the compiler *)
|
|
|
|
|
|
|
|
type desugared = [ `Desugared ]
|
|
|
|
(** {2 Phantom types used to select relevant cases on the generic AST}
|
|
|
|
|
|
|
|
we instantiate them with a polymorphic variant to take advantage of
|
|
|
|
sub-typing. The values aren't actually used. *)
|
|
|
|
|
|
|
|
type scopelang = [ `Scopelang ]
|
|
|
|
type dcalc = [ `Dcalc ]
|
|
|
|
type lcalc = [ `Lcalc ]
|
|
|
|
|
|
|
|
type 'a any = [< desugared | scopelang | dcalc | lcalc ] as 'a
|
|
|
|
(** ['a any] is 'a, but adds the constraint that it should be restricted to
|
|
|
|
valid AST kinds *)
|
|
|
|
|
2022-07-28 11:36:36 +03:00
|
|
|
(** {2 Types} *)
|
|
|
|
|
2022-07-26 22:58:04 +03:00
|
|
|
type typ_lit = TBool | TUnit | TInt | TRat | TMoney | TDate | TDuration
|
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
type typ = naked_typ Marked.pos
|
2022-07-26 22:58:04 +03:00
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
and naked_typ =
|
2022-07-26 22:58:04 +03:00
|
|
|
| TLit of typ_lit
|
2022-08-25 18:29:00 +03:00
|
|
|
| TTuple of typ list
|
2022-08-23 16:23:52 +03:00
|
|
|
| TStruct of StructName.t
|
|
|
|
| TEnum of EnumName.t
|
2022-08-25 18:29:00 +03:00
|
|
|
| TOption of typ
|
2023-01-11 13:22:42 +03:00
|
|
|
| TArrow of typ list * typ
|
2022-08-25 18:29:00 +03:00
|
|
|
| TArray of typ
|
2022-07-26 22:58:04 +03:00
|
|
|
| TAny
|
|
|
|
|
2022-07-28 11:36:36 +03:00
|
|
|
(** {2 Constants and operators} *)
|
|
|
|
|
2022-07-26 22:58:04 +03:00
|
|
|
type date = Runtime.date
|
|
|
|
type duration = Runtime.duration
|
|
|
|
|
|
|
|
type log_entry =
|
2022-08-25 18:29:00 +03:00
|
|
|
| VarDef of naked_typ
|
2022-07-26 22:58:04 +03:00
|
|
|
(** During code generation, we need to know the type of the variable being
|
|
|
|
logged for embedding *)
|
|
|
|
| BeginCall
|
|
|
|
| EndCall
|
|
|
|
| PosRecordIfTrueBool
|
|
|
|
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
module Op = struct
|
|
|
|
(** Classification of operators on how they should be typed *)
|
|
|
|
|
|
|
|
type monomorphic =
|
|
|
|
| Monomorphic (** Operands and return types of the operator are fixed *)
|
|
|
|
|
|
|
|
type polymorphic =
|
|
|
|
| Polymorphic
|
|
|
|
(** The operator is truly polymorphic: it's the same runtime function
|
|
|
|
that may work on multiple types. We require that resolving the
|
|
|
|
argument types from right to left trivially resolves all type
|
|
|
|
variables declared in the operator type. *)
|
|
|
|
|
|
|
|
type overloaded =
|
|
|
|
| Overloaded
|
|
|
|
(** The operator is ambiguous and requires the types of its arguments to
|
|
|
|
be known before it can be typed, using a pre-defined table *)
|
|
|
|
|
|
|
|
type resolved =
|
|
|
|
| Resolved (** Explicit monomorphic versions of the overloaded operators *)
|
|
|
|
|
|
|
|
(** Classification of operators. This could be inlined in the definition of
|
|
|
|
[t] but is more concise this way *)
|
|
|
|
type (_, _) kind =
|
|
|
|
| Monomorphic : ('a any, monomorphic) kind
|
|
|
|
| Polymorphic : ('a any, polymorphic) kind
|
|
|
|
| Overloaded : ([< desugared ], overloaded) kind
|
|
|
|
| Resolved : ([< scopelang | dcalc | lcalc ], resolved) kind
|
|
|
|
|
|
|
|
type (_, _) t =
|
|
|
|
(* unary *)
|
|
|
|
(* * monomorphic *)
|
|
|
|
| Not : ('a any, monomorphic) t
|
|
|
|
| GetDay : ('a any, monomorphic) t
|
|
|
|
| GetMonth : ('a any, monomorphic) t
|
|
|
|
| GetYear : ('a any, monomorphic) t
|
|
|
|
| FirstDayOfMonth : ('a any, monomorphic) t
|
|
|
|
| LastDayOfMonth : ('a any, monomorphic) t
|
|
|
|
(* * polymorphic *)
|
|
|
|
| Length : ('a any, polymorphic) t
|
|
|
|
| Log : log_entry * Uid.MarkedString.info list -> ('a any, polymorphic) t
|
|
|
|
(* * overloaded *)
|
|
|
|
| Minus : (desugared, overloaded) t
|
|
|
|
| Minus_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Minus_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Minus_mon : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Minus_dur : ([< scopelang | dcalc | lcalc ], resolved) t
|
2022-12-13 15:28:01 +03:00
|
|
|
| ToRat : (desugared, overloaded) t
|
|
|
|
| ToRat_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| ToRat_mon : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| ToMoney : (desugared, overloaded) t
|
|
|
|
| ToMoney_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Round : (desugared, overloaded) t
|
|
|
|
| Round_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Round_mon : ([< scopelang | dcalc | lcalc ], resolved) t
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
(* binary *)
|
|
|
|
(* * monomorphic *)
|
|
|
|
| And : ('a any, monomorphic) t
|
|
|
|
| Or : ('a any, monomorphic) t
|
|
|
|
| Xor : ('a any, monomorphic) t
|
|
|
|
(* * polymorphic *)
|
|
|
|
| Eq : ('a any, polymorphic) t
|
|
|
|
| Map : ('a any, polymorphic) t
|
|
|
|
| Concat : ('a any, polymorphic) t
|
|
|
|
| Filter : ('a any, polymorphic) t
|
2022-12-12 18:02:07 +03:00
|
|
|
| Reduce : ('a any, polymorphic) t
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
(* * overloaded *)
|
|
|
|
| Add : (desugared, overloaded) t
|
|
|
|
| Add_int_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Add_rat_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Add_mon_mon : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Add_dat_dur : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Add_dur_dur : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Sub : (desugared, overloaded) t
|
|
|
|
| Sub_int_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Sub_rat_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Sub_mon_mon : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Sub_dat_dat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Sub_dat_dur : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Sub_dur_dur : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Mult : (desugared, overloaded) t
|
|
|
|
| Mult_int_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Mult_rat_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Mult_mon_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Mult_dur_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Div : (desugared, overloaded) t
|
|
|
|
| Div_int_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Div_rat_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Div_mon_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Div_mon_mon : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Lt : (desugared, overloaded) t
|
|
|
|
| Lt_int_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Lt_rat_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Lt_mon_mon : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Lt_dat_dat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Lt_dur_dur : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Lte : (desugared, overloaded) t
|
|
|
|
| Lte_int_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Lte_rat_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Lte_mon_mon : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Lte_dat_dat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Lte_dur_dur : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Gt : (desugared, overloaded) t
|
|
|
|
| Gt_int_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Gt_rat_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Gt_mon_mon : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Gt_dat_dat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Gt_dur_dur : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Gte : (desugared, overloaded) t
|
|
|
|
| Gte_int_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Gte_rat_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Gte_mon_mon : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Gte_dat_dat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Gte_dur_dur : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
(* Todo: Eq is not an overload at the moment, but it should be one. The
|
|
|
|
trick is that it needs generation of specific code for arrays, every
|
|
|
|
struct and enum: operators [Eq_structs of StructName.t], etc. *)
|
|
|
|
| Eq_int_int : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Eq_rat_rat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Eq_mon_mon : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Eq_dur_dur : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
| Eq_dat_dat : ([< scopelang | dcalc | lcalc ], resolved) t
|
|
|
|
(* ternary *)
|
|
|
|
(* * polymorphic *)
|
|
|
|
| Fold : ('a any, polymorphic) t
|
|
|
|
end
|
|
|
|
|
|
|
|
type ('a, 'k) operator = ('a any, 'k) Op.t
|
2022-07-26 22:58:04 +03:00
|
|
|
type except = ConflictError | EmptyError | NoValueProvided | Crash
|
2022-07-28 11:36:36 +03:00
|
|
|
|
|
|
|
(** {2 Generic expressions} *)
|
|
|
|
|
|
|
|
(** Define a common base type for the expressions in most passes of the compiler *)
|
|
|
|
|
|
|
|
(** Literals are the same throughout compilation except for the [LEmptyError]
|
|
|
|
case which is eliminated midway through. *)
|
2022-07-27 13:18:14 +03:00
|
|
|
type 'a glit =
|
2022-07-28 11:36:36 +03:00
|
|
|
| LBool : bool -> 'a glit
|
|
|
|
| LEmptyError : [< desugared | scopelang | dcalc ] glit
|
|
|
|
| LInt : Runtime.integer -> 'a glit
|
|
|
|
| LRat : Runtime.decimal -> 'a glit
|
|
|
|
| LMoney : Runtime.money -> 'a glit
|
|
|
|
| LUnit : 'a glit
|
|
|
|
| LDate : date -> 'a glit
|
|
|
|
| LDuration : duration -> 'a glit
|
2022-07-27 13:18:14 +03:00
|
|
|
|
2022-08-25 13:09:51 +03:00
|
|
|
(** Locations are handled differently in [desugared] and [scopelang] *)
|
|
|
|
type 'a glocation =
|
|
|
|
| DesugaredScopeVar :
|
|
|
|
ScopeVar.t Marked.pos * StateName.t option
|
|
|
|
-> desugared glocation
|
|
|
|
| ScopelangScopeVar : ScopeVar.t Marked.pos -> scopelang glocation
|
|
|
|
| SubScopeVar :
|
|
|
|
ScopeName.t * SubScopeName.t Marked.pos * ScopeVar.t Marked.pos
|
|
|
|
-> [< desugared | scopelang ] glocation
|
2023-02-13 17:00:23 +03:00
|
|
|
| ToplevelVar :
|
|
|
|
TopdefName.t Marked.pos
|
|
|
|
-> [< desugared | scopelang ] glocation
|
2022-08-25 13:09:51 +03:00
|
|
|
|
2022-08-25 17:31:32 +03:00
|
|
|
type ('a, 't) gexpr = (('a, 't) naked_gexpr, 't) Marked.t
|
2022-07-28 11:36:36 +03:00
|
|
|
(** General expressions: groups all expression cases of the different ASTs, and
|
|
|
|
uses a GADT to eliminate irrelevant cases for each one. The ['t] annotations
|
|
|
|
are also totally unconstrained at this point. The dcalc exprs, for example,
|
2022-08-25 20:46:13 +03:00
|
|
|
are then defined with [type naked_expr = dcalc naked_gexpr] plus the
|
|
|
|
annotations.
|
2022-08-25 13:09:51 +03:00
|
|
|
|
|
|
|
A few tips on using this GADT:
|
|
|
|
|
|
|
|
- To write a function that handles cases from different ASTs, explicit the
|
2022-08-25 17:31:32 +03:00
|
|
|
type variables: [fun (type a) (x: a naked_gexpr) -> ...]
|
2022-08-25 13:09:51 +03:00
|
|
|
- For recursive functions, you may need to additionally explicit the
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
generalisation of the variable: [let rec f: type a . a naked_gexpr -> ...]
|
|
|
|
- Always think of using the pre-defined map/fold functions in [Expr] rather
|
|
|
|
than completely defining your recursion manually. *)
|
2022-07-26 22:58:04 +03:00
|
|
|
|
2022-08-25 17:31:32 +03:00
|
|
|
and ('a, 't) naked_gexpr =
|
2022-07-26 22:58:04 +03:00
|
|
|
(* Constructors common to all ASTs *)
|
2022-08-25 17:31:32 +03:00
|
|
|
| ELit : 'a glit -> ('a any, 't) naked_gexpr
|
2022-11-17 19:13:35 +03:00
|
|
|
| EApp : {
|
|
|
|
f : ('a, 't) gexpr;
|
|
|
|
args : ('a, 't) gexpr list;
|
|
|
|
}
|
|
|
|
-> ('a any, 't) naked_gexpr
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
| EOp : { op : ('a, _) operator; tys : typ list } -> ('a any, 't) naked_gexpr
|
2022-08-25 17:31:32 +03:00
|
|
|
| EArray : ('a, 't) gexpr list -> ('a any, 't) naked_gexpr
|
2022-08-26 16:21:47 +03:00
|
|
|
| EVar : ('a, 't) naked_gexpr Bindlib.var -> ('a any, 't) naked_gexpr
|
2022-11-17 19:13:35 +03:00
|
|
|
| EAbs : {
|
|
|
|
binder : (('a, 't) naked_gexpr, ('a, 't) gexpr) Bindlib.mbinder;
|
|
|
|
tys : typ list;
|
|
|
|
}
|
|
|
|
-> ('a any, 't) naked_gexpr
|
|
|
|
| EIfThenElse : {
|
|
|
|
cond : ('a, 't) gexpr;
|
|
|
|
etrue : ('a, 't) gexpr;
|
|
|
|
efalse : ('a, 't) gexpr;
|
|
|
|
}
|
|
|
|
-> ('a any, 't) naked_gexpr
|
|
|
|
| EStruct : {
|
|
|
|
name : StructName.t;
|
2022-11-21 12:12:45 +03:00
|
|
|
fields : ('a, 't) gexpr StructField.Map.t;
|
2022-11-17 19:13:35 +03:00
|
|
|
}
|
2022-08-26 12:06:00 +03:00
|
|
|
-> ('a any, 't) naked_gexpr
|
2022-11-17 19:13:35 +03:00
|
|
|
| EInj : {
|
|
|
|
name : EnumName.t;
|
|
|
|
e : ('a, 't) gexpr;
|
|
|
|
cons : EnumConstructor.t;
|
|
|
|
}
|
|
|
|
-> ('a any, 't) naked_gexpr
|
|
|
|
| EMatch : {
|
|
|
|
name : EnumName.t;
|
|
|
|
e : ('a, 't) gexpr;
|
2022-11-21 12:12:45 +03:00
|
|
|
cases : ('a, 't) gexpr EnumConstructor.Map.t;
|
2022-11-17 19:13:35 +03:00
|
|
|
}
|
2022-08-26 12:06:00 +03:00
|
|
|
-> ('a any, 't) naked_gexpr
|
2023-01-10 13:50:37 +03:00
|
|
|
| ETuple : ('a, 't) gexpr list -> ('a any, 't) naked_gexpr
|
|
|
|
| ETupleAccess : {
|
|
|
|
e : ('a, 't) gexpr;
|
|
|
|
index : int;
|
|
|
|
size : int;
|
|
|
|
}
|
|
|
|
-> ('a any, 't) naked_gexpr
|
2022-08-17 18:14:29 +03:00
|
|
|
(* Early stages *)
|
2022-08-25 20:46:13 +03:00
|
|
|
| ELocation :
|
|
|
|
'a glocation
|
|
|
|
-> (([< desugared | scopelang ] as 'a), 't) naked_gexpr
|
2022-11-17 19:13:35 +03:00
|
|
|
| EScopeCall : {
|
|
|
|
scope : ScopeName.t;
|
2022-11-21 12:12:45 +03:00
|
|
|
args : ('a, 't) gexpr ScopeVar.Map.t;
|
2022-11-17 19:13:35 +03:00
|
|
|
}
|
2022-10-21 16:47:17 +03:00
|
|
|
-> (([< desugared | scopelang ] as 'a), 't) naked_gexpr
|
2022-11-22 22:57:59 +03:00
|
|
|
| EDStructAccess : {
|
|
|
|
name_opt : StructName.t option;
|
|
|
|
e : ('a, 't) gexpr;
|
|
|
|
field : IdentName.t;
|
|
|
|
}
|
|
|
|
-> ((desugared as 'a), 't) naked_gexpr
|
2022-12-07 14:28:24 +03:00
|
|
|
(** [desugared] has ambiguous struct fields *)
|
2022-11-22 22:57:59 +03:00
|
|
|
| EStructAccess : {
|
|
|
|
name : StructName.t;
|
|
|
|
e : ('a, 't) gexpr;
|
|
|
|
field : StructField.t;
|
|
|
|
}
|
|
|
|
-> (([< scopelang | dcalc | lcalc ] as 'a), 't) naked_gexpr
|
2022-12-07 14:28:24 +03:00
|
|
|
(** Resolved struct/enums, after [desugared] *)
|
2022-07-26 22:58:04 +03:00
|
|
|
(* Lambda-like *)
|
2022-08-25 17:31:32 +03:00
|
|
|
| EAssert : ('a, 't) gexpr -> (([< dcalc | lcalc ] as 'a), 't) naked_gexpr
|
2022-07-28 11:36:36 +03:00
|
|
|
(* Default terms *)
|
2022-11-17 19:13:35 +03:00
|
|
|
| EDefault : {
|
|
|
|
excepts : ('a, 't) gexpr list;
|
|
|
|
just : ('a, 't) gexpr;
|
|
|
|
cons : ('a, 't) gexpr;
|
|
|
|
}
|
2022-08-25 17:31:32 +03:00
|
|
|
-> (([< desugared | scopelang | dcalc ] as 'a), 't) naked_gexpr
|
2022-11-17 19:13:35 +03:00
|
|
|
| EErrorOnEmpty :
|
2022-08-25 17:31:32 +03:00
|
|
|
('a, 't) gexpr
|
|
|
|
-> (([< desugared | scopelang | dcalc ] as 'a), 't) naked_gexpr
|
2022-07-26 22:58:04 +03:00
|
|
|
(* Lambda calculus with exceptions *)
|
2022-08-25 17:31:32 +03:00
|
|
|
| ERaise : except -> ((lcalc as 'a), 't) naked_gexpr
|
2022-11-17 19:13:35 +03:00
|
|
|
| ECatch : {
|
|
|
|
body : ('a, 't) gexpr;
|
|
|
|
exn : except;
|
|
|
|
handler : ('a, 't) gexpr;
|
|
|
|
}
|
2022-08-25 17:31:32 +03:00
|
|
|
-> ((lcalc as 'a), 't) naked_gexpr
|
2022-07-26 22:58:04 +03:00
|
|
|
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
type ('a, 't) boxed_gexpr = (('a, 't) naked_gexpr Bindlib.box, 't) Marked.t
|
|
|
|
(** The annotation is lifted outside of the box for expressions *)
|
|
|
|
|
|
|
|
type 'e boxed = ('a, 't) boxed_gexpr constraint 'e = ('a, 't) gexpr
|
|
|
|
(** [('a, 't) gexpr boxed] is [('a, 't) boxed_gexpr]. The difference with
|
|
|
|
[('a, 't) gexpr Bindlib.box] is that the annotations is outside of the box,
|
|
|
|
and can therefore be accessed without the need to resolve the box *)
|
2022-08-25 20:46:13 +03:00
|
|
|
|
|
|
|
type ('e, 'b) binder = (('a, 't) naked_gexpr, 'b) Bindlib.binder
|
|
|
|
constraint 'e = ('a, 't) gexpr
|
|
|
|
(** The expressions use the {{:https://lepigre.fr/ocaml-bindlib/} Bindlib}
|
|
|
|
library, based on higher-order abstract syntax *)
|
|
|
|
|
2022-08-26 16:21:47 +03:00
|
|
|
type ('e, 'b) mbinder = (('a, 't) naked_gexpr, 'b) Bindlib.mbinder
|
|
|
|
constraint 'e = ('a, 't) gexpr
|
|
|
|
|
2022-07-28 11:36:36 +03:00
|
|
|
(** {2 Markings} *)
|
|
|
|
|
|
|
|
type untyped = { pos : Pos.t } [@@ocaml.unboxed]
|
2022-08-25 18:29:00 +03:00
|
|
|
type typed = { pos : Pos.t; ty : typ }
|
2022-07-28 11:36:36 +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
|
2022-11-17 19:13:35 +03:00
|
|
|
appropriate. Expected to fill the ['t] parameter of [gexpr] and [gexpr] (a
|
|
|
|
['t] annotation different from this type is used in the middle of the typing
|
|
|
|
processing, but all visible ASTs should otherwise use this. *)
|
2022-07-28 11:36:36 +03:00
|
|
|
type _ mark = Untyped : untyped -> untyped mark | Typed : typed -> typed mark
|
|
|
|
|
|
|
|
(** Useful for errors and printing, for example *)
|
2022-09-13 16:20:13 +03:00
|
|
|
type any_expr = AnyExpr : (_, _ mark) gexpr -> any_expr
|
2022-07-28 11:36:36 +03:00
|
|
|
|
|
|
|
(** {2 Higher-level program structure} *)
|
|
|
|
|
2022-08-22 19:53:30 +03:00
|
|
|
(** Constructs scopes and programs on top of expressions. The ['e] type
|
2022-11-17 19:13:35 +03:00
|
|
|
parameter throughout is expected to match instances of the [gexpr] type
|
|
|
|
defined above. Markings are constrained to the [mark] GADT defined above.
|
|
|
|
Note that this structure is at the moment only relevant for [dcalc] and
|
|
|
|
[lcalc], as [scopelang] has its own scope structure, as the name implies. *)
|
2022-07-28 11:36: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. *)
|
|
|
|
type scope_let_kind =
|
|
|
|
| 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. *)
|
|
|
|
| CallingSubScope (** [let result = s ({ x = s.x; y = s.x; ...}) ]*)
|
|
|
|
| DestructuringSubScopeResults (** [let s.x = result.x ]**)
|
|
|
|
| Assertion (** [let _ = assert e]*)
|
|
|
|
|
2022-08-16 17:54:42 +03:00
|
|
|
type 'e scope_let = {
|
2022-07-28 11:36:36 +03:00
|
|
|
scope_let_kind : scope_let_kind;
|
2022-08-25 18:29:00 +03:00
|
|
|
scope_let_typ : typ;
|
2022-08-25 20:46:13 +03:00
|
|
|
scope_let_expr : 'e;
|
|
|
|
scope_let_next : ('e, 'e scope_body_expr) binder;
|
2023-01-23 14:19:36 +03:00
|
|
|
(* todo ? Factorise the code_item _list type below and use it here *)
|
2022-07-28 11:36:36 +03:00
|
|
|
scope_let_pos : Pos.t;
|
|
|
|
}
|
2022-08-26 12:06:00 +03:00
|
|
|
constraint 'e = (_ any, _ mark) gexpr
|
2022-07-28 11:36:36 +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-08-16 17:54:42 +03:00
|
|
|
and 'e scope_body_expr =
|
2022-08-25 20:46:13 +03:00
|
|
|
| Result of 'e
|
2022-08-16 17:54:42 +03:00
|
|
|
| ScopeLet of 'e scope_let
|
2022-08-26 12:06:00 +03:00
|
|
|
constraint 'e = (_ any, _ mark) gexpr
|
2022-07-28 11:36:36 +03:00
|
|
|
|
2022-08-16 17:54:42 +03:00
|
|
|
type 'e scope_body = {
|
2022-07-28 11:36:36 +03:00
|
|
|
scope_body_input_struct : StructName.t;
|
|
|
|
scope_body_output_struct : StructName.t;
|
2022-08-25 20:46:13 +03:00
|
|
|
scope_body_expr : ('e, 'e scope_body_expr) binder;
|
2022-07-28 11:36:36 +03:00
|
|
|
}
|
2022-08-26 12:06:00 +03:00
|
|
|
constraint 'e = (_ any, _ mark) gexpr
|
2022-07-28 11:36: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
|
|
|
|
a result expression that uses the let-binded variables. The first binder is
|
|
|
|
the argument of type [scope_body_input_struct]. *)
|
|
|
|
|
2023-01-23 14:19:36 +03:00
|
|
|
type 'e code_item =
|
|
|
|
| ScopeDef of ScopeName.t * 'e scope_body
|
|
|
|
| Topdef of TopdefName.t * typ * 'e
|
|
|
|
|
|
|
|
(* A chained list, but with a binder for each element into the next: [x := let a
|
|
|
|
= e1 in e2] is thus [Cons (e1, {a. Cons (e2, {x. Nil})})] *)
|
|
|
|
type 'e code_item_list =
|
2022-08-16 17:54:42 +03:00
|
|
|
| Nil
|
2023-01-23 14:19:36 +03:00
|
|
|
| Cons of 'e code_item * ('e, 'e code_item_list) binder
|
2022-07-28 11:36:36 +03:00
|
|
|
|
2022-11-21 12:12:45 +03:00
|
|
|
type struct_ctx = typ StructField.Map.t StructName.Map.t
|
|
|
|
type enum_ctx = typ EnumConstructor.Map.t EnumName.Map.t
|
2022-11-17 19:13:35 +03:00
|
|
|
|
|
|
|
type scope_out_struct = {
|
|
|
|
out_struct_name : StructName.t;
|
2022-11-21 12:12:45 +03:00
|
|
|
out_struct_fields : StructField.t ScopeVar.Map.t;
|
2022-11-17 19:13:35 +03:00
|
|
|
}
|
2022-10-21 16:47:17 +03:00
|
|
|
|
|
|
|
type decl_ctx = {
|
|
|
|
ctx_enums : enum_ctx;
|
|
|
|
ctx_structs : struct_ctx;
|
2022-11-28 18:23:27 +03:00
|
|
|
ctx_struct_fields : StructField.t StructName.Map.t IdentName.Map.t;
|
|
|
|
(** needed for disambiguation (desugared -> scope) *)
|
2022-11-21 12:12:45 +03:00
|
|
|
ctx_scopes : scope_out_struct ScopeName.Map.t;
|
2022-10-21 16:47:17 +03:00
|
|
|
}
|
|
|
|
|
2023-02-13 17:00:23 +03:00
|
|
|
type 'e program = { decl_ctx : decl_ctx; code_items : 'e code_item_list }
|