integrated logic systems part 1 wam and implementations of prolog

93
Integrated Logic Integrated Logic Systems Systems Part 1 Part 1 WAM and WAM and Implementations of Implementations of Prolog Prolog

Post on 22-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Integrated Logic SystemsIntegrated Logic Systems

Part 1Part 1WAM and Implementations of WAM and Implementations of

PrologProlog

Page 2: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Aim of the courseAim of the course

Implementation of Logic Based SystemsImplementation of Logic Based Systems

Implementation of PrologImplementation of Prolog– Warren’s abstract machine (known as the WAM) as Warren’s abstract machine (known as the WAM) as

the most widely used basis for implementing Prolog the most widely used basis for implementing Prolog systems systems

Other SystemsOther Systems– PETISCO (and connection to databases and web)PETISCO (and connection to databases and web)– InterProlog (and interface Java/Prolog)InterProlog (and interface Java/Prolog)– Gnu-Prolog (and contexts and constraints)Gnu-Prolog (and contexts and constraints)

Page 3: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Why?Why?

Learn how to implement logic systemsLearn how to implement logic systems

Understand specific behavior of Prolog Understand specific behavior of Prolog and relate to efficient execution of Prolog and relate to efficient execution of Prolog programsprograms

Get acquainted with systems that integrate Get acquainted with systems that integrate Prolog and other systems (databases, Prolog and other systems (databases, constraints, Java, web interfaces).constraints, Java, web interfaces).

Page 4: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

The WAMThe WAM

David H. David H. WWarren’s Abstract Machine is arren’s Abstract Machine is the standard basis for implementing the standard basis for implementing Prolog systems.Prolog systems.

Hassan Aît-Kaci. Hassan Aît-Kaci. Warren’s Abstract Warren’s Abstract Machine – A Tutorial Reconstruction.Machine – A Tutorial Reconstruction. MIT MIT Press, 1991.Press, 1991.

Page 5: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

WAM’s Basic PictureWAM’s Basic Picture

Prolog programs are compiled into WAM Prolog programs are compiled into WAM code.code.

Prolog queries are compiled into WAM Prolog queries are compiled into WAM code.code.

The code for queries (and for program The code for queries (and for program clauses) is executed and calls the code for clauses) is executed and calls the code for program clauses.program clauses.

Page 6: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Structure of the courseStructure of the course

WAM is presented starting from very simple WAM is presented starting from very simple programs:programs:– LL00: Program is simply one facts, and the query is : Program is simply one facts, and the query is

atomic (atomic (learn unificationlearn unification))– LL11: Program is a set of facts (only one per predicate), : Program is a set of facts (only one per predicate),

and the query is atomicand the query is atomic– LL22: Programs are sets of rules but only one per : Programs are sets of rules but only one per

predicate – Prolog without backtracking (predicate – Prolog without backtracking (learn learn execution of clausesexecution of clauses))

– LL33: Pure Prolog Programs (: Pure Prolog Programs (learn backtrackinglearn backtracking))– Optimizing the designOptimizing the design– Efficiency issuesEfficiency issues

Page 7: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

The Language The Language LL00

A program is a termA program is a termA query is a termA query is a termSemantics:Semantics:– The query The query qq fails wrt to program fails wrt to program ff iff iff qq and and ff do not do not

unifyunify– Otherwise Otherwise q q succeeds binding its variablessucceeds binding its variables

Example:Example:p(f(X),h(Y,f(a)),Y).p(f(X),h(Y,f(a)),Y).?- p(Z,h(Z,W),f(W))?- p(Z,h(Z,W),f(W))

Z= f(f(a))Z= f(f(a)) W = f(a)W = f(a)

Page 8: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

The Heap in WAMThe Heap in WAM00

WAMWAM00 contains an addressable contains an addressable HeapHeap (an array (an array

of data cells) for storing termsof data cells) for storing terms

Terms are either:Terms are either:– VariablesVariables

Identified by a reference pointer to a single heap cell Identified by a reference pointer to a single heap cell containing <REF,k> where k is the addresscontaining <REF,k> where k is the address

– Structures of the form f(@1,…, @n)Structures of the form f(@1,…, @n)Represented by n+2 cellsRepresented by n+2 cells

– The 1st is a reference to the 2nd (stating it is a structure STR)The 1st is a reference to the 2nd (stating it is a structure STR)

– The 2nd contains the functor and arityThe 2nd contains the functor and arity

– Each of the others contains one argumentEach of the others contains one argument

Page 9: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Heap representation of termsHeap representation of terms

Unbound VariableUnbound Variable

REFREF iii

Bound VariableBound Variable

REFREF jji i ≠ j

Term f(t1,…tn)Term f(t1,…tn)

i STRSTR i+1i+1

f/nf/n

arg 1arg 1

……

arg narg n

i+1

i+2

i+n+1

Page 10: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Representation of p(Z,h(Z,W),f(W))Representation of p(Z,h(Z,W),f(W))

00 STRSTR 11

11 h/2h/2

22 REFREF 22

33 REFREF 33

44 STRSTR 55

55 f/1f/1

66 REFREF 33

77 STRSTR 88

88 p/3p/3

99 REFREF 22

1010 STRSTR 11

1111 STRSTR 55

Page 11: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compiling an Compiling an LL00 query query

Preparing on side of the (unification) equation to Preparing on side of the (unification) equation to be solvedbe solved

A query is translated into a sequence of A query is translated into a sequence of instructions to build its heap representationinstructions to build its heap representation– For storing intermediate values, WAMFor storing intermediate values, WAM00 has registers has registers

XX11, X, X22,…,…

– Register XRegister X11 is assigned to the outermost term is assigned to the outermost term

