150 likes | 168 Views
Learn the fundamentals of Prolog programming, including syntax, resolution, and the use of lists and unification. Explore how Prolog databases work and how to match patterns using unification. This text provides examples and explanations to help you get started with Prolog.
E N D
Prolog fundamentals Module 14.2COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez
Logic Programming Fundamentals Resolution Prolog Fundamentals Prolog Syntax The Prolog Database Lists Unification Topics
Horn clauses: General form: H B1, B2, ... Bn Meaning: ifB1, B2, ... Bn are true, then H is true. Deductive reasoning: C A,B D C D A,B Logic Programming Fundamentals
Two sample sessions >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''. >man(socrates). Yes. >mortal(X) :- man(X). Yes. >?- mortal(socrates). Yes. >?- mortal(X). X=''socrates''.
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) Resolution
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 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 nota variable; it has no binding, it is a unique value. 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 structureddata, or logical facts. Prolog Fundamentals
<program> ::= <predicate> | <program><predicate> <predicate> ::= <clause> | <predicate><clause> <clause> ::= <base clause> | <nonbase clause> <base clause> ::= <structure> . <nonbase clause> ::= <structure> :- <structures> . <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). (partial) Prolog Syntax (using bnf)
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. The Prolog Database
List notation Term denoted [] [1|X] [] .(1,X) [1] [1,2|X] .(1,[]) .(1,.(2,X))) [1,2,3] [1,2|[3,4]] [1,2,3,4] .(1,.(2,.(3,[]))) [1,parent(X,Y)] .(1,.(parent(X,Y),[])) Lists • Similar to Lisp. • [a, b, c] is syntactic sugar. • Separate head from tail using '|'. • '.' similar to 'cons' in Lisp.
Examples: [george, X] unifieswith [george, tom] [X, fred, [1, X]] unifieswith [jane, fred, [1, Y]] by Y = X = jane [X, fred, [1, X]] doesnotunifywith [jane, fred, [1, ralph]], because X cannotmatchboth jane and ralph. Pattern Matching: Unification
A constant unifies only with itself. Two structures unify iff they have The same functor. The same number of arguments. The arguments unify recursively. 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. 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). Example of Unification
Logic Programming Fundamentals Resolution Prolog Fundamentals Prolog Syntax The Prolog Database Lists Unification summary