ModelicaSpecification
ModelicaSpecification copied to clipboard
size(A, j) as constant expression
Reported by perost on 20 Mar 2018 13:20 UTC The specification makes an exception from the normal variability rules for size(A, j), such that it's a parameter expression as long as j is a parameter expression and A isn't declared in a function. The rationale here seems to be that it's the variability of the particular dimension of A that matters, not the variability of A itself.
However, there's no such exception in the section for constant expressions, which means that size(A, j) where A and j are constant expressions is a constant expression. This is also used in the MSL, for example to define the constants nS and nC in Modelica.Media.Interfaces.PartialMedium.
This means that size can be used to "cast" a parameter expression into a constant expression:
parameter Integer p = 3;
// Not legal:
constant Integer c = p;
// Currently legal:
constant Real x[p] = zeros(p);
constant Integer c = size(x, 1);
Would it not make more sense to define the variability of size(A, j) in terms of the variability of dimension j of A instead? I.e. the variability of size(A, j) should be the most variable of the expression j and the dimension j of the variable A.
Migrated-From: https://trac.modelica.org/Modelica/ticket/2240
Comment by sjoelund.se on 20 Mar 2018 13:34 UTC
zeros(p) is not a constant expression.
Comment by perost on 20 Mar 2018 13:43 UTC Replying to [comment:1 Martin Sjölund]:
zeros(p)is not a constant expression.
zeros(size(x, 1)) then.
Comment by hansolsson on 21 Mar 2018 10:01 UTC Language group: Another way of formulating the problem is that we have a constant array with parameter-size; that seems odd. (Stated in 4.4.2.)
Could we forbid that instead?
Array dimensions shall be non-negative parameter expressions, or the colon operator denoting that the array dimension is left unspecified.
->
Array dimensions shall be non-negative parameter expressions, or the colon operator denoting that the array dimension is left unspecified. For components that are constant the array dimensions shall be non-negative constant expressions, or the colon operator denoting that the array dimension is left unspecified.
(seems more awkward to make it one sentence)
Need to analyze if this impacts MSL or other libraries. Note that the other possibility of always making size a parameter-expression would impact MSL. Continuing discussion whether structural parameters should be constants.
Comment by hansolsson on 21 Mar 2018 10:32 UTC Alternative: size is constant expression, except in function where it is a discrete expression if declared with ":" (and some more cases - since constant should lead to constant evaluation).
function foo
input Integer n;
protected
Real x[n]=ones(n);
Gerd: Add structural parameter to specification.
Need to test.
Comment by hansolsson on 21 Mar 2018 10:01 UTC Language group: Another way of formulating the problem is that we have a constant array with parameter-size; that seems odd. (Stated in 4.4.2.)
Could we forbid that instead?
Array dimensions shall be non-negative parameter expressions, or the colon operator denoting that the array dimension is left unspecified.
->
Array dimensions shall be non-negative parameter expressions, or the colon operator denoting that the array dimension is left unspecified. For components that are constant the array dimensions shall be non-negative constant expressions, or the colon operator denoting that the array dimension is left unspecified.
(seems more awkward to make it one sentence)
Need to analyze if this impacts MSL or other libraries. Note that the other possibility of always making size a parameter-expression would impact MSL. Continuing discussion whether structural parameters should be constants.
I tried adding an error for constant arrays with non-constant array dimensions in our compiler, I did not detect any errors in either MSL or the Modelon libraries.