# Inference rules for overloaded functions selection

Inference rules are the rules which help to determine the particular version of an overloaded function (that with generic parameters) in an expression.

Because the ST old standart doesn't support overloaded functions declared by an user, there are no ST language means how to describe them. So we will use C-like notation.

Example:

`ADD (ANY_MAGNITUDE, ANY_MAGNITUDE, ... ) => ANY_MAGNITUDE`

Overloaded functions may be specialized in an expression which means there are rules how to specify, which non-generic types will be used for the parameters for the function.

Example:

`ADD_INT (INT, INT, ... ) => INT`

Goal of the inference rules is to determine suitable specialized function for the expression given any particular argument values.

Example:

` VAR ix, iy : INT; END_VAR VAR dix, diy : DINT; END_VAR VAR rx, ry : REAL; END_VAR VAR rlx, rly : LREAL; END_VAR ADD (1, 2, 3) => the expression is the same as ADD_DINT. (note: if not specified otherwise then the 1, 2, 3 constants are interpreted as DINT types) ADD (x, y) => ADD_INT ADD (rx, ix) => ADD_REAL ADD (ix, diy, rlx) => ADD_LREAL`

Inference process for an expression goes roughly this way:

- Arguments to overloaded function must belong to the generic type categories of corresponding parameters of the overloaded function.
- Parameters of the function are grouped by their generic type categories (lets call that group p-group)
- For each p-group determine the most precise type (lets call it c-type) which can hold values of all arguments simultaneously, and that will be the type used for that generic type parameters in p-group.
- All arguments from the p-group are expected to be implicitly convertible to the group's c-type and are converted into that type before a function is called.

Example:

for an expression

`ADD (ix, diy, rlx)`

there is one p-group with generic type ANY_MAGNITUDE in the ADD function, and the parameters IN1, IN2, IN3 belongs into it and corresponding arguments are ix, diy, rlx. Thus c-type is determined to be LREAL. As a result the above expression is equivalent to the expression:

`ADD_LREAL (ix, diy, rlx) and result is LREAL`

Example:

`MUX (K := dix, IN0 := rx, IN1 := dix)`

there are two p-groups, ANY_INT and ANY. Parameter K belongs to former group and IN0, IN1 into the latter one. Thus ANY_INT group's c-type is DINT and ANY's group c-type is REAL. As a result the above expression is equivalent to the expression:

`MUX_DINT_REAL (K := dix, IN0 := rx, IN1 := dix) and result is REAL`