Skip to content

operators

tuprolog.core.operators

Operators: functors written in prefix, infix or postfix notation, e.g. + in 1 + 2.

In Prolog syntax, 1 + 2 * 3 is the term +(1, *(2, 3)): + and * are operators, each with a priority (the lower, the tighter it binds) and a specifier (its position, i.e. prefix, infix or postfix, and associativity). Parsers (see tuprolog.core.parsing) and formatters (see tuprolog.core.TermFormatter) consult an operator set to read and write such expressions; programs can declare new operators, with the op/3 directive.

This package offers the types Operator, OperatorSet and Specifier, the factories operator, operator_set and specifier, the standard operator sets (DEFAULT_OPERATORS, STANDARD_OPERATORS, EMPTY_OPERATORS), and the specifiers (XFX, YFX, FY, ...).

Examples:

>>> from tuprolog.core.operators import DEFAULT_OPERATORS, XFX, operator, operator_set
>>> from tuprolog.core.parsing import parse_term
>>> operators = DEFAULT_OPERATORS + operator_set(operator("++", XFX, 700))
>>> parse_term("a ++ b", operators)
'++'(a, b)
See Also

tuprolog.core.parsing, for parsing terms with operators.

Operator module-attribute

Operator = _operators.Operator

An operator declaration, letting a functor be written in prefix (e.g. - 1), infix (e.g. 1 + 2) or postfix notation, rather than as functor(args).

An operator has a functor (e.g. "+"), a specifier (a Specifier, its position and associativity, e.g. YFX), and a priority, from 1 to 1200: the lower, the tighter it binds (e.g. * has priority 400 and + 500, so 1 + 2 * 3 is 1 + (2 * 3)). Prolog declares operators with the directive :- op(Priority, Specifier, Functor).

Build operators with tuprolog.core.operators.operator, or Operator(functor, specifier, priority). Members: functor, specifier, priority, to_term() (the term op(Priority, Specifier, Functor)). Two operators are equal when their functor and specifier are, regardless of their priority.

Examples:

>>> from tuprolog.core.operators import *
>>> op = operator("++", XFX, 700)
>>> op, op.functor, op.priority, op.to_term()
(Operator(700, XFX, '++'), '++', 700, op(700, xfx, '++'))
Note

2P-Kt's it.unibo.tuprolog.core.operators.Operator.

OperatorSet module-attribute

OperatorSet = _operators.OperatorSet

An immutable set of operators, telling parsers and formatters which functors to read and write as expressions.

Python protocols: it is a set of Operators (len, in, iteration); s1 + s2 adds operators (those of s2 replacing those of s1 with the same functor and specifier), and s1 - s2 removes them (both also accept a single Operator).

Build operator sets with tuprolog.core.operators.operator_set, or use DEFAULT_OPERATORS, STANDARD_OPERATORS and EMPTY_OPERATORS.

Examples:

>>> from tuprolog.core.operators import *
>>> custom = operator_set(operator("++", XFX, 700))
>>> len(custom), operator("++", XFX, 700) in custom
(1, True)
>>> len(DEFAULT_OPERATORS + custom) == len(DEFAULT_OPERATORS) + 1
True
Note

2P-Kt's it.unibo.tuprolog.core.operators.OperatorSet.

Specifier module-attribute

Specifier = _operators.Specifier

The position and associativity of an operator, an enumeration.

f stands for the operator and x/y for its arguments: x is an argument of lower priority than the operator, y one of lower or equal priority, so y marks the side the operator associates to.

Prefix Postfix Infix Associativity
FX XF XFX none
YF YFX left
FY XFY right

For instance, - is YFX, so 1 - 2 - 3 is (1 - 2) - 3. Members: is_prefix, is_infix, is_postfix, to_term() (the atom, e.g. xfx), values() (all the specifiers).

Examples:

>>> XFX.is_infix, FY.is_prefix, XFX.to_term()
(True, True, xfx)
Note

2P-Kt's it.unibo.tuprolog.core.operators.Specifier.

