package catala
Install
Dune Dependency
Authors
Maintainers
Sources
md5=8f891209d18b6540df9c34b2d1a6a783
sha512=737770b87a057674bceefe77e8526720732552f51f424afcebcb6a628267eab522c4fd993caca1ae8ed7ace65a4a87e485af10c1676e51ca5939509a1b841ac2
doc/catala.shared_ast/Shared_ast/index.html
Module Shared_ast
Source
This module defines generic types for types, literals and expressions shared through several of the different ASTs.
Only used by surface
Used for unresolved structs/maps in desugared
Only used by desugared/scopelang
type scope_var_or_subscope =
| ScopeVar of ScopeVar.t
| SubScope of SubScopeName.t * ScopeName.t
Abstract syntax tree
Define a common base type for the expressions in most passes of the compiler
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.
These types allow to select the features present in any given expression type
'a any
is 'a, but adds the constraint that it should be restricted to valid AST kinds
type dcalc_lcalc_features =
< monomorphic : yes
; polymorphic : yes
; overloaded : no
; resolved : yes
; syntacticNames : no
; scopeVarStates : no
; scopeVarSimpl : no
; explicitScopes : no
; assertions : yes >
Features that are common to Dcalc and Lcalc
type ('a, 'b) dcalc_lcalc =
< dcalc_lcalc_features
; defaultTerms : 'a
; exceptions : 'b
; custom : no >
This type regroups Dcalc and Lcalc ASTs.
type ('a, 'b, 'c) interpr_kind =
< dcalc_lcalc_features
; defaultTerms : 'a
; exceptions : 'b
; custom : 'c >
This type corresponds to the types handled by the interpreter: it regroups Dcalc and Lcalc ASTs and may have custom terms
Types
and naked_typ =
| TLit of typ_lit
| TTuple of typ list
| TStruct of StructName.t
| TEnum of EnumName.t
| TOption of typ
| TArrow of typ list * typ
| TArray of typ
| TDefault of typ
| TAny
| TClosureEnv
(*Hides an existential type needed for closure conversion
*)
Constants and operators
type var_def_log = {
log_typ : naked_typ;
log_io_input : Runtime.io_input;
log_io_output : bool;
}
type log_entry =
| VarDef of var_def_log
(*During code generation, we need to know the type of the variable being logged for embedding as well as its I/O properties.
*)| BeginCall
| EndCall
| PosRecordIfTrueBool
Markings
Using empty markings will ensure terms can't be constructed: used for example in interfaces to ensure that they don't contain any expressions
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. The Custom
case can be used within passes that need to store specific information, e.g. typing
Type of values marked with the above standard mark GADT
Generic expressions
Define a common base type for the expressions in most passes of the compiler
type lit =
| LBool of bool
| LInt of Runtime.integer
| LRat of Runtime.decimal
| LMoney of Runtime.money
| LUnit
| LDate of date
| LDuration of duration
Literals are the same throughout compilation except for the LEmptyError
case which is eliminated midway through.
External references are resolved to strings that point to functions or constants in the end, but we need to keep different references for typing
type 'a glocation =
| DesugaredScopeVar : {
name : ScopeVar.t Catala_utils.Mark.pos;
state : StateName.t option;
} -> < scopeVarStates : yes.. > glocation
| ScopelangScopeVar : {
name : ScopeVar.t Catala_utils.Mark.pos;
} -> < scopeVarSimpl : yes.. > glocation
| SubScopeVar : {
scope : ScopeName.t;
alias : SubScopeName.t Catala_utils.Mark.pos;
var : ScopeVar.t Catala_utils.Mark.pos;
} -> < explicitScopes : yes.. > glocation
| ToplevelVar : {
name : TopdefName.t Catala_utils.Mark.pos;
} -> < explicitScopes : yes.. > glocation
Locations are handled differently in desugared
and scopelang
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 ex ample, 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.
The first argument of the base_gexpr type caracterises the "deep" type of the AST, while the second is the shallow type. They are always equal for well-formed AST types, but differentiating them ephemerally allows us to do well-typed recursive transformations on the AST that change its type
and ('a, 'b, 'm) base_gexpr =
| ELit : lit -> ('a, < .. >, 'm) base_gexpr
| EApp : {
f : ('a, 'm) gexpr;
args : ('a, 'm) gexpr list;
(*length may be 1 even if arity > 1 in desugared. scopelang performs detuplification, so length = arity afterwards
*)tys : typ list;
(*Set to
*)[]
before disambiguation
} -> ('a, < .. >, 'm) base_gexpr
| EAppOp : {
} -> ('a, < .. > as 'b, 'm) base_gexpr
| EArray : ('a, 'm) gexpr list -> ('a, < .. >, 'm) base_gexpr
| EVar : ('a, 'm) naked_gexpr Bindlib.var -> ('a, _, 'm) base_gexpr
| EAbs : {
binder : (('a, 'a, 'm) base_gexpr, ('a, 'm) gexpr) Bindlib.mbinder;
tys : typ list;
} -> ('a, < .. >, 'm) base_gexpr
| EIfThenElse : {
} -> ('a, < .. >, 'm) base_gexpr
| EStruct : {
name : StructName.t;
fields : ('a, 'm) gexpr StructField.Map.t;
} -> ('a, < .. >, 'm) base_gexpr
| EInj : {
name : EnumName.t;
e : ('a, 'm) gexpr;
cons : EnumConstructor.t;
} -> ('a, < .. >, 'm) base_gexpr
| EMatch : {
name : EnumName.t;
e : ('a, 'm) gexpr;
cases : ('a, 'm) gexpr EnumConstructor.Map.t;
} -> ('a, < .. >, 'm) base_gexpr
| ETuple : ('a, 'm) gexpr list -> ('a, < .. >, 'm) base_gexpr
| ETupleAccess : {
e : ('a, 'm) gexpr;
index : int;
size : int;
} -> ('a, < .. >, 'm) base_gexpr
| ELocation : 'b glocation -> ('a, < .. > as 'b, 'm) base_gexpr
| EScopeCall : {
scope : ScopeName.t;
args : ('a, 'm) gexpr ScopeVar.Map.t;
} -> ('a, < explicitScopes : yes.. >, 'm) base_gexpr
| EDStructAccess : {
name_opt : StructName.t option;
e : ('a, 'm) gexpr;
field : Ident.t;
} -> ('a, < syntacticNames : yes.. >, 'm) base_gexpr
(*
*)desugared
has ambiguous struct fields| EStructAccess : {
name : StructName.t;
e : ('a, 'm) gexpr;
field : StructField.t;
} -> ('a, < .. >, 'm) base_gexpr
(*Resolved struct/enums, after name resolution in
*)desugared
| EExternal : {
name : external_ref Catala_utils.Mark.pos;
} -> ('a, < explicitScopes : no.. >, 't) base_gexpr
| EAssert : ('a, 'm) gexpr -> ('a, < assertions : yes.. >, 'm) base_gexpr
| EDefault : {
} -> ('a, < defaultTerms : yes.. >, 'm) base_gexpr
| EPureDefault : ('a, 'm) gexpr -> ('a, < defaultTerms : yes.. >, 'm) base_gexpr
(*"return" of a pure term, so that it can be typed as
*)default
| EEmptyError : ('a, < defaultTerms : yes.. >, 'm) base_gexpr
| EErrorOnEmpty : ('a, 'm) gexpr -> ('a, < defaultTerms : yes.. >, 'm) base_gexpr
| ERaise : except -> ('a, < exceptions : yes.. >, 'm) base_gexpr
| ECatch : {
} -> ('a, < exceptions : yes.. >, 'm) base_gexpr
| ECustom : {
} -> ('a, < custom : yes.. >, 't) base_gexpr
(*A function of the given type, as a runtime OCaml object. The specified types for arguments and result must be the Catala types corresponding to the runtime types of the function.
*)
Useful for errors and printing, for example
The annotation is lifted outside of the box for expressions
('a, 'm) gexpr boxed
is ('a, 'm) boxed_gexpr
. The difference with ('a, 'm) 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, 'm) naked_gexpr, 'b) Bindlib.binder constraint 'e = ('a, 'm) gexpr
The expressions use the Bindlib library, based on higher-order abstract syntax
type ('e, 'b) mbinder = (('a, 'm) naked_gexpr, 'b) Bindlib.mbinder constraint 'e = ('a, 'm) gexpr
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.
type scope_let_kind =
| DestructuringInputStruct
(*
*)let x = input.field
| ScopeVarDefinition
(*
*)let x = error_on_empty e
| SubScopeVarDefinition
(*
*)let s.x = fun _ -> e
orlet 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
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 'e scope_let = {
scope_let_kind : scope_let_kind;
scope_let_typ : typ;
scope_let_expr : 'e;
scope_let_next : ('e, 'e scope_body_expr) binder;
scope_let_pos : Catala_utils.Pos.t;
} constraint 'e = ('a any, _) gexpr
This type is parametrized by the expression type so it can be reused in later intermediate representations.
and 'e scope_body_expr =
| Result of 'e
| ScopeLet of 'e scope_let
constraint 'e = ('a any, _) gexpr
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 Scopelang.Ast
statement.
type 'e scope_body = {
scope_body_input_struct : StructName.t;
scope_body_output_struct : StructName.t;
scope_body_expr : ('e, 'e scope_body_expr) binder;
} constraint 'e = ('a any, _) 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 '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 scope_info = {
in_struct_name : StructName.t;
out_struct_name : StructName.t;
out_struct_fields : StructField.t ScopeVar.Map.t;
}
In practice, this is a DAG: beware of repeated names
type decl_ctx = {
ctx_enums : enum_ctx;
ctx_structs : struct_ctx;
ctx_scopes : scope_info ScopeName.Map.t;
ctx_topdefs : typ TopdefName.Map.t;
ctx_struct_fields : StructField.t StructName.Map.t Ident.Map.t;
(*needed for disambiguation (desugared -> scope)
*)ctx_enum_constrs : EnumConstructor.t EnumName.Map.t Ident.Map.t;
ctx_scope_index : ScopeName.t Ident.Map.t;
(*only used to lookup scopes (in the root module) specified from the cli
*)ctx_modules : module_tree;
}
type 'e program = {
decl_ctx : decl_ctx;
code_items : 'e code_item_list;
lang : Catala_utils.Cli.backend_lang;
module_name : ModuleName.t option;
}
This module defines module names and path accesses, used to refer to separate compilation units.
Functions handling the code item structures of shared_ast
, in particular the scopes
Typing for the default calculus. Because of the error terms, we perform type inference using the classical W algorithm with union-find unification.
Reference interpreter for the default calculus
Optimization passes for default calculus and lambda calculus programs and expressions