TCLP Manual

Emmanuel COQUERY


1  What is TCLP ?

TCLP is a type checker for Prolog/CLP(X). TCLP thus aims to introduce a typing discipline to contraint logic programming. That is, given a description of the types’ universe, and the set of the types of used function symbols, TCLP will check the coherence of the use of these function symbols. TCLP take the program plus any type definitions as input and will output the type of infered predicates plus any errors w.r.t. the type checking.

Currently TCLP comes with definitions for three Prolog/CLP(X) dialects: ISO-Prolog, GNU-Prolog (with CLP(FD) extension) and SICStus Prolog with its libraries (including the constraint programming libraries CLP(B), CLP(FD), CLP(QR), CHR).

TCLP has been written in SICStus Prolog and comes in three forms: an online demo (Section 2), a SICStus Prolog library (Section 4) and a command line tool (Section 3).

1.1  Features

2  Online demo

2.1  Quick start

You can find the online demo at:

screen shot

There you can try to type check some programs by writing them into the “Program to type check” area, ex:

append([X|L],L2,[X|R]) :- append(L,L2,R).

By clicking on the type inference button, you will get the following result:

%% tclp 0.2.99g
%% starting type checking ...
:- typeof append(list(A),list(A),list(A)) is pred.
%% finished

You can also tell TCLP what Prolog/CLP dialect you are using with CLP dialect box.

2.2  Advanced usage

Here is a description of the different options you can use in the online demo:

3  Command line tool

3.1  Install

You can download the binaries at the following location:

3.1.1  Linux / MacOS X

Extract the archive in some folder, enter it and execute the install script:

tar xzvf tclp-i386-xxx.tgz
cd tclp-xxx

Install options:

–prefix <dir> the directory where TCLP files and directories will be installed (defaults to /usr/local)
–bindir <dir> the directory where the tclp executable will be installed, which should be in your PATH environment variable (defaults to prefix/bin)
–tclpdir <dir> the directory where TCLP files will be installed (defaults to prefix/tclp)

3.2  Get started

Basically, you just have to run on your file:



$ ../Devel/tclpchr/bin/tclp
%% tclp 0.3
%% tclp directory: /home/gewurz/coquery/Devel/tclpchr/bin
:- typeof append(list(A),list(A),list(A)) is pred.


3.3  Command line options

tclp [ --prolog <dialect> ] [ -I <dir> ] [ --stdlib <file> ] [ -v <sort> | -nv <sort> ] [ -i | --type_inference | --type_check ] [ -l | -nl ] [ --help ] <file> [ <files> ]

--prolog <dialect>
<dialect> is the name of a prolog dialect. This option causes TCLP to use type definitions for the corresponding dialect. Currently, available dialects are:
  • ISO: ISO Prolog (default)
  • gprolog: GNU Prolog
  • sicstus: SICStus Prolog
-I <dir>
This options adds <dir> to the list of directories where TCLP looks for files.
--stdlib <file>
Makes TCLP use <file> as the standard type library (for built-in predicates). By default TCLP will use the first file named stdlib.typ in the lookup directories (which contains the dialect directory tclpdir/lib/dialect).
-v <sort>
Enable verbose mode for <sort>, where <sort> can be either:
  • actions: prints the different actions of TCLP , like, e.g., loading a type library.
  • timing: prints CPU time used by TCLP for different actions.
  • defaults: output a message each time TCLP encounters a term that has no defined type. This can be useful to understand some error messages.
  • types: prints the type of each infered predicate (this is activated by default)
-nv <sort>
Disables verbose mode for <sort>.
-i, --type_inference
Enables predicate type inference (this is activated by default).
Disables predicate type inference.
Prints the different options.

4  SICStus Prolog library

4.1  Install and get started

You can download the library at the follwing location:

Just extract the archive where who want the library to be installed. To install the library as a SICStus Prolog library, extract it to a temporary folder and copy the files in the directory tclp-xxx to the SICStus Prolog library directory (e.g. /usr/local/lib/sicstus-3.9.1/library).

The library can be loaded with the use_module directive:

:- use_module(’tclp-installation-directory/tclp’)
or :- use_module(library(tclp)) if the TCLP library is installed into the SICStus Prolog library directory.

Then you must choose the prolog dialect of the files to type check, using one of the following predicates:

You can type check files and load types using the tclp/1 predicate:

:- tclp(’file1.typ’).
:- tclp([’file2.typ’, ’’]).

4.2  Available predicates

These predicate are available in the TCLP library

4.2.1  Type initialization