– A term is a set of flattened equations:A term is a set of flattened equations:

XXii = f(X = f(Xi1i1,…, X,…, Xinin))

Page 12: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compiling an Compiling an LL00 query (cont) query (cont)

Equations are then sorted such that Equations are then sorted such that registers in the right are defined beforeregisters in the right are defined beforeRegisters simply for variables are omittedRegisters simply for variables are omittedExample:Example:– Equations for p(Z,h(Z,W),f(W)) and sortingEquations for p(Z,h(Z,W),f(W)) and sorting

X1 = p(X2,X3,X4)X1 = p(X2,X3,X4) (3rd)(3rd)X2 = ZX2 = ZX3 = h(X2,X5)X3 = h(X2,X5) (1st)(1st)X4 = f(X4 = f( X5)X5) (2nd)(2nd)X5 = WX5 = W

Page 13: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compiling an Compiling an LL00 query (cont) query (cont)

Equation XEquation Xii = f(X = f(Xi1i1,…, X,…, Xinin) is translated into) is translated into– put_structure f/n, Xiput_structure f/n, Xi– set_variable Xi1set_variable Xi1 or or set_value Xi1set_value Xi1– ……– set_variable Xinset_variable Xin or or set_value Xinset_value Xin

Compilation of Compilation of p(Z,h(Z,W),f(W))p(Z,h(Z,W),f(W))

Page 14: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

WAMWAM00 instructions instructions

Page 15: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compiling an Compiling an LL00 program program

Executing a fact given a query amounts to check Executing a fact given a query amounts to check whether the term in the heap unifies with the factwhether the term in the heap unifies with the fact– Proceed checking if the fact matches the heap – Proceed checking if the fact matches the heap – read read

modemodeget_structureget_structure rather than rather than put_structureput_structure

unify_variableunify_variable rather than rather than set_variableset_variable

unify_valueunify_value rather than rather than set_valueset_value

– If an unbound variable (REF) is found in the heap, If an unbound variable (REF) is found in the heap, proceed by writing the unifyable term of the fact – proceed by writing the unifyable term of the fact – write modewrite mode similar to processing of the query similar to processing of the query

Page 16: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compiling an Compiling an LL00 program (cont) program (cont)

The fact is transformed into a sequence of flat The fact is transformed into a sequence of flat equations on registers, where the order is equations on registers, where the order is inverse of that for the query.inverse of that for the query.Example: p(f(X),h(Y,f(a)),Y). Example: p(f(X),h(Y,f(a)),Y).

Page 17: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

unifyunify instructions instructionsunify_variable Xiunify_variable Xi– In In writewrite mode is as mode is as set_variable Xiset_variable Xi– In In readread mode just sets Xi with the next subterm to be matched mode just sets Xi with the next subterm to be matched

(whose heap address is stored in global register (whose heap address is stored in global register SS).).

unify_value Xiunify_value Xi– In In write write mode is as mode is as set_value Xiset_value Xi– In In read mode read mode checks whether the term referenced by Xi unifies checks whether the term referenced by Xi unifies

with that referenced by with that referenced by SS

A function for dereferencing (chain) references for A function for dereferencing (chain) references for variables is needed:variables is needed:

Page 18: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

get_structure f/n, Xiget_structure f/n, Xi

If Xi references an unbound variable, then If Xi references an unbound variable, then start at start at write write mode so as to store in the mode so as to store in the heap the new term (in the fact)heap the new term (in the fact)

If Xi references a structure for the same If Xi references a structure for the same functor then, in functor then, in readread mode, start matching mode, start matching subterm (setting subterm (setting SS to the first one) to the first one)

Otherwise, Otherwise, failfail unification. unification.

Page 19: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

get_structure f/n, Xiget_structure f/n, Xi

For now, consider that bind(addr1,addr2) binds address 1 with <Ref,addr2>

Page 20: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

unifyunify instructions instructions

The The unify(addr1,addr2)unify(addr1,addr2) remains to be remains to be explained. It uses a unification stack (explained. It uses a unification stack (PPush-ush-DDown own LList).ist).

Page 21: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Unification procedureUnification procedure

Page 22: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Summary structure of WAMSummary structure of WAM00

X1, …, Xn registersX1, …, Xn registersSpecial registers S and HSpecial registers S and HHeap (for storing terms)Heap (for storing terms)PDL for use in unificationPDL for use in unificationInstructions set:Instructions set:

put_stucture f/n, Xiput_stucture f/n, Xiset_vatiable Xiset_vatiable Xiset_value Xiset_value Xiget_structure f/n, Xiget_structure f/n, Xiunify_variable Xiunify_variable Xiunify_variable Xiunify_variable Xi

CodeCode

HeapHeap

PDLPDL

H

S

Page 23: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

The Language The Language LL11

A query is an atom (term)A query is an atom (term)The program is now a set of atomsThe program is now a set of atomsSemantics:Semantics:– The query The query qq succeeds wrt to program succeeds wrt to program PP iff there exists no atom iff there exists no atom ff

in in PP such that such that qq and and ff unify unify– Otherwise Otherwise q q succeeds binding its variablessucceeds binding its variables

Extends Extends LL00 in that more than one (compiled) fact, for in that more than one (compiled) fact, for more than one predicate, is stored in the code areamore than one predicate, is stored in the code areaCode for fact p/n is stored at an address labeled @(p/n).Code for fact p/n is stored at an address labeled @(p/n).By calling the code in the appropriate address, there is By calling the code in the appropriate address, there is no need for representing the p/n in the Heapno need for representing the p/n in the Heap

Page 24: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Calling code in Calling code in LL11

There must be a register There must be a register PP with a pointer to the with a pointer to the next instruction to be callednext instruction to be called

