Module Stan_math_backend.Cpp

A set of data types representing the C++ we generate

type identifier = Core.string
val identifier_of_sexp : Sexplib0.Sexp.t -> identifier
val sexp_of_identifier : identifier -> Sexplib0.Sexp.t
val pp_identifier : Ppx_deriving_runtime.Format.formatter -> identifier -> Ppx_deriving_runtime.unit
val show_identifier : identifier -> Ppx_deriving_runtime.string
type type_ =
  1. | Auto
  2. | Void
  3. | Int
  4. | Double
  5. | Complex of type_
  6. | TemplateType of identifier
  7. | StdVector of type_
    (*

    A std::vector. For Eigen Vectors, use Matrix with a row or column size of 1

    *)
  8. | Array of type_ * Core.int
  9. | Tuple of type_ Core.list
  10. | TypeLiteral of identifier
    (*

    Used for things like Eigen::Index

    *)
  11. | NonTypeTemplateInt of Core.int
  12. | Matrix of type_ * Core.int * Core.int * Middle.Mem_pattern.t
  13. | Ref of type_
  14. | Const of type_
  15. | Pointer of type_
  16. | TypeTrait of identifier * type_ Core.list
    (*

    e.g. stan::promote_scalar, stan:base_type

    *)

C++ types

val type__of_sexp : Sexplib0.Sexp.t -> type_
val sexp_of_type_ : type_ -> Sexplib0.Sexp.t
val pp_type_ : Ppx_deriving_runtime.Format.formatter -> type_ -> Ppx_deriving_runtime.unit
val show_type_ : type_ -> Ppx_deriving_runtime.string
module Types : sig ... end

Helpers for constructing types

type operator =
  1. | Multiply
  2. | Divide
  3. | Add
  4. | Subtract
  5. | Eq
  6. | LEq
  7. | GEq
  8. | Lthn
  9. | Gthn
  10. | And
  11. | Or
val operator_of_sexp : Sexplib0.Sexp.t -> operator
val sexp_of_operator : operator -> Sexplib0.Sexp.t
type expr =
  1. | Literal of Core.string
    (*

    printed as-is

    *)
  2. | Var of identifier
  3. | VarRef of identifier
  4. | Parens of expr
  5. | FunCall of identifier * type_ Core.list * expr Core.list
  6. | MethodCall of expr * identifier * type_ Core.list * expr Core.list
  7. | StaticMethodCall of type_ * identifier * type_ Core.list * expr Core.list
  8. | Constructor of type_ * expr Core.list
    (*

    printed as type(expr1, expr2, ...)

    *)
  9. | InitializerExpr of type_ * expr Core.list
    (*

    printed as type{expr1, expr2, ...}

    *)
  10. | ArrayLiteral of expr Core.list
  11. | TernaryIf of expr * expr * expr
  12. | Cast of type_ * expr
  13. | Subscript of expr * expr
  14. | Deref of expr
  15. | AllocNew of type_ * expr Core.list
  16. | OperatorNew of identifier * type_ * expr Core.list
    (*

    See operator new for distinctions between allocating and placing news

    *)
  17. | Assign of expr * expr
    (*

    NB: Not all exprs are valid lvalues!

    *)
  18. | StreamInsertion of expr * expr Core.list
    (*

    Corresponds to operator<<

    *)
  19. | BinOp of expr * operator * expr
  20. | PMinus of expr
  21. | Increment of expr
val expr_of_sexp : Sexplib0.Sexp.t -> expr
val sexp_of_expr : expr -> Sexplib0.Sexp.t
module Exprs : sig ... end

Some helper values and functions

type init =
  1. | Assignment of expr
  2. | Construction of expr Core.list
  3. | InitializerList of expr Core.list
  4. | Uninitialized
val init_of_sexp : Sexplib0.Sexp.t -> init
val sexp_of_init : init -> Sexplib0.Sexp.t
type variable_defn = {
  1. static : Core.bool;
  2. constexpr : Core.bool;
  3. type_ : type_;
  4. name : identifier;
  5. init : init;
}
val make_variable_defn : ?static:??? -> ?constexpr:??? -> type_:type_ -> name:identifier -> ?init:??? -> unit -> variable_defn
val variable_defn_of_sexp : Sexplib0.Sexp.t -> variable_defn
val sexp_of_variable_defn : variable_defn -> Sexplib0.Sexp.t
type stmt =
  1. | Expression of expr
  2. | VariableDefn of variable_defn
  3. | For of variable_defn * expr * expr * stmt
  4. | ForEach of type_ * identifier * expr * stmt
  5. | While of expr * stmt
  6. | IfElse of expr * stmt * stmt Core.option
  7. | ConstexprIf of type_ * stmt
  8. | TryCatch of stmt Core.list * type_ * identifier * stmt Core.list
  9. | Block of stmt Core.list
  10. | Return of expr Core.option
  11. | Throw of expr
  12. | Break
  13. | Continue
  14. | Using of Core.string * type_ Core.option
  15. | Comment of Core.string
