Download presentation
Presentation is loading. Please wait.
Published byHilda Carter Modified over 6 years ago
1
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez
2
Topics Logic Programming Fundamentals Resolution Prolog Fundamentals
Prolog Syntax The Prolog Database Lists Unification
3
Logic Programming Fundamentals
Horn clauses: General form: H B1, B2, ... Bn Meaning: if B1, B2, ... Bn are true, then H is true. Deductive reasoning: C A,B D C D A,B
4
Two sample sessions >mother(X, Y):- parent(X,Y),female(X). Yes.
>parent(john, bill). >parent(jane, bill). >female(jane). >?- mother(jane, bill). >?- mother(john, bill). >?- mother(X, bill). X=''jane''. >man(socrates). Yes. >mortal(X) :- man(X). >?- mortal(socrates). >?- mortal(X). X=''socrates''.
5
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)
6
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.
7
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.
8
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.
9
(partial) Prolog Syntax (using bnf)
<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).
10
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). ?- 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.
11
Lists Similar to Lisp. [a, b, c] is syntactic sugar.
Separate head from tail using '|'. '.' similar to 'cons' in Lisp. List notation Term denoted [] [1] .(1,[]) [1,2,3] .(1,.(2,.(3,[]))) [1,parent(X,Y)] .(1,.(parent(X,Y),[])) [1|X] .(1,X) [1,2|X] .(1,.(2,X))) [1,2|[3,4]] [1,2,3,4]
12
Pattern Matching: Unification
Examples: [george, X] unifies with [george, tom] [X, fred, [1, X]] unifies with [jane, fred, [1, Y]] by Y = X = jane [X, fred, [1, X]] does not unify with [jane, fred, [1, ralph]], because X cannot match both jane and ralph.
13
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.
14
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).
15
summary Logic Programming Fundamentals Resolution Prolog Fundamentals
Prolog Syntax The Prolog Database Lists Unification
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.