Fundamentals of Prolog

Similar documents
More Non-logical Features of Prolog

Recursion, Structures, and Lists

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

Prolog Programming. Lecture Module 8

Logic Languages. Hwansoo Han

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

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

Prolog. Logic Programming vs Prolog

Tests, Backtracking, and Recursion

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

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

Logic Programming. Efficiency Issues. Temur Kutsia

Declarative Programming Prolog CS360

Topic B: Backtracking and Lists

Prolog. Intro to Logic Programming

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

Resolution (14A) Young W. Lim 6/14/14

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

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

Logical reasoning systems

proof through refutation

The Logic Paradigm. Joseph Spring. 7COM1023 Programming Paradigms

Lecture 4: January 12, 2015

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

Part I Logic programming paradigm

Constraint Solving. Systems and Internet Infrastructure Security

An introduction to logic programming with Prolog

Implementação de Linguagens 2016/2017

MIDTERM EXAM (Solutions)

Lecture 17 of 41. Clausal (Conjunctive Normal) Form and Resolution Techniques

SAT solver of Howe & King as a logic program

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

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

Visual Prolog Tutorial

Lecture 11: Feb. 10, 2016

A Pearl on SAT Solving in Prolog (extended abstract)

CS 561: Artificial Intelligence

PROLOG PROgramming in LOGic

Operational Semantics

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

Mixed Integer Linear Programming

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

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

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

CSL105: Discrete Mathematical Structures. Ragesh Jaiswal, CSE, IIT Delhi

Advanced Prolog Programming

SLDNF-Draw user s manual Chapter 1: static trees

Infinite Derivations as Failures

Chapter 16. Logic Programming Languages

6.034 Notes: Section 11.1

Knowledge Representation and Reasoning Logics for Artificial Intelligence

Introduction to predicate calculus

INTRODUCTION TO PROLOG

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

Introduction to Logic Programming in Prolog 1 / 39

Advanced Logic and Functional Programming

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

Logic Programming Languages

Exercises on the Fundamentals of Prolog

7COM1023 Programming Paradigms

Learning Rules. Learning Rules from Decision Trees

CS 321 Programming Languages and Compilers. Prolog

Answer Key #1 Phil 414 JL Shaheen Fall 2010

Knowledge Representation and Reasoning Logics for Artificial Intelligence

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

Logic Programming: Prolog

Prolog-2 nd Lecture. Prolog Predicate - Box Model

Homework 1. Due Date: Wednesday 11/26/07 - at the beginning of the lecture

Data Integration: Datalog

CMSC 331 Final Exam Section 0201 December 18, 2000

The current topic: Prolog. Announcements. Meaning of a Prolog rule. Prolog syntax. Reminder: The deadline for Lab 2 re-mark requests is Friday.

Type checking by theorem proving in IDRIS

Logic as a Programming Language

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

COMP4418 Knowledge Representation and Reasoning

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

Chapter 16. Logic Programming Languages ISBN

Mathematical Logic Prof. Arindama Singh Department of Mathematics Indian Institute of Technology, Madras. Lecture - 9 Normal Forms

Family Example: Some Facts. respects(barb,dan). respects(barb,katie). respects(dan,brian). respects(dan,barbara).

Logic: TD as search, Datalog (variables)

Programming Paradigms

2SAT Andreas Klappenecker

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

LOGIC AND DISCRETE MATHEMATICS

Logic Programming and Resolution Lecture notes for INF3170/4171

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

Racklog: Prolog-Style Logic Programming

zebra puzzle continued recap Prolog concepts family relations

QUTE: A PROLOG/LISP TYPE LANGUAGE FOR LOGIC PROGRAMMING

Prolog 2. Unification and Recursion

Logic Programming: The Prolog Language

Integrity Constraints (Chapter 7.3) Overview. Bottom-Up. Top-Down. Integrity Constraint. Disjunctive & Negative Knowledge. Proof by Refutation

Prolog Assessed Exercise

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

Description logic reasoning using the PTTP approach

On the BEAM Implementation

Lecture Notes on Prolog

Extending Pattern Directed Inference Systems. EECS 344 Winter 2008

Information Science 1

Quick n Dirty Prolog Tutorial

Transcription:

Fundamentals of Prolog Prof. Geraint A. Wiggins Centre for Cognition, Computation and Culture Goldsmiths College, University of London Contents Summary of Lecture 1 What makes a good Prolog program? What makes a bad Prolog program? More about Prolog clause syntax More about Prolog guery syntax Proof strategy

