19
Declarative Programming With Prolog COP 4020 University of Central Florida

Declarative Programming With Prolog

  • Upload
    pekelo

  • View
    42

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Declarative Programming With Prolog

Declarative Programming With

Prolog

COP 4020University of Central Florida

Page 2: Declarative Programming With Prolog

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

Page 3: Declarative Programming With Prolog

Getting Started 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

http://en.wikibooks.org/wiki/Prolog/Introduction

Lots of overheadDon’t recommend forjust a short homework.

Simple typing was not simple.

Seemed to work fine.

Page 4: Declarative Programming With Prolog

Getting Started

Like Lisp, the environment is interactive.

You present a query, which ends in a period.

Prolog responds with a Yes or No.

Page 5: Declarative Programming With Prolog

Getting Started

Commands like “cd” and “pwd” are supported.

A single backslash is an escape character.

Page 6: Declarative Programming With Prolog

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.

Page 7: Declarative Programming With Prolog

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)

Page 8: Declarative Programming With Prolog

Prolog Basic Parts of a Prolog Program

Facts Rules Queries (aka Questions)

Page 9: Declarative Programming With Prolog

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.

Page 10: Declarative Programming With Prolog

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!

Page 11: Declarative Programming With Prolog

Queries w/ Variables

Typed Enter here.

Typed semi-colon here.

Page 12: Declarative Programming With Prolog

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.

Page 13: Declarative Programming With Prolog

Rules – Logic Programming Propositional Logic

Conjunction (AND) Disjunction (OR) Exclusive Or (XOR) Negation (NOT) Implication (A->B) Equality (==)

http://en.wikibooks.org/wiki/Prolog/Introduction_to_logic

,;

not() \= \==

= ==

Put a comma between two predicates. Ex. human(john), engineer(john).

Rather than use a semi-colon between predicates, it is preferred to createtwo separate rules. The net effect will be OR.

not takes “true” and “fail” as arguments

xor(A,B) :- \==(A,B).

Prolog

Page 14: Declarative Programming With Prolog

Rules – Logic Programming Predicate Logic

Predicate is also known as a relation predicate(argument1, argument2, …).

Page 15: Declarative Programming With Prolog

More Examples

Some of these are built-in predicates and operators.

Here is one way to “print.”

nl prints a new line.

Display puts all “functors” in front of their arguments.

This doesn’t work but …

What is a variable while “is” isa built-in predicate.

Doesn’t all this seem overly simple to do anything complex?

Page 16: Declarative Programming With Prolog

Binary Tree Traversal

Lisp!

Really hard to read!

Page 17: Declarative Programming With Prolog

Binary Tree Traversal

Fact #1 – If an empty tree is presented, its inorder list is an empty list.

Page 18: Declarative Programming With Prolog

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.

Page 19: Declarative Programming With Prolog

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