Remove all types from the current type data base and reload the types corresponding to the current dialect, using tclp__reinit/1 and user:init_dialect/1.
Remove all types from the current type data base, executes Goal and real the standard library according to the current settings (usually, Goal is used to determine these settings).
Sets the current CLP dialect to Dialect.
This is a dynamic predicate used by tclp__reinit/0.
Sets the dialect to SICStus Prolog and reinits the types.
Sets the dialect to ISO Prolog and reinits the types.
Sets the dialect to GNU Prolog and reinits the types.

4.2.2  Processing

FileOrFiles is either on file name or a list of file names. TCLP first loads ’.typ’ files of FileOrFiles, then reads and type checks ’.pl’ files of FileOrFiles.
FileOrFiles is a file or a list files. TCLP loads TCLP declarations in these files.
FileOrFiles is a file or a list files. TCLP reads Prolog programs in these files and type check them.
PhraseList is a list of pairs Phrase-Location, where Phrase is a phrase to type check and Location is some information about the phrase, given by tclp__reader:read_one_term(Stream, Phrase, Location, FileName).

4.2.3  Options

TrueFalse is either true or false. This predicate en(dis)ables type predicate inference in TCLP.
Directory is a directory name. This predicate adds Directory to the directories used by TCLP for file lookup.
Sets the name of the file to load as the standard type library to File.
Enables verbose mode for Sort, where Sort can be one of:
  • actions: prints the different actions of TCLP , like, e.g., loading a type library.
  • timing: prints CPU time used by TCLP for different actions.
  • defaults: output a message each time TCLP encounters a term that has no defined type. This can be useful to understand some error messages.
  • types: prints the type of each infered predicate (this is activated by default)
Disables verbose mode for Sort, where Sort can take the values above.

4.2.4  The tclp() path alias

