Docsity

Logic Programming: Understanding Prolog and Basic Definitions - Prof. Abdelghani Bellaachi, Study notes of Computer Science

Toggle sidebar
bg1
Logic Programming
Table of Contents
1. Application Domains .................................................. 2
2. Definitions................................................................... 2
3. Basic Definitions......................................................... 4
4. Logic Programs........................................................... 5
5. Inferencing Process..................................................... 7
6. Programming Languages vs. IPL & LP.................... 13
7. Drawbacks of Logic programming........................... 13
8. Prolog........................................................................ 14
9. Getting Started .......................................................... 15
10. Create a Prolog program........................................... 17
A. Bellaachia Page: 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Logic Programming: Understanding Prolog and Basic Definitions - Prof. Abdelghani Bellaachi and more Study notes Computer Science in PDF only on Docsity!

Logic Programming

    1. Application Domains Table of Contents
    1. Definitions...................................................................
    1. Basic Definitions.........................................................
    1. Logic Programs...........................................................
    1. Inferencing Process.....................................................
    1. Programming Languages vs. IPL & LP....................
    1. Drawbacks of Logic programming
    1. Prolog........................................................................
    1. Getting Started
    1. Create a Prolog program

1. Application Domains - Logic programming language application areas include natural language processing, expert systems, specifications checking, theorem proving, and control systems (among others). 2. Definitions - Logic programming languages, also called declarative languages, differ substantially from the other programming languages we have discussed. - Declarative rather that procedural : Only specification of results are stated (not detailed procedures for producing them) - Most importantly, logic programs do not specify the execution sequence as in most in the case of other language paradigms. User specifies the specifications of a solution and the computer derives the execution sequence for that solution:

Let us have airline flight information of the form:

flight(flight_number, from_city, to_city, departure_time, arrival_time)

Then all the flights from Washington DC to Santa Clara can be specified as either direct flights or as flights with an intermediate stop:

3. Basic Definitions - Horn clause: ƒ Horn clause - can have only two forms o Headed : single atomic proposition on left side o Headless : empty left side (used to state facts )

ƒ A Horn clause is an IF clause and not iff, formally,

A0 if A1 and A2 and A3 and … and An

Each Ai is a simple assertion of the form Ri(…) where Ri is a relation name.

Informally, this clause means that, if A1, …, and An are all true, then we can infer that A0 is also true. But, we cannot conversely infer that A0 is false just because some Ai turns out to be false.

  • A logic program is a collection of Horn clauses.

4. Logic Programs - Basic elements: ƒ Term: - a constant, variable, or structure ƒ Constant: - an atom or an integer ƒ Atom: - symbolic value of Prolog ƒ Atom consists of either: - A string of letters, digits, and underscores beginning with a lowercase letter - a string of printable ASCII characters delimited by apostrophes

  • Statement: ƒ Fact Statements ƒ Rule Statements ƒ Query (or Goal) Statements
  • Fact statements: ƒ Concrete relations among objects ƒ Used for the hypotheses ƒ Structure: ƒ functor(parameter list)

ƒ Headless Horn clauses o student(Paul). o junior(Mary).

5. Inferencing Process - User query: ƒ Computation in a logic program consists of testing a given assertion (query) A. ƒ The testing of a query A can be implemented by a process called matching, satisfying, or resolution. ƒ The facts and the rules of the program are used to determine which substitutions for variables in the query (called unification) are consistent with the information in the database. ƒ If we can infer from the clauses of the program that A is true, then we say that A succeeds. Otherwise, we say that A fails. Note that this does not mean that A is false. ƒ For example, Prolog as an interpreter, ¾ prompts the user for an input query and ¾ outputs: ™ the truth value (“yes”) or falsity (“no”) of that query and ™ an assignment to the variables of the query that make the query true (that unifies the query). - If a goal is a compound proposition, each of the facts is a subgoal.

  • To prove a goal is true, must find a chain of inference rules and/or facts. For query Q:

B :- A C :- B … Q :- P

  • Process of answering a query is called matching, satisfying, or resolution.
  • Resolution types: ƒ Bottom-up resolution, forward chaining: ¾ Begin with facts and rules of database and attempt to find sequence that leads to goal ¾ works well with a large set of possibly correct answers ƒ Top-down resolution, backward chaining: ¾ begin with goal and attempt to find sequence that leads to set of facts in database ¾ works well with a small set of possibly correct answers ƒ Prolog implementations use backward chaining
  • Resolution uses Unification:

ƒ Unification: o It is the process of finding values for variables in propositions that allows matching process to succeed o To unify two expressions U and V, we need to find substitutions for the variables occurring in U and V that makes the two expressions identical. o Generally, it is denoted by σ and written as Uσ = Vσ

ƒ Example 1: o To unify the two expressions:

R1(X,John) with f(g(John), Z)

Bind X to g(John) and Z to John to get f(g(John), John) as the unified instance of both expressions.

ƒ If the database contains the fact ParentOf(John, Mary) then the unification of the following query: ParentOf(X,Mary)=ParentOf(John,Y) Would be ParentOf(John,Mary)