Every instruction, unless otherwise stated, Every instruction, unless otherwise stated, increments increments PP

There exists an unconditional jump to call the There exists an unconditional jump to call the code for a predicate:code for a predicate:– call p/ncall p/n simply defined by simply defined by P P ← @(p/n)← @(p/n)

There exist a control instruction There exist a control instruction proceedproceed to to denote the end of the code for p/n that for now is denote the end of the code for p/n that for now is simply a no-op code terminatorsimply a no-op code terminator

Page 25: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Root arguments and registersRoot arguments and registers

The first n registers will be used to store the n The first n registers will be used to store the n arguments of the predicatearguments of the predicateTo differentiate, they will be called argument To differentiate, they will be called argument registers A1, …, An.registers A1, …, An.Other registers are use as beforeOther registers are use as beforeThe predicate functor is not storedThe predicate functor is not storedExample:Example:– Equations for p(Z,h(Z,W),f(W))Equations for p(Z,h(Z,W),f(W))

A1 = ZA1 = ZA2 = h(A1,X4)A2 = h(A1,X4)A3 = f(X4)A3 = f(X4)X4 = WX4 = W

Page 26: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Handling of root variablesHandling of root variables

Now variables must be loaded into argument registers Now variables must be loaded into argument registers for queries, and extracted from argument registers for for queries, and extracted from argument registers for factsfactsNew instructions:New instructions:– put_variable Xn, Aiput_variable Xn, Ai

(for handling with the first occurrence of a root variable in a query)(for handling with the first occurrence of a root variable in a query)– put_value Xn, Aiput_value Xn, Ai

(for subsequent occurrences of a root variable in a query)(for subsequent occurrences of a root variable in a query)– get_variable Xn, Aiget_variable Xn, Ai

(for handling with the first occurrence of a root variable in a fact)(for handling with the first occurrence of a root variable in a fact)– get_value Xn, Aiget_value Xn, Ai

(for subsequent occurrences of a root variable in a fact)(for subsequent occurrences of a root variable in a fact)

Page 27: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Example of compilation into WAMExample of compilation into WAM11

Page 28: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

WAMWAM11 instructions instructions

Page 29: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Summary structure of WAMSummary structure of WAM11

X1, …, Xn and A1, … An registersX1, …, Xn and A1, … An registersSpecial registers P, S and HSpecial registers P, S and HHeapHeapPDLPDLCode areaCode areaInstructions set:Instructions set:

put_stucture f/n, Xiput_stucture f/n, Xiset_vatiable Xiset_vatiable Xiset_value Xiset_value Xiget_structure f/n, Xiget_structure f/n, Xiunify_variable Xiunify_variable Xiunify_variable Xiunify_variable Xicall p/ncall p/nproceedproceedput_variable Xn,Aiput_variable Xn,Aiput_value Xn,Aiput_value Xn,Aiget_variable Xn,Aiget_variable Xn,Aiget_value Xn,Aiget_value Xn,Ai

CodeCode

HeapHeap

PDLPDL

H

S

P

Page 30: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

The Language The Language LL22

Besides fact, programs may now have rules Besides fact, programs may now have rules At most one rule for each predicate:At most one rule for each predicate:– No backtracking yet!!No backtracking yet!!

Formally, an Formally, an LL22 program is a set of rules: program is a set of rules:aa00 :- a :- a11, …, a, …, ann (n (n ≥ 0, and a≥ 0, and aiis are atoms)s are atoms)– At most one rules per predicateAt most one rules per predicate– Rules with one body goal are called Rules with one body goal are called chain ruleschain rules, and , and

with more, with more, deep rulesdeep rules

LL22 queries are sequences of goal: queries are sequences of goal:?- a?- a11, …, a, …, akk (k (k ≥ 0, and a≥ 0, and aiis are atoms)s are atoms)– Called Called empty queryempty query, if k = 0., if k = 0.

Page 31: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

The Language The Language LL22

Besides fact, programs may now have rules Besides fact, programs may now have rules At most one rule for each predicate:At most one rule for each predicate:– No backtracking yet!!No backtracking yet!!

Formally, an Formally, an LL22 program is a set of rules: program is a set of rules:aa00 :- a :- a11, …, a, …, ann (n (n ≥ 0, and a≥ 0, and aiis are atoms)s are atoms)– At most one rules per predicateAt most one rules per predicate– Rules with one body goal are called Rules with one body goal are called chain ruleschain rules, and , and

with more, with more, deep rulesdeep rules

LL22 queries are sequences of goal: queries are sequences of goal:?- a?- a11, …, a, …, akk (k (k ≥ 0, and a≥ 0, and aiis are atoms)s are atoms)– Called Called empty queryempty query, if k = 0., if k = 0.

Page 32: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Execution of Execution of LL2 2 programsprograms

Executing ?- aExecuting ?- a11, …, a, …, akk amounts to repeated application amounts to repeated application of leftmost resolution, until of leftmost resolution, until empty queryempty query or or failure.failure.– Start by unifying aStart by unifying a11 with the head of the (only) rule for the with the head of the (only) rule for the

predicatepredicate– If none exists, then If none exists, then failfail– Otherwise, replace aOtherwise, replace a11 by the body of the rule, after unification by the body of the rule, after unification– Proceed until empty query (or failure)Proceed until empty query (or failure)

The result of a successful query is the (dereferenced) The result of a successful query is the (dereferenced) bindings of the original variables in the query.bindings of the original variables in the query.We can start to view it as concatenation of compiled We can start to view it as concatenation of compiled WAMWAM11 code for each goal in the query code for each goal in the queryCare must be taken wrt:Care must be taken wrt:– Continuation of execution of a goal sequenceContinuation of execution of a goal sequence– Avoid conflicting use of arguments registersAvoid conflicting use of arguments registers