The loading of the tclp module will cause a path alias ’tclp’ to be created. This alias can used, either to load a module of the TCLP implementation (like tclp(tclp__reader)) or to access the type files for ISO (resp. GNU and SICStus) Prolog using tclp(’lib/ISO/file.typ’) (resp. tclp(’lib/gprolog/file.typ’) and tclp(’lib/sicstus/file.typ’).

5  Syntax of TCLP declarations

5.1  Syntax of types

type ::= symbol( type , ... , type )
  | symbol
  | variable
symbol : a prolog atom, which corresponds to a type constructor
variable : a prolog variable, which corresponds to a type parameter

5.2  Type declaration

:- order Type1(A1, ... , AM) < Type2(B1, ... , BN)
Declares that the type constructor Type1 is smaller than the type constructor Type2. The mapping are given by the arguments, i.e. if AI == BJ then the I-th argument of Type1 corresponds to the J-th argument of Type2.
:- typeof +TypedTerm is +Type
Declares a type for the given term. TypedTerm is of the form Name(Type1,...,TypeN), where Type and TypeI are types. Type1,...,TypeN are the types of the arguments and Type is the type of the result.
:- type +TypeConstructor.
Declares a type. TypeConstructor is either of the form Name/Arity or of the form Name(A,B,...). It declares the existence of a type named Name which have Arity arguments.
:- type +Type is +TermConstructorList.
Combinaison of type and typeof. Type is of the form Name(A,B,...) and +TermConstructorList is alist of terms of the form Name(Type1,...,TypeN). The declared type is Type. The result type is Type.
:- untyped +SpecOrSpecList.
Avoid to type check the given terms. SpecOrSpecList is a functor specification of the form Name/Arity, or a list of such specifications. It causes TCLP to avoid to type check these terms.

5.3  TCLP meta declarations

:- tclp__include(+File)
Include TCLP declarations found in File.
:- user:args_location(+Location,?LocationList)
This predicate decompose the location of a term into the list of the locations of its subterms.
:- tclp__define_clause(+Phrase,+Location,+Heads,+Bodies,+Condition)
This will tell TCLP what the clauses look like. Phrase is the phrase to cut into bodies and heads. Location is the location of the phrase in the program source. Heads is a list of triplets Head-HLocation-Type, where Head is a head of the clause, HLocation is the location of Head in the program source (one can use user:args_location/2 to find it) and Type is the type expected for the head of the clause. Bodies is a list of pairs Body-BLocation, where Body is a body of the clause and BLocation is its location. Condition is a goal executed when recognizing clauses. E.g. (prolog directive “:- Body”):
:- tclp__define_clause((:- Body), Location, [], [Body-BodyLocation], user:args_location(Location,[BodyLocation]))
:- tclp__define_clause(+Phrase,+Location,+Heads,+Bodies)
Same as tclp__define_clause(Phrase,Location,Heads,Bodies,true)
:- tclp__define_clause_op(+BinOp,+Type)
This predicate is a shortcut of tclp__define_clause/5 for defining binary clause operators such as ’:-’/2. BinOp is the name of the operator and Type is the type expected for the head of the clause. E.g.
:- tclp__define_clause_op(’:-’,pred).
:- tclp__define_clause_op(+BinOp)
Same as tclp__define_clause_op(BinOp,pred)
:- tclp__executable(+Goal,+Condition)
Tells TCLP that the goal Goal is a TCLP declaration if the goal Condition succeeds. E.g. the op/3 directive:
:- tclp__executable(op(_,_,_),true).
:- tclp__executable(+Goal)
Same as tclp__executable(+Goal,true)
:- tclp__add_hook(+Goal,+Location,+Hook,+Condition)
Whenever TCLP encounters a TCLP declaration, it will handle it using a hook defined via this predicate. Goal is the TCLP declaration, Location is its location in the program source, Hook is the goal that will be executed by TCLP to handle the declaration and Condition is a goal that must succeed. E.g. the op/3 directive:
:- tclp__add_hook(op(Priority,Mode,Operators), _, op(Priority,Mode,Operators), true).
:- tclp__add_hook(+Goal,+Location,+Hook)
Same as tclp__add_hook(Goal,Location,Hook,true)
:- tclp__add_hook(+Goal,+Hook)
Same as tclp__add_hook(Goal,_,Hook)

5.4  Dialect specific declarations

:- tclp__load_prolog(+File)
Causes TCLP to consult File is in consult/1. This can be useful if you want to define complex treatments of some TCLP declarations.

6  Limitations

Currently, TCLP only loads .typ files automatly when it encounters the directives use_module/[123] and module/2. The functor ’:’/2 is not handled. Moreover if two predicates with the same name and arity exists in two different modules, they will be considered as the same predicate by TCLP.

A  TCLP source files
This the main file for the library. It mainly consists in wrappers for calling predicates in other TCLP modules.
The main file for the command line tool.
The file contains predicates for handling depandancies between the arguments of the different type constructors.
Defines dynamic arrays with predicates to manipulate them.
Defines the solver for constraints over subtyping inequalities.
Handles the computation of mutually recursive predicates in the program source, as well as the order in which the clauses must be type checked.
Defines the core meta declarations of TCLP.
Some constants definitions.
Handles printing of errors and warning. Also handles the verbose predicates (should be in an IO module I think)
Handles file loading, file names, etc ...
Handles the typing of the application of a functor to its arguments. In particular, it handles the treatment of overloading.
Core handling TCLP definitions.
Reads source phrases and handle their location in the program source.
Type checking predicates.
Defines how the inference of the type of predicates.
Handles the order between type constructors.
Miscellaneous predicates.
The version TCLP.
Predicates to handle some SICStus Prolog directives (e.g. use_module/1).

B  TCLP .typ files

ISO Prolog
Meta definitions for ISO Prolog. Included from lib/ISO/stdlib.typ.
Standard type library for ISO Prolog.
GNU Prolog
Meta definitions for GNU Prolog. Included from lib/gprolog/stdlib.typ
Standard type library for GNU Prolog.
SICStus Prolog
Meta definitions for SICStus Prolog. Included from lib/sicstus/stdlib.typ
Standard type library for SICStus Prolog.
Internal type library for SICStus Prolog. You need this one for predicates used with the prolog:predicate(...) prefix.
types for SICStus module arrays
types for SICStus module assoc
types for SICStus module atts
types for SICStus module bdb
types for SICStus module charsio
types for SICStus module chr implementing the CHR extension. It contains meta definitions to handle the solver definitions (e.g. ’<=>’/2).
types for SICStus module clpb implementing the CLP(B) extension
types for SICStus module clpfd implementing the CLP(FD) extension. It contains meta declarations to handle contraints definitions (e.g. ’:+’/2).
lib/sicstus/clpq.typ, lib/sicstus/clpr.typ and lib/sicstus/clpqr.typ
types for SICStus modules clpr, clpq and clpqr implementing extension CLP(Q) and CLP(R).
types for SICStus module fastrw
types for SICStus module heaps
types for SICStus module jasper
types for SICStus module linda
types for SICStus module lists
types for SICStus module ordsets
types for SICStus module queues
types for SICStus module random
types for SICStus module sockets
types for SICStus module system
types for SICStus module terms
types for SICStus module timeout
types for SICStus module trees
types for SICStus module ugraphs
types for SICStus module user
types for SICStus module wgraphs

This document was translated from LATEX by HEVEA.