PROLOG FUNDAMENTALS Module 14 2 COP 4020 Programming

  • Slides: 15
Download presentation
PROLOG FUNDAMENTALS Module 14. 2 COP 4020 – Programming Language Concepts Dr. Manuel E.

PROLOG FUNDAMENTALS Module 14. 2 COP 4020 – Programming Language Concepts Dr. Manuel E. Bermudez

TOPICS Logic Programming Fundamentals Resolution Prolog Fundamentals Prolog Syntax The Prolog Database Lists Unification

TOPICS Logic Programming Fundamentals Resolution Prolog Fundamentals Prolog Syntax The Prolog Database Lists Unification

LOGIC PROGRAMMING FUNDAMENTALS • Horn clauses: – General form: H B 1, B 2,

LOGIC PROGRAMMING FUNDAMENTALS • Horn clauses: – General form: H B 1, B 2, . . . Bn – Meaning: if B 1, B 2, . . . Bn are true, then H is true. • Deductive reasoning: C A, B D C D A, B

TWO SAMPLE SESSIONS >man(socrates). Yes. >mortal(X) : - man(X). Yes. >? - mortal(socrates). Yes.

TWO SAMPLE SESSIONS >man(socrates). Yes. >mortal(X) : - man(X). Yes. >? - mortal(socrates). Yes. >? - mortal(X). X=''socrates''. >mother(X, Y): - parent(X, Y), female(X). Yes. >parent(john, bill). Yes. >parent(jane, bill). Yes. >female(jane). Yes. >? - mother(jane, bill). Yes. >? - mother(john, bill). Yes. >? - mother(X, bill). X=''jane''.

RESOLUTION • Process of deriving new statements, combining old ones, cancelling like terms, etc.

RESOLUTION • Process of deriving new statements, combining old ones, cancelling like terms, etc. • Variables may acquire values through unification. More later. • Example: fun(X) sunny(X) sunny(Florida) fun(Florida)

PROLOG FUNDAMENTALS • All Prolog programs built from terms: – Programs – The data

PROLOG FUNDAMENTALS • All Prolog programs built from terms: – Programs – The data manipulated by programs • Three types of terms: – Constants: integers, real numbers, atoms. – Variables. – Compound terms.

PROLOG FUNDAMENTALS • Constants: Integers, e. g. 123; Reals, e. g. : 1. 23

PROLOG FUNDAMENTALS • Constants: Integers, e. g. 123; Reals, e. g. : 1. 23 • Atoms: – Lexically, a lowercase letter followed by any number of additional letters, digits or underscores, e. g. foobar, 'Hello'. – Atoms do look like variables in other languages, but foobar is not a variable; it has no binding, it is a unique value.

PROLOG FUNDAMENTALS • Variables: begin with an upper-case letter, e. g. X, My_var. –

PROLOG FUNDAMENTALS • Variables: begin with an upper-case letter, e. g. X, My_var. – Akin to “unknowns” in algebra. • Can be instantiated (take on a value) at run time. • A compound term, or structure, consists of an atom called a functor, and a list of arguments, e. g. sunny(florida), weird(prolog), related(jim, john). No space allowed • A compound term may look like a function call, but it isn’t. It is structured data, or logical facts.

(PARTIAL) PROLOG SYNTAX (USING BNF) <program> : : = <predicate> | <program><predicate> : :

(PARTIAL) PROLOG SYNTAX (USING BNF) <program> : : = <predicate> | <program><predicate> : : = <clause> | <predicate><clause> : : = <base clause> | <nonbase clause> <base clause> : : = <structure>. <nonbase clause> : : = <structure> : - <structures> : : = <structure> | <structure> , <structures> <structure> : : = <name> | <name> ( <arguments> ) <arguments> : : = <argument> | <argument> , <arguments> Even an arithmetic expression, usually written as 1+2, is just an abbreviation for +(1, 2).

THE PROLOG DATABASE • A Prolog system maintains a collection of facts and rules

THE PROLOG DATABASE • A Prolog system maintains a collection of facts and rules of inference, a database. • A Prolog program is just a “store” of facts for this database. • The simplest item in the database is a fact (term followed by a period. ? - sunny(florida). Yes. ? - father(jim, ann). Yes. ? - father(jim, tom). No. ? - sunny(X). Attempt to match X. X = florida; Type a semi-colon, and X = california; the system tries again. No. This time it fails.

LISTS • Similar to Lisp. • [a, b, c] is syntactic sugar. • Separate

LISTS • Similar to Lisp. • [a, b, c] is syntactic sugar. • Separate head from tail using '|'. • '. ' similar to 'cons' in Lisp. List notation [] [1, 2, 3] [1, parent(X, Y)] Term denoted []. (1, []). (1, . (2, . (3, []))). (1, . (parent(X, Y), [])) [1|X] [1, 2|[3, 4]] . (1, X). (1, . (2, X))) [1, 2, 3, 4]

PATTERN MATCHING: UNIFICATION Examples: • [george, X] unifies with [george, tom] • [X, fred,

PATTERN MATCHING: UNIFICATION Examples: • [george, X] unifies with [george, tom] • [X, fred, [1, X]] by unifies with [jane, fred, [1, Y]] Y = X = jane • [X, fred, [1, X]] does not unify with [jane, fred, [1, ralph]], because X cannot match both jane and ralph.

UNIFICATION 1. A constant unifies only with itself. 2. Two structures unify iff they

UNIFICATION 1. A constant unifies only with itself. 2. Two structures unify iff they have • The same functor. • The same number of arguments. • The arguments unify recursively. 3. A variable X unifies with anything. The other thing could: • have a value. So, instantiate X. • be an uninstantiated variable. Link the two variables, so that if either is instantiated later, they both share the value.

EXAMPLE OF UNIFICATION Unify ([p, q, [c, X]], [Y, q, [X, c]]) = Unify(p,

EXAMPLE OF UNIFICATION Unify ([p, q, [c, X]], [Y, q, [X, c]]) = Unify(p, Y) and Unify([q, [c, X]], [q, [X, c]]) = (Y=p) and Unify(q, q) and Unify( [[c, X]], [[X, c]]) = (Y=p) and Unify( [c, X], [X, c]) and Unify(nil, nil) = (Y=p) and Unify(c, X) and Unify([X], [c]) = (Y=p) and (X=c) and Unify(X, c) and Unify(nil, nil) = (Y=p) and (X=c) and Unify(valueof(X), c) = (Y=p) and (X=c) and Unify(c, c) = (Y=p) and (X=c).

SUMMARY Logic Programming Fundamentals Resolution Prolog Fundamentals Prolog Syntax The Prolog Database Lists Unification

SUMMARY Logic Programming Fundamentals Resolution Prolog Fundamentals Prolog Syntax The Prolog Database Lists Unification