Skip to content

domain

tuprolog.solve.exception.error.domain

Domain errors: an argument has the right type, but a value outside the admissible ones.

ISO Prolog's domain_error(Domain, Culprit) error, thrown as error(domain_error(Domain, Culprit), Extra), means that Culprit has the right type but does not belong to the set of values Domain names: e.g. atom_length(abc, -1) throws domain_error(not_less_than_zero, -1) (an integer, but a negative one), and open/3 with an unknown mode throws domain_error(io_mode, Culprit). Values of a wrong type raise type errors instead (see tuprolog.solve.exception.error.type).

The error is a DomainError, and the domains are the values of the Domain enumeration (also available as the DOMAIN_* constants). Build errors with domain_error_for_argument (the usual case, for primitives' arguments), domain_error_for_flag_values, domain_error_for_goal, or domain_error_for_term.

Examples:

>>> from tuprolog.solve import signature
>>> from tuprolog.solve.exception.error import *
>>> from tuprolog.solve.prolog import prolog_solver
>>> context = prolog_solver().current_context
>>> from tuprolog.core import integer
>>> procedure = signature("foo", 2)
>>> domain_error_for_argument(context, procedure, DOMAIN_NOT_LESS_THAN_ZERO, integer(-1)).type
domain_error(not_less_than_zero, -1)
>>> prolog_solver().solve_once("catch(atom_length(abc, -1), error(E, _), true)")["E"]
domain_error(not_less_than_zero, -1)
See Also

tuprolog.solve.primitive, whose ensuring_* checks raise these errors; 2P-Kt's documentation on errors and exceptions.

DomainError module-attribute

DomainError = errors.DomainError

The domain error, thrown as error(domain_error(Domain, Culprit), _).

It means that Culprit is not in Domain.

The JVM class (also a Python ValueError and a tuprolog.errors.PrologDomainError). Members (snake_case aliases as well): expected_domain, the Domain; culprit, the offending term; besides those of tuprolog.solve.exception.LogicError (e.g. type, error_struct, message).

Domain module-attribute

Domain = DomainError.Expected

The enumeration of the domains a domain error may refer to (2P-Kt's DomainError.Expected).

Each value (also available as a DOMAIN_* constant) is the Domain of a domain_error(Domain, Culprit) term: to_term() gives it as an atom (e.g. io_mode), and domain as a string. Domain.of(name) and Domain.fromTerm(term) look a value up (see expected_domain).

Examples:

>>> Domain.IO_MODE.to_term()
io_mode

DOMAIN_ATOM_PROPERTY module-attribute

DOMAIN_ATOM_PROPERTY = Domain.ATOM_PROPERTY

atom_property: the expected domain is a property of atoms.

DOMAIN_BUFFERING_MODE module-attribute

DOMAIN_BUFFERING_MODE = Domain.BUFFERING_MODE

buffering_mode: the expected domain is a buffering mode of streams (e.g. line, block).

DOMAIN_CHARACTER_CODE_LIST module-attribute

DOMAIN_CHARACTER_CODE_LIST = Domain.CHARACTER_CODE_LIST

character_code_list: the expected domain is a list of character codes.

DOMAIN_CLOSE_OPTION module-attribute

DOMAIN_CLOSE_OPTION = Domain.CLOSE_OPTION

close_option: the expected domain is an option of close/2 (e.g. force(true)).

DOMAIN_DATE_TIME module-attribute

DOMAIN_DATE_TIME = Domain.DATE_TIME

date_time: the expected domain is a date-time.

DOMAIN_EOF_ACTION module-attribute

DOMAIN_EOF_ACTION = Domain.EOF_ACTION

eof_action: the expected domain is an end-of-stream action (error, eof_code, reset).

DOMAIN_FLAG_VALUE module-attribute

DOMAIN_FLAG_VALUE = Domain.FLAG_VALUE

flag_value: the expected domain is an admissible value of a Prolog flag.

DOMAIN_FORMAT_CONTROL_SEQUENCE module-attribute

DOMAIN_FORMAT_CONTROL_SEQUENCE = (
    Domain.FORMAT_CONTROL_SEQUENCE
)

format_control_sequence: the expected domain is a control sequence of format/2,3.

DOMAIN_IO_MODE module-attribute

DOMAIN_IO_MODE = Domain.IO_MODE

io_mode: the expected domain is a mode of opening streams (read, write, append).

DOMAIN_WELL_FORMED_LIST module-attribute

DOMAIN_WELL_FORMED_LIST = Domain.WELL_FORMED_LIST

well_formed_list: the expected domain is a well-formed list, i.e. one ending with [].

DOMAIN_NON_EMPTY_LIST module-attribute

DOMAIN_NON_EMPTY_LIST = Domain.NON_EMPTY_LIST

non_empty_list: the expected domain is a non-empty list.

DOMAIN_NOT_LESS_THAN_ZERO module-attribute

DOMAIN_NOT_LESS_THAN_ZERO = Domain.NOT_LESS_THAN_ZERO

not_less_than_zero: the expected domain is a non-negative number.

DOMAIN_OPERATOR_PRIORITY module-attribute

DOMAIN_OPERATOR_PRIORITY = Domain.OPERATOR_PRIORITY

operator_priority: the expected domain is an operator priority (an integer, 0 to 1200).

DOMAIN_OPERATOR_SPECIFIER module-attribute

DOMAIN_OPERATOR_SPECIFIER = Domain.OPERATOR_SPECIFIER

operator_specifier: the expected domain is an operator specifier (e.g. xfx, fy, yfx).

DOMAIN_ORDER module-attribute

DOMAIN_ORDER = Domain.ORDER

order: the expected domain is a comparison order (<, =, >).

DOMAIN_OS_FILE_PERMISSION module-attribute

DOMAIN_OS_FILE_PERMISSION = Domain.OS_FILE_PERMISSION

os_file_permission: the expected domain is a permission of files in the operating system.

DOMAIN_OS_FILE_PROPERTY module-attribute

DOMAIN_OS_FILE_PROPERTY = Domain.OS_FILE_PROPERTY

os_file_property: the expected domain is a property of files in the operating system.

DOMAIN_OS_PATH module-attribute

DOMAIN_OS_PATH = Domain.OS_PATH

os_path: the expected domain is a path of the operating system.

DOMAIN_PREDICATE_PROPERTY module-attribute

DOMAIN_PREDICATE_PROPERTY = Domain.PREDICATE_PROPERTY

predicate_property: the expected domain is a property of predicates (e.g. dynamic).

DOMAIN_FLAG module-attribute

DOMAIN_FLAG = Domain.FLAG

flag: the expected domain is the name of a Prolog flag.

DOMAIN_READ_OPTION module-attribute

DOMAIN_READ_OPTION = Domain.READ_OPTION

read_option: the expected domain is an option of read_term/2,3 (e.g. variables(Vs)).

DOMAIN_SELECTABLE_ITEM module-attribute

DOMAIN_SELECTABLE_ITEM = Domain.SELECTABLE_ITEM

selectable_item: the expected domain is an item that can be selected.

DOMAIN_SOCKET_ADDRESS module-attribute

DOMAIN_SOCKET_ADDRESS = Domain.SOCKET_ADDRESS

socket_address: the expected domain is a socket address.

DOMAIN_SOCKET_DOMAIN module-attribute

DOMAIN_SOCKET_DOMAIN = Domain.SOCKET_DOMAIN

socket_domain: the expected domain is a socket domain.

DOMAIN_SOURCE_SINK module-attribute

DOMAIN_SOURCE_SINK = Domain.SOURCE_SINK

source_sink: the expected domain is a source or sink of a stream (e.g. a file name or URL).

DOMAIN_STREAM module-attribute

DOMAIN_STREAM = Domain.STREAM

stream: the expected domain is a stream.

DOMAIN_STREAM_OPTION module-attribute

DOMAIN_STREAM_OPTION = Domain.STREAM_OPTION

stream_option: the expected domain is an option of open/4 (e.g. alias(A)).

DOMAIN_STREAM_OR_ALIAS module-attribute

DOMAIN_STREAM_OR_ALIAS = Domain.STREAM_OR_ALIAS

stream_or_alias: the expected domain is a stream, or the alias of one.

DOMAIN_STREAM_POSITION module-attribute

DOMAIN_STREAM_POSITION = Domain.STREAM_POSITION

stream_position: the expected domain is a position in a stream.

DOMAIN_STREAM_PROPERTY module-attribute

DOMAIN_STREAM_PROPERTY = Domain.STREAM_PROPERTY

stream_property: the expected domain is a property of streams (e.g. alias(A), input).

DOMAIN_STREAM_SEEK_METHOD module-attribute

DOMAIN_STREAM_SEEK_METHOD = Domain.STREAM_SEEK_METHOD

stream_seek_method: the expected domain is a method of seeking in a stream.

DOMAIN_STREAM_TYPE module-attribute

DOMAIN_STREAM_TYPE = Domain.STREAM_TYPE

stream_type: the expected domain is a type of streams (text, binary).

DOMAIN_TERM_STREAM_OR_ALIAS module-attribute

DOMAIN_TERM_STREAM_OR_ALIAS = Domain.TERM_STREAM_OR_ALIAS

term_stream_or_alias: the expected domain is a term stream, or the alias of one.

DOMAIN_VAR_BINDING_OPTION module-attribute

DOMAIN_VAR_BINDING_OPTION = Domain.VAR_BINDING_OPTION

var_binding_option: the expected domain is an option binding variables.

DOMAIN_WRITE_OPTION module-attribute

DOMAIN_WRITE_OPTION = Domain.WRITE_OPTION

write_option: the expected domain is an option of write_term/2,3 (e.g. quoted(true)).

DOMAIN_CLAUSE module-attribute

DOMAIN_CLAUSE = Domain.CLAUSE

clause: the expected domain is a well-formed clause.

That is, a callable term, or a Head :- Body one with a callable head and body.

DOMAIN_RULE module-attribute

DOMAIN_RULE = Domain.RULE

rule: the expected domain is a rule, i.e. a Head :- Body clause.

DOMAIN_FACT module-attribute

DOMAIN_FACT = Domain.FACT

fact: the expected domain is a fact, i.e. a clause with no body.

DOMAIN_DIRECTIVE module-attribute

DOMAIN_DIRECTIVE = Domain.DIRECTIVE

directive: the expected domain is a directive, i.e. a :- Goal clause.

domain_error_for_flag_values

domain_error_for_flag_values(
    context: ExecutionContext,
    procedure: Signature,
    flag_values: Iterable[Term],
    actual: Term,
    index: int | None = None,
) -> DomainError

Creates the domain error for an argument that should be one of some values, e.g. of a flag.

Parameters:

Name Type Description Default
context ExecutionContext

the execution context in which the error occurs (e.g. a request's context).

required
procedure Signature

the signature of the predicate raising the error (e.g. a request's signature).

required
flag_values Iterable[Term]

the admissible values.

required
actual Term

the offending argument.

required
index int | None

the (0-based) position of the offending argument, if known (it only changes the message).

None

Returns:

Type Description
DomainError

The error, thrown as error(domain_error(flag_value, Actual), Message).

Examples:

>>> from tuprolog.solve import signature
>>> from tuprolog.solve.exception.error import *
>>> from tuprolog.solve.prolog import prolog_solver
>>> context = prolog_solver().current_context
>>> from tuprolog.core import atom
>>> error = domain_error_for_flag_values(
...     context, signature("foo", 1), [atom("on"), atom("off")], atom("maybe"), 0
... )
>>> error.type
domain_error(flag_value, maybe)

domain_error_for_argument

domain_error_for_argument(
    context: ExecutionContext,
    procedure: Signature,
    expected: Domain,
    actual: Term,
    index: int | None = None,
) -> DomainError

Creates the domain error for an argument of a predicate outside the expected domain.

The usual way of reporting that a primitive received an argument of the right type but with an inadmissible value.

Parameters:

Name Type Description Default
context ExecutionContext

the execution context in which the error occurs (e.g. a request's context).

required
procedure Signature

the signature of the predicate raising the error (e.g. a request's signature).

required
expected Domain

the expected domain (e.g. DOMAIN_NOT_LESS_THAN_ZERO).

required
actual Term

the offending argument.

required
index int | None

the (0-based) position of the offending argument, if known (it only changes the message).

None

Returns:

Type Description
DomainError

The error, thrown as error(domain_error(Expected, Actual), Message).

Examples:

>>> from tuprolog.solve import signature
>>> from tuprolog.solve.exception.error import *
>>> from tuprolog.solve.prolog import prolog_solver
>>> context = prolog_solver().current_context
>>> from tuprolog.core import integer
>>> procedure = signature("foo", 2)
>>> domain = DOMAIN_NOT_LESS_THAN_ZERO
>>> error = domain_error_for_argument(context, procedure, domain, integer(-1), 1)
>>> error.type
domain_error(not_less_than_zero, -1)

domain_error_for_term

domain_error_for_term(
    context: ExecutionContext,
    expected: Domain,
    actual_value: Term,
) -> DomainError

Creates the domain error for a term outside the expected domain.

Parameters:

Name Type Description Default
context ExecutionContext

the execution context in which the error occurs (e.g. a request's context).

required
expected Domain

the expected domain.

required
actual_value Term

the offending term.

required

Returns:

Type Description
DomainError

The error, thrown as error(domain_error(Expected, ActualValue), Message).

Examples:

>>> from tuprolog.solve import signature
>>> from tuprolog.solve.exception.error import *
>>> from tuprolog.solve.prolog import prolog_solver
>>> context = prolog_solver().current_context
>>> from tuprolog.core import atom
>>> domain_error_for_term(context, DOMAIN_IO_MODE, atom("rw")).message
'Term `rw` is not a valid io_mode'

domain_error_for_goal

domain_error_for_goal(
    context: ExecutionContext,
    procedure: Signature,
    expected: Domain,
    actual: Term,
) -> DomainError

Creates the domain error for a sub-goal of a predicate outside the expected domain.

Parameters:

Name Type Description Default
context ExecutionContext

the execution context in which the error occurs (e.g. a request's context).

required
procedure Signature

the signature of the predicate raising the error (e.g. a request's signature).

required
expected Domain

the expected domain (e.g. DOMAIN_CLAUSE).

required
actual Term

the offending sub-goal.

required

Returns:

Type Description
DomainError

The error, thrown as error(domain_error(Expected, Actual), Message).

expected_domain

expected_domain(name: str | Term) -> Domain

The Domain named by a string or an atom.

Parameters:

Name Type Description Default
name str | Term

the name, as a string (case-insensitive) or a tuprolog.core.Term (as in the error terms, e.g. atom("io_mode")).

required

Returns:

Type Description
Domain

The matching Domain; None for terms other than atoms.

Raises:

Type Description
IllegalArgumentException

if no Domain has that name.

Examples:

>>> from tuprolog.core import atom
>>> from tuprolog.solve.exception.error.domain import *
>>> expected_domain("io_mode") == expected_domain(atom("io_mode")) == DOMAIN_IO_MODE
True