TermBase#
- class discopy.closed.TermBase(name, dom, cod, **params)[source]#
Bases:
Box,discopy.biclosed.TermBaseA term in the internal language of a closed category, i.e. a lambda term which need not be linear: this module implements closed markov categories by design, so a variable may be copied and discarded, i.e. occur any number of times.
A term is evaluated in a context, a list of distinct variables containing its free ones: the variables that do not occur in the term are discarded, the others are permuted into the order of its
freevars, seeweaken(). The context isfreevarsitself by default.Note
Closed terms accept the
leftargument of their biclosed counterparts and ignore it: a closed category has one exponential, so an applicationx(f, left=True)isf(x)and an abstraction on the left is one on the right.>>> X, Y = Ty("X"), Ty("Y") >>> f, x = (X >> Y)("f"), X("x") >>> assert x(f, left=True) == f(x)
- compose(*others)[source]#
The composition of terms of function types:
t.compose(u)fort : x >> yandu : y >> zisx(lambda v: u(t(v))), i.e.tis applied first, in the order oft >> u, which composes the diagrams that terms also are and keeps its name.- Parameters:
others (Term) – Terms of function types, the exponent of each being the base of the previous one.
- Return type:
Term
Example
>>> X, Y, Z = map(Ty, "XYZ") >>> t, u = (X >> Y)("t"), (Y >> Z)("u") >>> print(t.compose(u)) X(lambda x: u(t(x)))
- weaken(functor, context=None)[source]#
The structural morphism from the image of a context to that of the free variables of the term: it discards the variables that do not occur in the term and permutes the others into the order of
freevars.- Parameters:
functor (Functor) – The functor to evaluate the types.
context – A list of distinct variables containing the free ones, the free variables themselves by default.
- Return type:
Example
>>> X, Y = Ty("X"), Ty("Y") >>> x, y = Variable("x", X), Variable("y", Y) >>> assert x.weaken(x.functor, [y, x])\ ... == Diagram.swap(Y, X) >> X @ Diagram.discard(Y)
The structural morphism from the image of the free variables of the term to that of the concatenation of
contexts, copying each variable once for each of the contexts it is in, i.e. how the subterms of a term share its free variables.- Parameters:
- Return type:
Example
>>> X, Y = Ty("X"), Ty("Y") >>> x, y = Variable("x", X), Variable("y", Y) >>> term = Tuple(x, y, x) >>> assert term.share(term.functor, [x], [y], [x])\ ... == Diagram.copy(X) @ Y >> X @ Diagram.swap(X, Y)
- eval_unpacked(functor=None, context=None)[source]#
The evaluation of a term followed by the unpacking of its product codomain, overridden by
Tupleso that binding a literal tuple never produces aPackfollowed by anUnpack.
- occurrences(variable)[source]#
The number of free occurrences of a variable in the term.
- Parameters:
variable (Variable)
- Return type:
int
- substitute(substitution)[source]#
The term with the free variables of a substitution replaced.
- Parameters:
substitution (Substitution)
- Return type:
Term
- classmethod from_biclosed(term)[source]#
Translate a biclosed term into a closed term, dropping planarity by collapsing left and right exponentials and applications.
- Parameters:
term (Term) – The biclosed term to translate.
- Return type:
Term
Note
This method is inherited by
Constant,Variable,ApplicationandAbstraction, i.e. every closedTerm.Example
>>> X, Y = biclosed.Ty("X"), biclosed.Ty("Y") >>> g, x = (Y << X)("g"), X("x") >>> print(TermBase.from_biclosed(g(x))) g(x)