Docsity

Programming in Logic - Programming Languages - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Programming in Logic - Programming Languages - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

PROLOG

PRO gramming in LOG ic

A Brief Introduction

PRO-LOG

1975, Phillippe Roussell

Predicate Calculus

Declarative Language

Predicates and Rules

Inference Mechanism

No effective standardization

PROLOG Paradigm

The PROLOG Programmer

Loads facts and rules into the database.

Makes queries to the database to see if a fact is:

in the database or

can be implied from the facts and rules therein

Prolog Database

Facts + Rules

Query

System Interaction

Problem solving in PROLOG

  1. insert facts and rules into the database
  2. ask questions (queries) based on the contents of the database

 Facts

Used to represent unchanging information about objects and their relationships. Only facts in the PROLOG database can be used for problem solving. Insert facts into the database by,  typing the facts into a file and loading (consulting) the file into a running PROLOG system

 Logic Programming

 Logic programming is a form of declarative programming

 A program is a collection of axioms

Each axiom is a Horn clause of the form: H :- B1, B2, ..., Bn. where H is the head term and Bi are the body terms Meaning H is true if all Bi are true

 A user of the program states a goal (a theorem) to be

proven

The logic programming system attempts to find axioms using inference steps that imply the goal (theorem) is true

Basic Proof technique - modus ponens

A -> B

A


B

Prolog Uses backward chaining

More efficient than forward chaining for larger collections of axioms

Interactive (hybrid compiled/interpreted)

Applications: expert systems, artificial

intelligence, natural language

understanding, logical puzzles and games

PROLOG Paradigm

Examples (Facts)

English PROLOG

“A dog is a mammal” isa(dog, mammal).

“A sparrow is a bird” isa(sparrow, bird).

PROLOG Paradigm

Examples (Queries)

English PROLOG

“is a sparrow an animal?” ?- animal(sparrow).

answer: “yes” yes

“is a table an animal?” ?- animal(table).

answer: “no” no

“what is a dog?” ?- isa(dog, X).

answer: “a mammal” X = mammal

PROLOG syntax

Constants

Atoms

 Alphanumeric atoms - alphabetic character sequence starting with a lower case letter Examples: apple a1 apple_cart  Quoted atoms - sequence of characters surrounded by single quotes Examples: ‘Apple’ ‘hello world’  Symbolic atoms - sequence of symbolic characters Examples: & < > * - + >>  Special atoms Examples:! ; [ ] {}

Numbers

 Integers and Floating Point numbers Examples: 0 1 9821 -10 1.3 -1.3E

Prolog syntax

The names of all relationships and objects must

begin with a lower case letter. For example

studies, ali, programming

The relationship is written first and the objects

are enclosed within parentheses and are written

separated by commas. For example studies(ali,

programming)

The full stop character ‘.’ must come at the end

of a fact.

Order is arbitrary but it should be consistent

Example

 Program with three facts and one rule:

rainy(columbo). rainy(ayubia). cold(ayubia). snowy(X) :- rainy(X), cold(X).

 Query and response: ?- snowy(ayubia). yes

 Query and response: ?- snowy(columbo). no

 Query and response: ?- snowy(lahore). No

 ?- snowy(C).

C = ayubia

because rainy(ayubia) and cold(ayubia) are sub-goals that are both true facts in the database

snowy(X) with X=columbo is a goal that fails, because cold(X) fails, triggering backtracking

Logic Representation :

Propositional Calculus

 Propositional Logic

Boolean statements Logic connectives ¬^ ∧ ∨ ⇔ ⇒

Example – a family

Atomic statements

p : Ahmed is father of Belal q : Ahmed is brother of Aslam r : Aslam is uncle of Belal

Complex statements ⇒

Statement Logic ¬p Ahmed is not father of Belal

p ∨ q Ahmed is father of Belal or Ahmed is brother of Aslam

p ∧ q ⇒ r If Ahmed is father of Belal and brother of Aslam then Aslam is uncle of Belal

Prolog Programming

Predicate Calculus  Predicates man(Ahmed) father(Ahmed, Belal) brother(Belal, Chand) brother(Chand, Delawar) owns(Belal, car) tall(Belal) hates(Ahmed, Chand) family()  Formulae ∀ X,Y,Z(man(X) ∧ man(Y) ∧ father(Z,Y) ∧ father(Z,X) ⇒ brother(X,Y))  Variables X, Y and Z

 Constants Ahmed, Belal, Chand, Delawar and car

Prolog code Predicates man(symbol) father(symbol, symbol) brother(symbol, symbol) owns(symbol, symbol) tall(symbol) hates(symbol, symbol) family() Clauses man(ahmed). father(ahmed, belal). brother(ahmed, chand). owns(belal, car). tall(belal). hates(ahmed, chand). family(). brother(X,Y):- man(X), man(Y), father(Z,Y), father(Z,X). Goal brother(ahmed,belal).