Hgskola I Gvle - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Hgskola I Gvle

Description:

Head is called the 'car' and Tail is called the 'cdr'. ( These ... Horn Clauses provide a particular structure which can be used for most logical expressions ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 44
Provided by: user62
Category:
Tags: gvle | hgskola

less

Transcript and Presenter's Notes

Title: Hgskola I Gvle


1
Högskola I Gävle
  • Logic Programming Languages
  • Brahim Hnich
  • http//www.csd.uu.se/brahim
  • Brahim_at_csd.uu.se

2
Overview
  • Introduction
  • Predicate Calculus
  • Overview of Logic Programming
  • Origins of Prolog
  • Basic Elements of Prolog
  • Deficiencies of Prolog
  • Summary

3
Predicate calculus
  • Provides a formal means for logical expressions
    (I.e. those that evaluate to true or false)

4
Predicate Calculus Logical Connectors
5
Qualifiers
6
Propositions
7
Horn Clauses
8
Declarative Semantics
  • There is a simple way of determining the meaning
    of a statement
  • That meaning does not depend how the statement
    will be used to solve the problem
  • Focuses on what instead of how
  • This means that a statement can be examined
    without looking at the context of the statement
    in the overall program (e.g. variable
    declarations)
  • Is nonprocedural, meaning that the statements
    can be placed in any order.

9
Declaration Semantics Example
  • sort(old_list,new_list) ? permute(old_list,new_lis
    t) ? sorted(new_list)
  • sorted(list) ?? ?j such that 1? j lt n, list(j) ?
    list(j1)
  • (Of course, a definition for permute would also
    be needed.)

10
Basic Elements of Prolog
  • Terms
  • constant
  • variable
  • structure
  • lists (to be discussed later)

11
Basic Elements of Prolog (continued)
  • Constants
  • atoms
  • string of letters, digits, or underscores
    beginning with a lowercase letter (e.g. bill)
  • string of ASCII characters delimited by
    apostrophes (e.g. Hello there)
  • integers
  • Variables
  • string of letters, digits, or underscores
    beginning with a uppercase letter (e.g. Sum)
  • is more like a variable in math than one in CS
  • gets its type at resolution (analogous to
    run-time)

12
Basic Elements of Prolog (continued)
  • Structures
  • functor(parameter list)
  • functor uses the form of the first definition of
    an atom
  • parameter list can be a list of constants,
    variables, or lists
  • are like the propositions in predicate calculus

13
Prolog Statements
  • Facts
  • Rules
  • Goals
  • Note All Prolog statements end with a period.

14
Facts
  • Are part of the Prolog program
  • Look like headless Horn clauses
  • Represent statements that are always true
  • The parameters are usually (but not always)
    constants
  • Examples
  • female(mary).
  • father(bill,jake).
  • sum(X,0,X).

15
Example Facts
16
Rules
  • Are also part of the Prolog program
  • Look like headed Horn clauses
  • Use - instead of ? and a comma instead of a ?
  • Example
  • grandparent(X,Z) - parent(X,Y), parent(Y,Z).

17
Example Rules
18
Prolog Programs
  • Are a series of facts and/or rules.
  • Can be placed in any order, due to the
    nonprocedural nature of logic-based languages
  • Are executed in a Prolog environment using goal
    statements

19
Goals
  • A series of one or more propositions, separated
    by commas
  • Should be thought of as a query
  • If the parameters of the goal are all constants,
    then the answer to the query should be Yes or
    No
  • If the parameters of the goal contain
    variable(s), then the answer to the query should
    be all the values for those variables which
    satisfy the query, or No if no value satisfies
    it.

20
Goals (continued)
  • Examples
  • male(jake). / Answer should be Yes /
  • father(X,shelley). / Answer should be X bill
    /
  • father(X,mary). / Answer should be No /
  • father(bill,X), mother(mary,X). / Answer should
    be X jake and X shelley /

21
Inferencing in Prolog
  • Used to answer goals
  • Each proposition in the goal is checked against
    each fact or rule to see if it can satisfy the
    proposition
  • For rules, the lefthand side must be the same
    type of proposition as the one in the goal.
  • The righthand side is then checked in the same
    way as the goal
  • A depth-first search is employed in the
    resolution process

22
Trace Example
23
Figure 15.1
24
Simple Arithmetic
  • Should not be done with parameters
  • Done with the is operator
  • Example
  • A is B / 17 C
  • Either both sides must have all variables
    instantiated (in which case is acts as a
    relational ) or just the lefthand side is not
    instantiated (which means the lhs receives a
    value)
  • Therefore, the following is never appropriate
  • Sum is Sum Number.

25
Arithmetic Example
26
Trace of Arithmetic Example
27
Recursion
  • Is the only way to do iteration in Prolog
  • Is usually accomplished with at least one fact
    and one rule
  • Example Consider the following mathematical
    definition of factorial
  • 0! 1
  • n! (n-1)! n ? n gt 0
  • Here is the equivalent Prolog statements
  • fact(0,1).
  • fact(N,NFact) - N gt 0, N1 is N-1,
    fact(N1,N1Fact), NFact is N1Fact N.

28
Overview of Prolog Lists
  • The value of a list consists of zero or more
    elements, separated by commas and enclosed in
    square brackets.
  • Example apple, prune, grape, kumquat
  • Each element can be an atom or a list

29
Overview of Prolog Lists (continued)
  • A variable such a L can be used to represent an
    entire list in a statement.
  • The expression E in a statement denotes a
    one-element list.
  • The expression in a statement denotes an
    empty list.

30
Overview of Prolog Lists (continued)
  • The expression Head Tail in a statement
    denotes a list with one or more elements where
    the first element is Head and the rest of the
    list (which may be empty) is Tail.
  • This is how recursion can be used to traverse
    each element of a list.
  • Head is called the car and Tail is called the
    cdr. (These terms are from Lisp.)
  • For example, in apple, prune, grape, kumquat,
    apple is the car, and prune, grape, kumquat is
    the cdr.

31
Example 1
  • Appending two lists together
  • append( , List, List).
  • append( Head List_1, List_2, Head List_3)
    - append(List_1, List_2, List_3).
  • Note how mathematical induction plays a role here!

32
(No Transcript)
33
Example 2
  • Reversing a list
  • reverse( , ).
  • reverse(Head Tail, List) - reverse(Tail,
    Result), append(Result, Head, List).

34
(No Transcript)
35
Example 3
  • Seeing if a list has a particular member
  • member(Element, Element _ ).
  • member(Element, _ List - member(Element,List
    ).
  • The _ is an anonymous variable I.e., we dont
    care what the value is, although a value does
    need to be there.

36
(No Transcript)
37
Deficiencies of Prolog
  • Resolution Order Control
  • Closed World Assumption
  • Negation Problem
  • Intrinsic Limitations

38
Resolution Order Control
  • Depth-first search method can cause infinite
    recursion
  • Example
  • ancestor(X,X).
  • ancestor(X,Y) - ancestor(Z,Y), parent(X,Z).
  • Keeps trying to satisfy the second rule
  • Can be solved by reversing the two propositions
    on the right, but that is against the basic
    nonprocedural philosophy of Prolog

39
Resolution Order Control (continued)
  • The cut operator !
  • Can eliminate backtracking
  • Is useful when a proposition can only be
    satisfied once
  • Form is a,b,!,c,d
  • If c is not satisfied, the statement cannot go
    back and find another possible value for b
  • Example
  • member(Element, Element _ ) - !
  • member(Element, _ List) -
    member(Element,List).
  • The change in the first statement assumes that
    the list consists of unique members.
  • The cut operator also is contrary to the Prolog
    philosophy of nonprocedural programming

40
Close World Assumption
  • If Prolog has insufficient data to answer a
    question, the answer is no, just as it would be
    if it had sufficient data to answer no.

41
The Negation Problem
  • Consider the following statement
  • sibling(X,Y) - parent(M,X), parent(M,Y).
  • Nothing keeps a person from being their own
    sibling!
  • Can be solved with a not proposition
  • sibling(X,Y) - parent(M,X), parent(M,Y), not(X
    Y).
  • However, the not proposition is not a not
    operator (double negation is not allowed), which
    causes some limitations

42
Intrinsic Limitations
  • Prolog is often not efficient
  • Example
  • sorted( ).
  • sorted(x).
  • sorted(x, y list) - x lt y, sorted(y
    list).
  • All permutations of list must be tried until the
    right one is found.

43
Summary
  • Predicate calculus provides a formal means for
    logical expressions (I.e. those that evaluate to
    true or false)
  • Horn Clauses provide a particular structure which
    can be used for most logical expressions
  • Declarative semantics allows both for the focus
    of problem solving to be on what rather than
    how and for nonprocedural programming
  • Prolog uses declarative semantics
  • There are some deficiencies in Prolog, some of
    which are inherent to declarative semantics
Write a Comment
User Comments (0)
About PowerShow.com