object

# `type`

Type checking predicates. User extensible. New types can be defined by adding clauses for the `type/1` and `check/2` multifile predicates.

Availability:
`logtalk_load(types(loader))`
Author: Paulo Moura
Version: 2:5:1
Date: 2024-09-26
Compilation flags:
`static, context_switching_calls, complements(restrict)`
Uses:
list
Remarks:

> - Logtalk specific types: `entity`, `object`, `protocol`, `category`, `entity_identifier`, `object_identifier`, `protocol_identifier`, `category_identifier`, `event`, `predicate`.

> - Prolog module related types (when the backend compiler supports modules): `module`, `module_identifier`, `qualified_callable`.

> - Prolog base types: `term`, `var`, `nonvar`, `atomic`, `atom`, `number`, `integer`, `float`, `compound`, `callable`, `ground`.

> - Atom derived types: `non_quoted_atom`, `non_empty_atom`, `boolean`, `character`, `in_character`, `char`, `operator_specifier`, `hex_char`.

> - Atom derived parametric types: `atom(CharSet)`, `atom(CharSet,Length)`, `non_empty_atom(CharSet)`, `character(CharSet)`, `in_character(CharSet)`, `char(CharSet)`.

> - Number derived types: `positive_number`, `negative_number`, `non_positive_number`, `non_negative_number`.

> - Float derived types: `positive_float`, `negative_float`, `non_positive_float`, `non_negative_float, probability`.

> - Integer derived types: `positive_integer`, `negative_integer`, `non_positive_integer`, `non_negative_integer`, `byte`, `in_byte`, `character_code`, `in_character_code`, `code`, `operator_priority`, `hex_code`.

> - Integer derived parametric types: `character_code(CharSet)`, `in_character_code(CharSet)`, `code(CharSet)`.

> - List types (compound derived types): `list`, `non_empty_list`, `partial_list`, `list_or_partial_list`, `list(Type)`, `list(Type,Length)`, `list(Type,Min,Max)`, `list(Type,Length,Min,Max)`, `non_empty_list(Type)`, `codes`, `chars`.

> - Difference list types (compound derived types): `difference_list`, `difference_list(Type)`.

> - Other compound derived types: `compound(Name,Types)`, `predicate_indicator`, `non_terminal_indicator`, `predicate_or_non_terminal_indicator`, `clause`, `grammar_rule`, `pair`, `pair(KeyType,ValueType)`, `cyclic`, `acyclic`.

> - Stream types: `stream`, `stream_or_alias`, `stream(Property)`, `stream_or_alias(Property)`.

> - Other types: `Object::Closure`, `between(Type,Lower,Upper)`, `property(Type,LambdaExpression)`, `one_of(Type,Set)`, `var_or(Type)`, `ground(Type)`, `types(Types)`, `constrain(Type,Closure)`, `type`.

> - Type `predicate` notes: This type is used to check for an object public predicate specified as `Object::Functor/Arity`.

> - Type `boolean` notes: The two value of this type are the atoms `true` and `false`.

> - Stream types notes: In the case of the `stream(Property)` and `stream_or_alias(Property)` types, Property must be a valid stream property.

> - Type `order` notes: The three possible values of this type are the single character atoms `<`, `=`, and `>`.

> - Type `character_code` notes: This type takes into account Unicode support by the backend compiler. When Unicode is supported, it distinguishes between BMP and full support. When Unicode is not supported, it assumes a byte representation for characters.

> - Type `Object::Closure` notes: Allows calling a public object predicate for type-checking. The predicate should provide `valid/2` predicate semantics and assume called with a bound argument. The `Closure` closure is extended with a single argument, the value to be checked.

> - Type `compound(Name,Types)` notes: This type verifies that a compound term have the given `Name` and its arguments conform to `Types`.

> - Type `between(Type, Lower, Upper)` notes: The type argument allows distinguishing between numbers and other types. It also allows choosing between mixed integer/float comparisons and strict float or integer comparisons. The term is type-checked before testing for interval membership.

> - Type `property(Type, Lambda)` notes: Verifies that `Term` satisfies a property described using a lambda expression of the form `[Parameter]>>Goal`. The lambda expression is applied in the context of `user`. The term is type-checked before calling the goal.

> - Type `one_of(Type, Set)` notes: For checking if a given term is an element of a set. The set is represented using a list. The term is type-checked before testing for set membership.

