Prolog and Datalog

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Prolog and DATALOG

By: Mohamad El Kebbe


Khaldoun Kassam
Safaa Elhajj
2 What is Prolog

 It’s a general-purpose logic programming language associated


with artificial intelligence and computational linguistics.

 Prolog has its roots in first order logic and formal logic

 It is declarative: the program logic is expressed in terms of relations,


represented as facts and rules

 Prolog is well-suited for specific tasks that benefit from rule-based logical
queries such as searching databases, voice control systems, and filling
templates.
3 History

 The name Prolog was chosen by Philippe Roussel as an abbreviation for


programmation en logique

 was created around 1972 by Alain Colmerauer with Philippe Roussel

 the first Prolog system was developed in 1972

 The first implementations of Prolog were interpreters

 David Warren created the Warren Abstract Machine , an early and


influential Prolog compiler

 European AI researchers favored Prolog while Americans favored Lisp


4 Use in industry

 Prolog has been used in Watson, for software called DeepQA


software and the Apache UIMA framework which is written in
various languages including Prolog.

 Prolog is used for pattern matching over natural language parse


trees.

 It was found that Prolog was the ideal choice for the language due
to its simplicity and expressiveness.
5 Implementations

 Compilation: Typically it is compiled to abstract machine code influenced


by WAM
 Tail recursion: is a special kind of recursion where the recursive call is the
very last thing in the function.
 Term indexing: data structure to facilitate fast lookup of terms and clauses
in a logic program
 Hashing: help handle large datasets more efficiently
 Tabling: frees the user from manually storing intermediate results
 Implementation in hardware: Attempts to implement Prolog at the
hardware level. A recent approach is to compile Prolog programs on FPGA
but the rapid progress of General purpose hardware has overtaken these
hardware.
6 Extensions
Certain extensions were implemented to extend prolog’s logic programming
capabilities.
 Types :Prolog is untyped but there was attempts to introduce types
because typed information is used for type safety and for reasoning about
prolog problems
 Modes: In the syntax of prolog no argument is input and output however
this info is significant when reasoning and can be used to accelerate
execution.
 Constraints: are condition testing to satisfy a clause. This is suitable for large
scale combinational optimization problems
 Object-orientations : certain implementations were done to extend prolog
to an Object oriented language. example : Flora-2, logtalk,
oblog,OBJlog,Prolog++
 Graphics: Certain graphics libraries were added to provide graphical
capabilities to Prolog
 Web programming: Some Prolog implementations support server side web
programming such as HTML and XML
7 Syntax and Semantics

 In Prolog, program logic is expressed in terms of relations

 A computation is initiated by running a query over these relations

 Relations and Queries are constructed using the ‘term’

 The ‘term’ is Prolog’s single data type


8 Syntax and Semantics

 Relations are defined by clauses.


 Given a query, the Prolog engine attempts to find a resolution refutation of
the negated query.
 This makes Prolog particularly useful for database, symbolic mathematics,
and language parsing applications.
 Because Prolog allows impure predicates, checking the truth value of
certain special predicates may have some deliberate side effect, such as
printing a value to the screen.
9 DATA TYPES

 Prolog's single data type is the term.


 Terms are either atoms, numbers, variables or compound terms.

Data Types

Compound
Atoms Numbers Variables
Terms
10 Atoms, Numbers AND Variables

 Atom: General purpose name without any meaning; can be a single word starting
with a small case letter, but if it consists of more words or starts with a capital case
letter, then it should be surrounded by single quotes. Ex: red, tree, ‘collage c1’,
‘Red’…
 Numbers: Can be floats or Integers
 Variables: A place holder of arbitrary term. Name of a variable is an alphanumeric
string that can contain underscores. The name can only start with a capital letter or
an underscore. Ex: X_a1, _x1
11 compound terms

 Compound Terms: Composed of an atom called the ‘functor’, followed by


an ordered list of arguments separated by commas and placed between
parenthesis. The functor and the arguments are atoms.
 Ex: relation(woman, man), ‘My collage’(‘LAU’, [‘CCE’, ele], fall, 2016)…
 Arity denotes the number of arguments. An atom is a compound terms with
arity 0.
12 compound terms

 Special cases of compound terms:


1. List: Ordered collection of terms. Terms are separated by commas and the
whole List is surrounded by brackets. Ex: [‘CCE’, ele, fall, 123]
2. String: As in other programming language, it is a sequence of characters.
These two types can be hold in variables and used as arguments for
compound terms.
13 Rules and facts

 Prolog programs describe relations, defined by means of clauses


 Clauses can be: rules or facts
 A rule is of the form: Head :- Body. It is read as: Head is true if body is true.
 Ex: employed(mohammad) :- graduated(mohammad).