Page 33: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compiling facts in Compiling facts in LL22

Very similar to compiling Very similar to compiling LL11 fact, but… fact, but…

In the end, the In the end, the proceedproceed can no longer stop can no longer stop executionexecution– It has to return to the It has to return to the calling calling procedure (query)procedure (query)

WAMWAM22 has an extra global register has an extra global register CPCP

Page 34: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compiling rules in Compiling rules in LL22

General structure is similar to a combination of General structure is similar to a combination of compiling facts and queries into WAMcompiling facts and queries into WAM11::– get arguments of the head (as in a WAMget arguments of the head (as in a WAM11 fact) fact)– put arguments of the body atoms (as in a sequence of put arguments of the body atoms (as in a sequence of

WAMWAM11 queries) queries)

Roughly, aRoughly, a00 :- a :- a11, …, a, …, ann translates into: translates into:– Get arguments of aGet arguments of a00 (from A registers) (from A registers)– Put arguments of aPut arguments of a11 (in A registers) (in A registers)– Call aCall a11

– ……– Put arguments of aPut arguments of ann (in A registers) (in A registers)– Call aCall ann

Page 35: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Permanent variablesPermanent variables

Variables used more than once in a rule Variables used more than once in a rule ((permanent variablespermanent variables) cannot be accessible by ) cannot be accessible by an argument register onlyan argument register only– They would be overwritten!They would be overwritten!

In a rule aIn a rule a00 :- a :- a11, …, a, …, ann a variable Y is permanent a variable Y is permanent iffiff– Y occurs in more than of the sets:Y occurs in more than of the sets:

Vars(aVars(a00) U Vars(a) U Vars(a11), Vars(a), Vars(a22), … Vars(a), … Vars(ann))

Other variables are called Other variables are called temporarytemporary– There is no need to store these elsewhere!There is no need to store these elsewhere!

Page 36: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Permanent variables examplePermanent variables example

Consider rule:Consider rule:p(X,Y) :- q(X,Z), r(Z,Y), s(Z,W)p(X,Y) :- q(X,Z), r(Z,Y), s(Z,W)

Permanent variables are:Permanent variables are:– Z: Need to store it to pass from q to Z and then to sZ: Need to store it to pass from q to Z and then to s– Y: Need to store it from the call to pass to rY: Need to store it from the call to pass to r

Temporary variables are:Temporary variables are:– X: No need to store, since it is (only) immediately X: No need to store, since it is (only) immediately

used after the callused after the call– W: No need to store, since it is never used elsewhereW: No need to store, since it is never used elsewhere

Page 37: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

WAMWAM22 Stack Stack

Permanent variables must be stored (environment).Permanent variables must be stored (environment).– get_variableget_variable//put_variableput_variable instructions must get/put these instructions must get/put these

variables in the environmentvariables in the environment

Since the same predicate can be called more than onceSince the same predicate can be called more than once– permanent variables must be stored in a permanent variables must be stored in a StackStack of environments. of environments.– Continuation point must also be stored in environmentContinuation point must also be stored in environment

Global register Global register EE stores top of Stack stores top of Stackallocateallocate//deallocatedeallocate instructions for building instructions for building environment before executing rule, and freeing it after environment before executing rule, and freeing it after executionexecution

Page 38: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Environment frameEnvironment frame

For each rule, with For each rule, with nn permanent variables, the permanent variables, the environment to be push into the Stack is:environment to be push into the Stack is:

EE CE (continuation environment)CE (continuation environment)

E+1E+1 CP (continuation execution point)CP (continuation execution point)

E+2E+2 n n (number of permanent variables)(number of permanent variables)

E+3E+3 Y1 (permanent variable 1)Y1 (permanent variable 1)

……

E+n+2E+n+2 Yn (permanent variable n)Yn (permanent variable n)

Page 39: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Allocate/deallocate environmentsAllocate/deallocate environments

Just fill in (resp. free) an environment:Just fill in (resp. free) an environment:

Page 40: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compiling an Compiling an LL22 query query

Just as compiling a rule, but without the Just as compiling a rule, but without the part of the headpart of the head

It must allocate an environment, with an It must allocate an environment, with an initial top of stack, an initial continuation initial top of stack, an initial continuation point, and the point, and the nn permanent variables of permanent variables of the query (in case of a conjunction).the query (in case of a conjunction).

Page 41: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Example of compilation into WAMExample of compilation into WAM22

Compilation of p(X,Y) :- q(X,Z), r(Z,Y).Compilation of p(X,Y) :- q(X,Z), r(Z,Y).

Page 42: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Summary structure of WAMSummary structure of WAM22

X1, …, Xn and A1, … An registersX1, …, Xn and A1, … An registers

Special registers E, CP, P, S, and HSpecial registers E, CP, P, S, and H

Code areaCode area

HeapHeap

StackStack

PDLPDL

Instructions set, as for WAMInstructions set, as for WAM11 except: except:call p/n call p/n redefinedredefined

proceed proceed redefinedredefined

New instructionNew instruction allocate N allocate N

New instructionNew instruction deallocate deallocate

CodeCode

HeapHeap

StackStack

PDLPDL

H

S

P

E

CP

Page 43: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

The Language The Language LL33

LL33 simply is Pure Prolog simply is Pure Prolog

In other words, it is as In other words, it is as LL22 but now with possibly but now with possibly

more than one rule per predicatemore than one rule per predicate

It has to deal with backtracking!It has to deal with backtracking!– Failure no longer means abortion of the whole Failure no longer means abortion of the whole

