What needs to be kept track of ffl The current substitution ffl The current goal ffl The clause currently in use ffl Backtrack Information 2

Similar documents
Warren s Abstract Machine

Implementação de Linguagens 2016/2017

Implementação de Linguagens 2017/2018

This lecture covers: Prolog s execution strategy explained more precisely. Revision of the elementary Prolog data types

tuprolog with exceptions

Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy

On the BEAM Implementation

Operational Semantics

Prolog Programming. Lecture Module 8

PUP: An Architecture to Exploit Parallel Unification in Prolog

Efficient Instance Retrieval of Subgoals for Subsumptive Tabled Evaluation of Logic Programs arxiv: v1 [cs.pl] 27 Jul 2011

Fundamentals of Prolog

Recursion, Structures, and Lists

Combining depth-first and breadth-first search in Prolog execution

(low) Code Area. feature symbol table type symbol table addtype table. unification table feature_val table HEAP STACK

Warren s Abstract Machine

Logic Languages. Hwansoo Han

INTRODUCTION TO PROLOG

ACHARACTERIZATION OF PROLOG EXECUTION MARK ANDREW FRIEDMAN

UNIVERSITETET I OSLO

Part I Logic programming paradigm

Infinite Derivations as Failures

Prolog-2 nd Lecture. Prolog Predicate - Box Model

Introduction to Logic Programming in Prolog 1 / 39

Logic Programming. Efficiency Issues. Temur Kutsia

The Idea Underlying Logic Programming. CSci 8980, Fall 2012 Specifying and Reasoning About Computational Systems An Introduction to Logic Programming

Notes for Chapter 12 Logic Programming. The AI War Basic Concepts of Logic Programming Prolog Review questions

Prolog. Intro to Logic Programming

Operators (2A) Young Won Lim 10/2/13

Agenda. CS301 Session 20. A logic programming trick. A simple Prolog program. Introduction to logic programming Examples Semantics

What s the problem? fib(1,1). fib(2,1). fib(n,x) :- N>2, N1 is N-1, N2 is N-2, fib(n1,x2), fib(n2,x2).

The Algernon Abstract Machine: Compiling and Executing Rule-Based Programs

A Partial Breadth-First Execution Model for Prolog*

Conjunctive queries. Many computational problems are much easier for conjunctive queries than for general first-order queries.

Operators (2A) Young Won Lim 10/5/13

TELIOS: A Tool for the Automatic Generation of Logic Programming Machines

The object level in Prolog. Meta-level predicates and operators. Contents. The flow of computation. The meta level in Prolog

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Functional & Logic Programming - Backtracking October, 01

Tests, Backtracking, and Recursion

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

A General Framework for Heterogeneous Associative Logic Programming

Map coloring example

Dept. of Comp. Eng. and Info. Sc.,Bilkent University, Bilkent,Ankara,Turkey

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

Artificial Intelligence Course Sharif University of Technology

A second life for Prolog

Global Analysis of Standard Prolog Programs

Chapter 16. Logic Programming. Topics. Predicate Calculus and Proving Theorems. Resolution. Resolution: example. Unification and Instantiation

CMSC 331 Final Exam Section 0201 December 18, 2000

Functional programming Primer I

Thread-Based Competitive Or-Parallelism

Can Logic Programming Execute as Fast as Imperative Programming? Copyright 1990 Peter Lodewijk Van Roy. Volume 1 Chapters & References

CMSC 330: Organization of Programming Languages

Exceptions and Design

Logic Programming. Let us have airline flight information of the form: 1. Application Domains: 2. Definitions

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

Outcome-Oriented Programming (5/12/2004)

Mathematical Logic Prof. Arindama Singh Department of Mathematics Indian Institute of Technology, Madras. Lecture - 37 Resolution Rules

Lecture 9: A closer look at terms

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Packing sub-word-size arguments. Zoltan Somogyi

Run-time Environments

The Prolog to Mercury transition guide

Run-time Environments

Incremental Flow Analysis. Andreas Krall and Thomas Berger. Institut fur Computersprachen. Technische Universitat Wien. Argentinierstrae 8

