In OpenCog, the ContextLink is used to alter the interpretation of an Atom, giving it an alternate TruthValue within a sepcific context.

A truth value gives each Atom a valuation or interpretation, thus all Atoms always carry a default interpretation along with them. This prevents atoms in a single AtomSpace from having more than one default interpretation. The ContextLink can be used to assign additional interpretations to an Atom. That is, the ContextLink represents the contextualised truth value between an atom and another context node (which may be anything, although is usually a ConceptNode).

For example, we usually think of the sky as being blue, however, when the context is the Moon, this isn't the case:

ContextLink <0.0, 0.2>
ConceptNode "Moon"
PredicateNode isBlue
ConceptNode "sky"


Note that the EvaluationLink for isBlue has a higher strength than the ContextLink. The TV for the EvaluationLink is the default for Earth, where the sky is usually blue when its not cloudy. The TV on the ContextLink is appropriate for the Moon, where the sky is not blue.

Contexts provide a mechanism for OpenCog to store distinct multiple TVs for an atom (in the above example, for an EvaluationLink).

See the page claims and contexts to see an example of how a ContextLink is used to express claims such as He said that..., I believe that..., etc.

From the point of view of probability theory or of measure theory, a context is an element of a sigma algebra. That is, a context is a subset of the 'universe'; and one may take the intersection of two contexts (using the AndLink), or their union (with the OrLink). The rules for combining truth values on a context link are analogous to the requirement that a probability be sigma-additive. Similarly, just as the axioms of probability theory tell one how to work with probabilities, so contexts inform how to work with truth values, when considering a subset of the total universe of possibilities.

# Definition

ContextLink <TV>
C
R A B


is equivalent to

R <TV>
(A AND C)
(B AND C)


Here, C is an atom defining the context, and R is some relationship between A and B. The case where R is a node can be handled as follows. Since

A <TV>


is equivalent to

ExtensionalInheritanceLink <TV>
Universe
A


Therefore

ContextLink <TV>
C
A


is equivalent to

ExtensionalInheritanceLink <TV>
C
A AND C


When R is an EvaluationLink, then it appears that the PredicateNode does not need to be "Andified". Under some natural assumptions, only the arguments would be andified, such as follows:

ContextLink <0.0, 0.2>
Moon
PredicateNode isBlue
sky


The above would be equivalent to

EvaluationLink <0.0, 0.2>
PredicateNode isBlue
(sky AND Moon)


However, for the general case of a relation R (that is not an EvaluationLink), there is no apparent reason for skipping andification like this.

# Properties

This section discusses and reviews some basic properties of the ContextLink. If you are already familiar with the measure-theoretic formulation of probability theory, then contexts can be understood in a straight-forward way: they are essentially the members of a sigma algebra. Thus, the intersection (AndLink) and union (OrLink) of two contexts govern how truth values combine. This is expanded upon in the next section.

Note also that this conception of a context as a set-like thing implies that it is extensional, and not intentional: contexts are not defined in terms of their properties, but in terms of their size and membership. This is the same reason that SubsetLink uses extensional inheritance in it's definition. This is underscored below by making reference to the ExtensionalAndLink, rather than the plain AndLink.

ContextLink <TV1>
C
D
R<TV3> A B


is equivalent to

ContextLink <TV2>
D
R<TV3> A B

ContextLink <TV1>
(D ANDExt C)
R<TV3> A B


which is equivalent to

R<TV3> A B

R<TV2>
(D ANDExt A)
(D ANDExt B)

R<TV1>
(C ANDExt D ANDExt A)
(C ANDExt D ANDExt B)


These semantics, however, only hold when R is a form of inheritance/implication or similarity/equivalence; they don't hold for relationships R generally (see [1]).

However

ContextLink <TV1>
C
D
R<TV3> A B


is not equivalent to

ContextLink <TV2>
D
C
R<TV3> A B


That is because in the first case TV1 is the contextualized TV with context (C AND D ) while in the second case TV1 is the contextualized TV with context C only.

On the other hand

ContextLink <TV1>
C
D
R<TV3> A B


is equivalent to

ContextLink <TV1>
D
C
R<TV3> A B


where TV2' is a new contextualized TV with context C alone.

# Probabilistic interpretation

