Optionally / conditionally mutable parameters
Introduction
In the textS / textM language, currently all parameters are immutable. Let us consider the following use case: We have a model with a mistake (or an error), for example:
a = 1
b = a / 0
c = 2
f(x) = b * x
print(f(c))
In this model, variable b
will never have a value and can never be used again in the same model. What is more, it can be referenced but all statements including such references will never be evaluated. Also references can be included in functions and such functions must be redefined under new names in order to be used.
In instant and deferred evaluation mode, mutable parameters are not needed because the lack of persistence. The correction can be done in the model source and then the model can be evaluated once again. In contrast, in workflow evaluation mode erroneous parameters prevent the completion of possibly large parts of the model.
FireWorks has a spec update method. But this method is limited to editing the spec keys, including the tasks and their parameters. One cannot add new dependencies or remove dependencies. Thus, for the example above it is not valid to redefine b = 2 / 0
because the reference to a
is removed. Also b := a / c
will not be valid because it adds a new reference to c
. But if we want to change some literals like b := a / 2
this can be possible. Thus the first constraint would be to preserve the list of references. Thus, b := a / (a + 1)
is a valid modification.
Because of static typing and possible references to b
, the type of b
may also not change after the modification. Otherwise there might be type errors in parameters with such references. In the example above, the original type of b
is Float. However, replacing the 0
with a complex number, like b := a / (1 + 1 j)
will still yield a valid input for f(x) = b * x
. Therefore, the type might be changed, and because all variables including references to b
have to be re-evaluated anyway, also their types will be re-evaluated and checked.
Implementation
Extend grammar
ModifiedVariable:
name = GeneralReference &/,|\)|=/ (':=' parameter = Parameter)? (resources = Resources)?
;
The name
attribute may need to be avoided to keep all references to existing name
unambiguous.
Apply constraints
To keep things simple, prevent using the ModifiedVariable
objects in deferred and instant mode. This can be done with a constraint and further process models only in workflow mode.
For each ModifyVariable
model object
- One-to-one relationship must hold, i.e. there may be no two or more
ModifiedVariable
objects referencing the sameVariable
. - The sets of references in the
parameter
of theModifyVariable
and of the referencedVariable
object are identical.Resources
may include no references by grammar - no checks needed.
Manipulate the model
For each ModifyVariable
model object
- Replace the
parameter
and theresources
of theVariable
object with those of the pertinentModifiedVariable
- Set a special attribute to indicate that the variable has been reset
- Replace all other attributes such as
_source_code
,_grammar_version
etc.
Apply all other constraints: cycles, type checks etc.
Run the interpreter
- Process reset variables (sub-case of persistent variables)
- the fireworks of reset variables must be found, updated and rerun
- the descendants of reset variables will be automatically rerun by FireWorks
- Process all the rest
Session
In the cases when we want to re-evaluate a variable parameter, i.e. rerun a node without changing it, we can add a magic like %rerun b
or multiple %rerun (z, b)
. This is helpful, if some nodes are in FIZZLED state due to a side effect.