of Prolog Andreas Krall and Thomas Berger Institut fur Computersprachen Technische Universitat Wien Argentinierstrae 8 A-1040 Wien

COMBINATION OF XML-RPC AND MOBILE AGENT TECHNOLOGIES

Logic Programming. Manipulating Programs. Temur Kutsia

If a program is well-typed, then a type can be inferred. For example, consider the program

Research Report AI A Numerical Equation Solver in Prolog Michael A. Covington Artificial Intelligence Programs The University of Georgia

Prolog. Logic Programming vs Prolog

Logic Programming and Resolution Lecture notes for INF3170/4171

Prolog (cont d) Remark. Using multiple clauses. Intelligent Systems and HCI D7023E

Using Prolog and CLP for. Designing a Prolog Processor

The Metalanguage λprolog and Its Implementation

Incremental Global Compilation of. Prolog with the Vienna Abstract. The Vienna Abstract Machine (VAM) is an abstract machine which has

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

Bindings & Substitution

CAP 5602 Summer, Lesson 4: Loops. The topics 1. the cut and some of its uses 2. the while loop 3. the do until loop

Register Allocation in a Prolog Machine

Information technology - Programming languages - Prolog - Part 1: General Core

Exercises on the Fundamentals of Prolog

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

Prolog - 3 Prolog search trees + traces

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Global Dataow Analysis of Prolog. Andreas Krall and Thomas Berger. Institut fur Computersprachen. Technische Universitat Wien. Argentinierstrae 8

Q1:a. Best first search algorithm:

Logic-Oriented Programming (5/11/2004)

CS 360: Programming Languages Lecture 10: Logic Programming with Prolog

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

A Pearl on SAT Solving in Prolog (extended abstract)

CS143 Final Fall 2009

StackVsHeap SPL/2010 SPL/20

Heap Memory Management in Prolog with Tabling: Principles and Practice

Topic B: Backtracking and Lists

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Tabling in Mercury: Design and Implementation

Transcription:

Prolog Implementation ffl The Generic Idea: Everything except unification. ffl An Introduction to the WAM: Flat unification only. 1

What needs to be kept track of ffl The current substitution ffl The current goal ffl The clause currently in use ffl Backtrack Information 2

A Stack based Implementation" Adapted from the ISO standard This execution model is based on a stack. ES. St-index A value defined by the current number of components of S. sglstk A stack of subgoals which defines a sequence of activators that might be activated during execution. subst A substitution which defines the state of the instantiations of the variables. BI Backtrack information: a value which defines how to re-execute a goal. currstate is the top of the stack ES. 3

The current subgoal is the given by the top of sglstk. It contains: 1. The current activator, curract, 2. Its cutparent. 4

BI has value: nil Its initial value, or ctrl The procedure is a control construct, or bip The activated procedure is a built-in predicate, or up(cl) CL is a list of the clauses of a user-defined procedure whose predicate is identical to curract, and which are still to be executed, and DM is the module in whose body these clauses appear. 5

The choicepoint for the execution state ES i+1 is ES i. St-index Subgoal Stack Substitution BI 6

A subgoal DS on the subgoal stack is a structured data type with components: activator A predication P which must be executed successfully in order to satisfy the goal. cutparent A pointer to a deeper execution state that indicates where control is resumed should a cut be re-executed. Subgoal Cutparent 7

Initialization St- Decorated Substi- BI index Subgoal Stack, tution 1 ((goal, 0), newstack DS ), fg nil newstack ES 8

Sucess and Failure ffl A goal succeeds when the subgoal stack of currstate is empty ffl A goal fails when the execution stack is empty 9

Backtracking 1. Examine the value of BI for the new currstate. 2. If BI is up(cl) then p is a user defined procedure remove the head of CL and continue executing a user defined procedure. 3. If BI is bip then p is a built-in predicate, continue by executing the builtin. 4. If BI is ctrl the effect of re-executing depends on the control construct 5. If BI is nil then the new curract has not been executed, continue execution by selecting a clause. 10

