Logic Programming. Efficiency Issues. Temur Kutsia

Similar documents
Ecient Prolog: A Practical Guide. Michael A. Covington. The University of Georgia. Abstract

Logic Programming. Manipulating Programs. Temur Kutsia

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

Logic Programming. Using Data Structures Part 2. Temur Kutsia

Lecture 11: Feb. 10, 2016

Fundamentals of Prolog

COMP2411 Lecture 20: Logic Programming Examples. (This material not in the book)

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

The Prolog to Mercury transition guide

Combining Lists & Built-in Predicates

Implementação de Linguagens 2016/2017

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).

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

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

Chemistry Studio. B.Tech Project. Ashish Gupta (Y8140) Akshay Mittal (Y8056)

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Automated Reasoning Systems

COP4020 Programming Languages. Logical programming with Prolog Prof. Xin Yuan

CMSC 330: Organization of Programming Languages

Exercises on the Fundamentals of Prolog

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

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

Snakes. Declarative Languages. Relation different. Overview. Negation as failure. Relation different

Lecture 9: A closer look at terms

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

What is a Logic Program. Introduction to Logic Programming. Introductory Examples. Introductory Examples. Foundations, First-Order Language

Recursion, Structures, and Lists

Topic B: Backtracking and Lists

Chapter 6. Predefined Predicates

BCS THE CHARTERED INSTITUTE FOR IT. BCS Higher Education Qualifications BCS Level 6 Professional Graduate Diploma in IT EXAMINERS' REPORT

First-Order Logic (FOL)

Lecture 16: Logic Programming in Prolog

Introduction to Logic Programming

Prolog. Intro to Logic Programming

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

Connection between Lists and Terms. Prolog-Lecture 2. Unifying Lists. Unifying Lists. Steps to a Recursive Predicate. List Predicates.

Advanced Prolog Programming

Prolog-2 nd Lecture. Prolog Predicate - Box Model

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

Logic Programming: Prolog

Logic Programming: The Prolog Language

Logic Languages. Hwansoo Han

Overview. Declarative Languages. operation of del. Deleting an element from a list. functions using del. inserting elements with del

Logic Programming Languages

Problem Solving and Search

Prolog Programming. Lecture Module 8

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

UNIVERSITETET I OSLO

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

Chapter 6 Fundamentals of the Logical Paradigm. Programming Languages and Paradigms J. Fenwick, B. Kurtz, C. Norris (to be published in 2012)

Symbolic Computation Example Programming Exercises for Prolog

zebra puzzle continued recap Prolog concepts family relations

Part I Logic programming paradigm

Prolog Examples. Distributed Systems / Technologies. Sistemi Distribuiti / Tecnologie

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

Programming Languages

Chapter 16. Logic Programming Languages

INTRODUCTION TO HASKELL

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

Tests, Backtracking, and Recursion

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

Prolog Introduction. Gunnar Gotshalks PI-1

CSI 2165 Winter Diana Inkpen SITE University of Ottawa. Prolog Part III. Input and output. Prolog databases

Prolog. Prolog: Programmation en Logique R.A. Kowalski: Predicate logic as a programming language, Proc IFIP 1974, pp.

INTRODUCTION TO PROLOG

Lecture 9. Exercises. Theory. Solutions to exercises LPN 8.1 & 8.2. Patrick Blackburn, Johan Bos & Kristina Striegnitz

Chapter 16. Logic Programming Languages ISBN

proof through refutation

Q1:a. Best first search algorithm:

Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3.

Example Programming Exercises for Prolog

Programming Languages Prolog Programming Project Due Wednesday, December 5 th, 2001

The Prolog to Mercury transition guide

Introduction. CSc 372. Comparative Programming Languages. 22 : Prolog Lists. Department of Computer Science University of Arizona.

Programming Paradigms Written Exam (6 CPs)

Prolog. Logic Programming vs Prolog

Logic Programming. Backtracking and Cut. Temur Kutsia

List Functions, and Higher-Order Functions

A Small Interpreted Language

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

Prolog - 3 Prolog search trees + traces

LOGIC AND DISCRETE MATHEMATICS

3 Lists. List Operations (I)

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Prolog. IFT 6802 Introduction to Prolog. Declarative programming LOGIC PROGRAMMING BASICS. LOGIC PROGRAMMING BASICS (suite) Facts

Derived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses

Programming Paradigms

Baby Steps Toward an Implementation of Axiomatic Language

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

Languages with Contexts II: An Applicative Language

MODIFYING THE KNOWLEDGE BASE DON T DO IT...

Prolog 2. Unification and Recursion

Map coloring example

PM - A REDUCE Pattern Matcher

Chapter 6 Control Flow. June 9, 2015

Thread-Based Competitive Or-Parallelism

CSEN403 Concepts of Programming Languages. Topics: Logic Programming Paradigm: PROLOG Search Tree Recursion Arithmetic

