package catala
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=2615968670ac21b1d00386a9b04b3843
sha512=eff292fdd75012f26ce7b17020f5a8374eef37cd4dd6ba60338dfbe89fbcad3443d1b409e44c182b740da9f58dff7e76dcb8ddefe47f9b2b160666d1c6930143
doc/catala.runtime_ocaml/Catala_runtime/index.html
Module Catala_runtime
Source
The OCaml runtime.
Types
type code_location = {
filename : string;
start_line : int;
start_column : int;
end_line : int;
end_column : int;
law_headings : string list;
}
type io_input =
| NoInput
(*For an internal variable defined only in the scope, and does not appear in the input.
*)| OnlyInput
(*For variables that should not be redefined in the scope, because they appear in the input.
*)| Reentrant
(*For variables defined in the scope that can also be redefined by the caller as they appear in the input.
*)
This type characterizes the three levels of visibility for a given scope variable with regards to the scope's input and possible redefinitions inside the scope.
Exceptions
type error =
| AssertionFailed
(*An assertion in the program doesn't hold
*)| NoValue
(*No computation with valid conditions found
*)| Conflict
(*Two different valid computations at that point
*)| DivisionByZero
(*The denominator happened to be 0 here
*)| ListEmpty
(*Element access on an empty list
*)| NotSameLength
(*Traversing multiple lists of different lengths
*)| UncomparableDurations
(*Comparing durations in different units (e.g. months vs. days)
*)| AmbiguousDateRounding
(*ambiguous date computation, and rounding mode was not specified
*)| IndivisibleDurations
(*Dividing durations that are not in days
*)| Impossible
(*The "impossible" keyword was reached
*)
Value Embedding
type runtime_value =
| Unit
| Bool of bool
| Money of money
| Integer of integer
| Decimal of decimal
| Date of date
| Duration of duration
| Enum of string * string * runtime_value
| Struct of string * (string * runtime_value) list
| Array of runtime_value Array.t
| Tuple of runtime_value Array.t
| Unembeddable
Logging
Global process
The logging is constituted of two phases:
- The first one consists of collecting raw events (see
raw_event
) during the program execution (seeretrieve_log
) throught Logging instruments. - The second one consists in parsing the collected raw events into structured ones (see
event
).
Data structures
Represents information about a name in the code -- i.e. variable name, subscope name, etc...
It's a list of strings with a length varying from 2 to 3, where:
- the first string is the name of the current scope -- starting with a capitalized letter
Scope_name
, - the second string is either: the name of a scope variable or, the name of a subscope input variable --
a_subscope_var.input_var
- the third string is either: a subscope name (starting with a capitalized letter
Subscope_name
or, theinput
(resp.output
) string -- which corresponds to the input (resp. the output) of a function.
The raw events
type raw_event =
| BeginCall of information
(*Subscope or function call.
*)| EndCall of information
(*End of a subscope or a function call.
*)| VariableDefinition of information * io_log * runtime_value
(*Definition of a variable or a function argument.
*)| DecisionTaken of code_location
(*Source code position of an event.
*)
The structured events
The corresponding grammar of the event
type, is the following:
<event> := <fun_call> | <subscope_call> | <var_def> | <var_def_with_fun> | VariableDefinition <fun_call> := VariableDefinition (function input) <fun_call_beg> <event>* (<var_def> | <var_def_with_fun>) (function output) EndCall <var_def_with_fun> := /-- DecisionTaken pos of | <fun_call>+ (function calls needed to compute the variable value) \-> VariableDefinition <subscope_call> := <sub_var_def>* (sub-scope attributes def) <sub_call_beg> <event>+ EndCall <var_def> := DecisionTaken VariableDefinition(info, _) (when info.length = 2 && info[1] == "id") <sub_var_def> := DecisionTaken VariableDefinition(info, _) (when info.length = 3) <fun_call_beg> := BeginCall(info) (when info.length = 2) <sub_call_beg> := BeginCall(info) (when info.length = 2 and '.' in info[1])
and var_def = {
pos : code_location option;
name : information;
io : io_log;
value : runtime_value;
fun_calls : fun_call list option;
}
and fun_call = {
fun_name : information;
fun_inputs : var_def list;
body : event list;
output : var_def;
}
Parsing
retrieve_log ()
returns the current list of collected raw_event
.
Helping functions
Logging instruments
Pretty printers
pp_events ~is_first_call ppf events
pretty prints in ppf
the string representation of events
.
If is_first_call
is set to true, the formatter will be flush at the end. By default, is_first_call
is set to false.
Constructors and conversions
Rounding
This helper function rounds a rational to the nearest integer. Tie-breaker is the "half away from zero" rule: 0.5
is rounded to 1.0
and -0.5
is rounded to -1.0
. This function shall be used anytime rounding is necessary.
Money
Decimals
Integers
Dates
Usage: date_of_numbers year month day
.
Raises Failure on invalid inputs
Durations
Usage : duration_of_numbers year mounth day
.
Times
Defaults
val handle_exceptions :
('a * code_location) Optional.t array ->
('a * code_location) Optional.t
Operators
Modules API
Registers a module by the given name defining the given bindings. Required for evaluation to be able to access the given values. The last argument is expected to be a hash of the source file and the Catala version, and will in time be used to ensure that the module and the interface are in sync
Returns Ok
if it has been registered with the correct hash, Error h
if there is a hash mismatch.
Raises Not_found if the module does not exist at all