FoldLink is a kind of FunctionLink that implements the reduction of repeated terms. It is used by both PlusLink and TimesLink to consolidate repeated NumberNodes.
FoldLinks can be reduced with the
Users should never use the FoldLink atom directly; rather, it is a point in the type hierarchy that provides tooling and functions for other atom types.
FoldLinks implement the concept of the fold functor in computer science.
The reduction of FoldLinks proceeds by grouping together similar terms under the fold operation. Thus, for example, the reduction of
(PlusLink (NumberNode 1) (NumberNode 2) (NumberNode 3))
because the number nodes are all of the same type, and addition is defined on number nodes.
The FoldLink reduction routine attempts to be generic, by assuming only that the list to be folded is associative, and not commutative. Thus, arithmetic reduction also makes use of sort phase, so that numbers, variables, and general terms are grouped next to one-another.
The reduction routine for FoldLink is written in C++, but its surprising complexity suggests that perhaps implementing reduction in C++ is not a wise choice. See the cog-reduce! page for additional comments on reduction design.