1 / 15

Prolog fundamentals

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.

maplesj
Download Presentation

Prolog fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prolog fundamentals Module 14.2COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

  2. Logic Programming Fundamentals Resolution Prolog Fundamentals Prolog Syntax The Prolog Database Lists Unification Topics

  3. 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

  4. 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''.

  5. 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

  6. 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

  7. 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

  8. 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

  9. <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)

  10. 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

  11. 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.

  12. 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

  13. 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

  14. 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

  15. Logic Programming Fundamentals Resolution Prolog Fundamentals Prolog Syntax The Prolog Database Lists Unification summary

More Related