processprocess– Alternatives must be consideredAlternatives must be considered

Uses a top-down left most resolutionUses a top-down left most resolution– Chronological backtracking: when failure, the latest Chronological backtracking: when failure, the latest

choice is reexamined first.choice is reexamined first.

Page 44: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Choice pointsChoice points

If a rule has alternatives, the state of If a rule has alternatives, the state of computation must be savedcomputation must be saved– Only this way we can guarantee that it is Only this way we can guarantee that it is

possible to restore it if backtracking is neededpossible to restore it if backtracking is needed

Such a state is called a Such a state is called a choice pointchoice point– First idea: store them in a separate StackFirst idea: store them in a separate Stack

Environments: AND-StackEnvironments: AND-Stack

Choice points: OR-StackChoice points: OR-Stack

Page 45: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Environment ProtectionEnvironment Protection

a :- b(X), c(X).a :- b(X), c(X).

b(X) :- e(X).b(X) :- e(X).

c(1).c(1).

e(X) :- f(X).e(X) :- f(X).

e(X) :- g(X).e(X) :- g(X).

f(2).f(2).

g(1).g(1).

Environment for a

Environment for b

Environment for e

Choice point for e

Environment for f

Environment for c

AND Stack

Or Stack

Ups!And now where is the state of arguments before the call of e/1?

The choice point must protect from deallocation all environments whose creation precedes that of the existing choice point.

Page 46: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Environment for e

Environment ProtectionEnvironment Protection

Use only one stack (both for environments and choice Use only one stack (both for environments and choice points).points).E E register points to top of the stack. register points to top of the stack.New New B B register point to last choice pointregister point to last choice point

a :- b(X), c(X).a :- b(X), c(X).

b(X) :- e(X).b(X) :- e(X).

c(1).c(1).

e(X) :- f(X).e(X) :- f(X).

e(X) :- g(X).e(X) :- g(X).

f(2).f(2).

g(1).g(1).

Environment for a

Environment for b

Choice Point for e

Environment for f

Environment for c

Stack

Environment for e

Environment for b

B

EE

E

E

E

EEE

E

Page 47: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

What to stored in a choice point?What to stored in a choice point?

Information for Information for unbinding unbinding variables.variables.– I.e. making variables free again.I.e. making variables free again.

Which variables?Which variables?– Put this information in another stack, and put in the Put this information in another stack, and put in the

choice pointchoice point the pointer to the top of that stack when the pointer to the top of that stack when called.called.

New (and last!) data area: New (and last!) data area: TrailTrail– New register New register TR: TR: TrailTrail pointerpointer

New register New register HBHB to store the value of to store the value of HH at the at the last choice point:last choice point:– Only bindings before, i.e. less than, Only bindings before, i.e. less than, HBHB need to be need to be

stored in Trail.stored in Trail.

Page 48: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Choice point frameChoice point frameEach choice point pushes into the Stack:Each choice point pushes into the Stack:

BB n n (number of arguments)(number of arguments)

B+1B+1 A1 (argument register 1)A1 (argument register 1)

……

B + nB + n An (argument register n)An (argument register n)

B+n+1B+n+1 CE (Current Environment)CE (Current Environment)

B+n+2B+n+2 CP (Continuation Pointer)CP (Continuation Pointer)

B+n+3B+n+3 B (previous choice point)B (previous choice point)

B+n+4B+n+4 BP (next clause)BP (next clause)

B+n+5B+n+5 TR (Trail pointer)TR (Trail pointer)

B+n+6B+n+6 H (Heap pointer)H (Heap pointer)

Needed because put instructions below may overwrite them

For protecting environment (and

then freeing it)

Where to continue upon proceed

What is the previous choice point, in case all

rules failCode pointer of next rule of this predicate When backtracking,

remove bindings at the trail, up to hereNeeded for

freeing Heap space after

failure

Page 49: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

New allocate/deallocate instructionNew allocate/deallocate instruction

Must also take care of choice points:Must also take care of choice points:

Page 50: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compiling rules in Compiling rules in LL33

Extends that of WAMExtends that of WAM2 2 now with instructions for dealing now with instructions for dealing with choice pointswith choice pointsRoughly, a set of rules is translates into:Roughly, a set of rules is translates into:– In the first rule (if more exist) create choice pointIn the first rule (if more exist) create choice point– Allocate environment (if it is a rule, i.e. not a fact)Allocate environment (if it is a rule, i.e. not a fact)– Compilation of 1st rule as beforeCompilation of 1st rule as before– End with End with deallocatedeallocate (if needed) and (if needed) and proceedproceed– In the second rule (if more exist) reset information from first ruleIn the second rule (if more exist) reset information from first rule– Compilation of 2nd rule as beforeCompilation of 2nd rule as before– ……– In the last rule reset information from previous and reset In the last rule reset information from previous and reset BB to to

previous (remove choice point)previous (remove choice point)– Compilation of last rule as beforeCompilation of last rule as before

Page 51: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Example of compilation into WAMExample of compilation into WAM33

Page 52: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compiling an Compiling an LL33 query query

Exactly the same as for Exactly the same as for LL22! !

– No choice points for queries!!No choice points for queries!!

Page 53: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Handling backtrackingHandling backtrackingInstead of Instead of fail fail causing abortion, it now should cause causing abortion, it now should cause backtracking to the right place of the code:backtracking to the right place of the code:

All instruction that dealt with failure must be change so as to call All instruction that dealt with failure must be change so as to call backtrackbacktrack::– E.g. E.g. get_structureget_structure or any one calling or any one calling unifyunify

Add to their end:Add to their end:

Page 54: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Instructions for choice pointsInstructions for choice pointstry_me_elsetry_me_else

