Repository navigation
Fold_c
higher-order functions fold_c combinator
- fold_c(step, condition, maxN, initial state)
- fold_c(F(a,
_,_), G(b,_), maxN, initial state) - instantiate fold_c(step, condition, maxN, initial state)
fold_c(step, condition, maxN, initial state) applies the function step for as long as the function condition is true for the state, at most maxN times. It starts from initial state, tests condition on the state before each application, and yields the first state for which condition is false. When condition is false for initial state, fold_c yields initial state.
function AddStep(parameter<uint32> k; parameter<uint32> acc) -> parameter<uint32> := acc + k + 1u;
function Below(parameter<uint32> limit; parameter<uint32> acc) -> parameter<bool> := acc < limit;
parameter<uint32> total := fold_c(AddStep, Below(20u, _), 100u, 0u); // 0, 1, 3, 6, 10, 15, 21: total is 21
The step has two parameters. The first takes the number of the step, a parameter<uint32> that counts from 0; the second takes the state. The result of the step must fit its second parameter, since the next application takes it as its state. A step that does not need the number still declares the parameter, so the same step can serve fold, whose step takes an element of a domain in that place.
The condition has one parameter, the state, and returns a parameter<bool>: true to take another step, false to stop.
maxN is a ceiling, not a stopping rule. When condition is still true after maxN steps, fold_c reports an error instead of yielding a state that has not converged. Choose maxN as a bound that a correct calculation never exceeds, such as the number of points when each step removes at least one.
The state can be a parameter, an attribute or a unit with subitems, as with fold:
unit<uint32> Vals5 : nrofrows = 5
{
attribute<float64> v : [5, 3, 8, 1, 4];
}
function DropMax(parameter<uint32> e; unit<uint32> s { attribute<float64> v; }) -> unit<uint32> := kept
{
unit<uint32> kept := select_with_org_rel(s/v < max(s/v))
{
attribute<float64> v := s/v[kept/org_rel];
}
}
function Longer(parameter<uint32> n; unit<uint32> s) -> parameter<bool> := #s > n;
unit<uint32> two := fold_c(DropMax, Longer(2u, _), 10u, Vals5); // the two smallest values: two/v is [3, 1]
Whether to take another step depends on data, so GeoDMS calculates the condition of each state at meta-information time, when it determines the item that fold_c defines and before the data of that item is asked for. The meta information of a fold_c item, such as the calculation rule it stands for and, with a unit as the state, which unit it is, therefore depends on data: determining the item, for instance by opening it or one of its sub-items in the GUI, calculates the states up to the one where fold_c stops, which takes about as long as calculating the item itself. The data of each state is kept while the next one is calculated, so the conditions cost one pass over the steps.
The item keeps the data of the state where fold_c stops, and of the state before it, from which a sub-item of a unit state may still have to be calculated, and takes its data from there, so each step is calculated once, as in a fold over the same number of steps. These two states are released as soon as the data of the item, or of a sub-item that its state declares, has been asked for, and the calculation that this starts holds what it reads. Until then they stay in memory: an item that is determined but whose data is never asked for keeps them until the item is invalidated, for instance when a source changes, or until the configuration is closed.
The definition check of a function does not depend on that data. It checks the types of the step twice, first for initial state and then for the state that the step returns, and those of the condition on both, so a step whose result does not fit its own input, or a condition that does not return a parameter<bool>, is reported when the function is defined. This holds for a function and for a partial application of one, as in Below(20u, _) above.
The step and the condition may be functions that hold variants, as with fold. fold_c chooses the variant of the condition on initial state, before it tests it the first time, and the variant of the step on the step number 0 and initial state, before the first step; every later test and step uses the same variant:
function AddV
{
variant u32(parameter<uint32> k; parameter<uint32> acc) -> parameter<uint32> := acc + k + 1u;
variant f64(parameter<uint32> k; parameter<float64> acc) -> parameter<float64> := acc + float64(k) + 1.0;
}
function BelowV
{
variant u32(parameter<uint32> acc) -> parameter<bool> := acc < 6u;
variant f64(parameter<float64> acc) -> parameter<bool> := acc < 6.0;
}
parameter<float64> total := fold_c(AddV, BelowV, 10u, 0.0); // both use the variant f64; the states are 0, 1, 3 and 6, so total is 6
When a later state would choose another variant, or none, fold_c reports an error that names the chosen variant and the value type of that state; it does not switch variants on the way. The definition check of a function in which fold_c is used does not check a step or condition that holds variants; the chosen variant is checked when it is chosen.
container trace := instantiate fold_c(...) makes an item for each step that is taken, as instantiate fold(...) does (fold): step0, step1, ..., each holding the state after that step, and result, the state that fold_c yields. How many items there are depends on the data, so the items appear once the states have been calculated. In a function body it works as instantiate fold(...) does there: a copy of the function made with instantiate F(args) gets the items, and the result of a function that is applied as a value can refer to trace/result and to the steps.
container trace := instantiate fold_c(DropMax, Longer(2u, _), 10u, Vals5); // step0, step1, step2 and result
-
step: a function of two parameters, a function whose variants each take two parameters, or a partial application with two
_holes of a function without variants. -
condition: a function of one parameter that returns a
parameter<bool>, a function whose variants each take one parameter and return aparameter<bool>, or a partial application with one_hole of a function without variants. -
maxN: a parameter with an integer value type, such as
parameter<uint32>. - initial state: a parameter, an attribute or a unit, not a function.
- fold, which applies the step once per element of a domain
- Function-definition (repeating a step)
- loop and iterate, which repeat a Template
20.29.0
GeoDMS ©Object Vision BV. Source code distributed under GNU GPL-3. Documentation distributed under CC BY-SA 4.0.