clingo.ast module

The clingo.ast-5.4.0 module.

The grammar below defines valid ASTs. For each upper case identifier there is a matching function in the module. Arguments follow in parenthesis: each having a type given on the right-hand side of the colon. The symbols ?, *, and + are used to denote optional arguments (None encodes abscence), list arguments, and non-empty list arguments.

# Terms

term = Symbol
        ( location : Location
        , symbol   : clingo.Symbol
        )
     | Variable
        ( location : Location
        , name     : str
        )
     | UnaryOperation
        ( location : Location
        , operator : UnaryOperator
        , argument : term
        )
     | BinaryOperation
        ( location : Location
        , operator : BinaryOperator
        , left     : term
        , right    : term
        )
     | Interval
        ( location : Location
        , left     : term
        , right    : term
        )
     | Function
        ( location  : Location
        , name      : str
        , arguments : term*
        , external  : bool
        )
     | Pool
        ( location  : Location
        , arguments : term*
        )

csp_term = CSPSum
            ( location : Location
            , terms    : CSPProduct
                          ( location    : Location
                          , coefficient : term
                          , variable    : term?
                          )*
            )

theory_term = Symbol
               ( location : Location
               , symbol   : clingo.Symbol
               )
            | Variable
               ( location : Location
               , name     : str
               )
            | TheoryTermSequence
               ( location : Location
               , sequence_type : TheorySequenceType
               , terms         : theory_term*
               )
            | TheoryFunction
               ( location  : Location
               , name      : str
               , arguments : theory_term*
               )
            | TheoryUnparsedTerm
               ( location : Location
               , elements : TheoryUnparsedTermElement
                             ( operators : str*
                             , term      : theory_term
                             )+
               )

# Literals

symbolic_atom = SymbolicAtom
                 ( term : term
                 )

literal = Literal
           ( location : Location
           , sign     : Sign
           , atom     : Comparison
                         ( comparison : ComparisonOperator
                         , left       : term
                         , right      : term
                         )
                      | BooleanConstant
                         ( value : bool
                         )
                      | symbolic_atom
           )

        | CSPLiteral
           ( location : Location
           , term     : csp_term
           , guards   : CSPGuard
                         ( comparison : ComparisonOperator
                         , term       : csp_term
                         )+
           )

# Head and Body Literals

aggregate_guard = AggregateGuard
                   ( comparison : ComparisonOperator
                   , term       : term
                   )

conditional_literal = ConditionalLiteral
                       ( location  : Location
                       , literal   : Literal
                       , condition : Literal*
                       )

aggregate = Aggregate
             ( location    : Location
             , left_guard  : aggregate_guard?
             , elements    : conditional_literal*
             , right_guard : aggregate_guard?
             )

theory_atom = TheoryAtom
               ( location : Location
               , term     : term
               , elements : TheoryAtomElement
                             ( tuple     : theory_term*
                             , condition : literal*
                             )*
               , guard    : TheoryGuard
                             ( operator_name : str
                             , term          : theory_term
                             )?
               )

body_atom = aggregate
          | BodyAggregate
             ( location    : Location
             , left_guard  : aggregate_guard?
             , function    : AggregateFunction
             , elements    : BodyAggregateElement
                              ( tuple     : term*
                              , condition : literal*
                              )*
             , right_guard : aggregate_guard?
             )
          | Disjoint
             ( location : Location
             , elements : DisjointElement
                           ( location  : Location
                           , tuple     : term*
                           , term      : csp_term
                           , condition : literal*
                           )*
             )
          | theory_atom

body_literal = literal
             | conditional_literal
             | Literal
                ( location : Location
                , sign     : Sign
                , atom     : body_atom
                )

head = literal
     | aggregate
     | HeadAggregate
        ( location    : Location
        , left_guard  : aggregate_guard?
        , function    : AggregateFunction
        , elements    : HeadAggregateElement
                         ( tuple     : term*
                         , condition : conditional_literal
                         )*
        , right_guard : aggregate_guard?
        )
     | Disjunction
        ( location : Location
        , elements : conditional_literal*
        )
     | theory_atom

# Theory Definitions

theory = TheoryDefinition
          ( location : Location
          , name     : str
          , terms    : TheoryTermDefinition
                        ( location  : Location
                        , name      : str
                        , operators : TheoryOperatorDefinition
                                       ( location      : Location
                                       , name          : str
                                       , priority      : int
                                       , operator_type : TheoryOperatorType
                                       )*
                        )
          , atoms    : TheoryAtomDefinition
                        ( location  : Location
                        , atom_type : TheoryAtomType
                        , name      : str
                        , arity     : int
                        , elements  : str*
                        , guard     : TheoryGuardDefinition
                                       ( operators : str*
                                       , term      : str
                                       )?
                        )
          )

# Statements

