object

# type

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

author:
  `Paulo Moura`
version:
  `1.17`
date:
  `2018/4/4`

compilation flags:
  `static, context_switching_calls, complements(restrict)`

(no dependencies on other entities)

## Public interface

### 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 – number of proofs:
  `type(?callable) – 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 – 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 – 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)`
mode – 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 interface

(none)

## Private predicates

(none)

## Operators

### (none)

## 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}

Base types from Prolog
  {term, var, nonvar, atomic, atom, number, integer, float, compound, callable, ground}

Atom derived types
  {non_empty_atom, boolean, character}

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, character_code}

List types (compound derived types)
  {list, non_empty_list, partial_list, list_or_partial_list, list(Type), list(Type, Min, Max), non_empty_list(Type), difference_list, difference_list(Type)}

Other compound derived types
  {predicate_indicator, non_terminal_indicator, predicate_or_non_terminal_indicator, clause, clause_or_partial_clause, grammar_rule, pair, pair(KeyType,ValueType), cyclic, acyclic}

Stream types
  {stream, stream_or_alias, stream(Property), stream_or_alias(Property)}

Other types
  {between(Type,Lower,Upper), property(Type, LambdaExpression), one_of(Type, Set), var_or(Type), ground(Type), types(Types)}

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

boolean type 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.

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

character_code type 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.

between(Type, Lower, Upper) type 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.

property(Type, Lambda) type 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.

one_of(Type, Set) type 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.

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

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

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

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.

Caveats
  The type argument to the predicates is never itself type-checked for performance reasons.

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.

## See also

- arbitrary
- os_types