123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469(* 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. *)(** 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 *)openCatala_utilsmoduleRuntime=Runtime_ocaml.RuntimemoduleScopeName=Uid.Gen()moduleTopdefName=Uid.Gen()moduleStructName=Uid.Gen()moduleStructField=Uid.Gen()moduleEnumName=Uid.Gen()moduleEnumConstructor=Uid.Gen()(** Only used by surface *)moduleRuleName=Uid.Gen()moduleLabelName=Uid.Gen()(** Used for unresolved structs/maps in desugared *)moduleIdentName=String(** Only used by desugared/scopelang *)moduleScopeVar=Uid.Gen()moduleSubScopeName=Uid.Gen()moduleStateName=Uid.Gen()(** {1 Abstract syntax tree} *)(** Define a common base type for the expressions in most passes of the compiler *)typedesugared=[`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. *)typescopelang=[`Scopelang]typedcalc=[`Dcalc]typelcalc=[`Lcalc]type'aany=[<desugared|scopelang|dcalc|lcalc]as'a(** ['a any] is 'a, but adds the constraint that it should be restricted to
valid AST kinds *)(** {2 Types} *)typetyp_lit=TBool|TUnit|TInt|TRat|TMoney|TDate|TDurationtypetyp=naked_typMarked.posandnaked_typ=|TLitoftyp_lit|TTupleoftyplist|TStructofStructName.t|TEnumofEnumName.t|TOptionoftyp|TArrowoftyplist*typ|TArrayoftyp|TAny(** {2 Constants and operators} *)typedate=Runtime.datetypeduration=Runtime.durationtypelog_entry=|VarDefofnaked_typ(** During code generation, we need to know the type of the variable being
logged for embedding *)|BeginCall|EndCall|PosRecordIfTrueBoolmoduleOp=struct(** Classification of operators on how they should be typed *)typemonomorphic=|Monomorphic(** Operands and return types of the operator are fixed *)typepolymorphic=|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. *)typeoverloaded=|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 *)typeresolved=|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:('aany,monomorphic)kind|Polymorphic:('aany,polymorphic)kind|Overloaded:([<desugared],overloaded)kind|Resolved:([<scopelang|dcalc|lcalc],resolved)kindtype(_,_)t=(* unary *)(* * monomorphic *)|Not:('aany,monomorphic)t|GetDay:('aany,monomorphic)t|GetMonth:('aany,monomorphic)t|GetYear:('aany,monomorphic)t|FirstDayOfMonth:('aany,monomorphic)t|LastDayOfMonth:('aany,monomorphic)t(* * polymorphic *)|Length:('aany,polymorphic)t|Log:log_entry*Uid.MarkedString.infolist->('aany,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|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(* binary *)(* * monomorphic *)|And:('aany,monomorphic)t|Or:('aany,monomorphic)t|Xor:('aany,monomorphic)t(* * polymorphic *)|Eq:('aany,polymorphic)t|Map:('aany,polymorphic)t|Concat:('aany,polymorphic)t|Filter:('aany,polymorphic)t|Reduce:('aany,polymorphic)t(* * 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|Div_dur_dur:([<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:('aany,polymorphic)tendtype('a,'k)operator=('aany,'k)Op.ttypeexcept=ConflictError|EmptyError|NoValueProvided|Crash(** {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. *)type'aglit=|LBool:bool->'aglit|LEmptyError:[<desugared|scopelang|dcalc]glit|LInt:Runtime.integer->'aglit|LRat:Runtime.decimal->'aglit|LMoney:Runtime.money->'aglit|LUnit:'aglit|LDate:date->'aglit|LDuration:duration->'aglit(** Locations are handled differently in [desugared] and [scopelang] *)type'aglocation=|DesugaredScopeVar:ScopeVar.tMarked.pos*StateName.toption->desugaredglocation|ScopelangScopeVar:ScopeVar.tMarked.pos->scopelangglocation|SubScopeVar:ScopeName.t*SubScopeName.tMarked.pos*ScopeVar.tMarked.pos->[<desugared|scopelang]glocation|ToplevelVar:TopdefName.tMarked.pos->[<desugared|scopelang]glocationtype('a,'t)gexpr=(('a,'t)naked_gexpr,'t)Marked.t(** 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,
are then defined with [type naked_expr = dcalc naked_gexpr] plus the
annotations.
A few tips on using this GADT:
- To write a function that handles cases from different ASTs, explicit the
type variables: [fun (type a) (x: a naked_gexpr) -> ...]
- For recursive functions, you may need to additionally explicit the
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. *)and('a,'t)naked_gexpr=(* Constructors common to all ASTs *)|ELit:'aglit->('aany,'t)naked_gexpr|EApp:{f:('a,'t)gexpr;args:('a,'t)gexprlist;}->('aany,'t)naked_gexpr|EOp:{op:('a,_)operator;tys:typlist}->('aany,'t)naked_gexpr|EArray:('a,'t)gexprlist->('aany,'t)naked_gexpr|EVar:('a,'t)naked_gexprBindlib.var->('aany,'t)naked_gexpr|EAbs:{binder:(('a,'t)naked_gexpr,('a,'t)gexpr)Bindlib.mbinder;tys:typlist;}->('aany,'t)naked_gexpr|EIfThenElse:{cond:('a,'t)gexpr;etrue:('a,'t)gexpr;efalse:('a,'t)gexpr;}->('aany,'t)naked_gexpr|EStruct:{name:StructName.t;fields:('a,'t)gexprStructField.Map.t;}->('aany,'t)naked_gexpr|EInj:{name:EnumName.t;e:('a,'t)gexpr;cons:EnumConstructor.t;}->('aany,'t)naked_gexpr|EMatch:{name:EnumName.t;e:('a,'t)gexpr;cases:('a,'t)gexprEnumConstructor.Map.t;}->('aany,'t)naked_gexpr|ETuple:('a,'t)gexprlist->('aany,'t)naked_gexpr|ETupleAccess:{e:('a,'t)gexpr;index:int;size:int;}->('aany,'t)naked_gexpr(* Early stages *)|ELocation:'aglocation->(([<desugared|scopelang]as'a),'t)naked_gexpr|EScopeCall:{scope:ScopeName.t;args:('a,'t)gexprScopeVar.Map.t;}->(([<desugared|scopelang]as'a),'t)naked_gexpr|EDStructAccess:{name_opt:StructName.toption;e:('a,'t)gexpr;field:IdentName.t;}->((desugaredas'a),'t)naked_gexpr(** [desugared] has ambiguous struct fields *)|EStructAccess:{name:StructName.t;e:('a,'t)gexpr;field:StructField.t;}->(([<scopelang|dcalc|lcalc]as'a),'t)naked_gexpr(** Resolved struct/enums, after [desugared] *)(* Lambda-like *)|EAssert:('a,'t)gexpr->(([<dcalc|lcalc]as'a),'t)naked_gexpr(* Default terms *)|EDefault:{excepts:('a,'t)gexprlist;just:('a,'t)gexpr;cons:('a,'t)gexpr;}->(([<desugared|scopelang|dcalc]as'a),'t)naked_gexpr|EErrorOnEmpty:('a,'t)gexpr->(([<desugared|scopelang|dcalc]as'a),'t)naked_gexpr(* Lambda calculus with exceptions *)|ERaise:except->((lcalcas'a),'t)naked_gexpr|ECatch:{body:('a,'t)gexpr;exn:except;handler:('a,'t)gexpr;}->((lcalcas'a),'t)naked_gexprtype('a,'t)boxed_gexpr=(('a,'t)naked_gexprBindlib.box,'t)Marked.t(** The annotation is lifted outside of the box for expressions *)type'eboxed=('a,'t)boxed_gexprconstraint'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 *)type('e,'b)binder=(('a,'t)naked_gexpr,'b)Bindlib.binderconstraint'e=('a,'t)gexpr(** The expressions use the {{:https://lepigre.fr/ocaml-bindlib/} Bindlib}
library, based on higher-order abstract syntax *)type('e,'b)mbinder=(('a,'t)naked_gexpr,'b)Bindlib.mbinderconstraint'e=('a,'t)gexpr(** {2 Markings} *)typeuntyped={pos:Pos.t}[@@ocaml.unboxed]typetyped={pos:Pos.t;ty:typ}(** 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. 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. *)type_mark=Untyped:untyped->untypedmark|Typed:typed->typedmark(** Useful for errors and printing, for example *)typeany_expr=AnyExpr:(_,_mark)gexpr->any_expr(** {2 Higher-level program structure} *)(** Constructs scopes and programs on top of expressions. The ['e] type
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. *)(** 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. *)typescope_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]*)type'escope_let={scope_let_kind:scope_let_kind;scope_let_typ:typ;scope_let_expr:'e;scope_let_next:('e,'escope_body_expr)binder;(* todo ? Factorise the code_item _list type below and use it here *)scope_let_pos:Pos.t;}constraint'e=(_any,_mark)gexpr(** 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. *)and'escope_body_expr=|Resultof'e|ScopeLetof'escope_letconstraint'e=(_any,_mark)gexprtype'escope_body={scope_body_input_struct:StructName.t;scope_body_output_struct:StructName.t;scope_body_expr:('e,'escope_body_expr)binder;}constraint'e=(_any,_mark)gexpr(** 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]. *)type'ecode_item=|ScopeDefofScopeName.t*'escope_body|TopdefofTopdefName.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'ecode_item_list=|Nil|Consof'ecode_item*('e,'ecode_item_list)bindertypestruct_ctx=typStructField.Map.tStructName.Map.ttypeenum_ctx=typEnumConstructor.Map.tEnumName.Map.ttypescope_out_struct={out_struct_name:StructName.t;out_struct_fields:StructField.tScopeVar.Map.t;}typedecl_ctx={ctx_enums:enum_ctx;ctx_structs:struct_ctx;ctx_struct_fields:StructField.tStructName.Map.tIdentName.Map.t;(** needed for disambiguation (desugared -> scope) *)ctx_scopes:scope_out_structScopeName.Map.t;}type'eprogram={decl_ctx:decl_ctx;code_items:'ecode_item_list}