Did you know ... | Search Documentation: |
Pack type_check -- README.md |
We are grateful for useful comments and feedback from:
Define polymorphic algebraic data types like:
:- type pair(A,B) ---> A - B. :- type list(T) ---> [] ; [T|list(T)]. :- type boolean ---> true ; false.
(NOTE: the above types are predefined, as well as integer
and float
.)
Type definitions can also be empty, e.g.
:- type an_empty_type.
This means that the type is not inhabited by instantiated values. Only logical variables are possible.
Predicates are given a signature like:
:- pred append(list(E), list(E), list(E)). :- pred not(boolean, boolean). :- pred lookup(list(pair(Key,Value)), Key, Value).
Optionally, modes can be specified as well, by prefixing
the argument types with +
, -
or ?
. For instance:
:- pred not(+boolean, -boolean).
Note that the type checker fully ignores mode declarations. Hence, mode declarations have not other purpose than to serve as documentation. In particular, the validity of mode declarations is not verified.
:- pred concat(list(list(integer)),list(integer)). concat(LL,L) :- flatten(LL,L) :: flatten(list(list(integer)),list(integer)).
which results in runtime type checking. The annotation is also used for static type checking of the code surrounding the annotated call.
A variant of the annotation is only used for static type checking, and does not result in runtime checks:
concat(LL,L) :- flatten(LL,L) :< flatten(list(list(integer)),list(integer)).
:- trust_pred sort(list(integer),list(integer)).
This signature is only into account when checking calls from typed code.
Untypable code, e.g. using Prolog built-ins, may be encapsulated in a trusted predicate. E.g.
:- trust_pred new_array(list(T),array(T)). new_array(List,Array) :- Array =.. [array|List].
No additional runtime checks are performed for trusted predicates.
Similarly, untyped imported predicates may be given a type signature with the trust_pred declaration.
Options can be passed to the type checker with the declaration
:- type_check_options(Options).
where Options is a list containing zero or more of the following elements:
check(Flag)
where Flag is on
or off
,
to enable or disable the type checker
enabled by defaultruntime(Flag)
where Flag is on
or off
,
to enable or disable runtime type checking
disabled by defaultverbose(Flag)
where Flag is on
or off
,
to enable or disable printed summary at end of type checking
enabled by defaultlist(T)
not defined twicetc_stats(Errors,Total)