1 / 19

Declarative Programming With Prolog

Declarative Programming With Prolog. COP 4020 University of Central Florida. References. UCF Library The Art of Prolog , Sterling and Shapiro, 1994 Prolog Programming in Depth , Covington et al, 1997 Programming in Prolog , Clocksin and Mellish, 1994 Internet

pekelo
Download Presentation

Declarative Programming With Prolog

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. Declarative Programming With Prolog COP 4020 University of Central Florida

  2. References • UCF Library • The Art of Prolog, Sterling and Shapiro, 1994 • Prolog Programming in Depth, Covington et al, 1997 • Programming in Prolog, Clocksin and Mellish, 1994 • Internet • http://web.mac.com/ulrichfurbach/iWeb/KI-Programmierung/Materialien_files/19-2.ppt • http://en.wikibooks.org/wiki/Programming:Prolog • http://www.cs.ccu.edu.tw/~dan/advinpro/index.htm

  3. Getting Started Seemed to work fine. Simple typing was not simple. • SWI Prolog • http://www.swi-prolog.org/ • A small and robust open-source implementation that is compliant with both Prolog standards (ISO and Edinburgh) and has many extra libraries and built-in predicates. There's even a separate toolkit for creating windows and graphics, called XPCE. Supports many :platforms. • GNU Prolog • http://pauillac.inria.fr/~diaz/gnu-prolog/ • A relatively new open source implementation. Has support for Constraint Logic Programming, an extension of prolog. • Visual Prolog • http://www.visual-prolog.com/ • A complete development environment for an Object-Oriented extension of Prolog. Includes compiler, linker, text editor, graphical dialog editors, build system, debugger, large library, and much more Lots of overhead Don’t recommend for just a short homework. http://en.wikibooks.org/wiki/Prolog/Introduction

  4. Getting Started Like Lisp, the environment is interactive. Prolog responds with a Yes or No. You present a query, which ends in a period.

  5. Getting Started A single backslash is an escape character. Commands like “cd” and “pwd” are supported.

  6. Getting Started Consult and reconsult load files. Two styles of comments exist. Two simple terms that evaluate to Yes and No respectively. Query a fact from the knowledge base.

  7. Declarative Programming • A program is “declarative” if it describes what something is like, rather than how to create it. • John was declared to be a human. • John was declared to like flowers and Mary. • Prolog is also a “logic programming” language as it uses formal mathematical logic as the basis for the language. • Created in 1972 by Colmerauer and Roussel • Influenced by John McCarthy (Lisp)

  8. Prolog • Basic Parts of a Prolog Program • Facts • Rules • Queries (aka Questions)

  9. Facts • Defined by the programmer • Undefined queries are considered to fail or not provable (slightly different than false) • English language interpretation is also left to the programmer • Should be consistent • likes(john,mary). • John likes Mary (preferred) • Mary likes John • Formal representation of a fact: • predicate(argument1,…). • Collection of facts is known as a database.

  10. Queries • ?- • Queries are preceded by question mark – hyphen, which is typically the prompt in a Prolog environment. • The response is either “yes” or “no” depending upon the current database (knowledge base). • Variables • Capitalized! • likes(john,Somevariable). Note our facts have all been lowercase!

  11. Queries w/ Variables Typed Enter here. Typed semi-colon here.

  12. Rules • Rules declare relationships between objects (or facts). SWI-Prolog supports “up- arrow” to repeat old statements. Mary likes John due to the rule given that says she likes engineers and the fact that John is an engineer.

  13. Rules – Logic Programming • Propositional Logic • Conjunction (AND) • Disjunction (OR) • Exclusive Or (XOR) • Negation (NOT) • Implication (A->B) • Equality (==) , ; not() \= \== = == Prolog xor(A,B) :- \==(A,B). Put a comma between two predicates. Ex. human(john), engineer(john). Rather than use a semi-colon between predicates, it is preferred to create two separate rules. The net effect will be OR. not takes “true” and “fail” as arguments http://en.wikibooks.org/wiki/Prolog/Introduction_to_logic

  14. Rules – Logic Programming • Predicate Logic • Predicate is also known as a relation • predicate(argument1, argument2, …).

  15. More Examples Some of these are built-in predicates and operators. Here is one way to “print.” nl prints a new line. Doesn’t all this seem overly simple to do anything complex? Display puts all “functors” in front of their arguments. This doesn’t work but … What is a variable while “is” is a built-in predicate.

  16. Binary Tree Traversal Lisp! Really hard to read!

  17. Binary Tree Traversal Fact #1 – If an empty tree is presented, its inorder list is an empty list.

  18. Binary Tree Traversal Fact #1 – If an empty tree is presented, its inorder list is an empty list. Fact #2 – If a single number is presented (a one-node tree), then its inorder list is just a list containing that number.

  19. Binary Tree Traversal Fact #1 – If an empty tree is presented, its inorder list is an empty list. Fact #2 – If a single number is presented (a one-node tree), then its inorder list is just a list containing that number. Fact #3 – If a tree structure is presented AND that tree is a binary tree AND no logical errors appear recursively calling the inorder rule on the left AND right side THEN ASSIGN Xs the list [Ls Element Rs]. Ls: left-hand side result Rs: right-hand side result

More Related