Docsity

Prolog Programming: Axioms, Goals, and Unification at UNC Chapel Hill - Prof. Aaron Block, Study notes of Programming Languages

Partial preview of the text

Download Prolog Programming: Axioms, Goals, and Unification at UNC Chapel Hill - Prof. Aaron Block and more Study notes Programming Languages in PDF only on Docsity!

Lecture 20: Logic Languages

COMP 524 Programming Language Concepts Aaron Block April 10, 2007 Based on notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Axioms and Goals

  • Logical Programming is based on a series of axioms.
    • Axioms define the language
    • After the axioms have been stated the user states a goal and the logical language attempts to find a series of axioms to satisfy the goal. 2 C←A,B D←C

D←A,B

Prolog

  • A prolog interpreter runs in the context of a database of clauses.
  • Each clause is composed of terms, which may be constants, variables, or structures.
  • An atom is Prolog is an identifier beginning with a lowercase letter, a sequence of “punctuation” character, or a quoted character string: 4 a. b. +. ‘Hi, Mom’

Numbers

  • Numbers resemble integers and floating point constants of other languages 5

Structures

  • Structures consist of an atom called the functor and a list of arguments.
  • The parentheses must come directly after the atom (no white space) 7 apple(bar, qud). bin_tree(foo, bin_tree(pear,glarch)

Clauses

  • Clauses are classified as factors or rules each of which ends with a period.
  • A fact is a Horn clause w/o a right-hand side.
  • A rule has a right hand side. 8 man(adam). looser(X):-not_unc(X),basketball(X)

Resolution Principle

  • Resolution Principle states that for two Horn clauses A and B. If the head of A matches one of the terms in B, then the body of A can replace the term in B. 10 takes(jane_doe,comp524). takes(jane_doe,comp121). takes(john_smith,comp524). takes(john_smith,art101). classmates(X,Y):-takes(X,Z),takes(Y,Z). X= jane_doe. Z=comp524. classmates(jane_doe,Y):-takes(Y,comp524).

Unification

  • Unification is the process of pattern-matching process used to associate a variable with values. Variables that are given values as a result of unification are said to be instantiated. 11 ... classmates(X,Y):-takes(X,Z),takes(Y,Z). ?-classmates(john_smith,jane_doe).

Arithmetic

  • Can’t unify arithmetic 13 ?-(2+3) = 5. no ?-X is 1+2. X= ?-1+2 is 4- no ?-X is Y % Y isn’t instantiated ?-Y is 1+2, X is Y. X= Y=

Search/Execution Order

  • Two approaches
    • Start with existing clause and work forward (forward chaining)
    • Start with the goal and work backwards (Backwords chaining)
    • Backward chaining is faster 14

Infinite Recursion

  • Prolog takes a depth first approach for search states, this can cause problems if not careful. 16

Infinite recursion

17 edge(a,b).edge(b,c).edge(c,d). edge(d,e).edge(b,e).edge(d,f). path(X,X). path(X,Y):-edge(Z,Y),path(X,Z). edge(a,b).edge(b,c).edge(c,d). edge(d,e).edge(b,e).edge(d,f). path(X,X). path(X,Y):-path(X,Z),edge(Z,Y). edge(a,b).edge(b,c).edge(c,d). edge(d,e).edge(b,e).edge(d,f). path(X,Y):-path(X,Z),edge(Z,Y). path(X,X).

Unification rules

19 ?-a=a. yes ?-a=b. no

  • A constant unifies only with itself

Unification rules

  • Two structures unify iff they have the same functor and the same number of arguments, and the corresponding arguments unify recursively 20 ?-foo(a,b)= foo(a,b). yes ?-foo(a,b)=foo(X,b). X = a; no