Brief Introduction to Prolog

Similar documents
TDDD08 Tutorial 1. Who? From? When? 6 september Victor Lagerkvist (& Wªodek Drabent)

COMP2411 Lecture 10: Propositional Logic Programming. Note: This material is not covered in the book. Resolution Applied to Horn Clauses

Computational Logic Fundamentals (of Definite Programs): Syntax and Semantics

Knowledge base (KB) = set of sentences in a formal language Declarative approach to building an agent (or other system):

Advanced Topics in LP and FP

Prolog: Resolution, Unification, & Cuts

Resolution (14A) Young W. Lim 8/15/14

Advanced Topics in LP and FP

Prolog and Logic Programming. CS152 Chris Pollett Dec. 3, 2008.

Chapter 16. Logic Programming. Topics. Logic Programming. Logic Programming Paradigm

How to determine if a statement is true or false. Fuzzy logic deal with statements that are somewhat vague, such as: this paint is grey.

CS 380: ARTIFICIAL INTELLIGENCE

Unification and occur check. The programming language Prolog

Foundations of Logic Programming

ITEC Prolog Programming

CS 380: ARTIFICIAL INTELLIGENCE PREDICATE LOGICS. Santiago Ontañón

7.5.2 Proof by Resolution

First Order Logic (FOL)

Top-Down Execution CS240B Notes

Extensions to the Logic of All x are y: Verbs, Relative Clauses, and Only

A Logic Primer. Stavros Tripakis University of California, Berkeley. Stavros Tripakis (UC Berkeley) EE 144/244, Fall 2014 A Logic Primer 1 / 35

Definite Logic Programs

Strong AI vs. Weak AI Automated Reasoning

Propositional and Predicate Logic - V

Artificial Intelligence Chapter 7: Logical Agents

Chapter 2: Basic Notions of Predicate Logic

A Logic Primer. Stavros Tripakis University of California, Berkeley

CS 730/730W/830: Intro AI

Declarative Programming Techniques

Prolog. Resolution (for beginners?) Resolution (cont.) Resolution (cont.) Unification. Resolution (still cont.) Resolution & Unification

[1] Describe with examples, the three inference rules involving quantifiers

COMP9414: Artificial Intelligence Propositional Logic: Automated Reasoning

Self-assessment due: Monday 3/18/2019 at 11:59pm (submit via Gradescope)

CSC242: Intro to AI. Lecture 12. Tuesday, February 26, 13

Declarative Programming Techniques

Logic. Knowledge Representation & Reasoning Mechanisms. Logic. Propositional Logic Predicate Logic (predicate Calculus) Automated Reasoning

Predicate Calculus. Lila Kari. University of Waterloo. Predicate Calculus CS245, Logic and Computation 1 / 59

First-Order Theorem Proving and Vampire. Laura Kovács (Chalmers University of Technology) Andrei Voronkov (The University of Manchester)

The non-logical symbols determine a specific F OL language and consists of the following sets. Σ = {Σ n } n<ω

Propositional Resolution Introduction

CS 730/830: Intro AI. 3 handouts: slides, asst 6, asst 7. Wheeler Ruml (UNH) Lecture 12, CS / 16. Reasoning.

ILP = Logic, CS, ML Stop counting, start reasoning

Logic: Top-down proof procedure and Datalog

Propositional Reasoning

Lecture Notes in Machine Learning Chapter 3: Languages for learning

Propositional Logic: Review

Logic & Computation. Autonomous Systems. Sistemi Autonomi. Andrea Omicini

Artificial Intelligence Chapter 9: Inference in First-Order Logic

Logical Inference. Artificial Intelligence. Topic 12. Reading: Russell and Norvig, Chapter 7, Section 5

An Early (1971) Conversation. Logic Programming (PLP 11.3) Horn Clauses Introduction to Prolog: Resolution, Unification.

Logical agents. Chapter 7. Chapter 7 1

Introduction to Metalogic

Title: Logical Agents AIMA: Chapter 7 (Sections 7.4 and 7.5)

Logical reasoning - Can we formalise our thought processes?

Rigorous Software Development CSCI-GA

First Order Logic Semantics (3A) Young W. Lim 8/9/17

Lecture Notes on Logic Programming

First-order Logic (Session 1) Anthony W. Lin Yale-nus College, Singapore

Logic Programming Theory Lecture 7: Negation as Failure

Overview of Topics. Finite Model Theory. Finite Model Theory. Connections to Database Theory. Qing Wang

Chapter 7 R&N ICS 271 Fall 2017 Kalev Kask