val stmt_of_sexp : Sexplib0.Sexp.t -> stmt
val sexp_of_stmt : stmt -> Sexplib0.Sexp.t
module Stmts : sig ... end

Helpers for common statement constructs

module Decls : sig ... end

Declarations which get reused often in the generated model

type template_parameter =
  1. | Typename of Core.string
    (*

    A typename, e.g. template <typename Foo>

    *)
  2. | Bool of Core.string
    (*

    A named boolean non-type template parameter

    *)
  3. | Require of Core.string * Core.string Core.list
    (*

    A straightforward require_...<...> template.

    *)
  4. | RequireAllCondition of Core.string * type_ Core.list
    (*

    A C++ type trait (e.g. is_arithmetic) and the template types which need to satisfy that. These are collated into one require_all_t<...>.

    *)
val template_parameter_of_sexp : Sexplib0.Sexp.t -> template_parameter
val sexp_of_template_parameter : template_parameter -> Sexplib0.Sexp.t
type cv_qualifiers =
  1. | Const
  2. | Final
  3. | NoExcept
val cv_qualifiers_of_sexp : Sexplib0.Sexp.t -> cv_qualifiers
val sexp_of_cv_qualifiers : cv_qualifiers -> Sexplib0.Sexp.t
type fun_defn = {
  1. templates_init : template_parameter Core.list Core.list * Core.bool;
    (*

    Double list since some functions (mainly reduce_sum functors) need two sets of templates

    *)
  2. inline : Core.bool;
  3. return_type : type_;
  4. name : identifier;
  5. args : (type_ * Core.string) Core.list;
  6. cv_qualifiers : cv_qualifiers Core.list;
  7. body : stmt Core.list Core.option;
}
val make_fun_defn : ?templates_init:??? -> ?inline:??? -> return_type:type_ -> name:identifier -> ?args:??? -> ?cv_qualifiers:??? -> ?body:??? -> unit -> fun_defn
val fun_defn_of_sexp : Sexplib0.Sexp.t -> fun_defn
val sexp_of_fun_defn : fun_defn -> Sexplib0.Sexp.t
val split_fun_decl_defn : fun_defn -> fun_defn * fun_defn
type constructor = {
  1. args : (type_ * Core.string) Core.list;
  2. init_list : (identifier * expr Core.list) Core.list;
  3. body : stmt Core.list;
}
val make_constructor : ?args:??? -> ?init_list:??? -> ?body:??? -> unit -> constructor
val constructor_of_sexp : Sexplib0.Sexp.t -> constructor
val sexp_of_constructor : constructor -> Sexplib0.Sexp.t
type directive =
  1. | Include of Core.string
  2. | IfNDef of Core.string * defn Core.list
  3. | MacroApply of Core.string * Core.string Core.list

Incomplete list of C++ preprocessor directives

and class_defn = {
  1. class_name : identifier;
  2. final : Core.bool;
  3. public_base : type_;
  4. private_members : defn Core.list;
  5. public_members : defn Core.list;
  6. constructor : constructor;
  7. destructor_body : stmt Core.list;
}

The Stan model class always has a non-default constructor and destructor

and struct_defn = {
  1. param : template_parameter Core.option;
  2. struct_name : identifier;
  3. body : defn Core.list;
}
and defn =
  1. | FunDef of fun_defn
  2. | Class of class_defn
  3. | Struct of struct_defn
  4. | GlobalVariableDefn of variable_defn
  5. | GlobalComment of Core.string
  6. | GlobalUsing of Core.string * type_ Core.option
  7. | Namespace of identifier * defn Core.list
  8. | Preprocessor of directive
val directive_of_sexp : Sexplib0.Sexp.t -> directive
val class_defn_of_sexp : Sexplib0.Sexp.t -> class_defn
val struct_defn_of_sexp : Sexplib0.Sexp.t -> struct_defn
val defn_of_sexp : Sexplib0.Sexp.t -> defn
val sexp_of_directive : directive -> Sexplib0.Sexp.t
val sexp_of_class_defn : class_defn -> Sexplib0.Sexp.t
val sexp_of_struct_defn : struct_defn -> Sexplib0.Sexp.t
val sexp_of_defn : defn -> Sexplib0.Sexp.t
val make_class_defn : name:identifier -> public_base:type_ -> ?final:??? -> private_members:defn Core.list -> public_members:defn Core.list -> constructor:constructor -> ?destructor_body:??? -> unit -> class_defn
val make_struct_defn : param:template_parameter Core.option -> name:identifier -> body:defn Core.list -> unit -> struct_defn
module DSL : sig ... end

A set of operators and helpers to make the OCaml code look more like the resultant C++

val (!//) : Core.string -> defn

Shorthand for a C++ comment. This one is rather harmless, so it isn't in its own module

type program = defn Core.list

Much like in C++, we define a translation unit as a list of definitions

val program_of_sexp : Sexplib0.Sexp.t -> program
val sexp_of_program : program -> Sexplib0.Sexp.t
module Printing : sig ... end

Pretty-printing of the C++ type

module Tests : sig ... end