concepts.dsl.function_domain.FunctionDomain#

class FunctionDomain[source]#

Bases: DSLDomainBase

A basic domain definition that contains a collection of types, constants, and functions.

Methods

define([implementation])

A context manager that defines the types, constants, and functions in the domain.

define_const(dtype, value)

Define a constant with the given type and value.

define_function(function[, implementation])

Define a function in the domain.

define_overloaded_function(name, overloads)

Define a function with multiple overloads.

define_type(t)

Define a type in the domain.

deserialize(dictionary)

Deserialize a program from a dictionary.

format_summary()

Format the summary of the domain.

get_function(name)

Get the function with the given name.

has_function(name)

Check whether the domain has a function with the given name.

lam(lambda_expression[, name, typing_cues])

Parse a lambda expression into a Function instance.

overload(function)

Overload a function.

print_summary()

Print the summary of the domain.

serialize(program)

Serialize a program into a dictionary.

Attributes

AllowedTypes

alias of ObjectType | ValueType | FunctionType

name

The name of the domain.

types

The types defined in the domain, as a dictionary from type names to types.

functions

The functions defined in the domain, as a dictionary from function names to functions.

constants

The constants defined in the domain, as a dictionary from the name to the Value objects.

__init__(name=None)[source]#

Initialize the domain.

Parameters:

name (str | None) – the name of the domain. If not specified, the name of the class will be used.

__new__(**kwargs)#
define(implementation=True)[source]#

A context manager that defines the types, constants, and functions in the domain.

Usage:

with domain.define():
    A = domain.define_type('A', ...)
    B = domain.define_type('B', ...)

    # Definition of constants. Equivalent to `domain.define_const(domain.types['A'], 'a')`.
    a: A
    b: B

    # Definition of functions.
    def f(a: A, b: B) -> A:
        pass

    # Definition of overloaded functions.
    @domain.overload
    def g(a: A, b: B) -> A:
        pass

    @domain.overload
    def g(b: B, a: A) -> B:
        pass

Note that the definition for constants is only allowed when the code is executed at the global scope.

define_const(dtype, value)#

Define a constant with the given type and value.

Parameters:
  • dtype (ObjectType | ValueType) – the type of the constant.

  • value (str) – the value of the constant. The value should be a string that is the name of the constant.

define_function(function, implementation=True)#

Define a function in the domain.

Parameters:
  • function (Function | Callable) – the function to be defined.

  • implementation (bool) – whether to store the function body of function as the implementation of the function.

Returns:

the function that is defined.

Return type:

Function

define_overloaded_function(name, overloads, implementation=True)[source]#

Define a function with multiple overloads.

Parameters:
  • name (str) – the name of the function.

  • overloads (List[Function | Callable]) – the overloads of the function. Each overload should be a python function or a Function instance.

  • implementation (bool) – whether to store the function body of function as the implementation of the function.

define_type(t)#

Define a type in the domain.

Parameters:

t (ObjectType | ValueType) – the type to be defined.

Returns:

the type that is defined.

Return type:

ObjectType | ValueType

deserialize(dictionary)[source]#

Deserialize a program from a dictionary.

Parameters:

dictionary (Dict[str, Any]) – the dictionary to be deserialized.

Returns:

the deserialized program.

Return type:

FunctionApplicationExpression | Value

format_summary()[source]#

Format the summary of the domain.

Return type:

str

get_function(name)#

Get the function with the given name.

Parameters:

name (str) – the name of the function.

Returns:

the function with the given name.

Return type:

Function

has_function(name)#

Check whether the domain has a function with the given name.

Parameters:

name (str) – the name of the function.

Returns:

whether the domain has a function with the given name.

Return type:

bool

lam(lambda_expression, name='__lambda__', typing_cues=None)[source]#

Parse a lambda expression into a Function instance.

Parameters:
  • lambda_expression (Callable) – the lambda expression.

  • name (str) – the name of the function.

  • typing_cues (Dict[str, str | TypeBase] | None) – the typing cues for the function. It should be a dictionary that maps the argument names to the types.

  • type (If you want to specify the return)

  • 'return'. (use the key)

Returns:

The parsed Function instance.

Return type:

Function

overload(function)[source]#

Overload a function. See the docstring for define() for usage.

print_summary()[source]#

Print the summary of the domain.

serialize(program)[source]#

Serialize a program into a dictionary.

Parameters:

program (FunctionApplicationExpression | Value) – the program to be serialized.

Returns:

the serialized program.

Return type:

Dict[str, Any]

AllowedTypes#

alias of ObjectType | ValueType | FunctionType

constants: Dict[str, Value]#

The constants defined in the domain, as a dictionary from the name to the Value objects.

functions: Dict[str, Function]#

The functions defined in the domain, as a dictionary from function names to functions.

name: str#

The name of the domain.

types: Dict[str, ObjectType | ValueType]#

The types defined in the domain, as a dictionary from type names to types.