concepts.dsl.parsers.fol_python_parser.FOLPythonParser#

class FOLPythonParser[source]#

Bases: ParserBase

A parser to parse first-order logic (FOL) expressions in Python syntax. Currrently supported features:

  • logic operations: and, or, not

  • quantifiers: forall, exists (see below for the syntax)

  • function application: f(x, y, z)

  • function definition: def f(x, y, z): return x + y + z

The syntax for quantifiers is as follows:
from typing import Any, Type, Callable

def forall(dtype: Type, func: Callable[[Any], bool]) -> bool: ...
def exists(dtype: Type, func: Callable[[Any], bool]) -> bool: ...

Examples

from concepts.dsl.dsl_types import ObjectType
from concepts.dsl.function_domain import FunctionDomain

domain = FunctionDomain()
domain.define_type(ObjectType('Person'))

parser = FOLPythonParser(domain, inplace_definition=True)
parser.parse_expression('exists(Person, lambda x: is_phd(x))')

function_string = '''
def is_grandfather(x: Person, y: Person) -> bool:
    # x is the grandfather of y
    return exists(Person, lambda z: is_father(x, z) and is_parent(y, z))
'''
parser.parse_function(function_string)

Methods

parse_domain_file(path)

Parse a domain from a file.

parse_domain_string(string)

Parse a domain from a string.

parse_expression(string[, arguments])

Parse an expression from a string.

parse_expression_ast(expression[, arguments])

parse_function(string)

parse_multiple_expressions(string[, arguments])

Attributes

domain

The domain for types and functions.

inplace_definition

Whether to allow functions to be defined in-place.

__init__(domain, inplace_definition=False, inplace_polymorphic_function=False, inplace_definition_type=False)[source]#

Initialize the parser.

Parameters:
  • domain (DSLDomainBase) – the domain to use.

  • inplace_definition (bool) – whether to allow expressions to contain functions that are not defined in the domain. If set to True, the parser will automatically define these functions.

  • inplace_polymorphic_function (bool) – whether inplace functions are assumed to be polymorphic.

  • inplace_definition_type (bool) – whether to allow expressions to contain types that are not defined in the domain. If set to True, the parser will automatically define these types.

__new__(**kwargs)#
parse_domain_file(path)#

Parse a domain from a file.

Parameters:

path (str) – the path to the file.

Returns:

the parsed domain.

Return type:

DSLDomainBase

parse_domain_string(string)[source]#

Parse a domain from a string.

Parameters:

string (str) – the string to parse.

Returns:

the parsed domain.

Return type:

DSLDomainBase

parse_expression(string, arguments=tuple())[source]#

Parse an expression from a string.

Parameters:
Returns:

the parsed expression.

Return type:

ValueOutputExpression

parse_expression_ast(expression, arguments=tuple())[source]#
Parameters:
Return type:

ValueOutputExpression

parse_function(string)[source]#
Parameters:

string (str)

Return type:

Function

parse_multiple_expressions(string, arguments=tuple())[source]#
Parameters:
Return type:

Tuple[ValueOutputExpression, …]

domain: DSLDomainBase#

The domain for types and functions.

inplace_definition: bool#

Whether to allow functions to be defined in-place.