clingo.symbol

Functions and classes for symbol manipulation.

Examples

>>> from clingo.core import Library
>>> from clingo.symbol import Function, Number, parse_term
>>>
>>> lib = Library()
>>>
>>> num = Number(lib, 42)
>>> num.number
42
>>> fun = Function(lib, "f", [num])
>>> fun.name
'f'
>>> [ str(arg) for arg in fun.arguments ]
['42']
>>> parse_term(lib, str(fun)) == fun
True
>>> parse_term(lib, 'p(1+2)')
p(3)
def Function( lib: clingo.core.Library, name: str, arguments: Sequence[Symbol] = [], is_positive: bool = True) -> Symbol:

Construct a function symbol.

This includes constants and tuples. Constants have an empty argument list and tuples have an empty name. Functions can represent classically negated atoms. Argument is_positive has to be set to False to represent such atoms.

Arguments:
  • lib: A library object to store the function in.
  • name: The name of the function.
  • arguments: The arguments in the form of a list of symbols.
  • is_positive: Whether the function is positive.
Infimum: Symbol
def Number(lib: clingo.core.Library, number: int) -> Symbol:

Construct a numeric symbol given a number.

Arguments:
  • lib: A library object to store the number in.
  • number: The given number.
def String(lib: clingo.core.Library, string: str) -> Symbol:

Construct a string symbol given a string.

Arguments:
  • lib: A library object to store the string in.
  • string: The given string.
Supremum: Symbol
class Symbol:

Represents a clingo symbol.

This includes #inf and #sup, numbers, strings, tuples, functions (including constants with len(arguments) == 0.

Symbol objects implement Python's rich comparison operators and are ordered like in clingo. They can also be used as keys in dictionaries. Their string representation corresponds to their clingo representation.

Note that this class does not have a constructor. Instead there are the preconstructed symbols Infimum and Supremum and the functions Number, String, Tuple_, and Function to construct symbol objects.

def match(*args, **kwds):

Helper for @overload to raise when called.

arguments: Sequence[Symbol]

The list of arguments.

arity: int

The arity of a function or tuple.

is_negative: bool

Whether the symbol is negative, e.g, -1 or -p.

is_positive: bool

Whether the symbol is positive, e.g., 1 or p.

name: str

The name.

number: int

The numeric value.

signature: tuple[str, int, bool] | None

Get the signature of function symbols.

The Boolean in the signature is True if the associated symbol is positive.

string: str

The string value.

type: SymbolType

The type of the symbol.

class SymbolType:

Enumeration of symbols types.

SymbolType(value: int)
Function: ClassVar[SymbolType] = SymbolType.Function

A function symbol, e.g., c, -c, or f(1,"a").

Infimum: ClassVar[SymbolType] = SymbolType.Infimum

The #inf symbol.

Number: ClassVar[SymbolType] = SymbolType.Number

A numeric symbol, e.g., 1.

String: ClassVar[SymbolType] = SymbolType.String

A string symbol, e.g., "a".

Supremum: ClassVar[SymbolType] = SymbolType.Supremum

The #sup symbol.

Tuple: ClassVar[SymbolType] = SymbolType.Tuple

"A tuple symbol (1,a)."

name: str
value: int
def Tuple_( lib: clingo.core.Library, arguments: Sequence[Symbol]) -> Symbol:

Construct a tuple symbol.

This is a shortcut for Function(lib, "", arguments).

Arguments:
  • lib: A library object to store the tuple in.
  • arguments: The arguments in form of a list of symbols.
def parse_term(lib: clingo.core.Library, string: str) -> Symbol:

Parse the given string using clingo's term parser for ground terms.

The function also evaluates arithmetic functions.

Arguments:
  • lib: A library object to store parsed symbols in.
  • string: The string to be parsed.