statement = Rule
             ( location : Location
             , head     : head
             , body     : body_literal*
             )
          | Definition
             ( location   : Location
             , name       : str
             , value      : term
             , is_default : bool
             )
          | ShowSignature
             ( location   : Location
             , name       : str
             , arity      : int
             , sign       : bool
             , csp        : bool
             )
          | Defined
             ( location   : Location
             , name       : str
             , arity      : int
             , sign       : bool
             )
          | ShowTerm
             ( location : Location
             , term     : term
             , body     : body_literal*
             , csp      : bool
             )
          | Minimize
             ( location : Location
             , weight   : term
             , priority : term
             , tuple    : term*
             , body     : body_literal*
             )
          | Script
             ( location    : Location
             , script_type : ScriptType
             , code        : str
             )
          | Program
             ( location   : Location
             , name       : str
             , parameters : Id
                             ( location : Location
                             , id       : str
                             )*
             )
          | External
             ( location : Location
             , atom     : symbolic_atom
             , body     : body_literal*
             , type     : term
             )
          | Edge
             ( location : Location
             , u        : term
             , v        : term
             , body     : body_literal*
             )
          | Heuristic
             ( location : Location
             , atom     : symbolic_atom
             , body     : body_literal*
             , bias     : term
             , priority : term
             , modifier : term
             )
          | ProjectAtom
             ( location : Location
             , atom     : symbolic_atom
             , body     : body_literal*
             )
          | ProjectSignature
             ( location   : Location
             , name       : str
             , arity      : int
             , positive   : bool
             )

Overview

Functions

def Aggregate()
def AggregateGuard()
def BinaryOperation()
def BodyAggregate()
def BodyAggregateElement()
def BooleanConstant()
def CSPGuard()
def CSPLiteral()
def CSPProduct()
def CSPSum()
def Comparison()
def ConditionalLiteral()
def Defined()
def Definition()
def Disjoint()
def DisjointElement()
def Disjunction()
def Edge()
def External()
def Function()
def HeadAggregate()
def HeadAggregateElement()
def Heuristic()
def Id()
def Interval()
def Literal()
def Minimize()
def Pool()
def Program()
def ProjectAtom()
def ProjectSignature()
def Rule()
def Script()
def ShowSignature()
def ShowTerm()
def Symbol()
def SymbolicAtom()
def TheoryAtom()
def TheoryAtomDefinition()
def TheoryAtomElement()
def TheoryDefinition()
def TheoryFunction()
def TheoryGuard()
def TheoryGuardDefinition()
def TheoryOperatorDefinition()
def TheorySequence()
def TheoryTermDefinition()
def TheoryUnparsedTerm()
def TheoryUnparsedTermElement()
def UnaryOperation()
def Variable()

Classes

class AST(type:ASTType, **arguments:Mapping[str,Any])

Represents a node in the abstract syntax tree.

AST nodes implement Python's rich comparison operators and are ordered structurally ignoring the location. They can also be used as dictionary keys. Their string representation corresponds to their gringo representation.

Parameters

type : ASTType
The type of the onde.
arguments : Mapping[str,Any]
Additionally, the functions takes an arbitrary number of keyword arguments. These should contain the required fields of the node but can also be set later.

Notes

It is also possible to create AST nodes using one of the functions provided in this module. The parameters of the functions correspond to the nonterminals as given in the grammar above.

Instance variables

var child_keys : List[str]

List of names of all AST child nodes.

var type : ASTType

The type of the node.

Methods

def items(self) -> List[Tuple[str,AST]]

The list of items of the AST node.

Returns

List[Tuple[str,AST]]
def keys(self) -> List[str]

The list of keys of the AST node.

Returns

List[str]
def values(self) -> List[AST]

The list of values of the AST node.

Returns

List[AST]
class ASTType

Enumeration of ast node types.

Class variables

var Aggregate
var AggregateGuard
var BinaryOperation
var BodyAggregate
var BodyAggregateElement
var BooleanConstant
var CSPGuard
var CSPLiteral
var CSPProduct
var CSPSum
var Comparison
var ConditionalLiteral
var Defined
var Definition
var Disjoint
var DisjointElement
var Disjunction
var Edge
var External
var Function
var HeadAggregate
var HeadAggregateElement
var Heuristic
var Id
var Interval
var Literal
var Minimize
var Pool
var Program
var ProjectAtom
var ProjectSignature
var Rule
var Script
var ShowSignature
var ShowTerm
var Symbol
var SymbolicAtom
var TheoryAtom
var TheoryAtomDefinition
var TheoryAtomElement
var TheoryDefinition
var TheoryFunction
var TheoryGuard
var TheoryGuardDefinition
var TheoryOperatorDefinition
var TheorySequence
var TheoryTermDefinition
var TheoryUnparsedTerm
var TheoryUnparsedTermElement
var UnaryOperation
var Variable
class AggregateFunction

Enumeration of aggegate functions.

AggregateFunction objects have a readable string representation, implement Python's rich comparison operators, and can be used as dictionary keys.

Furthermore, they cannot be constructed from Python. Instead the following preconstructed class attributes are available:

Attributes

Count : AggregateFunction
The #count function.
Sum : AggregateFunction
The #sum function.
SumPlus : AggregateFunction
The #sum+ function.
Min : AggregateFunction
The #min function.
Max : AggregateFunction
The #max function.