Summary of Lecture 1 Course structure Examinations, practicals, books Prolog in English Using the Prolog system Fundamental Prolog syntax Predicates Clauses Heads & Bodies Atoms & Numbers Variables Full-stops (periods) Goldsmiths College, University of London 2

What makes a good Prolog program? In order of decreasing importance: correctness & careful design clarity & comments speed (at least for our purposes here) Because we understand the logical meaning of logic programs we can rely on the computer to transform elegant but inefficient programs into ugly (but invisible) efficient ones Goldsmiths College, University of London 3

What makes a bad Prolog program? In no particular order: hacking undesigned code at the terminal using obscure or meaningless variable and predicate names not commenting code abusing the procedural aspects of Prolog to produce logically meaningless code Goldsmiths College, University of London 4

More about Prolog clause syntax (1) Recall that a Prolog program is made up of predicates... and a predicate is made up of clauses. A clause has a head and maybe a body. The head of a clause always takes one of two forms: predname predname( argument1, argument2,...) If the predicate is always true, there is no body, and we finish with a full-stop (period). Goldsmiths College, University of London 5

More about Prolog clause syntax (2) If the predicate is conditional, we connect it to a body of subgoals with the if operator, :- Each subgoal is a Prolog literal... which is either like a head, as before; or a negated literal, written \+ predname \+ predname( argument1,...) Note the space after the \+ operator We will return to \+ later Goldsmiths College, University of London 6

More about Prolog clause syntax (3) We can combine literals in Prolog clauses with the operators, (comma) meaning and conjunction ; (semicolon) meaning or disjunction, (and) binds more strongly than ;, so a, b ; c means that either both a and b are true, or c is true You should never need to use ; in a program because you can express disjunction via multiple clauses. It makes Prolog compilation less effective and your program less clear Goldsmiths College, University of London 7

More about Prolog clause syntax (4) There is also -> (minus, greater than) supposedly meaning implies You should never need to use ->, and it is best avoided because it does not mean the same as logical implies and can therefore beconfusing We can make complex expressions using brackets ( l1( a1, a2 ); l2( a3 )), l3( a1 ) l1( a1, a2 ); ( l2( a3 ), l3( a1 )) l1( a1, a2 ); l2( a3 ), l3( a1 ) Goldsmiths College, University of London 8

More about Prolog query syntax We run a Prolog program by posing a query Prolog query syntax is the same as the syntax of the clause body Literals in queries can be combined with and, or, not and implies, just as in programs Goldsmiths College, University of London 9

Proof strategy Prolog answers queries by attempting to prove them true Suppose we have consulted the ancestor/2 program: ancestor/2 is defined as ancestor( A, B ) :- parent( A, B ). ancestor( A, B ) :- parent( A, C ), ancestor( C, B ). Suppose parent/2 is defined as parent( alan, clive ). parent( clive, dave ). and we ask the question ancestor( alan, dave ). Goldsmiths College, University of London 10

Proof strategy (2) To prove this, prolog starts at the top of the database, and tries to find a predicate called ancestor; Then it looks at each clause in turn, and tries to unify its head with the goal; Once unification is complete, it attempts to prove the literals in the body, in order of appearance Goldsmiths College, University of London 11

Unification Unification works by comparing the structure of terms and literals: First compare the predicate/functor name; Then, for each argument; If both of the two unificands are variables, then make them the same variable, and unification succeeds; Otherwise, if one unificand is a variable, then set it to the value of the other, and unification succeeds; Otherwise, if both unificands are atomic and are the identical, unification succeeds; Otherwise, unification succeeds if the two arguments can be unified Otherwise, unification fails Goldsmiths College, University of London 12

Proof Strategy (3) Prove: ancestor( alan, dave ) Find: ancestor clause 1 Unify: A = alan, B = dave Prove: parent( alan, dave ) FAIL Try again: Find: ancestor clause 2 Unify: A = alan, B = dave Prove: parent( alan, C ) Find: parent clause 1 Unify: C = clive SUCCEED Next goal: Prove: ancestor( clive, dave ) Find: ancestor clause 1 Unify: A = clive, B = dave Prove: parent( clive, dave ) SUCCEED Answer: yes. Goldsmiths College, University of London 13

Summary In this lecture, we have covered: Prolog style More Prolog syntax Prolog standard proof (search) strategy Goldsmiths College, University of London 14