try_me_elsetry_me_else LL– LL is a label for code position of next rule is a label for code position of next rule– Allocates a new choice point frame and set Allocates a new choice point frame and set BB

accordingly:accordingly:

Page 55: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

retry_me_elseretry_me_else

retry_me_elseretry_me_else LL– LL is a label for code position of next rule is a label for code position of next rule– Resets all necessary information from the choice point and call Resets all necessary information from the choice point and call

code in code in LL : :

Page 56: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

trust_metrust_me

trust_metrust_me– Removes choice point, and updates Removes choice point, and updates BB

accordingly:accordingly:

Page 57: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Dealing with the TrailDealing with the Trail

Every binding of variable must be stored in the Every binding of variable must be stored in the Trail (if needed, i.e. if is below Trail (if needed, i.e. if is below HBHB).).

This requires changing the This requires changing the bindbind procedure: procedure:

Page 58: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Pushing bindings in the TrailPushing bindings in the Trail

Only if the address of the binding is below Only if the address of the binding is below HBHB or if it is in an environment (local or if it is in an environment (local variables) below variables) below B:B:

Page 59: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Unwinding the TrailUnwinding the Trail

Unbound all variable on top of the Trail:Unbound all variable on top of the Trail:

Page 60: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Summary structure of WAMSummary structure of WAM33

X1, …, Xn and A1, … An registersX1, …, Xn and A1, … An registersSpecial registers E, CP, P, S, H, HB, Special registers E, CP, P, S, H, HB, B, and TR.B, and TR.Code areaCode areaHeapHeapStackStackTrailTrailPDLPDLInstructions set, as for WAMInstructions set, as for WAM22 redefined for failure and different redefined for failure and different allocation/deallocationallocation/deallocationNew instructions:New instructions:

try_me_else Ltry_me_else Lretry_me_else Lretry_me_else Ltrust_metrust_me

CodeCode

HeapHeap

StackStack

TrailTrail

PDLPDL

H

S

P

E

CP

B

HB

TR

Page 61: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Implementing CUTImplementing CUT

The extra-logical predicate CUT (!) trims search The extra-logical predicate CUT (!) trims search spacespace– It succeeds and has side-effect of forgetting It succeeds and has side-effect of forgetting

information on alternatives (for the calling predicate information on alternatives (for the calling predicate and for those before in the body).and for those before in the body).

– Obtained by discarding choice points created after Obtained by discarding choice points created after that for the calling predicatethat for the calling predicate

– Eg. InEg. Inp(X) :- q(X), r(X), !, s(X).p(X) :- q(X), r(X), !, s(X).

the ! discards choice points created after that of p/1 the ! discards choice points created after that of p/1 and also that of p/1.and also that of p/1.

Page 62: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Extending WAM for dealing with !Extending WAM for dealing with !

Use an extra global register Use an extra global register B0B0, for storing , for storing the value of the value of BB at the moment the at the moment the procedure (in the head) is called.procedure (in the head) is called.

Instruction Instruction callcall must be changed in order must be changed in order to set to set B0B0 to the current value of to the current value of BB

This way, ! amounts to (re)setting This way, ! amounts to (re)setting B B to the to the value of value of B0B0

Page 63: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Shallow and deep cutsShallow and deep cuts

It is useful to distinguish between shallow and deep cuts:It is useful to distinguish between shallow and deep cuts:a :- !, b, c.a :- !, b, c. (shallow cut)(shallow cut)a :- b, !, c.a :- b, !, c. (deep cut)(deep cut)

In shallow cut there are no worries with the cut register In shallow cut there are no worries with the cut register B0B0::– Just set Just set BB to to B0 B0 (with instruction (with instruction neck_cutneck_cut))

In deep cut the In deep cut the B0B0 must be stored in the stack so that must be stored in the stack so that other body literals don’t overwrite itother body literals don’t overwrite it– Use a pseudo variable (which need to be allocate in clauses that Use a pseudo variable (which need to be allocate in clauses that

have deep cuts) in the environment for this.have deep cuts) in the environment for this.– Instruction Instruction get_levelget_level YY to store to store B0B0 in the variable in the variable– Instruction Instruction cutcut YY for setting for setting BB to the value of the variable to the value of the variable

Page 64: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Example of compilation with !Example of compilation with !

Body of a :- !, bBody of a :- !, b Body of a :- b, !, c.Body of a :- b, !, c.

Page 65: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Optimizing the WAMOptimizing the WAM

What we’ve studied up to know is enough for What we’ve studied up to know is enough for implementing Prolog (apart from some system implementing Prolog (apart from some system predicates, which are not that difficult!)predicates, which are not that difficult!)But some optimizations can be madeBut some optimizations can be madePrinciples for optimizingPrinciples for optimizing

1.1. Use Heap as little as possible Use Heap as little as possible – Terms in the Heap tend to persistTerms in the Heap tend to persist

2.2. Allocate registers so as to avoid unnecessary data Allocate registers so as to avoid unnecessary data movement (and minimize code size)movement (and minimize code size)

3.3. Situation that occur often may have specific Situation that occur often may have specific treatment (and instructions) if that saves time or treatment (and instructions) if that saves time or spacespace

Page 66: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Heap representationHeap representation

The extra STR cells The extra STR cells (in 0,4 and 7) can (in 0,4 and 7) can obviously be saved!obviously be saved!– In line with principle 1In line with principle 1

This requires some This requires some easy changes in the easy changes in the WAMWAM00 instructions. instructions.

Eg:Eg:

00 STRSTR 11

11 h/2h/2

22 REFREF 22

33 REFREF 33

44 STRSTR 55

55 f/1f/1

66 REFREF 33