Executing a user defined procedure 1. If there are no more clauses for p then pop currstate from ES. 2. Else select the clause c from up(cjcl). 3. Unify the head of c and curract producing a most general unifier MGU. 4. Apply the substitution MGU to the body of c. 5. Make a copy C of currstate. 6. Apply the substitution MGU to the current goal in C. 11

7. Replace the current activator of the current goal in C by the MGU modified body of c. 8. Create a new entry for ES whose elements are C, BI = nil and with substitution the composition of the substitution in currstate and MGU). 9. Set cutparent of the new first subgoal of the subgoal stack of this new entry to be the current choice point. 10. Push this new entry on to S. It becomes the new currstate and the previous currstate becomes its choicepoint. 11. Continue execution by selecting a clause.

Selecting a clause 1. Search the database for a procedure p corresponding to the identifier and arity of curract. 2. If p is a control construct (true, fail, call, cut, conjunction, disjunction, if-then, if-then-else, catch, throw) then BI is set to ctrl and execution continues according to the rules for control constructs. 3. If p is a built-in predicate BP then BI is set to bip and continue execution for a bip.. 4. If p is a user-defined procedure BI is set to up(cl), where CL is a list of the current clauses of p of the procedure; continue by executing the user-defined procedure. 12

The Warren Abstract Machine Most implementations of Prolog now use some version of the WAM. This introduction follows that of Hassan Aït- Kaci in his tutorial reconstruction of the WAM. We cover a simple version of unification. 13

Unification in L 0 : Term Representation Terms are represented on a HEAP which is an array. The term's reference is it's index on the HEAP. To represent f(x; Y; a) we need to tag the HEAP items to distinguish structures such as f(@ 1 ;:::;@ n ) from the references @ i. Cells that are references to variables will be of the form REF; k where k is the cell referred to. Cells that reference structures will be of the form STR; k Structures themselves are denoted by functor=arity 14

Heap representation of p(z; h(z; W ; f(w )). 0 STR 1 1 h/2 2 REF 2 3 REF 3 4 STR 5 5 f/1 6 REF 1 7 STR 8 8 p/3 9 REF 2 10 STR 1 11 STR 5 Note that (the first occurrence of) a variable is a self reference. So 2 is Z and 3 is W. Note also that the arity tells you how many references to take. 15

Unification in L 0 : Compiling Queries To compile a query q we have to translate it into a representation of its heap format. Thus while processing part of a term we need to be able to save temporarily pieces of terms yet to be processed, or a variable that may appear later in a term. For this (variable) registers X1, X2 etc. are used. are used. For our example we would allocate as follows. X1 = p(x2;x3;x4) X2 = Z X3 = h(x2;x5) X4 = f(x5) X5 = W 16

Unification in L 0 : Compiling Queries 2 There are three kinds of tokens that must be processed: 1. A register associated with a structure functor; 2. A register argument not previously encountered; 3. A register argument previously seen. 17

Leading to three instructions for the machine M 0 1. put structure f=n; Xi 2. set variable Xi 3. set value Xi The heap is therefor used as a stack for building terms. 18

Unification L 0 : Compiling Queries 3 put_structure h/2,x3 % X3 = h set_variable X2 % (Z, set_variable X5 % W) put_structure f/1,x4 % X4 = f set_value X5 % (W), put_structure p/3, X1 % X1 = p set_value X2 % (Z, set_value X3 % X3, set_value X4 % X4). Note that the address of the query is X1. 19

Unification in L 0 : Compiling Programs Unifying a query q to p (a program term) can proceed by following the term structure in X1 as long as it matches the structure of p. There is a complication with unbound REF terms. So if we have a program term. p(f(x);h(y;f(a));y) the matching we should get is: X1 = p(x2;x3;x4) X2 = f(x5) X3 = h(x4;x6) X4 = Y X5 = X X6 = f(x7) X7 = a 20

Flattening the program. 1. A register associated with a structure functor; 2. a first seen register argument; 3. an already seen register argument. Instructions: 1. get structure f=n; Xi 2. unify variable Xi 3. unify value Xi 21