2Programming Paradigms A pattern that serves as a school of thoughts for programming of computersFour main programming paradigmsImperative paradigmObject oriented paradigmFunctional paradigmLogic paradigm
3Imperative 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 computedWhen solving a problem, put concern to method than meaning of the problemExample: Pascal, C, …
4Object oriented Paradigm Data as well as operations are encapsulated in objectsObjects interact by means of message passingObjects are grouped in classesAllows programming of the classes (as opposed to programming of individual object)Examples: C++, Java, …
5Functional ParadigmOriginates from purely mathematical discipline: the theory of functionsComputation is based on functionsFunctions have the same status as others (numbers, lists, …)Functions are first-class valuesExample: LISP, Haskell, …
6Logic Paradigm Based on mathematical logic. Specifies relationships among data values.Using explicit facts and rules to defines a base of knowledgePose queries to the environment.
7A brief of history Logic programming Prolog Introduced by Robert Kowalski in 1974Algorithm = Logic + ControlPrologProgramming in logicProgramming language that uses logic programming for computingIntroduced by Alain Colmerauer in 1970s
8Prolog Implementasi Prolog yang digunakan SWI Prolog versi 5.6.32 Download free atDeveloped by Jan Wielemaker, University of AmsterdamThere are some other implementations (e.g. SICStus Prolog, XSB, etc.)
9LP 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.
10LP Paradigm – Example 1 List of direct flights direct(jakarta,denpasar).direct(jakarta,surabaya).direct(denpasar,mataram).direct(mataram,kupang).
11LP 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 Ydirect(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).
12Program flight.pl Knowledge base = database consist of facts and rules direct(jakarta,denpasar). direct(jakarta,surabaya). direct(denpasar,mataram). direct(mataram,kupang). connection(X,Y) :- direct(X,Y). connection(X,Y) :- direct(X,Z),connection(Z,Y).Knowledge base = database consist of facts and rules
13LP Paradigm – Example 1 To answer the previous questions: Write the programRun program with queries related to questionsQ1: Is there a flight from Jakarta to Kupang??- connection(jakarta,kupang).
14LP Paradigm – Example 1 Q2: Where can one fly from Denpasar? ?- connection(denpasar,X).Q3: Can someone fly from Kupang??- connection(kupang,X).Q4: From where can one fly to Kupang??- connection(X,kupang).Etc.
15LP Paradigm – Example 1 Two aspects of Prolog Same program to compute answers to different problems (or queries)Program can be used much like a databaseKnowledge is stored in the form of facts and rules deductive databaseProlog models query processing in deductive database- Find the meaning of deductive or deduction (vs. inductive or induction)
16LP Paradigm – Example 2Finding all elements which are members of two given listsList: [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]]
17LP Paradigm – Example 2We need to define when an element X is a member of a listIf X is the head, then the answer is positivemember(X,[X | List]).Otherwise, check whether X is a member of the tailmember(X,List) member(X,[Y | List]).X is a member of both L1 and L2 if X is a member of L1 and X is a member of L2member(X,L1),member(X,L2) member_both(X,L1,L2)
21LP 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)Prolog solution can be used in a number of ways (cf. C solution):Testing membership?- member_both(2,[1,2,3],[2,3,4,5]).Instantiating an element of a list?- member_both(2,[1,2,3],[X,3,4,5]).
22LP Paradigm – Example 2 Other aspects of Prolog (cont’d): Prolog constructs a list dynamicallyNo size of list has to be defined in advance (cf. use of array to represent list in C program)See the use of pointers to represent list in CSee how list can be represented in Java
24Find (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.
25All the mammals:animal(mammal,X,Y,Z).All the carnivores that are mammalsanimal(mammal,X,carnivore,Z).c. All the mammals with stripesanimal(mammal,X,Y,stripes).d. Whether there is a reptile that has a mane.animal(reptile,X,Y,mane).
26LP Applications Reasoning agents Agent: perceive environment through sensors and act upon environment through actuators (Russell & Norvig, AI: a modern approach)Capabilities are characteristics of human-like intelligenceMental representation of the worldCorrect reasoning with this representationLP to encode (incomplete) world models, continuously update model upon the performance of an action, reason and draw logical conclusions based on world model
27LP 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)
28LP Applications Semantic Web (cont’d) 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.
29LP ApplicationsNatural language processing (computational linguistics)LP is used to implement grammarsAnalyze the syntaxDefine the meaning (of a fragment of a natural language)Several applicationsNatural language queries to databaseNatural language system specificationReferences:Attempto Controlled English (ACE):Natural Language Processing Techniques in Prolog:
30LP Applications Security protocol analysis Security protocols are designed to meet security propertiesSecurity protocols and security properties are specified using logic programAnalysis of security protocolsModel attackers’ capabilities using logic programOutputs traces of attacks (if exist) for specified bounded number of sessionsReferencesCoProVe:ProVerif:NRL Protocol Analyzer:
31LP Applications Molecular biology Using inductive logic programming Understand relationship between chemical structure and activity of drugsPredicting mutagenesis to understand and predict carcinogenesisPredicting protein secondary structuresUsing constraint-based methodsWorkshops on constraint-based methods in bioinformatics 2005:ReferencesInductive Logic Programming:
32STUDI KASUS (PROLOG)PROLOG (Programming in Logic) terdiri dari logika dan kontrol.Logika: Fakta dan aturan yang menerangkan suatu problemKontrol: implementasi algoritma menggunakan aturanSintaks Prolog berbentuk klausa atau formula First Order Predicate Logic (FOL)
33Contoh: Hubungan keluarga AliSitiAbuUmiDiagram diatas menyatakan fakta :Ali dan Abu laki-lakiSiti PerempuanAli menikahi SitiAli dan Siti punya anak AbuAli bapak dari AbuSiti ibu dari AbuAli dan Siti orangtua AbuAbu anak laki-laki dari Ali dan Siti
34Sintaks Prolog Penulisan sintaks fakta: Ali laki-laki : lelaki(ali). Siti Perempuan: perempuan(siti).Ali menikahi Siti: menikah(ali,siti).Ali bapak dari Abu: bapak(ali,abu).Siti ibu dari Abu : ibu(siti,abu).Ali dan Siti orangtua Abu:ortu(ali,siti,abu).Abu anak dari Ali dan Siti: anak(abu,ali,siti).Ali dan Siti punya anak Abu: anak(abu,ali,siti).