77 STRSTR 88

88 p/3p/3

99 REFREF 22

1010 STRSTR 11

1111 STRSTR 55

Page 67: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Heap representation exampleHeap representation example00 h/2h/2

11 REFREF 11

22 REFREF 22

33 f/1f/1

44 REFREF 22

55 p/3p/3

66 REFREF 11

77 STRSTR 00

88 STRSTR 33

00 STRSTR 11

11 h/2h/2

22 REFREF 22

33 REFREF 33

44 STRSTR 55

55 f/1f/1

66 REFREF 33

77 STRSTR 88

88 p/3p/3

99 REFREF 22

1010 STRSTR 11

1111 STRSTR 55

Page 68: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Special treatment of constantsSpecial treatment of constants

Constants (0-ary structures) are used often, and Constants (0-ary structures) are used often, and may use optimized codemay use optimized code– So, in line with principle 3, create special purpose So, in line with principle 3, create special purpose

instructionsinstructions

Consider: Consider: This simply binds a

register and then either checks it has a (in read

mode), or puts an a in the Heap (in write mode)

Page 69: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Treatment of constantsTreatment of constantsSequences of instructionsSequences of instructions

unify_variable Xiunify_variable Xiget_structure c/0, Xiget_structure c/0, Xi

can be simplified into:can be simplified into:unify_constant cunify_constant c

This also uses registers more sparsely (principle 2)This also uses registers more sparsely (principle 2)The same applies toThe same applies to

put_structure c/0, Xiput_structure c/0, Xiset_variable Xiset_variable Xi

which can be simplified into:which can be simplified into:set_constant cset_constant c

A special tag is required for constants CONA special tag is required for constants CON

Page 70: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Representation of f(b,g(a))Representation of f(b,g(a))

00 g/1g/1

11 CONCON aa

22 f/2f/2

33 CONCON bb

44 STRSTR 00

Page 71: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

New instructions for constantsNew instructions for constants

Page 72: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Special treatment of ListsSpecial treatment of Lists

Lists are also used quite often.Lists are also used quite often.– So if it is possible to specialize, let’s do itSo if it is possible to specialize, let’s do it

Introduce special put and get instructions Introduce special put and get instructions for list terms, using special tag LIS:for list terms, using special tag LIS:

Page 73: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compiling a query with listsCompiling a query with lists

(W) 1(W) 1 REFREF 11

22 CONCON [][]

A1: 3A1: 3 REFREF 33

44 REFREF 33

55 REFREF 11

A3: 6A3: 6 f/1f/1

77 REFREF 11

A2: A2: LISLIS 44

Page 74: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Example of fact with listExample of fact with list

p(f(X),[Y,f(a)],Y).p(f(X),[Y,f(a)],Y).

Page 75: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Treatment of anonymous variablesTreatment of anonymous variables

Also anonymous variables “_” can be Also anonymous variables “_” can be treated in a specialized mannertreated in a specialized mannerIf variable is anonymous, there is no need If variable is anonymous, there is no need to bind it to any registerto bind it to any register– Just put free variable directly in the head (in Just put free variable directly in the head (in

set or write)set or write)– Or ignore variable as it always unify with Or ignore variable as it always unify with

everything (in read mode)everything (in read mode)

Add two instructions for thisAdd two instructions for this

Page 76: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Instructions for anonymous Instructions for anonymous variablesvariables

Example:Example:

Page 77: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Saving registers usageSaving registers usage

Sometimes registers are used only locally for Sometimes registers are used only locally for (unnecessarily) saving temporal variables:(unnecessarily) saving temporal variables:E.g. in the compilation of E.g. in the compilation of append([],L,L).append([],L,L).get_constant [], A1get_constant [], A1get_variable X1,A2get_variable X1,A2get_value X1,A3get_value X1,A3proceedproceedWhy not, instead:Why not, instead:get_constant [], A1get_constant [], A1get_value A2,A3get_value A2,A3proceedproceed

Page 78: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Another exampleAnother example

Page 79: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Saving registers usage Saving registers usage

In general, the instructionsIn general, the instructions– get_variable Xi, Ajget_variable Xi, Aj– put_value Xi, Ajput_value Xi, Aj

are void, as they only change values from are void, as they only change values from registers.registers.

It is difficult to find out when optimizations can It is difficult to find out when optimizations can be made.be made.

Special purpose (peep-hole) algorithms exist.Special purpose (peep-hole) algorithms exist.– Outside the scope of this course!Outside the scope of this course!

Page 80: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Last call optimizationLast call optimization

When the last procedure of a rule is called, the When the last procedure of a rule is called, the environment can be removed!environment can be removed!– The permanent variables there, once loaded into A The permanent variables there, once loaded into A

registers, are no longer needed.registers, are no longer needed.

Swap Swap deallocatedeallocate and and callcall instructions in instructions in rules.rules.– This requires some changes:This requires some changes:

deallocatedeallocate no longer sets no longer sets PP to the continuation code, as it to the continuation code, as it is no longer the last instruction. Instead it sets is no longer the last instruction. Instead it sets CPCP

The last The last callcall must not change the must not change the CPCP. Since the . Since the callcall is is needed for others than the last, a new instruction needed for others than the last, a new instruction executeexecute is is neededneeded

Page 81: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

New instructions for LCONew instructions for LCO

Example of usage:Example of usage:

Page 82: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Consequences of LCOConsequences of LCO

If there are no protected choice points (E > B) If there are no protected choice points (E > B) then the space then the space deallocateddeallocated can be can be immediately used in the next allocate or immediately used in the next allocate or try_me_elsetry_me_else..This considerably saves stack space!This considerably saves stack space!If the last call of the last rule is a recursive call, If the last call of the last rule is a recursive call, with LCO the stack does not grow:with LCO the stack does not grow:– The same space is used over and over for successive The same space is used over and over for successive