Class variables

var Count
var Max
var Min
var Sum
var SumPlus
class BinaryOperator

Enumeration of binary operators.

BinaryOperator objects have a readable string representation, implement Python's rich comparison operators, and can be used as dictionary keys.

Furthermore, they cannot be constructed from Python. Instead the following preconstructed class attributes are available:

Attributes

XOr : BinaryOperator
For bitwise exclusive or.
Or : BinaryOperator
For bitwise or.
And : BinaryOperator
For bitwise and.
Plus : BinaryOperator
For arithmetic addition.
Minus : BinaryOperator
For arithmetic subtraction.
Multiplication : BinaryOperator
For arithmetic multipilcation.
Division : BinaryOperator
For arithmetic division.
Modulo : BinaryOperator
For arithmetic modulo.
Power : BinaryOperator
For arithmetic exponentiation.

Class variables

var And
var Division
var Minus
var Modulo
var Multiplication
var Or
var Plus
var Power
var XOr
class ComparisonOperator

Enumeration of comparison operators.

ComparisonOperator objects have a readable string representation, implement Python's rich comparison operators, and can be used as dictionary keys.

Furthermore, they cannot be constructed from Python. Instead the following preconstructed class attributes are available:

Attributes

GreaterThan : ComparisonOperator
The > operator.
LessThan : ComparisonOperator
The < operator.
LessEqual : ComparisonOperator
The <= operator.
GreaterEqual : ComparisonOperator
The >= operator.
NotEqual : ComparisonOperator
The != operator.
Equal : ComparisonOperator
The = operator

Class variables

var Equal
var GreaterEqual
var GreaterThan
var LessEqual
var LessThan
var NotEqual
class ScriptType

Enumeration of theory atom types.

ScriptType objects have a readable string representation, implement Python's rich comparison operators, and can be used as dictionary keys.

Furthermore, they cannot be constructed from Python. Instead the following preconstructed class attributes are available:

Attributes

Python : ScriptType
For Python code.
Lua : ScriptType
For Lua code.

Class variables

var Lua
var Python
class Sign

Enumeration of signs for literals.

Sign objects have a readable string representation, implement Python's rich comparison operators, and can be used as dictionary keys.

Furthermore, they cannot be constructed from Python. Instead the following preconstructed class attributes are available:

Attributes

NoSign : Sign
For positive literals.
Negation : Sign
For negative literals (with prefix not).
DoubleNegation : Sign
For double negated literals (with prefix not not)

Class variables

var DoubleNegation
var Negation
var NoSign
class TheoryAtomType

Enumeration of theory atom types.

TheoryAtomType objects have a readable string representation, implement Python's rich comparison operators, and can be used as dictionary keys.

Furthermore, they cannot be constructed from Python. Instead the following preconstructed class attributes are available:

Attributes

Any : TheoryAtomType
For atoms that can occur anywhere.
Body : TheoryAtomType
For atoms that can only occur in rule bodies.
Head : TheoryAtomType
For atoms that can only occur in rule heads.
Directive : TheoryAtomType
For atoms that can only occur in facts.

Class variables

var Any
var Body
var Directive
var Head
class TheoryOperatorType

Enumeration of operator types.

TheoryOperatorType objects have a readable string representation, implement Python's rich comparison operators, and can be used as dictionary keys.

Furthermore, they cannot be constructed from Python. Instead the following preconstructed class attributes are available:

Attributes

Unary : TheoryOperatorType
For unary operators.
BinaryLeft : TheoryOperatorType
For binary left associative operators.
BinaryRight : TheoryOperatorType
For binary right associative operator.

Class variables

var BinaryLeft
var BinaryRight
var Unary
class TheorySequenceType

Enumeration of theory term sequence types.

TheorySequenceType objects have a readable string representation, implement Python's rich comparison operators, and can be used as dictionary keys.

Furthermore, they cannot be constructed from Python. Instead the following preconstructed class attributes are available:

Attributes

Tuple : TheorySequenceType
For sequences enclosed in parenthesis.
List : TheorySequenceType
For sequences enclosed in brackets.
Set : TheorySequenceType
For sequences enclosed in braces.

Class variables

var List
var Set
var Tuple

Instance variables

var left_hand_side : str

Left-hand side representation of the sequence.

var right_hand_side : str

Right-hand side representation of the sequence.

class UnaryOperator

Enumeration of signs for literals.

UnaryOperator objects have a readable string representation, implement Python's rich comparison operators, and can be used as dictionary keys.

Furthermore, they cannot be constructed from Python. Instead the following preconstructed class attributes are available:

Attributes

Negation : UnaryOperator
For bitwise negation.
Minus : UnaryOperator
For unary minus and classical negation.
Absolute : UnaryOperator
For taking the absolute value.

Class variables

var Absolute
var Minus
var Negation

Instance variables

var left_hand_side : str

Left-hand side representation of the operator.

var right_hand_side : str

Right-hand side representation of the operator.