Type Checking

BAP Online Manuals : Compiler Reference Manual : Language Description: : Type Checking
Previous: Brain Aid Prolog Syntax :
Next: Keywords :

3.5.4. Type Checking

Everybody who has ever tried to write a large program in any non-typed computer language will surely have encountered a lot of problems ...

We decided to provide an optional type checking mechanism: The definition of every predicate must be preceded by a declaration. Some programmers will think of this as a major restriction. We think, that this is the only way to create large programs that really run.

In PROLOG, there are various data types (Stating that BAP is not typed means that at any place any type of data may appear. It does not mean that the data has no type!). The standard types are:

By the use of the DOMAINS keyword you are able to build up new data types from the predefined ones. The effect of such a type declaration is that a new predicate is built up (with the name of the domain), that checks whether its single argument fits its type declaration or not. If it fits, the predicate succeeds. If not, the predicate creates a runtime error and fails. The following example defines some new data-types describing the properties of a person:

DOMAINS
		sex		:- 	male; female.
		age		:-	integer.
		name	:-		string.
		namelist:-	name*. % this means 'list of names'
		person :-		person(name,sex,age).
Example Usage:

PREDICATE is_old_man(person).
is_old_man(person(_,male,Age)) :-
	Age > 70.
The result of the domain declaration is that some new predicates are created that look similar like:

PREDICATE sex(void).
sex(Var) :-	free(Var),!.
sex(Var) :-	Var = male,!.
sex(Var) :-	Var = female,!.


PREDICATE age(void).
age(Var):-	free(Var),!.
age(Var):-	integer(Var),!.


PREDICATE name(void).
name(Var):-	free(Var),!.
name(Var):-	string(Var),!.


PREDICATE namelist(void).
namelist(Var)	:-	free(Var),!.
namelist([])	:-!.
namelist([Name|Rest]):-
	name(Name),
	namelist(Rest),!.


PREDICATE person(void).
person(Var)	:-	free(Var),!.
person(person(Name,Sex,Age)):-
	name(Name),
	sex(Sex),
	age(Age),!.

This mechanism gives you a chance to avoid declaring erroneously a person as being 'male' years old.


BAP Online Manuals : Compiler Reference Manual : Language Description: : Type Checking
Previous: Brain Aid Prolog Syntax :
Next: Keywords :