TYPICAL / Guided Tour / Type
Bottom 

Predicate declaration examples


append/3

Concatenation of two lists.
Comparison with results of conventional type-checkers (marked with "old").

:- pred append_old(list(T),list(T),list(T)).
% is identical with
% :- pred append_old(list(@T1),list(@T2),list(@T)) with (T1=<T, T2=<T, T3=<T).

:- pred append(list(T),list(T),list(@T)).
% is identical with
% :- pred append(list(@T1),list(@T2),list(@T3)) with (T1=<T3, T2=<T3).

File app
Output of type checking (app.tap)

Top Bottom 

Simplification of declarations

Use of anchored type parameters ("@T").

File simpdecl
Internal data of the declarations (simpdecl.all)

Top Bottom 

member/2 and '='/2 and insert_uniq/3

Comparison with results of conventional type-checkers (marked with "old").

File member
Output of type checking (member.tap)

Top Bottom 

reverse/2

Predicate reverse/2 reverses the order of a given list.

File reverse
Output of type checking (reverse.tap)

Top Bottom 

fibonacci/2

Fibonacci numbers.

File fibonacci
Output of type checking (fibonacci.tap)

Top Bottom 

partially ordered set

Predicate declarations with a type parameter (a partially ordered set).

% a partially ordered set is internally represented by a structure


:- type poset(T) -->
        poset(list(T),          % domain
              list(order1(T))). % order relationships

:- type order1(T) -> T=<T.

Example: poset([a,b,c,d,e,f], [a=<b, c=<d, e=<d, b=<d, b=<f, c=<f])


d   f   d
 \ / \ / \
  b   c   e
  \
   a
(lub=least upper bound, mub=minimal upper bound)
For this partially ordered set:
lub(c,e)=d, mub(c,e)=d,
there is no lub(b,c),
mub(b,c)=f and mub(b,c)=d.


File poset
Output of type checking (poset.tap)

Top Bottom 

overlap/2 and disjoint/2

disjoint/2 is the negation of the predicate overlap/2 and is therfore negatively declared ("pred_neg").

:- pred overlap(list(@S),list(@T)) with U=<S, U=<T.
:- pred_neg disjoint(list(@S),list(@T)) with U=<S, U=<T.

:- disjoint([-1,-2],[2,3]).
% now useless atom "false"

File overlap
Output of type checking (overlap.tap)

Top Bottom 

DCG grammars

As a convenience for declaring grammar rules,

:- rule p(T1,...,Tk).

has the same effect as

:- predl p(T1,...,Tk, list(@T), list(@T)).

where T is a new type parameter.

The predicate 'C'/3 is predeclared in TYPICAL library prolog_dcg.pl as

:- pred 'C'(list(@T),T,list(T)).

File dcg
Output of type checking (dcg.tap)

Top 
PREV: Predicate declaration
NEXT: Case-study: Complex list structures

TYPICAL / Guided Tour / Type