type
tuprolog.solve.exception.error.type
¶
Type errors: an argument does not have the expected type, e.g. a number where an atom is due.
ISO Prolog's type_error(Type, Culprit) error, thrown as error(type_error(Type, Culprit), Extra),
means that Culprit should have been of type Type (e.g. integer, atom, callable, list),
and it is not: e.g. atom_length(f, a) throws type_error(integer, a). Unbound arguments raise
instantiation errors instead (see tuprolog.solve.exception.error.instantiation), and values of
the right type outside the admissible ones raise domain errors (see
tuprolog.solve.exception.error.domain).
The error is a TypeError (the JVM class: import it explicitly, as it shadows Python's built-in
name, though it is also a Python TypeError, see tuprolog.errors), and the expected types are
the values of the Type enumeration (also available as the TYPE_* constants). Build errors with
type_error_for_argument (the usual case, for primitives' arguments),
type_error_for_argument_list, type_error_for_goal, type_error_for_term, or type_error.
Examples:
>>> from tuprolog.solve import signature
>>> from tuprolog.solve.prolog import prolog_solver
>>> context = prolog_solver().current_context
>>> from tuprolog.core import atom
>>> error = type_error_for_argument(context, signature("foo", 2), TYPE_INTEGER, atom("a"), 1)
>>> error.type
type_error(integer, a)
>>> error.message
'The 1-th argument of `foo/2` should be a `integer`, but `a` has been provided instead'
>>> prolog_solver().solve_once("catch(atom_length(f, a), error(E, _), true)")["E"]
type_error(integer, a)
See Also
tuprolog.solve.primitive, whose ensuring_argument_is_* checks raise these errors;
2P-Kt's documentation on
errors and exceptions.
Type
module-attribute
¶
Type = TypeError.Expected
The enumeration of the types a type error may expect (2P-Kt's TypeError.Expected).
Each value (also available as a TYPE_* constant) is the Type of a
type_error(Type, Culprit) term: to_term() gives it as an atom (e.g. integer).
Type.of(name) and Type.fromTerm(term) look a value up (see expected_type).
Examples:
>>> Type.INTEGER.to_term()
integer
TYPE_ATOMIC
module-attribute
¶
TYPE_ATOMIC = Type.ATOMIC
atomic: the expected type is an atomic term, i.e. an atom or a number.
TYPE_BOOLEAN
module-attribute
¶
TYPE_BOOLEAN = Type.BOOLEAN
boolean: the expected type is a boolean, i.e. true or false.
TYPE_BYTE
module-attribute
¶
TYPE_BYTE = Type.BYTE
byte: the expected type is a byte, i.e. an integer from 0 to 255.
TYPE_CALLABLE
module-attribute
¶
TYPE_CALLABLE = Type.CALLABLE
callable: the expected type is a callable term, i.e. an atom or a compound term (a goal).
TYPE_CHARACTER
module-attribute
¶
TYPE_CHARACTER = Type.CHARACTER
character: the expected type is a character, i.e. an atom of length 1.
TYPE_COMPOUND
module-attribute
¶
TYPE_COMPOUND = Type.COMPOUND
compound: the expected type is a compound term, i.e. a structure with arguments.
TYPE_DEALIASING_EXPRESSION
module-attribute
¶
TYPE_DEALIASING_EXPRESSION = Type.DEALIASING_EXPRESSION
dealiasing_expression: the expected type is a de-aliasing expression of OOPLib.
That is, $Alias with an atom Alias (see tuprolog.solve.libs).
TYPE_EVALUABLE
module-attribute
¶
TYPE_EVALUABLE = Type.EVALUABLE
evaluable: the expected type is an evaluable functor, i.e. one of an arithmetic function.
The culprit is the indicator of the functor, e.g. foo/1.
TYPE_FLOAT
module-attribute
¶
TYPE_FLOAT = Type.FLOAT
float: the expected type is a floating-point number.
TYPE_INTEGER
module-attribute
¶
TYPE_INTEGER = Type.INTEGER
integer: the expected type is an integer.
TYPE_IN_CHARACTER
module-attribute
¶
TYPE_IN_CHARACTER = Type.IN_CHARACTER
in_character: the expected type is an input character, i.e. a character or end_of_file.
TYPE_NUMBER
module-attribute
¶
TYPE_NUMBER = Type.NUMBER
number: the expected type is a number, i.e. an integer or a floating-point one.
TYPE_OBJECT_REFERENCE
module-attribute
¶
TYPE_OBJECT_REFERENCE = Type.OBJECT_REFERENCE
object_reference: the expected type is a reference to a Python object.
See OOPLib, in tuprolog.solve.libs.
TYPE_PAIR
module-attribute
¶
TYPE_PAIR = Type.PAIR
pair: the expected type is a pair, i.e. a Key - Value term.
TYPE_PREDICATE_INDICATOR
module-attribute
¶
TYPE_PREDICATE_INDICATOR = Type.PREDICATE_INDICATOR
predicate_indicator: the expected type is a predicate indicator, i.e. a Name/Arity term.
TYPE_REFERENCE
module-attribute
¶
TYPE_REFERENCE = Type.REFERENCE
reference: the expected type is a reference to a Python object or type.
See OOPLib, in tuprolog.solve.libs.
TYPE_TYPE_REFERENCE
module-attribute
¶
TYPE_TYPE_REFERENCE = Type.TYPE_REFERENCE
type_reference: the expected type is a reference to a Python type.
See OOPLib, in tuprolog.solve.libs.
TYPE_URL
module-attribute
¶
TYPE_URL = Type.URL
url: the expected type is a URL or a file path, e.g. for open/3,4 or consult/1.
TYPE_VARIABLE
module-attribute
¶
TYPE_VARIABLE = Type.VARIABLE
variable: the expected type is an unbound variable.
type_error
¶
type_error(
context: ExecutionContext,
expected: Type,
actual: Term,
message: str,
) -> TypeError
Creates a type error with a custom message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ExecutionContext
|
the execution context in which the error occurs (e.g. a request's |
required |
expected
|
Type
|
the expected type (e.g. |
required |
actual
|
Term
|
the offending term. |
required |
message
|
str
|
the error message. |
required |
Returns:
| Type | Description |
|---|---|
TypeError
|
The error, thrown as |
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
>>> type_error(context, TYPE_ATOM, integer(1), "not an atom").error_struct
error(type_error(atom, 1), 'not an atom')
type_error_for_argument_list
¶
type_error_for_argument_list(
context: ExecutionContext,
procedure: Signature,
expected: Type,
actual: Term,
index: int | None = None,
) -> TypeError
Creates the type error for an argument that should be a list of items of some type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ExecutionContext
|
the execution context in which the error occurs (e.g. a request's |
required |
procedure
|
Signature
|
the signature of the predicate raising the error (e.g. a request's |
required |
expected
|
Type
|
the type the items should have. |
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 |
|---|---|
TypeError
|
The error, thrown as |
type_error_for_argument
¶
type_error_for_argument(
context: ExecutionContext,
procedure: Signature,
expected: Type,
actual: Term,
index: int | None = None,
) -> TypeError
Creates the type error for an argument of a predicate not having the expected type.
The usual way of reporting that a primitive received an argument of a wrong type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ExecutionContext
|
the execution context in which the error occurs (e.g. a request's |
required |
procedure
|
Signature
|
the signature of the predicate raising the error (e.g. a request's |
required |
expected
|
Type
|
the expected type (e.g. |
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 |
|---|---|
TypeError
|
The error, thrown as |
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
>>> procedure = signature("foo", 2)
>>> error = type_error_for_argument(context, procedure, TYPE_INTEGER, atom("a"), 0)
>>> error.type
type_error(integer, a)
>>> error.message
'The 0-th argument of `foo/2` should be a `integer`, but `a` has been provided instead'
type_error_for_term
¶
type_error_for_term(
context: ExecutionContext,
expected: Type,
actual_value: Term,
) -> TypeError
Creates the type error for a term not having the expected type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ExecutionContext
|
the execution context in which the error occurs (e.g. a request's |
required |
expected
|
Type
|
the expected type. |
required |
actual_value
|
Term
|
the offending term. |
required |
Returns:
| Type | Description |
|---|---|
TypeError
|
The error, thrown as |
Note
2P-Kt has no TypeError.forTerm (unlike DomainError.forTerm): this mirrors its 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
>>> type_error_for_term(context, TYPE_CALLABLE, integer(1)).message
'Term `1` is not a valid callable term'
type_error_for_goal
¶
type_error_for_goal(
context: ExecutionContext,
procedure: Signature,
expected: Type,
actual: Term,
) -> TypeError
Creates the type error for a sub-goal of a predicate not having the expected type.
E.g. for call/1 invoked with a non-callable goal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ExecutionContext
|
the execution context in which the error occurs (e.g. a request's |
required |
procedure
|
Signature
|
the signature of the predicate raising the error (e.g. a request's |
required |
expected
|
Type
|
the expected type (usually |
required |
actual
|
Term
|
the offending sub-goal. |
required |
Returns:
| Type | Description |
|---|---|
TypeError
|
The error, thrown as |
expected_type
¶
The Type named by a string or an atom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | Term
|
the name, as a string (case-insensitive) or a |
required |
Returns:
| Type | Description |
|---|---|
Type
|
The matching |
Raises:
| Type | Description |
|---|---|
IllegalArgumentException
|
if no |
Examples:
>>> from tuprolog.core import atom
>>> from tuprolog.solve.exception.error.type import *
>>> expected_type("integer") == expected_type(atom("integer")) == TYPE_INTEGER
True