ProLog

[|These web pages] describe the ISO conformant version of the Prolog language. The pages are in several parts:
 * [|A history and overview of the standard];
 * The builtin predicates and control constructs of the language;
 * Prolog semantics;
 * [|Test suites] Test of conformance to the ISO standard;
 * [|Input Output in ISO Prolog];
 * [|Exception Handling in ISO Prolog]
 * [|The modules proposal];
 * [|A Prolog Bibliography].

[|Description of the ISO conformant version of the Prolog language.]

[|ISO/IEC 13211 - Programming language - Prolog]

__SWI-Prolog Reference Manual__  documents the system, but it does not explain the basics of the Prolog language and it leaves many details of the syntax, semantics and built-in primitives undefined where SWI-Prolog follows the standards.

Linux versions are often available as a package for your distribution. We collect information about available packages and issues for building on specific distros [|here]. We provide a [|PPA] for [|Ubuntu].

Starting with version 6.1.7, the Windows binary is compiled using the [|MinGW] GCC based compiler suite. Due to changed naming conventions this implies that extension DLLs are neither forward nor backward compatible. Please check the [|windows release nodes] (also in the SWI-Prolog startup menu of your installed version).

[|SWI-Prolog Reference Manual]

Google Search results for [|pdf **//bratko//** prolog:]

[PDF] [|//Prolog// Programming for Artificial Intelligence.//pdf// - Higher Intellect **...**] cdn.preterhuman.net/.../**PROLOG**%20PROGRAMMING%20FOR%20AR...‎ Printed in Great Britain bv Commercial Colour Press. British Library Cataloguing in Publication Data. //Bratko//. Ivan. //Prolog// programming for artificial intelligence.

[pdf pg 256] pg 238 PROLOG PROGRAMMING FOR ARTIFICIAL INTELLIGENCE % Insertion and deletion in the vertically balanced 2-3 dictionary

% Add X to Tree giving Treel % Tree grows in breadth add23( Tree, X, Treel) :- l ins( Tree, X, Treel). I

% Tree grows upwards add23( Tree, X, n2(T1, M2, T2) ) :- ins( Tree, X, T1, i&'{2T, 2).

% Delete X from Tree giving Treel del23( Tree, X, Treel) :- add23( Treel, X, Tree).

ins( nil, X, l(X) ).

ins( l(A), X, l(A), X, l(X) ) :- gt( X, A).

ins( l(A), X, l(X), A, l(A) ) :- gt( A, X).

ins( n2( T1, M, T2), X, n2( NTl, M, TZ) ) :- gt( M, X), ins( T1, X, NTl).

ins( n2( T1, M, T2), X, n3( NTla, Mb, NTlb, M, T2) ) :- gt( M, X), ins( T1, X, NTla, Mb, NTlb).

ins( n2( T1, M, T2), X, n2(T1, M, NT2) ) :- gt( X, M), ins( T2, X, NT2).

ins( n2( T1, M, T2), X, n3( T1, M, NT2a, Mb, NT2b) ) :- gt( X, M), ins( T2, X, NT2a, Mb, NT2b).

ins( n3( T1, M2, T2, M3, T3), X, n3( NTl, M2, T2, M3, T3) ) :- gt( M2, X), ins( T1, X, NT1).

ins( n3( T1, M2, T2, M3, T3), X, n2( NTla, Mb, NTlb), M2, n2(T2, M3, T3) ) :- gt( M2, X), ins( T1, X, NTla, Mb, NTlb).

ins(n3( T1, M2, T2, M3, T3), X, n3( T1, M2, NT2, M3, T3) ) :- gt( X, M2), gt( M3, X), ins( T2, X, NT2).

%ADVANCED TREE REPRESENTATIONS pg 230 [pdf pg 257]

ins( n3( T1, M2, T2, M3, T3), X, n2( T1, M2, NT2a), Mb, n2( NT2b, M3, T3) ) '- gt( X, M2), gt( M3, X), ins( T2, X, NT2a, Mb, NT2b).

ins( n3( T1, M2, T2, M3, T3), X, n3( T1, M2, T2, M3, NT3) ) :- gt( x, M3), ins( T3, X, NT3).

ins( n3( T1, M2, T2, M3, T3), X, n2( T1, M2, T2), M3, n2( NT3a, Mb, NT3b) ) :- gt( x, M3), ins( T3, X, NT3a, Mb, NT3b).

Figure 10.6 Inserting and deleting in the 2-3 dictionary. In this program, an attempt to insert a duplicate item will fail.

Figure 10.6 shows the complete program for inserting into and deleting from the 2-3 dictionary. The add23 procedure can be used in the reverse direction for deleting by:

del23( T, X, T1) :- add23( T1, X, T).

Figure 10.7 shows a program for displaying 2-3 trees.

Our program occasionally does some unnecessary backtracking. If the three-argument ins fails then the five-argument ins is called, which redoes part of the work. This source of inefficiency can easily be eliminated by, for example, redefining ins as:

ins2( Tree, X, NewTrees)

NewTrees is a list of length 1 or 3, as follows:

NewTrees : [ NewTree] if ins( Tree, X, NewTree) NewTrees - [ NTa, Mb, NTb] if ins( Tree, X, NTa, Mb, NTb)

Solution for ins2:

ins2( nil, X, [l(X)] ).

ins( l(A), X, [l(A), X, l(X)] ) :- gt( X, A). ins( l(A), X, [l(X), A, l(A)] ) :- gt( A, X).

The add23 relation would be, accordingly, redefined as:

add23( T, X, T1) :- ins2( T, X, Trees), combine( Trees, T1).

The combine relation has to produce a single tree, T1, from the list Trees.

combine( [T], T). combine( [Ta, M, Tb]), n2(Ta, M, Tb) ).

PROLOG PROGRAMMING FOR ARTIFICIAL INTELLIGENCE % Displaying 2-3 dictionary

show( T) :- show( T, 0).

show( nil, -).

show( l(A), H) :- tab( H), write( A), nl.

show( n2( T1, M, T2), H) :- Hl is H+5, show( T2, H1), tab( H), write( --), nl, tab( H), write( M), nl, tab( H), write( --), trI, show( T1, H1).

show( n3( T1, M2, T2, M3, T3), H) :- H1 is H+5, show( T3, H1), tab( H), write( --), nl, tab( H), write( M3), nl, show( T2, H1), tab( H), write( M2), nl, tab( H), write( --), nl, show( T1, H1).

Figure 10.7 (a) A program to display a 2-3 dictionary. (b) The dictionary of Figure 10.2 as displayed by this program.

10.1 Define the relation: in( Item, Tree) to search for Item in a 2-3 dictionary Tree.
 * Exercises**

Solution 10.1:

in( X, l(X) );

in(X, t2(LT, M, RT) ) :- gt( M, X), in(X, LT); gt( X, M), in(X, RT); X=M.

in(X, t2(LT, LM, MT, RM, RT) gt( LM, M), in( X, LT); X=LM; gt(RM, M), in(X, MT), X=RM; gt(M, RM), in(X, RT).

ins( n3( T1, Ifu{z, T2, M3, T3), X, n3( T1, M.2, T2, <span style="display: block; font-family: sans-serif; font-size: 10.4227px; left: 259.525px; top: 120.903px; transform-origin: 0% 0% 0px; transform: scale(1.08446,1);">M3, <span style="display: block; font-family: sans-serif; font-size: 10.4227px; left: 280.787px; top: 120.903px; transform-origin: 0% 0% 0px; transform: scale(1.21598,1);">NT3) <span style="display: block; font-family: sans-serif; font-size: 10.4227px; left: 307.678px; top: 122.779px; transform-origin: 0% 0% 0px; transform: scale(1.26983,1);">) <span style="display: block; font-family: sans-serif; font-size: 10.4227px; left: 318.434px; top: 120.903px; transform-origin: 0% 0% 0px; transform: scale(1.26983,1);">:- <span style="display: block; font-family: sans-serif; font-size: 10.4227px; left: 35.0202px; top: 132.785px; transform-origin: 0% 0% 0px; transform: scale(1.13746,1);">gt( <span style="display: block; font-family: sans-serif; font-size: 10.4227px; left: 50.279px; top: 133.16px; transform-origin: 0% 0% 0px; transform: scale(1.04227,1);">x, <span style="display: block; font-family: sans-serif; font-size: 10.4227px; left: 64.7874px; top: 133.16px; transform-origin: 0% 0% 0px; transform: scale(1.08793,1);">M3), <span style="display: block; font-family: sans-serif; font-size: 10.4227px; left: 34.8951px; top: 145.542px; transform-origin: 0% 0% 0px; transform: scale(1.08644,1);">ins( <span style="display: block; font-family: sans-serif; font-size: 10.4227px; left: 54.4064px; top: 145.542px; transform-origin: 0% 0% 0px; transform: scale(1.20351,1);">T3, <span style="display: block; font-family: sans-serif; font-size: 10.4227px; left: 73.2923px; top: 145.542px; transform-origin: 0% 0% 0px; transform: scale(1.12485,1);">X, <span style="display: block; font-family: sans-serif; font-size: 10.4227px; left: 87.6756px; top: 145.667px; transform-origin: 0% 0% 0px; transform: scale(1.21598,1);">NT3).