Artificial Intelligence

07 Equational Logic and Algebraic Reasoning

First Order Logic (FOL)

Intelligent Agents. Formal Characteristics of Planning. Ute Schmid. Cognitive Systems, Applied Computer Science, Bamberg University

Advanced DB CHAPTER 5 DATALOG

Foundations of Artificial Intelligence

CSC384: Intro to Artificial Intelligence Knowledge Representation II. Required Readings: 9.1, 9.2, and 9.5 Announcements:

11. Automata and languages, cellular automata, grammars, L-systems

7. Propositional Logic. Wolfram Burgard and Bernhard Nebel

Intelligent Agents. First Order Logic. Ute Schmid. Cognitive Systems, Applied Computer Science, Bamberg University. last change: 19.

Propositional Logic: Methods of Proof (Part II)

Logic Programming (PLP 11) Predicate Calculus, Horn Clauses, Clocksin-Mellish Procedure

CS 520: Introduction to Artificial Intelligence. Review

Logical agents. Chapter 7. Chapter 7 1

6.825 Techniques in Artificial Intelligence. Logic Miscellanea. Completeness and Incompleteness Equality Paramodulation

Methods of Partial Logic for Knowledge Representation and Deductive Reasoning in Incompletely Specified Domains

Inference in first-order logic

Introduction to Logic in Computer Science: Autumn 2006

Logic + control: An example of program construction and verification

Composing intensions. Thomas Ede Zimmermann (Frankfurt) University of Hyderabad March 2012

Logic Programming. Adrian Crăciun. November 22, 2013

Logical Inference 2 rule based reasoning

Revised by Hankui Zhuo, March 21, Logical agents. Chapter 7. Chapter 7 1

THE LANGUAGE OF FIRST-ORDER LOGIC (FOL) Sec2 Sec1(1-16)

First Order Logic Semantics (3A) Young W. Lim 9/17/17

TDT4136 Logic and Reasoning Systems

Logic (3A) Young W. Lim 12/5/13

AI Programming CS S-09 Knowledge Representation

Convert to clause form:

First-Order Theorem Proving and Vampire

Artificial Intelligence

Accumulators More on Arithmetic and Recursion

Logic: Bottom-up & Top-down proof procedures

Inference in First-Order Logic

Deliberative Agents Knowledge Representation I. Deliberative Agents

Logic (3A) Young W. Lim 10/29/13

CS1021. Why logic? Logic about inference or argument. Start from assumptions or axioms. Make deductions according to rules of reasoning.

Lecture Notes on Logic Programming

Propositional, First-Order And Higher-Order Logics: Basic Definitions, Rules of Inference, and Examples

Transcription:

Brief to Prolog Joana Côrte-Real jcr@dcc.fc.up.pt CRACS & INESC TEC Faculty of Sciences University of Porto University of Aizu 5th December 2014 1 / 27

Overview 1 to Prolog Prolog Syntax Tutorial 1 2 Lists Trees Tutorial 2 3 Tutorial 3 4 2 / 27

to Prolog Prolog Syntax Tutorial 1 Section 1 3 / 27

Programming paradigms to Prolog Prolog Syntax Tutorial 1 Imperative translation from machine language to user commands Object object-oriented, versatile and recent Declarative detachment between program s goal and execution details Functional concerned with recursion, pattern matching,... Logic focus on automatically reasoning about knowledge in the program 4 / 27

to Prolog to Prolog Prolog Syntax Tutorial 1 Prolog represents Horn Clauses - which are a subset of First Order Logics - where each clause can have at most one positive literal in the head. head: body. A Prolog program is a set of facts and/or rules defining relations between entities. Facts represent relations which are assumed to be true (axioms) Rules can be true or false, depending on other relations in the program A Prolog program is resolved by computing the consequents of rules. 5 / 27

Prolog syntax 1 to Prolog Prolog Syntax Tutorial 1 Characters Letters and numbers Terms can be variables Person, Father constants mary, The Family compound family( mary&tom, date(25, feb, 1954)) Symbols :- ;,. Comments can be used for line %Comment text /* Also a comment */ 6 / 27

Prolog syntax 2 to Prolog Prolog Syntax Tutorial 1 Operators + - * / Unification = finds a set of variable substitutions that make two terms exacty the same. Arithmetic Prolog does not evaluate mathematical expressions unless the operator is is used numbers is > < =\= terms == \== 7 / 27

