package catala
Install
Dune Dependency
Authors
Maintainers
Sources
md5=1408a1cce45c7d5990b981e83e7589c2
sha512=eb3b923aa1f743378b4a05e30f50be5d180dc862a716270d747a90e469017f42fa5fc41352f02fbbf59cd2560f91c4f1b32cf38d80085b105d9387b0aed2039d
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
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.
'a any
is 'a, but adds the constraint that it should be restricted to valid AST kinds
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
| TAny
Constants and operators
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 'a glocation =
| DesugaredScopeVar : ScopeVar.t Catala_utils.Marked.pos * StateName.t option -> desugared glocation
| ScopelangScopeVar : ScopeVar.t Catala_utils.Marked.pos -> scopelang glocation
| SubScopeVar : ScopeName.t * SubScopeName.t Catala_utils.Marked.pos * ScopeVar.t Catala_utils.Marked.pos -> [< desugared | scopelang ] glocation
| ToplevelVar : TopdefName.t Catala_utils.Marked.pos -> [< desugared | scopelang ] 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 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 =
| ELit : 'a glit -> ('a any, 't) naked_gexpr
| EApp : {
} -> ('a any, 't) naked_gexpr
| EOp : {
} -> ('a any, 't) naked_gexpr
| EArray : ('a, 't) gexpr list -> ('a any, 't) naked_gexpr
| EVar : ('a, 't) naked_gexpr Bindlib.var -> ('a any, 't) naked_gexpr
| EAbs : {
binder : (('a, 't) naked_gexpr, ('a, 't) gexpr) Bindlib.mbinder;
tys : typ list;
} -> ('a any, 't) naked_gexpr
| EIfThenElse : {
} -> ('a any, 't) naked_gexpr
| EStruct : {
name : StructName.t;
fields : ('a, 't) gexpr StructField.Map.t;
} -> ('a any, 't) naked_gexpr
| EInj : {
name : EnumName.t;
e : ('a, 't) gexpr;
cons : EnumConstructor.t;
} -> ('a any, 't) naked_gexpr
| EMatch : {
name : EnumName.t;
e : ('a, 't) gexpr;
cases : ('a, 't) gexpr EnumConstructor.Map.t;
} -> ('a any, 't) naked_gexpr
| ETuple : ('a, 't) gexpr list -> ('a any, 't) naked_gexpr
| ETupleAccess : {
e : ('a, 't) gexpr;
index : int;
size : int;
} -> ('a any, 't) naked_gexpr
| ELocation : 'a glocation -> ([< desugared | scopelang ] as 'a, 't) naked_gexpr
| EScopeCall : {
scope : ScopeName.t;
args : ('a, 't) gexpr ScopeVar.Map.t;
} -> ([< desugared | scopelang ] as 'a, 't) naked_gexpr
| EDStructAccess : {
name_opt : StructName.t option;
e : ('a, 't) gexpr;
field : IdentName.t;
} -> (desugared as '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
| EAssert : ('a, 't) gexpr -> ([< dcalc | lcalc ] as 'a, 't) naked_gexpr
| EDefault : {
} -> ([< desugared | scopelang | dcalc ] as 'a, 't) naked_gexpr
| EErrorOnEmpty : ('a, 't) gexpr -> ([< desugared | scopelang | dcalc ] as 'a, 't) naked_gexpr
| ERaise : except -> (lcalc as 'a, 't) naked_gexpr
| ECatch : {
} -> (lcalc as 'a, 't) naked_gexpr
The annotation is lifted outside of the box for expressions
('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.binder constraint 'e = ('a, 't) gexpr
The expressions use the Bindlib library, based on higher-order abstract syntax
type ('e, 'b) mbinder = (('a, 't) naked_gexpr, 'b) Bindlib.mbinder constraint 'e = ('a, 't) gexpr
Markings
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.
Useful for errors and printing, for example
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 = (_ any, _ mark) 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 = (_ any, _ mark) 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 = (_ 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 'e code_item =
| ScopeDef of ScopeName.t * 'e scope_body
| Topdef of TopdefName.t * typ * 'e
type scope_out_struct = {
out_struct_name : StructName.t;
out_struct_fields : StructField.t ScopeVar.Map.t;
}
type decl_ctx = {
ctx_enums : enum_ctx;
ctx_structs : struct_ctx;
ctx_struct_fields : StructField.t StructName.Map.t IdentName.Map.t;
(*needed for disambiguation (desugared -> scope)
*)ctx_scopes : scope_out_struct ScopeName.Map.t;
}
Functions handling the code item structures of shared_ast
, in particular the scopes