Logic Languages. Hwansoo Han

Similar documents
10. Logic Programming With Prolog

Chapter 16. Logic Programming Languages

Logic Programming Languages

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

Chapter 16. Logic Programming Languages ISBN

Introduction to predicate calculus

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

Logic Programming: The Prolog Language

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

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Chapter 16. Logic Programming Languages ISBN

Prolog. Logic Programming vs Prolog

will take you everywhere.

Prolog. Intro to Logic Programming

Prolog Programming. Lecture Module 8

Lecture 16: Logic Programming in Prolog

Lecture 4: January 12, 2015

Advanced Logic and Functional Programming

INTRODUCTION TO PROLOG

Logic Programming: Prolog

Implementação de Linguagens 2016/2017

Operational Semantics

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

First-Order Logic (FOL)

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

Part I Logic programming paradigm

Programming Languages Third Edition

Constraint Solving. Systems and Internet Infrastructure Security

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

Logic Programming and Resolution Lecture notes for INF3170/4171

Logic Programming (PLP 11) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not

Fundamentals of Prolog

Logical reasoning systems

Declarative Programming Prolog CS360

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

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

Logic (or Declarative) Programming Foundations: Prolog. Overview [1]

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

Predicate Calculus. Problems? Syntax. Atomic Sentences. Complex Sentences. Truth

The Logic Paradigm. Joseph Spring. 7COM1023 Programming Paradigms

LOGIC AND DISCRETE MATHEMATICS

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

COP4020 Programming Languages. Prolog Chris Lacher Based on Robert van Engelen

What is Prolog? - 1. A Prolog Tutorial. What is Prolog? - 2. Prolog Programming. » Declaring some facts about objects and their relationships

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

CS 381: Programming Language Fundamentals

COP4020 Programming Languages. Logic programming and Prolog Prof. Xin Yuan

Introduction to Logic Programming in Prolog 1 / 39

COMP4418 Knowledge Representation and Reasoning

PROLOG PROgramming in LOGic

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

CS 321 Programming Languages and Compilers. Prolog

Introduction to Prolog

Logic as a Programming Language

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

Artificial Intelligence Course Sharif University of Technology

COP4020 Programming Languages. Prolog Prof. Robert van Engelen

6.034 Notes: Section 11.1

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

Logic Programming. Chapter 13

Range Restriction for General Formulas

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

Automated Reasoning PROLOG and Automated Reasoning 13.4 Further Issues in Automated Reasoning 13.5 Epilogue and References 13.

Week 7 Prolog overview

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

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Overview. CS389L: Automated Logical Reasoning. Lecture 6: First Order Logic Syntax and Semantics. Constants in First-Order Logic.

Logic Programming Paradigm

References. Topic #16: Logic Programming. Motivation. What is a program? Prolog Program Structure

Introducing Prolog. Chapter THE IDEA OF PROLOG

Topic B: Backtracking and Lists

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

Logic and its Applications

For Wednesday. No reading Chapter 9, exercise 9. Must be proper Horn clauses

Towards a Logical Reconstruction of Relational Database Theory

Chapter 9: Constraint Logic Programming

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

Defining Datalog in Rewriting Logic

Theorem proving. PVS theorem prover. Hoare style verification PVS. More on embeddings. What if. Abhik Roychoudhury CS 6214

Knowledge Representation and Reasoning Logics for Artificial Intelligence

Lecture 9: A closer look at terms

PROgramming in LOGic. Part II. By Forrest Pepper 12/7/07

Markov Logic: Representation

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

Topic 1: Introduction to Knowledge- Based Systems (KBSs)

Chapter 2 & 3: Representations & Reasoning Systems (2.2)

Knowledge Representation. CS 486/686: Introduction to Artificial Intelligence

Learning Rules. Learning Rules from Decision Trees

Software Paradigms (Lesson 7) Logic Programming & Software Engineering

CS 561: Artificial Intelligence

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

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

Roman Barták Charles University, Prague (CZ)

An Experimental CLP Platform for Integrity Constraints and Abduction

CILOG User Manual Version 0.14

STRUCTURES AND STRATEGIES FOR STATE SPACE SEARCH

An introduction to logic programming with Prolog

LING/C SC/PSYC 438/538. Lecture 20 Sandiway Fong

Programming Paradigms

Functional Logic Programming. Kristjan Vedel

Transcription:

Logic Languages Hwansoo Han

Logic Programming Based on first-order predicate calculus Operators Conjunction, disjunction, negation, implication Universal and existential quantifiers E A x for all x... x for some x... Statements Sometimes true, sometimes false, often unknown Axioms - assumed true Theorems - provably true Hypotheses (goals) - things we'd like to prove true

Logic Programming Concepts If we make certain restrictions on the format of statements you can prove theorems mechanically We insist that all statements be in the form of Horn Clauses The language implementation finds a collection of axioms and inference steps that imply the goal A Horn Clause consists of a Head and a Body H B 1, B 2,... B n A head, H, is a consequent term, a body consists of terms, B i Meaning is H if B 1, B 2,... and B n

Logic Programming Concepts (cont d) Resolution To derive a new statement, a logic programming system combines existing statements and deduces statements Unification C A, B E C, D E A, B, D During resolution, free variables may acquire values through unification with expressions in matching terms flowery(x) rainy(x) rainy(suwon) flowery(suwon)

Horn Clauses in Prolog A statement in the form of Horn clause Means that the conjunction of the terms in the body implies the head (H B 1, B 2,... B n means H if B 1, B 2,... and B n ) A clause with an empty body is called a fact A clause with both sides is a rule A clause with an empty head is a query, or top-level goal The Prolog interpreter has a collection of facts and rules in its database Facts are axioms - things the interpreter assumes to be true Using inferences with rules, resolve the query

