Execution

From OpenCog
(Redirected from Cog-execute!)

All Atoms can be used to represent symbolic knowledge as a graph: this is what the AtomSpace is -- an in-RAM database for (hyper-)graphs encoding knowledge. However, many atoms can also be executed, thus performing the actual computations that they encode as graphs.

The most direct example of this would be the graph

(Plus (Number 2) (Number 3))

This is a graph -- a tree -- that symbolically encodes the concept of "adding together 2 and 3". As such, this graph can be saved to disk, for later retrieval; one can apply symbolic reasoning or inference; one can perform reduction, or any number of other graphical manipulations to this expression.

It can also be directly executed, to produce the number 5.

Graphs which encode knowledge, but which also can be executed are referred to as Atomese. They can be visualized as "pipes" through which values "flow", like water through water pipes. The symbolic representation, as graphs, allows "plumbers" - such as reasoning, inferencing or reduction systems to analyze and alter the shapes and connections of the pipes (of the graphs). These plumbers represent knowledge by arranging the pipes in certain ways. Once some particular executable graph has been constructed, it can then be put to work, actually preforming the computation that it abstractly, symbolically represents.

Thus, many Atoms play a dual role: they are used to represent knowledge, and also to perform computation. A large class of executable atoms are the FunctionLinks, which include the arithmetic link types, as well as many others. Examples include the ValueOf Link, which can be used to introduce values into the processing pipeline. Another is the ExecutionOutputLink, which can be used to interface to external (non-Atomese) systems.

cog-execute!

The cog-execute! function is a scheme function that is able to execute all executable links types. It is part of (opencog exec) module. This page reviews what cog-execute! does today. This may change over time.

When cog-execute! is called on an atom, it forces execution to take place immediately. The result (output) of execution may be an Atom or a Value. If the result is an Atom, then it is immediately placed in the current AtomSpace. (This is subject to change in the future, as the semantics and use-cases of multiple AtomSpaces become more clear).

Non-executable Atoms are left alone, when executed: they are treated as constants. Thus, for example:

(cog-execute! (ConceptNode "asdf"))

results in:

(ConceptNode "asdf")

Example: PlusLink

The PlusLink is an excellent, easy example of a FunctionLink that cog-execute! knows how to handle. Thus

(cog-execute! 
   (PlusLink
      (NumberNode "2")
      (NumberNode "3")))

returns

(NumberNode "5")

The word "returns" here has two meanings: the scheme function cog-execute! literally returns (NumberNode "5") as it's return value, and also, as a side effect, the atom (NumberNode "5") is placed into the atomspace. These are two different concepts of "return" that are easy to confuse and conflate with each-other. Be careful.

Dynamic Execution

The cog-execute! function forces execution to happen at the time that it is called. Execution can also be made implicit, with promises and futures. For example,

(FormulaStream (TimeLink))

wraps the TimeLink into a FormulaStream, so that any access to the Value causes the TimeLink to be executed anew. Continuing the example:

(define fut (FormulaStream (TimeLink)))
(cog-value-ref fut 0)
(cog-value-ref fut 0)
(cog-value-ref fut 0)

Note that each new reference provides the time at that instance. Note that there is no need to call cog-execute! to get the updated value: the execution to get the update happens automatically, upon access. This is the basic different between static Values and Streams -- Streams are always "live" and flowing.

The FutureStream is a Value, like any other Value; it can be installed anywhere, using either cog-set-value! or SetValueLink. Any reference to such values automatically causes the execution to happen.

The installation of Streams can be automated with the PromiseLink.

List of executable atom types

See Category:Executable Atom Types for a partial list. All FunctionLinks are executable, but are not listed in that category. All Category:Arithmetic Atom Types and Category:Query Atom Types are executable.

See also