EMPTY_OPERATORS module-attribute

EMPTY_OPERATORS: OperatorSet = OperatorSet.EMPTY

The empty operator set: terms are parsed and written in canonical form only.

DEFAULT_OPERATORS module-attribute

DEFAULT_OPERATORS: OperatorSet = OperatorSet.DEFAULT

The default operator set, the same as STANDARD_OPERATORS, used by parsers and formatters unless told otherwise.

STANDARD_OPERATORS module-attribute

STANDARD_OPERATORS: OperatorSet = OperatorSet.STANDARD

The operators of ISO Prolog, e.g. :-, ,, ;, ->, =, is, <, +, -, *, /, mod.

Examples:

>>> from tuprolog.core.operators import *
>>> len(STANDARD_OPERATORS), operator("+", YFX, 500) in STANDARD_OPERATORS
(40, True)

XF module-attribute

The specifier of postfix, non-associative operators.

YF module-attribute

The specifier of postfix, left-associative operators.

FX module-attribute

The specifier of prefix, non-associative operators, e.g. :- in directives.

FY module-attribute

The specifier of prefix, right-associative operators, e.g. \+ and unary -.

XFX module-attribute

XFX: Specifier = Specifier.XFX

The specifier of infix, non-associative operators, e.g. = and is.

XFY module-attribute

XFY: Specifier = Specifier.XFY

The specifier of infix, right-associative operators, e.g. , and ^.

YFX module-attribute

YFX: Specifier = Specifier.YFX

The specifier of infix, left-associative operators, e.g. + and *.

operator

operator(
    functor: str, specifier: Specifier, priority: int
) -> Operator

Creates an operator, from its parts or from an op/3 term.

It takes either (functor, specifier, priority), as Python values, or (priority, specifier, functor), as terms (as in op(Priority, Specifier, Functor)), or a single op/3 structure.

Parameters:

Name Type Description Default
functor str

the functor of the operator, e.g. "+".

required
specifier Specifier

its position and associativity (a Specifier, see specifier).

required
priority int

its priority, from 1 (binding tightest) to 1200.

required

Returns:

Type Description
Operator

The operator.

Examples:

>>> from tuprolog.core.operators import *
>>> operator("++", XFX, 700)
Operator(700, XFX, '++')
>>> from tuprolog.core import atom, integer, struct
>>> operator(integer(700), atom("xfx"), atom("++"))
Operator(700, XFX, '++')
>>> operator(struct("op", integer(700), atom("xfx"), atom("++")))
Operator(700, XFX, '++')
Note

Wraps 2P-Kt's Operator constructor, Operator.fromTerms and Operator.fromTerm.

operator_set

operator_set(
    *operators: Operator | Iterable[Operator],
) -> OperatorSet

Creates an operator set.

Parameters:

Name Type Description Default
*operators Operator | Iterable[Operator]

the operators, either as many arguments or as a single iterable. No operators give an empty set.

()

Returns:

Type Description
OperatorSet

The operator set.

Examples:

>>> from tuprolog.core.operators import *
>>> operator_set(operator("++", XFX, 700), operator("--", FY, 200))
OperatorSet([...])
>>> len(operator_set())
0
Note

Wraps 2P-Kt's OperatorSet constructor.

specifier

specifier(name: str) -> Specifier

Returns the specifier with a given name, as a string or a term.

Parameters:

Name Type Description Default
name str

the name of the specifier, as a string in any case (e.g. "xfx"), or as a term (an atom, e.g. xfx).

required

Returns:

Type Description
Specifier

The specifier.

Raises:

Type Description
TypeError

if name is neither a string nor a term.

IllegalArgumentException

if there is no specifier with that name.

Examples:

>>> from tuprolog.core import atom
>>> specifier("xfx"), specifier("YFX"), specifier(atom("fy"))
(XFX, YFX, FY)
Note

Wraps 2P-Kt's Specifier.valueOf and Specifier.fromTerm.