Family - facts to Prolog Prolog Syntax Tutorial 1 %F a c t s T1 m o t h e r o f ( mary, john ). m o t h e r o f ( mary, anne ). f a t h e r o f ( tom, anne ). f a t h e r o f ( tom, john ). %Query T1? m o t h e r o f ( mary, Person ). 8 / 27

Observations to Prolog Prolog Syntax Tutorial 1 There are two predicates in this example: mother of/2 and father of/2. pred/n represents predicate pred(arg1, arg2,..., argn), with arity of N arguments. These predicates have no body and so they are called facts. Facts can be seen as a multi-relational database in Prolog. 10 / 27

Lists Trees Tutorial 2 Section 2 11 / 27

Data structures: List Lists Trees Tutorial 2 A few examples: []: empty list. [ the,men,[like,to,fish]] [a,v1,b,[x,y]] List is composed of: [Head Tail] Head: first element of the list (can be of any type). Tail: remaining elements as a list. List Head Tail [a,b,c] a [b,c] [a] a [] [] no head no tail 12 / 27

Equality of Lists Lists Trees Tutorial 2 Unifications can happen inside lists The empty list does not have head or tail This is used as a stop criterion in list recursion Examples: [X,Y,Z] = [john,likes,fish] X=john, Y=likes, Z=fish [cat] = [X Y] X=cat, Y=[] [] = [X Y] will always fail 13 / 27

Data structures: Tree Lists Trees Tutorial 2 Tree below can be represented as either a+b*c or +(a,*(b,c)) + / \ a * / \ b c Trees can be used to represent concept such as: sentence(noun(john),verb phrase(verb(likes),noun(mary))) sentence / \ noun verb_phrase / / \ john verb noun likes mary 14 / 27

Family - rules Lists Trees Tutorial 2 %F a c t s T2 f a t h e r o f ( tom, b i l l ). m o t h e r o f ( mary, j a n e ). %Rule T2 s i b l i n g ( Person1, Person2 ) : m o t h e r o f ( Mother, Person1 ), m o t h e r o f ( Mother, Person2 ), Person1\==Person2. s i b l i n g ( Person1, Person2 ) : f a t h e r o f ( Father, Person1 ), f a t h e r o f ( Father, Person2 ), Person1\==Person2. %Query T2? s i b l i n g ( anne, Person ). 15 / 27

Observations Lists Trees Tutorial 2 findall/3 predicate gathers all possible solutions for the query in argument 2 in a list. Procedural interpretation: to solve sibling/2, predicates mother of/2 and father of/2 must be evaluated first. The resolution mechanism (SLD resolution) builds an execution tree. 17 / 27

Execution tree for sibling Lists Trees Tutorial 2 18 / 27

Tutorial 3 Section 3 19 / 27

Tutorial 3 Obtaining multiple solutions in Prolog in Prolog can be used to obtain multiple solutions for a goal (predicate fails vs user-induced failure). When Prolog is computing a goal, every choice it makes is stored as a choice point. If a given goal fails, it can be backtracked (choices undone) until the previous choice point is restored. From there, Prolog starts looking for a new alternative for solving the goal. 20 / 27

Family - recursive Tutorial 3 %F a c t s T3 f a t h e r o f ( r o g e r, tom ). f a t h e r o f ( greg, r o g e r ). %Rule T3 m a l e a n c e s t o r ( Person1, Person2, 1 ) : f a t h e r o f ( Person1, Person2 ). m a l e a n c e s t o r ( Person1, Person2, L e v e l ) : f a t h e r o f ( Person1, NewPerson ), m a l e a n c e s t o r ( NewPerson, Person2, NewLevel ), L e v e l i s NewLevel +1. %Query T3? m a l e a n c e s t o r (P, anne, 2 ). 21 / 27

Observations Tutorial 3 Declarative interpretation of query: are greg and anne related through the male line? male ancestor/3 can solve both the level of the relation and find a person who is related to someone else. Note that recursion must be made with new variables every time: NewLevel, NewPerson. 23 / 27

Tutorial 3 Execution tree for male ancestor 24 / 27

Section 4 25 / 27

s Prolog allows for an almost direct match between a FOL algorithm and its execution syntax. Because Prolog is based on FOL, if the facts are true, then the program will produce true results. Prolog s declarative nature is particularly suited to build and consult multi-relational data. There are many Prolog systems in the literature with different features such as Yap Prolog 1, SWI-Prolog 2, SICStus Prolog 3, etc. 1 www.dcc.fc.up.pt/ vsc/yap/documentation.html 2 www.swi-prolog.org 3 sicstus.sics.se 26 / 27

Thank you Joana Côrte-Real 27 / 27