Syntax proposals / Expression and constant values
Is your feature request related to a problem? Please describe. It is quite common in GBOML to want to define "patterns", "functions" or similar things that depends on Node, Variables or Parameters, that will be reused multiple times without actually needing a variable. For example:
#NODE X
#PARAMETERS
unit_price = 2;
//...
#VARIABLES
internal: price; //only read
external: quantity; //can be constrained from outside
#CONSTRAINTS
price == quantity * unit_price;
This is redundant, as price
does not need to be a separate variable in the solver.
Moreover, with the various proposals #9 #10 #11 #12 we have arising ambiguity over "what is a constant, what is a variable". Take this example:
#NODE X
#PARAMETERS
x = {a, b};
#VARIABLES
internal: a;
internal: b;
#CONSTRAINTS
x[0] == x[1];
or
#NODE X
#PARAMETERS
a = 3
#VARIABLES
internal: x[4];
#CONSTRAINTs
x[i] == 0 for i in [0:a];
#NODE Y
#PARAMETERS
a = {x[0], x[1], x[2], x[3]};
#VARIABLES
internal: x[4];
#CONSTRAINTs
v == 0 for v in a;
Moreover users may want to define reusable bits of code, aka functions, which is not currently possible.
Describe the solution you'd like We propose to separate expression values from constant value:
- A constant value is a value that either:
- is an integer, a float, or a string
- or depends only on other constant values
- An expression value is a value that may depend on something that is not a constant value, that is a Node, a Variable, or something that itself may depend on an expression value.
This definition implies that a constant value IS AN expression value.
We believe it is needed for readibility to ensure that the users understands what is a constant value and what is an expression value. For this we introduce a new notation, <-
:
#PARAMETERS
a = 2; //a is a constant value
b <- 2; //b is an expression value
Expressions can thus contains arrays/dict/... containing mostly anything, from constants to Nodes via Variables. There are not evaluated directly.
We can create a syntax for functions:
#PARAMETERS
f(x) <- 2*x;
#VARIABLES
internal a;
#CONSTRAINTS
f(a) == 1;
Semantically, we can say that all expression value are actually functions:
a <- b;
//is equivalent to
a() <- b;
//parenthesis can be omitted when calling functions without parameters
Open questions
Redifining a constant value to an expression
It's probably a good idea to allow redefining constant values to expressions (the opposite is trivial, as a constant value IS an expression value). However we want to avoid copy pasting. Here is an example:
#NODE A
a = 2
b = a * 2
#NODE B import A with
a <- 2 * x
//boom, b is a constant and depends on a, so if we stop here it fails!
//we can redefine it like this, but this is a copy-paste:
b <- a * 2
//an idea of notation to avoid copy/paste. Avoid adding a new keyword, but maybe not that readable...
b <- b;
Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.
Additional context Add any other context or screenshots about the feature request here.