Skip to content

evaluation

tuprolog.solve.exception.error.evaluation

Evaluation errors: an arithmetic expression cannot be evaluated, e.g. a division by zero.

ISO Prolog's evaluation_error(Error) error, thrown as error(evaluation_error(Error), Extra), means that evaluating an arithmetic expression (e.g. by is/2 or </2) failed because of Error: zero_divisor, undefined (e.g. 0 / 0), int_overflow, float_overflow, or underflow. Expressions containing non-evaluable terms raise type errors instead (e.g. type_error(evaluable, foo/0)).

The error is an EvaluationError, and the kinds of errors are the values of the ErrorType enumeration (also available as the ERROR_* constants). Build errors with evaluation_error.

Examples:

>>> from tuprolog.solve.prolog import prolog_solver
>>> prolog_solver().solve_once("catch(X is 1 / 0, error(E, _), true)")["E"]
evaluation_error(zero_divisor)
See Also

tuprolog.solve.function, for arithmetic functions defined in Python; 2P-Kt's documentation on errors and exceptions.

EvaluationError module-attribute

EvaluationError = errors.EvaluationError

The evaluation error, thrown as error(evaluation_error(Error), _): an expression failed.

The JVM class (also a Python ArithmeticError and a tuprolog.errors.PrologEvaluationError). Members (snake_case aliases as well): error_type, the ErrorType; besides those of tuprolog.solve.exception.LogicError (e.g. type, error_struct, message).

ErrorType module-attribute

ErrorType = EvaluationError.Type

The enumeration of the kinds of evaluation errors (2P-Kt's EvaluationError.Type).

Each value (also available as an ERROR_* constant) is the Error of an evaluation_error(Error) term: to_term() gives it as an atom (e.g. zero_divisor). ErrorType.valueOf(NAME) and ErrorType.fromTerm(term) look a value up (see error_type).

ERROR_INT_OVERFLOW module-attribute

ERROR_INT_OVERFLOW = ErrorType.INT_OVERFLOW

int_overflow: an integer result is too large to be represented.

ERROR_FLOAT_OVERFLOW module-attribute

ERROR_FLOAT_OVERFLOW = ErrorType.FLOAT_OVERFLOW

float_overflow: a floating-point result is too large to be represented.

ERROR_UNDERFLOW module-attribute

ERROR_UNDERFLOW = ErrorType.UNDERFLOW

underflow: a floating-point result is too small to be represented.

ERROR_ZERO_DIVISOR module-attribute

ERROR_ZERO_DIVISOR = ErrorType.ZERO_DIVISOR

zero_divisor: a number is divided by zero.

ERROR_UNDEFINED module-attribute

ERROR_UNDEFINED = ErrorType.UNDEFINED

undefined: the result is mathematically undefined (e.g. 0 / 0, log(0)).

evaluation_error

evaluation_error(
    context: ExecutionContext, type: ErrorType, message: str
) -> EvaluationError

Creates an evaluation error.

Parameters:

Name Type Description Default
context ExecutionContext

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

required
type ErrorType

the kind of error (e.g. ERROR_ZERO_DIVISOR).

required
message str

the error message, also the error term's extra data.

required

Returns:

Type Description
EvaluationError

The error, thrown as error(evaluation_error(Type), 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
>>> evaluation_error(context, ERROR_ZERO_DIVISOR, "division by zero").error_struct
error(evaluation_error(zero_divisor), 'division by zero')

error_type

error_type(name: str | Term) -> ErrorType

The ErrorType 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("zero_divisor")).

required

Returns:

Type Description
ErrorType

The matching ErrorType; None for terms other than atoms.

Raises:

Type Description
IllegalArgumentException

if no ErrorType has that name.

Examples:

>>> from tuprolog.core import atom
>>> from tuprolog.solve.exception.error.evaluation import *
>>> error_type("zero_divisor") == error_type(atom("zero_divisor")) == ERROR_ZERO_DIVISOR
True