where X is substituted by John and Y is substituted by Mary.

ƒ Example 2: grandmother(X,Y) :- mother(X,Z), mother(Z,Y). grandfather (X,Y) :- father (X,Z), father(Z,Y). mother(anna,peter). mother(anna,clara). father(joe,peter). father(jim,clara). mother(mary,anna). father(tom,joe).

Answer: ?- grandfather(tom,X). ?- grandmother(X,pet er).

  • For example, the programmer supplies a list of facts and rules: ƒ Rules: 9 Eagle has feather. 9 Eagle has beak. 9 Eagle has two legs. 9 Betty eats vegetables. 9 Chicken eats seeds. 9 Chicken has two legs. ƒ Facts: 9 If someone eats meat then it is a carnivore. 9 If someone has feather then it is a bird. 9 If someone eats X then X is food.

6. Programming Languages vs. IPL & LP

y In a logic programming language, the programmer does not identify the computations or functions necessary to derive an answer.

  • The great advantage of a declarative language is that the programmer only needs to supply the information that is known (facts and rules), and ask questions - NOT worry about the ways the rules must be applied to answer those questions.
  • From a programmer's perspective, this is a much more high-level approach to problem solving than in imperative or functional languages. 7. Drawbacks of Logic programming
  • The query process can be quite slow and inefficient
  • The programmer must supply sufficient facts
  • Rules to allow the engine to answer the queries
  • The programmer must understand how to phrase the facts, rules, and queries

8. Prolog - University of Aix-Marseille ƒ Natural language processing - University of Edinburgh ƒ Automated theorem proving - Prolog (PROgramming LOGic) (1972): was the first widespread formal logic language. It is a non-procedural, goal oriented language. - Computation takes place by trying to infer the response to queries through accessing a database of facts and rules. - The user supplies the facts, rules, and queries (expressed in predicate calculus), while the computer carries out resolution to try and infer the replies. - By far the dominant logic programming language is Prolog, so that is the language we will examine in some detail. - Prolog is based predominantly on the Horn clause, A :- B, C, D, ...

This tells us A is true if B is true and C is true and ... etc. It does not tell us the circumstances under which a is false

function call, we give the relationship name first, then in brackets the two entities that are related, e.g. eats(wilma,meat), eats(wilma,vegetables) ƒ rules: we can establish rules that describe how one relationship or property is true if some other properties or conditions are true. For example, a rule could state that if someone eats meat and they eat vegetables then they are an omnivore.

  • Rules in Prolog have three parts:

ƒ The result : this is the relationship or property which results from the rule, and goes on the left hand side of our rule, e.g. omnivore(Individual) Note that the entity here will usually be a variable.

ƒ The "if" symbol :

:- which separates the result from

the clauses

ƒ The clauses: the set of relationships and properties which determine if the rule is to be applied, this goes on the right hand side of the rule.

  • There are several logic symbols which can be used in the right hand side: ƒ The comma represents logical AND, ƒ The semi-colon represents logical OR, ƒ The word NOT in some ways functions like logical NOT, but there are some important differences that we'll consider later 10. Create a Prolog program
  • Create our set of properties, relationships, and rules. Typically we will enter these into a text file ending with the extension ".pl" (e.g. "flint.pl").
  • Start Prolog and load our database from the file.
  • Begin issuing queries based on the loaded information.
  • From this point on, everything you type is treated as a query, not a new fact or rule:

eats(fred,vegetables). is now asking whether or not fred eats vegetables, not stating it as a new fact.

  • Example: (http://intra.engr.uark.edu/~wessels/lang/spring00/prolog.html) 9 Create a file, called flint.pl, containing the following facts and rules: eats(fred,meat). eats(wilma,meat). eats(betty,vegetables). eats(wilma,vegetables). eats(barney,meat). eats(barney,vegetables).

carnivore(Individual) :- eats(Individual,meat). omnivore(Individual) :- eats(Individual,meat),eats(Individual,vegetables). food(Thing) :- eats(Individual,Thing).

9 Once that is created, we can load the database of facts and rules from the file, then begin issuing queries and getting answers. 9 In the example below assume > is the prompt:

['flint.pl']. flint.pl compiled

Yes

food(meat). Yes

carnivore(fred). Yes

food(dirt). No

omnivore(Individual).

Individual = wilma Yes

  • How queries are answered:

ƒ Suppose we are given the query "is fred an omnivore?" omnivore(fred).

ƒ In trying to solve this query, Prolog does the following:

  • search for all the relationships which match the goal, i.e. everything in our database that begins with omnivore(...)
  • if we encounter a fact in our database that explicitly states omnivore(fred). then we can quit and return Yes as soon as we encountered it.
  • if we encounter a rule of the form omnivore(...variable...) :- ... then we can say fred is an omnivore if we can substitute fred for the variable (throughout the rule) and still satisfy all the clauses on the right side of the rule
  • In the case of our example, the rule with this form is omnivore(Individual) :- eats(Individual,meat),eats(Individual,vegetables).

and after substituting we want to check

if eats(fred,meat) AND eats(fred,vegetables).