SWI-Prolog

From Bauman National Library
This page was last modified on 21 May 2016, at 23:50.
SWI-Prolog
Paradigm logic programming
Designed by Jan Wielemaker
First appeared 1987
Stable release 7.2.3 / 25 august, 2015
Typing discipline dynamic
OS cross-platform(Windows NT, Mac OS, UNIX)
License GNU LGPL
Filename extensions .pl
Website https://swi-prolog.org/

SWI-Prolog is a free implementation of the programming language Prolog, commonly used for teaching and semantic web applications. It has a rich set of features, libraries for constraint logic programming, multithreading, unit testing, GUI, interfacing to Java, ODBC and others, literate programming, a web server, SGML, RDF, RDFS, developer tools (including an IDE with a GUI debugger and GUI profiler), and extensive documentation. SWI-Prolog runs on Windows NT, Mac OS and UNIX platforms.

History

SWI-Prolog has been under continuous development since 1987. Its main author is Jan Wielemaker. The name SWI is derived from Sociaal-Wetenschappelijke Informatica ("Social Science Informatics"), the former name of the group at the University of Amsterdam, where Wielemaker is employed. The name of this group has changed to HCS (Human-Computer Studies).

XPCE

XPCE is a platform independent object oriented GUI toolkit for SWI-Prolog, Lisp and other interactive and dynamically typed languages. Although XPCE was designed to be language-independent, it has gained popularity most with Prolog. The development XPCE graphic toolkit started in 1987, together with SWI-Prolog. It supports buttons, menus, sliders, tabs and other basic GUI widgets. XPCE is available for all platforms supported by SWI-Prolog.

PreEmacs

PceEmacs is a SWI-Prolog builtin editor. PceEmacs is an Emacs clone implemented in Prolog (and XPCE). It supports proper indentation, syntax highlighting, full syntax checking by calling the SWI-Prolog parser, warning for singleton variables and finding predicate definitions based on the source-information from the Prolog database.

SWI-Prolog program structure

SWI-Prolog program structure severely differs from program written in standart procedure language. SWI-Prolog comprises a set of facts and rules. The task resolution is attained by interpretation of these facts and rules. Technically the programmer doesn't need to define the exact instruction set for run-time result evaluation. Instead of this he only defines the possible task resolutions and assures the presence of facts and rules which are necessary to allow the intrepreter to reach the resoution.

Term

Term is a basic SWI-Prolog construction. It contains:

  • Simple terms
    • Constants
      • Atoms mia, hit_this_Qb
      • Numbers [0-9]
    • Variables X, Y, HitThisQ
  • Compound term f(t_1, t_2, ..., t_n)

Variables

  • Variables - is a data pass method in space and time.
  • Variables - maintains access for data storage and passing it to subroutines.

Free variables

A variable is called free then and only then when the variable has no assigned value.

Binded variable

Variable is called binded then and only then when a variable has assigned a value (the variable cannot be changed).

Knowledge base

Knowledge base is a set of facts and rules which describe relations in a subject area. The intrepeter processes this set in top-down manner.

Fact

Fact is a clause in knowledge base.

  • Fact is a statement which establishes a relation between terms and asserts unconditional truth.
  • Fact is a statement which is always true.
  • Fact is an unconditional truth.
  • Fact states that terms have established a relation.
  • Fact is a particular case of rule (which has no body).

Example:

 woman(mia).
 woman(jody). loves(mia, jody).

Rule

Another knowledge base clause is a rule. Rules allow to infer new facts based on the existing facts.

  • Rule is a statement which is true then and only then when the rule condition are true.
  • Rule is a statement which establishes a relation between terms if the rule conditions are true.
  • Rule is a conditional truth.

Example:

 happy(mia).        % fact listen2Music(mia). % fact listen2Music(yolanda) :- happy(yolanda). % rule

Data types

SWI-Prolog supports dynamic data types.