This section briefly reviews the relationship between contexts sigma algebras. Recall that a sigma algebra is a pair ${\displaystyle (\Omega ,\Sigma )}$ where ${\displaystyle \Omega }$ is a universe, and ${\displaystyle \Sigma }$ is a set of subsets of ${\displaystyle \Omega }$. The sigma algebra is closed under intersection and union: the intersection and union of any two elements in ${\displaystyle \Sigma }$ is also in ${\displaystyle \Sigma }$. A context, in opencog, can be imagined to be a member of a sigma-algebra, with AndLink defining the intersection, and OrLink defining the union.

More precisely, this is why contexts can only be combined with extensional and-links, and not intentional ones; this is the same reason that the SubsetLink is extensional, not intentional. Roughly speaking, it is because we are interested in the size of a set, and its members, rather than it's properties. When taking the intersection or union of two sets, we intersect or union based on membership; we do not intersect or union the properties of the sets.

The measure-theoretic formulation of probability theory is a triple ${\displaystyle (\Omega ,\Sigma ,\mu )}$; the third element ${\displaystyle \mu }$ being a (real-valued) function that assigns a probability to each member of ${\displaystyle \Sigma }$. That is, the probability P(A) of an event A in ${\displaystyle \Sigma }$ is just ${\displaystyle \mu (A)}$; that is, ${\displaystyle \mu (A)=P(A)}$ by definition; its just a different notation for the same thing. See probability space for details. Probabilities must obey certain rules when different events are combined: these are axioms that require that the function ${\displaystyle \mu }$ be sigma-additive. Analogously, truth values must be combined in such a way as to respect sigma-additivity.

# Quantification

Contexts are used for quantification. Thus,

Implication
Inheritance (Ben AND “possible universe C”) ugly
Inheritance (Ben AND “possible universe C”) beef_eater


is equivalent by definition to

ContextLink C
Implication
Inheritance Ben ugly
Inheritance Ben beef_eater


From this we conclude that

Implication <TV>
Inheritance Ben ugly
Inheritance Ben beef_eater


is implicitly taken as

AverageQuantifier $C <TV> AND Inheritance$C possible_world
Implication
Inheritance (Ben AND $C) ugly Inheritance (Ben AND$C) beef_eater


That is, in the probabilistic interpretation, the truth of an atom in the universe is the probability-weighted average of its truth in all possible contexts (denoted with the AverageLink).

# Context Rules

There are 3 context rules

# Implementation

At this time, there is no optimization for the handling of ConextLinks in the atomspace. However, due to thier fundamental importance, perhaps there should be one.

From the memory-management point of view, it might be best to insert all ContextLinks of a given kind into a sub-Atomspace; see multiple atom spaces for details.

## Implementation conception

To guide the implementation, think of the problem as follows. In the abstract: Atoms, by them selves, do not have a TV; it is impossible to assign a TV to any atom at all, when that atom is outside of a context. That is, atoms never have TV's; only context links do.

However, if we allow a default context, then one possible implementation would be exactly what we have today: every atom points to its TV for the default context. Viz, what we have today is conceptually identical to the case where only ContextLinks have TV's, and there is a default context.

Next, every PLN rule should be specified relative to a context. I suspect the current PLN design does not do this, and does not even anticipate this. I suspect that the current PLN design assumes only a default context for everything. I suspect that the current PLN does not even handle ContextLinks....

Next, if all computation is relative to a context, then we need some mechanism to rapidly obtain the TV associated with the (atom, context) pair. The old version-handle code did this (although the implementation caused severe technical issues). The time is coming that we once-again need a rapid lookup of the TV associated with a (atom, context) pair.

As Ben points out: actually having and using ContextLinks is kind-of klunky, so perhaps we should never, ever allow actual ContextLinks, and instead force PLN to just use TV's obtained from (atom, context) pairs, and have some super-easy-to-use default context (e.g. what we defacto have today).

So, in this way, we are lead full circle back to versioned TV's. It may be better if the AtomSpace API was written to always use (atom, context) pairs. That is, instead of fetching the TV by making the C++ call atom->getTV(); it should instead make a wrapper like this:

TVPtr getTV (AtomPtr a, ContextPtr c) {
if (c==NULL) return a->getTV();
else return do_something_else_to_get_tv(a,c);
}