> - Type `var_or(Type)` notes: Allows checking if a term is either a variable or a valid value of the given type.

> - Type `ground(Type)` notes: Allows checking if a term is ground and a valid value of the given type.

> - Type `types(Types)` notes: Allows checking if a term is a valid value for one of the types in a list of types.

> - Type `constrain(Type,Closure)` notes: Allows checking if a term is a valid value for the given type and satisfies the given closure.

> - Type `type` notes: Allows checking if a term is a valid type.

> - Type `qualified_callable` notes: Allows checking if a term is a possibly module-qualified callable term. When the term is qualified, it also checks that the qualification modules are type correct. When the term is not qualified, its semantics are the same as the callable type.

> - Design choices: The main predicates are `valid/2` and `check/3`. These are defined using the predicate `check/2`. Defining clauses for `check/2` instead of `valid/2` gives the user full control of exception terms without requiring an additional predicate.

> - Error context: The built-in execution-context method `context/1` can be used to provide the calling context for errors when using the predicate `check/3`.

> - Registering new types: New types can be registered by defining clauses for the `type/1` and `check/2` multifile predicates. Clauses for both predicates must have a bound first argument to avoid introducing spurious choice-points when type-checking terms.

> - Meta-types: Meta-types are types that have one or more sub-type arguments. E.g. `var_or(Type)`. The sub-types of a meta-type can be enumerated by defining a clause for the `meta_type/3` multifile predicate.

> - Character sets: When testing character or character code based terms (e.g., atom), it is possible to choose a character set (`ascii_identifier`, `ascii_printable`, `ascii_full`, `byte`, `unicode_bmp`, or `unicode_full`) using the parameterizable types.

> - Caveats: The type argument (and any type parameterization) to the predicates is not type-checked (or checked for consistency) for performance reasons.

> - Unicode limitations: Currently, correct character/code type-checking is only ensured for XVM and SWI-Prolog as other backends do not provide support for querying a Unicode code point category.

Inherited public predicates:
arbitrary/1 arbitrary/2 edge_case/2 get_seed/1 max_size/1 mutation/3 set_seed/1 shrink/3 shrink_sequence/3 shrinker/1

## Public predicates

### `type/1`

Table of defined types. A new type can be registered by defining a clause for this predicate and adding a clause for the `check/2` multifile predicate.

Compilation flags:
`static, multifile`
Template:
`type(Type)`
Mode and number of proofs:
`type(?callable)` - `zero_or_more`

### `meta_type/3`

Table of defined meta-types. A registered type that is a meta-type can be described by defining a clause for this predicate to enumerate its sub-types and optional values in case of a single sub-type.

Compilation flags:
`static, multifile`
Template:
`meta_type(MetaType,SubTypes,Values)`
Mode and number of proofs:
`meta_type(?callable,-list,-list)` - `zero_or_more`

### `valid/2`

True if the given term is of the specified type. Fails otherwise.

Compilation flags:
`static`
Template:
`valid(Type,Term)`
Mode and number of proofs:
`valid(@callable,@term)` - `zero_or_one`

### `check/3`

True if the given term is of the specified type. Throws an error otherwise using the format `error(Error, Context)`. For the possible values of `Error` see the `check/2` predicate.

Compilation flags:
`static`
Template:
`check(Type,Term,Context)`
Mode and number of proofs:
`check(@callable,@term,@term)` - `one_or_error`

### `check/2`

True if the given term is of the specified type. Throws an error otherwise. A new type can be added by defining a clause for this predicate and registering it by adding a clause for the `type/1` multifile predicate.

Compilation flags:
`static, multifile`
Template:
`check(Type,Term)`
Meta-predicate template:
`check(::,*)`
Mode and number of proofs:
`check(@callable,@term)` - `one_or_error`
Exceptions:
`Term` is not bound as required:
`instantiation_error`
`Term` is bound but not of the specified type:
`type_error(Type,Term)`
`Term` is the of the correct type but not in the specified domain:
`domain_error(Domain,Term)`
`Term` is the of the correct type and domain but the resource it represents does not exist:
`existence_error(Type,Term)`

## Protected predicates

(no local declarations; see entity ancestors if any)

## Private predicates

(no local declarations; see entity ancestors if any)

## Operators

(none)

See also

arbitrary, os_types, either, maybe