Prolog Prolog can be declarative or imperative We ll emphasize the declarative semantics for now, because that's what makes logic programming interesting We'll get into the imperative semantics later Prolog allows you to state a list of axioms Then you pose a query (goal) and the system tries to find a series of inference steps (deductions and unifications) that allow it to prove your query starting from the axioms

Atoms, Variables, Structures in Prolog Atoms Identifier beginning with a lowercase letter, a sequence of punctuation characters, or a quoted character string Variables foo my_const + Hi, Mom Identifier beginning with a uppercase letter Structures 7 Foo My_var X Consists of an atom (functor) and a list of arguments Arity : number of arguments (pronounced air-i-tee) rainy(seoul) teach(prof, pl) bin_tree(foo, bin_tree(bar, glarch)) Logically, structures are predicates (part of clause except the subject)

Facts, Rules in Prolog Facts Horn clauses without right sides sunny(suwon). Rules are theorems that allow the interpreter to infer things To be interesting, rules generally contain variables employed(x) :- employs(y,x). can be read: for all X, X is employed if there exists a Y such that Y employs X Note the direction of the implication A :- B means A B

Variables in Prolog Scope of a variable is the clause in which it appears Variables whose first appearance is on the left hand side of the clause have implicit universal quantifiers (for all, ) Variables whose first appearance is in the body of the clause have implicit existential quantifiers (there exist, ) Another example grandmother(a, C) :- mother(a, B), mother(b, C). for all A, C [A is the grandmother of C if there exists a B such that A is the mother of B and B is the mother of C]. E A

Queries in Prolog To run a Prolog program One asks the interpreter a question by stating a theorem which the interpreter tries to prove A query is entered with a special?- The interpreter says yes or no If your predicate contains variables, the interpreter prints the values which make the predicate true rainy(seoul). rainy(suwon).?- rainy(c). C = seoul ; C = suwon ; No % find an answer which comes first in database % typing a semicolon(;) gives the next answer % gives No if no more answers

Resolution in Prolog Resolution principle If C 1 and C 2 are Horn clauses and the head of C 1 matches with one of the terms in the body of C 2, we can replace the term in C 2 with the body of C 1 A B, C X A, D X B, C, D takes(jane, cs320). takes(john, cs320). classmate(x, Y) :- takes(x, Z), takes(y, Z). X=jane, Z = cs320 classmate(jane, Y) :- takes(jane, cs320), takes(y, cs320) Resolution Principle classmate(jane, Y) :- takes(y, cs320). 11

Unification in Prolog Unification The pattern matching process used to associate a variable with a specific value (e.g., associate X with jane) A variable is given a value as a result of unification. Then we say the variable is instantiated Unification rules A constant unifies only with itself Two structures unify if and if only they have the same functor and the same arity, and the corresponding arguments unify recursively A variable unifies with anything (If the other thing has a value, the variable is instantiated) 12

Equality in Prolog Equality is defined in terms of unifiability = (A, B) succeeds if and if only A and B can be unified We can also write A = B, instead of =(A, B)?- a = a.?- foo(a,b) = foo(x,b). Yes X = a ;?- a = b. No No?- foo(a,b) = foo(a,b). Yes?- X = a. X = a ; % unified only once with constant No 13

Arithmetic in Prolog Arithmetic operators are predicates not functions +(2, 3) or 2 + 3 is a structure with arity 2?- (2 + 3) = 5. No % the structure (2 + 3) will not unify with 5 Built-in predicate is 14 is unifies its first argument with the arithmetic value of the second argument?- is(x, 1+2).?- 1+2 is 4-1. X = 3 No % the first arg (1+2) is?- X is 1+2. % already instantiated X = 3?- X is Y. ERROR % the second arg must % be already instantiated

Prolog Interpreter The interpreter works by Backward Chaining Begins with the thing to prove and works backwards looking for things that would imply it, until it gets to facts Work forward from the facts In theory, it is possible to see if any of the things you can prove from the facts But can be very time-consuming Fancier logic languages use both kinds of chaining with special smarts or hints from the user to bound the searches

Prolog Interpreter Steps to find the goal Starts at the beginning of your database (which contains facts and rules in the order specified) Searches facts or rules with which to the current goal can be unified If it finds a fact, great; it succeeds If it finds a rule, it attempts to satisfy the terms in the body of the rule in depth first and left-to-right The term to satisfy is the subgoal and start at the beginning of the database and searches facts or rules again If a subgoal fails, returns to the previous goal and attempts to satisfy in a different way (backtracking)

Backtracking Search in Prolog facts, rules?- snowy(c). % goal 17

Search Tree in Prolog Search tree The root is the top-level goal and the leaves are facts The children of the goal (or the subgoal) are all the facts and the heads of the rules with which the goal can unify The interpreter does an OR across them: one of them must succeed in order for the goal to succeed The children of a head are the terms in the body of the rule The interpreter does an AND across these: all of them must succeed in order for parent to succeed The overall search tree then consists of alternating AND and OR levels

Search Order in Prolog Prolog is not purely declarative The ordering of the database and the left-to-right pursuit of sub-goals gives a deterministic imperative semantics to searching and backtracking Changing the order of statements in the database can give you different results It can lead to infinite loops It can certainly result in inefficiency

Infinite Regression in Prolog?- path(a, a).

SWI-Prolog Prolog interpreter in Linux Install SWI-prolog from Interpreters $ pl?- [user]. : factorial(0,1). : factorial(a,b) :- A > 0, C is A-1, factorial(c, D), B is A*D. : <ctrl-d> Yes?- factorial(5, What). What = 120 ; No?- [ myprog.pl ]. % load a file which contains facts and rules?- halt. $ 21