If an object name is in lowercase then this object has a value e.g. it is a constant. Objects with known or constant values must be in lowercase if they are not numebers.

 likes(mary,apples). /* mary and apples are constants. */

Words which are in uppercase are SWI-Prolog variables.

Inner unificational subroutines evaluate the variables.

Evaluated variables and constants which have values known by SWI-Prolog.

Free (non-evaluated) variables do not have values.

Variables can be objects of predicate both in assertions and subtargets.

 likes(mary,apples). /* Assertion - Mary likes apples */
 likes(mary,What). /* Target - What does Mary like? */

Numbers

 ?- A = 12, B = -0.5e4.
 A = 12,
 B = -5000.0. ?- number($A), number($B).
 true. % result - variable types are numeric.

Atoms

 ?- A = abc, B = 'Hello World'.
 A = abc,
 B = 'Hello World'. ?- atom($A), atom($B).
 true. % result - variable types are atomic

Strings

 ?- S = "Hello world".
 S = [1055, 1088, 1080, 1074, 1077, 1090, 32, 1084, 1080|...].

It is clear that strings represent symbolic codes list e.g. all list operations are applicable for them.

Lists

 ?- A=[], B=[a, foo, 123, [[[[[1,2,42]],bar]]], "Hello", A], C=[A,B].
 A = [], % empty list B = [a, foo, 123, [[[[[1|...]], bar]]], [1055, 1088, 1080, 1074|...], []],
 C = [[], [a, foo, 123, [[[[...]|...]]], [1055, 1088|...], []]]

Lists can be:

  • Heterogeneous (May comprise any combinations of all types above and followings)
  • Compound

Structures

 ?- A = aaa(bb), B = aaa(bbbbbb, 123, [456, c]), C = ccc(ddd(eee), fff, g(h(i(j(kkkkk))))).
 A = aaa(bb),
 B = aaa(bbbbbb, 123, [456, c]),
 C = ccc(ddd(eee), fff, g(h(i(j(kkkkk)))))

Clear example:

 ?- Family = family(father(bill, age(37)), mother(ann, age(34)), children([son(john, age(10)), daughter(jill, age(8))])).
 Family = family(father(bill, age(37)), mother(ann, age(34)), children([son(john, age(10)), daughter(jill, age(8))]))

Structure in SWI-Prolog is represented by functor (strucure identifier before the brackets) and parameters (inside the brackets). As we see structures also can be inserted into other structures. Parameters number is called functor arity.

Unification

Unification is the most important operation in SWI-Prolog. Matching causes comparison of two terms equality check. Wherein free variables obtain values which make the two terms equal. The matching may be carried out either clearly (in the body of built-in predicate X=Y) or unclearly (by matching of target with fact or rules left part. The matching implements all basic data handling operations of logic programming:

  • Single assignment.
  • Passing of parameters.
  • Structured objects creation.
  • Structured object field acess with possibility of simultaneous reading/writing.
  • Matching is done according to following rules:
    • Free variable is matchable to any object and therefore this object becomes the value of the variable (definiton).
    • Numbers and atoms are only matchable to identical numbers and atoms.
    • Structures are only matchable if they have the same fucntor, the same component number and appropriate components.
    • Free variables are matchable to each other and they become binded to each other. If one of them obtains some defined value then the other one obtains the same.

According to the logic syntax of SWI-Prolog a programmer defines which facts and rules give one or another result with different subtargets and the built-in unification subroutines execute the rest of the work.

The built-in unification subroutines also evaluate the variables. Evaluated variables and constant0s have value known by SWI-Prolog. Free variables do not have values.

Rollback

Rollback is a mechanism which is used by SWI-Prolog for searching for extra facts and rules which are neccesary for the target evaluation if the current evalutaion attempt proved to fail. SWI-Prolog uses rollback to try new routes to find resolution. SWI-Prolog all accessible information for choosing for finding an appropriate route.

References