14 Rules and facts

 A rule’s body can be made of multiple predicates.


 Such predicates can be joined using conjunction (And functionality) using
a comma, or disjunction (Or functionality) using the semi colon.
 Ex: (a, b) means a and b. (a; b) means a or b.
 A rule with empty body is called a fact
 Ex: cat(tom).
15 Query examples

 A prolog program is run through queries on the rules and facts present in a
program
 A query maybe used to ask whether something is true or false, where we
get answers like ‘yes’ or ‘no’
 We can also try to find/search for the value of a variable given some
relations.
16 Query Examples

1. Assume the prolog program:


loves(boy1, girl1).
loves(boy3, girl2).
loves(boy2, girl1).
jealous(X, Y) :- loves(X, Z), loves(Y, Z), \+(X = Y).
By running the query: ?- jealous(X, Y).
17 Some Query Examples (Cont’d)

This will result in the following result:


X = boy1
Y = boy2
And next solution is:’
X = boy2
Y = boy1
18 Loops and recursion

 Iterative algorithms can be implemented by means of recursive predicates.


 There is no general rule to create a loop in prolog
 To create a loop one has to understand the goal of the loop and
implement it in a recursive logical manner to simulate a loop.
19 Negation

 Prolog has a built in predicate ‘\+’ to show the negation of a fact or an


equality.
 Ex1: illegal(X) :- \+ legal(X).
20 Limitations

 have not had a significant impact on the computer industry in general.


 Most applications are small by industrial standards
 not all Prolog compilers support modules
 Portability of Prolog code across implementations
 criticized for having a high performance penalty
 Prolog is not purely declarative
21 DataLog

 Datalog is a declarative logic programming language that syntactically is a


subset of Prolog.
 It became prominent as a separate area around 1977
 David Maier is credited with coining the term Datalog.
 Used as query language for deductive databases
 Datalog has found new application in data integration, information
extraction, networking, program analysis, security, and cloud computing
22 DataLog (cont'd)

Features, limitations and extensions:


 Unlike in Prolog, statements of a DataLog program can be stated in any
order.
 DataLog queries on finite sets are guaranteed to terminate, so DataLog
does not have Prolog's cut operator.
 This makes Datalog a truly declarative language.
23 DataLog (cont'd)

In contrast to Prolog, Datalog:


 disallows complex terms as arguments of predicates e.g. p (1, 2) is
admissible but not p (f1 (1), 2)
 imposes certain restrictions on the use of negation and recursion
 Some widely used database systems include ideas and algorithms
developed for DataLog
24 DataLog (cont'd)

Several extensions have been made to DataLog


 To support aggregate functions, to allow object oriented programming, or
to allow disjunctions as heads of clauses.
 These extensions have significant impacts on the definition of Datalog's
semantics and on the implementation of a corresponding Datalog
interpreter.
25 Demo – Zebra Puzzle (Einstein’s Riddle)

 The Zebra puzzle, a.k.a. Einstein's Riddle, is a logic puzzle which is to be solved
programmatically.
It has several variants, one of them this:
 There are five houses.
 The English man lives in the red house.
 The Swede has a dog.
 The Dane drinks tea.
 The green house is immediately to the left of the white house.
 They drink coffee in the green house.
 The man who smokes Pall Mall has birds.
 In the yellow house they smoke Dunhill.
 In the middle house they drink milk.
26 Demo – Zebra Puzzle (Einstein’s Riddle)

 The Norwegian lives in the first house.


 The man who smokes Blend lives in the house next to the house with cats.
 In a house next to the house where they have a horse, they smoke Dunhill.
 The man who smokes Blue Master drinks beer.
 The German smokes Prince.
 The Norwegian lives next to the blue house.
 They drink water in a house next to the house where they smoke Blend.
 The question is, who owns the zebra?
 Additionally, list the solution for all the houses. Optionally, show the solution
is unique.
 Link to run environment: http://swish.swi-prolog.org/
27 References

 http://www.liquisearch.com/datalog/features_limitations_and_extensions
 http://rosettacode.org/wiki/Zebra_puzzle
 http://www.deransart.fr//prolog/docs.html
 https://www.cpp.edu/~jrfisher/www/prolog_tutorial/contents.html
 http://link.springer.com/chapter/10.1007%2F978-3-642-24206-9_5

You might also like