220 likes | 407 Views
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
E N D
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 • 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
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
Getting Started Like Lisp, the environment is interactive. Prolog responds with a Yes or No. You present a query, which ends in a period.
Getting Started A single backslash is an escape character. Commands like “cd” and “pwd” are supported.
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.
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)
Prolog • Basic Parts of a Prolog Program • Facts • Rules • Queries (aka Questions)
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.
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!
Queries w/ Variables Typed Enter here. Typed semi-colon here.
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.
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
Rules – Logic Programming • Predicate Logic • Predicate is also known as a relation • predicate(argument1, argument2, …).
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.
Binary Tree Traversal Lisp! Really hard to read!
Binary Tree Traversal Fact #1 – If an empty tree is presented, its inorder list is an empty list.
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.
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