Tutorial 1.5: Using Python Syntax to Write FOL Expressions#

[1]:
import inspect
[2]:
class Person: ...

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))
[3]:
from concepts.dsl.dsl_types import ObjectType, Variable
from concepts.dsl.function_domain import FunctionDomain
[4]:
domain = FunctionDomain()
domain.define_type(ObjectType('Person'))
domain.print_summary()
TypeSystem: FunctionDomain
  Types:
    Person
  Constants:
  Functions:
[5]:
from concepts.dsl.parsers.fol_python_parser import FOLPythonParser
parser = FOLPythonParser(domain, inplace_definition=True)
[6]:
parent_string = 'is_father(x, y) or is_mother(x, y)'
expression = parser.parse_expression(
    parent_string,
    [Variable('x', domain.types['Person']), Variable('y', domain.types['Person'])]
)

print('Input string:')
print(parent_string)

print('\nParsed expression:')
print(repr(expression))
Input string:
is_father(x, y) or is_mother(x, y)

Parsed expression:
OrExpression<or(is_father(V::x, V::y), is_mother(V::x, V::y))>
[7]:
grandfather_string = inspect.getsource(is_grandfather)
function = parser.parse_function(grandfather_string)

print('Input string:')
print(grandfather_string)

print('\nParsed concepts.dsl.dsl_functions.Function object:')
print(repr(function))

print('\nDerived expression:')
print(repr(function.derived_expression))
Input 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))


Parsed concepts.dsl.dsl_functions.Function object:
Function<def is_grandfather(x: Person, y: Person): return exists(z: Person: and(is_father(V::x, V::z), is_parent(V::y, V::z)))>

Derived expression:
ExistsExpression<exists(z: Person: and(is_father(V::x, V::z), is_parent(V::y, V::z)))>
[8]:
print('Automatically registered functions:')
domain.print_summary()
Automatically registered functions:
TypeSystem: FunctionDomain
  Types:
    Person
  Constants:
  Functions:
    is_father(#0: Person, #1: Person) -> bool
    is_mother(#0: Person, #1: Person) -> bool
    is_parent(#0: Person, #1: Person) -> bool