8 Logic Programming
8 Logic Programming
Programming Paradigms
Programming paradigm
A pattern that serves as a school of thoughts for programming of computers
Imperative Paradigm
Computation as a sequence of action
first do this and next do that
Stress on how the computation takes place than on what is being computed
When solving a problem, put concern to method than meaning of the problem
Example: Pascal, C,
Functional Paradigm
Originates from purely mathematical discipline: the theory of functions
Computation is based on functions Functions have the same status as others (numbers, lists, )
Functions are first-class values
Logic Paradigm
Based on mathematical logic. Specifies relationships among data values. Using explicit facts and rules to defines a base of knowledge Pose queries to the environment.
A brief of history
Logic programming
Introduced by Robert Kowalski in 1974 Algorithm = Logic + Control
Prolog
Programming in logic Programming language that uses logic programming for computing Introduced by Alain Colmerauer in 1970s
Prolog
Implementasi Prolog yang digunakan SWI Prolog versi 5.6.32 Download free at http://www.swi-prolog.org/ Developed by Jan Wielemaker, University of Amsterdam There are some other implementations (e.g. SICStus Prolog, XSB, etc.)
LP Paradigm Example 1
Setting up a database of flight connections
Is there a direct flight from A to B? Can I fly from C to D? What are possible destinations I can reach from E? Etc.
LP Paradigm Example 1
List of direct flights
direct(jakarta,denpasar). direct(jakarta,surabaya). direct(denpasar,mataram). direct(mataram,kupang).
LP Paradigm Example 1
To find connections between two cities:
There is a connection from X to Y, if there is a direct flight from X to Y
direct(X,Y) connection(X,Y).
There is a connection from X to Y, if there is a direct flight from X to Z and a connection from Z to Y.
direct(X,Z),connection(Z,Y) connection(X,Y).
LP Paradigm Example 1
To answer the previous questions:
Write the program Run program with queries related to questions
LP Paradigm Example 1
Q2: Where can one fly from Denpasar?
?- connection(denpasar,X).
Etc.
LP Paradigm Example 1
Two aspects of Prolog
Same program to compute answers to different problems (or queries) Program can be used much like a database
Knowledge is stored in the form of facts and rules deductive database Prolog models query processing in deductive database
LP Paradigm Example 2
Finding all elements which are members of two given lists
List: [a1,a2,an] or [a1 | [a2,an]] a1 is called head of [a1,a2,an] [a2,an] is called tail of [a1,a2,an] Ex: [1,2,3,4,5] = [1 | [2,3,4,5]]
LP Paradigm Example 2
X is a member of both L1 and L2 if X is a member of L1 and X is a member of X2
member_both(X,L1,L2) member(X,L1),member(X,L2)
LP Paradigm Example 2
Run the program to solve the problem
?member_both(X,[1,2,3],[2,3,4,5]).
#define SIZE1 3 #define SIZE2 4 void memberBoth(int a[],int b[],int c[]) { int i, j; int k=0;
LP Paradigm Example 2
Other aspects of Prolog:
Searching mechanism does not need explicitly to be specified (it is implicitly given)
Generate all elements of the first list, which are then tested for membership in the second list (cf. the rule)
LP Paradigm Example 2
Other aspects of Prolog (contd):
Prolog constructs a list dynamically
No size of list has to be defined in advance (cf. use of array to represent list in C program)
LP Paradigm Example 3
Ontology Animal Databases animal(mammal,tiger,carnivore,stripes). animal(mammal,hyena,carnivore,ugly). animal(mammal,lion,carnivore,mane). animal(mammal,zebra,herbivore,stripes). animal(bird,eagle,carnivore,large). animal(bird,sparrow,scavenger,small). animal(reptile,snake,carnivore,long). animal(reptile,lizard,scavenger,small).
Find (a) all the mammals, (b) all the carnivores that are mammals, (c) all the mammals with stripes, (d) whether there is a reptile that has a mane.
a. All the mammals: animal(mammal,X,Y,Z). b. All the carnivores that are mammals animal(mammal,X,carnivore,Z). c. All the mammals with stripes animal(mammal,X,Y,stripes). d. Whether there is a reptile that has a mane. animal(reptile,X,Y,mane).
LP Applications
Reasoning agents
Agent: perceive environment through sensors and act upon environment through actuators (Russell & Norvig, AI: a modern approach) Reasoning agents
Capabilities are characteristics of human-like intelligence
Mental representation of the world Correct reasoning with this representation
LP to encode (incomplete) world models, continuously update model upon the performance of an action, reason and draw logical conclusions based on world model
LP Applications
Semantic web
Current web content is for humans to read, not for computer programs to manipulate meaningfully For the semantic web to function, computers must have access to structured collections of information and sets of inference rules that they can use to conduct automated reasoning. (Tim Berners-Lee, 2001) LP is used to represent knowledge (in the form of rules) and reason on them (by making inferences with the rules)
LP Applications
Semantic Web (contd)
References:
The Semantic Web, Tim Berners-Lee, et.al., Scientific American -- May 2001 (available online) Semantic Web Logic Programming Tools, Alferes, et.al. Workshop on Principles and Practice of Semantic Web Reasoning, at 19th Int. Conf. on Logic Programming (ICLP03) (available online) International Workshop on Applications of Logic Programming in the Semantic Web and Semantic Web Services. http://events.deri.at/alpsws2006
LP Applications
Natural language processing (computational linguistics)
LP is used to implement grammars
Analyze the syntax Define the meaning (of a fragment of a natural language)
Several applications
Natural language queries to database Natural language system specification
References:
Attempto Controlled English (ACE): http://www.ifi.unizh.ch/attempto/description/index.html Natural Language Processing Techniques in Prolog: http://www.coli.uni-saarland.de/~kris/nlp-with-prolog/html
LP Applications
Security protocol analysis
Security protocols are designed to meet security properties Security protocols and security properties are specified using logic program Analysis of security protocols
Model attackers capabilities using logic program Outputs traces of attacks (if exist) for specified bounded number of sessions
References
CoProVe: http://130.89.144.15/cgi-bin/psltl/show.cgi ProVerif: http://www.di.ens.fr/~blanchet/crypto-eng.html NRL Protocol Analyzer: http://chacs.nrl.navy.mil/projects/crypto.html
LP Applications
Molecular biology
Using inductive logic programming
Understand relationship between chemical structure and activity of drugs Predicting mutagenesis to understand and predict carcinogenesis Predicting protein secondary structures
References
Inductive Logic Programming: http://www.doc.ic.ac.uk/~shm/ilp.html