10. Logic Programming With Prolog

Introduction to Machine-Independent Optimizations - 1

Transcription:

Logic Programming Efficiency Issues Temur Kutsia Research Institute for Symbolic Computation Johannes Kepler University of Linz, Austria kutsia@risc.uni-linz.ac.at

Efficiency Issues in Prolog Narrow the Search Let Unification do the Work Avoid assert and retract Understand Tokenization Avoid String Processing Recognize Tail Recursion Let Indexing Help Use Accumulators Use Difference Lists

Narrow the Search Efficient programs must search efficiently. Example Knowledge base contains 1000 grey objects and 10 horses.?- horse(x), grey(x). is 100 times as fast as?- grey(x), horse(x). Narrow the search space as early as possible.

Narrow the Search Example Determine whether two lists are equal as sets. Bad solution: set_equal(l1,l2) :- permute(l1,l2). N element list has N! permutations. Testing set-equality of 20-element list can require 2.4 10 18 comparisons. Better solution: set_equal(l1,l2) :- sort(l1,l3), sort(l2,l3). N-element list can be sorted in N log N steps. Faster that the first solution by a factor of more that 10 16.

Let Unification do the Work Example Write a predicate that accepts a list and succeeds if the list has three elements. Bad solution: had_three_elements(l):-length(l,n),n=3. Slightly better solution: had_three_elements(l):-length(l,3). Good solution: had_three_elements([_,_,_]).

Let Unification do the Work Example Write a predicate that accepts a list and generates from it a similar list with the first two elements swapped. Good solution: swap_first_two([a,b Rest],[B,A Rest]). The data structures [A,B Rest] and [B,A Rest], or templates for them, are created when the program is compiled, and unification gives values to the variables at run time.

Avoid assert and retract Reasons: assert and retract are relatively slow and they lead to a messy logic. In most implementation the dynamic predicates can not run in a full compiled speed. The effect of assert and retract can not be undone by backtracking. Programs get hard to debug.

Avoid assert and retract Legitimate Uses: To record new knowledge in the knowledge base. To store the intermediate results of a computation that must backtrack past the point at which it gets its result. (Think about using setof or bagof instead. It might be faster.)

Understand Tokenization Fundamental unit: Term (numbers, atoms, structures). Numbers are stored in fixed-point or floating-point binary. Atoms are stored in a symbol table in which each atom occurs only once. Atoms in the program are replaced by their addresses in the symbol table (tokenization).

Understand Tokenization Because of tokenization the structure f( What a long atom this seems to be, What a long atom this seems to be, What a long atom this seems to be ) is more compact than g(aaaaa,bbbbb,ccccc). To compare two atoms, even long ones, the computer needs only compare their addresses. By contrast, comparing lists or structures requires every element to be examined individually.

Avoid String Processing Strings: Lists of numbers representing ASCII codes of characters. abc an atom. "abc" a list [97, 98, 99]. Strings are designed to be easily taken apart. Their only proper use is in situations where access to the individual characters is essential.

Recognize Tail Recursion Recursion: Can be inefficient. Each procedure call requires information to be saved so that control can return to the calling procedure. If a clause calls itself 1000 times, there will be 1000 copies of its stack frame in memory. Exception: Tail Recursion. Control need not return to the calling procedure because there is nothing more for it to do.

Recognize Tail Recursion Tail recursion exists when: The recursive call is the last subgoal in the clause, and There are no untried alternative clauses, and There are no untried alternatives for any subgoal preceding the recursive call in the same clause.

Recognize Tail Recursion Example This predicate is tail recursive. test1 :- write(hello), nl, test1. Example This predicate is not tail recursive because the recursive call is not last. test2 :- test2, write(hello), nl.

Recognize Tail Recursion Example This predicate is not tail recursive because it has an untried alternative. test3 : - write(hello), nl, test3. test3 : - write(goodbye).

Recognize Tail Recursion Example This predicate is not tail recursive because a subgoal has an untried alternative. test4 : - g, write(hello), nl, test4. g : - write(starting). g : - write(beginning).

Let Indexing Help To match the query?- f(a,b). PROLOG does not look at all the clauses in the knowledge base. It looks only the clauses for f. Indexing.

Let Indexing Help Implementation dependent. Many implementations index not only the predicate symbol but also the main functor of the first argument First-argument indexing. For?- f(a,b). The search considers only clauses that match f(a,...) and neglects clauses such as f(b,c).

Let Indexing Help Consequences of (first-argument) indexing. Argument order: The first argument should be the one most likely to be known at search time, and Preferably the most diverse. Better to have f(a,x). f(b,x). f(c,x). than f(x,a). f(x,b). f(x,c).

Let Indexing Help Consequences of (first-argument) indexing. Indexing can make a predicate tail recursive when it otherwise would not be. Example p(f(a,b)) :- p(a). p(a). is tail-recursive because indexing eliminates p(a) from consideration.