environment framesenvironment frames– This amounts to iteration!This amounts to iteration!

So, LCO is a general case of tail recursion So, LCO is a general case of tail recursion optimization!!optimization!!

Page 83: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Special case of chain rulesSpecial case of chain rules

In chain rules p(…) :- q(…) there are no In chain rules p(…) :- q(…) there are no permanent variablespermanent variables– So the environment frame only has the value of So the environment frame only has the value of CPCP

(plus (plus CECE))– But with LCO this is immediately undone by But with LCO this is immediately undone by

deallocate!deallocate!

So the translation of a chain rule can simply be:So the translation of a chain rule can simply be:p:p: get argument of pget argument of p

put arguments of qput arguments of qexecuteexecute q q

Note that this way chain rules don’t use the Note that this way chain rules don’t use the stack!stack!

Page 84: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Environment trimmingEnvironment trimming

It may happen that part of the environment It may happen that part of the environment (variables) can be discarded before the (variables) can be discarded before the last call.last call.

For example, inFor example, inp(X) :- q(X,Y), r(Y,Z), s(Z,W), t(W).p(X) :- q(X,Y), r(Y,Z), s(Z,W), t(W).

the permanent variable Y can be freed the permanent variable Y can be freed after r, and Z after safter r, and Z after s

This is a generalization of LCOThis is a generalization of LCO

Page 85: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Environment trimmingEnvironment trimming

Environment trimming requires sorting of Environment trimming requires sorting of variables (put first variable that lasts variables (put first variable that lasts longer)longer)

Add an extra argument to call, with the Add an extra argument to call, with the number of variable still needed after the number of variable still needed after the callcall– Apart from environment protection, call extra Apart from environment protection, call extra

argument allows reducing environment spaceargument allows reducing environment space

Page 86: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Stack variablesStack variables

According to principle 1 (save space in heap) According to principle 1 (save space in heap) one could put in the environment frame the one could put in the environment frame the whole structure of permanent (local) variableswhole structure of permanent (local) variables– Now, the REF is in the stack, and the structure is in Now, the REF is in the stack, and the structure is in

the heap.the heap.

Idea is to put the whole structure in the stack, Idea is to put the whole structure in the stack, rather than in the heaprather than in the heap

Together with LCO and environment trimming, Together with LCO and environment trimming, this is not so easy to implementthis is not so easy to implement– See book to know exactly how…See book to know exactly how…

Page 87: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

IndexingIndexing

The 3 instructions for dealing with choice points The 3 instructions for dealing with choice points impose a sequential search over the rulesimpose a sequential search over the rules– This is as it should, if all head arguments are unboundThis is as it should, if all head arguments are unbound– But if arguments are (partially) bound this could be But if arguments are (partially) bound this could be

improvedimproved

Interleave search of the rule with unificationInterleave search of the rule with unification– It is difficult in general to generate filters It is difficult in general to generate filters

discriminating patterns based on all predicate discriminating patterns based on all predicate argumentsarguments

– Compromise solution: Discriminate on 1st argument Compromise solution: Discriminate on 1st argument patternspatterns

First argument is indexing key.First argument is indexing key.

Page 88: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Partitioning clauses for indexingPartitioning clauses for indexing

A free variable in the A free variable in the indexing key makes indexing key makes clauses to be explored in clauses to be explored in all casesall casesMoreover, order of Moreover, order of clauses must be clauses must be preserved.preserved.This requires partitioning This requires partitioning clauses:clauses:– Maximal subsequences not Maximal subsequences not

containing free variable in containing free variable in indexing keyindexing key

Page 89: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Choice points and partitionsChoice points and partitions

Now Now try_me_elsetry_me_else, , retry_me_elseretry_me_else and and trust_metrust_me are for are for partitions rather than rulespartitions rather than rulesIndexing code is needed Indexing code is needed in the beginning of each in the beginning of each partition which has no free partition which has no free variable in keyvariable in keyIndexing techniques are Indexing techniques are used for directly jumping used for directly jumping to the appropriate rule(s) to the appropriate rule(s) in a partitionin a partition

Page 90: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Levels of indexingLevels of indexing

First see A1 has a variable, constant, list or First see A1 has a variable, constant, list or structure (structure (switch_on_term V,C,L,Sswitch_on_term V,C,L,S):):– If a variable, run the first ruleIf a variable, run the first rule– If a constant, then use a hash table to determine If a constant, then use a hash table to determine

where to jump to (where to jump to (switch_on_constant N, HTswitch_on_constant N, HT))– If a structure or list do similarlyIf a structure or list do similarly

More optimization by avoiding More optimization by avoiding try_me_elsetry_me_else, , retry_me_elseretry_me_else and and trust_metrust_me and using and using alternative alternative trytry, , retryretry and and trusttrust that go that go directly to the rules of that functor or list.directly to the rules of that functor or list.

Page 91: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Indexing exampleIndexing example

Page 92: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Compilation of append/3Compilation of append/3

If called with an instantiated 1st argument no choice point is ever If called with an instantiated 1st argument no choice point is ever created!created!– Indexing may also reduce the creation of choice pointsIndexing may also reduce the creation of choice points

Page 93: Integrated Logic Systems Part 1 WAM and Implementations of Prolog

Indexing and cutsIndexing and cuts

This indexing interferes with the treatment This indexing interferes with the treatment of cutsof cuts

Sometimes indexing creates one extra Sometimes indexing creates one extra choice point, that must be freed by !choice point, that must be freed by !

See detail in the book…See detail in the book…