# Declarative Programming Autumn 2014 Introduction.

## Presentation on theme: "Declarative Programming Autumn 2014 Introduction."— Presentation transcript:

Declarative Programming Autumn 2014 Introduction

Lecture times Regular lectures on Wednesdays 16:30-18:05, room 12 16 lectures The lectures at the following dates will be rescheduled (dates/times to be agreed, but likely to some time in November/December): 10.09. 17.10. Some other changes are possible (but hopefully, not too many).

Web page(s) http://susurs.mii.lu.lv/juris/courses/dp2014.html Is expected to contain: short summaries of lectures power point presentations (when present) problems for programming assignments/project your grades other relevant information (exam dates, changes in lecture times etc) Course material also available as e-course: https://estudijas.lu.lv/login/index.php

Contact information Juris Vīksna Room 421, Rainis boulevard 29 email: juris.viksna@mii.lu.lv phone: +371-67213716 Consultations: "official" consultation times: Thursdays (???) 16:30-18:00, Rainis bouleverd 29, room 421; by individual arrangements. In "official" times I will be usually (but probably not always) available without an appointment, so just in case I recommend to check in advance.

Programming languages ImperativeFunctionalLogical C++LispProlog PascalML JavaHaskell... Imperative Declarative

Declarative vs imperative languages [Adapted from U.Nilsson]

Declarative vs imperative languages [Adapted from U.Nilsson]

Imperative languages Program is a list of explicit steps of computation list procedure cat(list a, list b) { list t = list u = copylist(a); while (t.tail != nil) t = t.tail; t.tail = b; return u; }

Functional languages Program is a composition of functions cat(a,b) if b = nil then a else cons(head(a), cat(tail(a),b))

Logical languages Program is a list of facts that are known to be true and a query, which has to be proved or disproved cat([], Z, Z). cat([H|T], L, [H|Z]) :- cat(T, L, Z).  u (S(u)   z (R(z)   P(u,z)))  y (S(y) &  w (Q(w)  P(y,w)))  y (Q(y)   R(y)) Can this really be done (at least efficiently)? Depends...

Propositional logic (propositional calculus) Infinite set of variables:A,B,C,D,... Finite set of operators: , , , ,  Finite set of auxiliary symbols:(,) Set of rules that defines syntactically correct formulas, e.g.: (  A  B)  (A  B) Interpretation of formulas: We assign truth values 0 and 1 to each of variables in a formula. The truth value of formula is computed using rules such as:  A = 1  A = 0 A  B = 1  A = 1 and B = 1

Propositional logic (propositional calculus)

Predicate logic (first-order) Infinite set of constants:c 1,c 2,... Infinite set of variables:x 1,x 2,... Infinite set of function symbols:f 1,f 2,... Infinite set of relation symbols:r 1,r 2,... Finite set of operators: , , , , , ,  Finite set of auxiliary symbols:(,),”,”, Set of rules that defines syntactically correct formulas, e.g.: (  x)(r(x)  c)  (  y)q(c,y) (  x)(  y)r(x,y,c) r(x,y,c) “might” mean e.g.: “for given c and for any x there exists y such that x+y=c”

Predicate logic (first-order) How to assign truth values to predicate logic formulas? Again we use notion of interpretation, however now it gets more complicated. interpretation domain D c3c3 c1c1 c2c2 f(c 3 ) = c 2 r(c 2,c 2 ) = 1 or 0 (true or false) Thus, interpretation defines: - domain D - “semantics” of functional and relational symbols. Given interpretation we can assign truth values to formulas!

Predicate logic (first-order)

Motivation  logic programming is quite important and useful concept in computer science  kind of fun!  there are problems for which PROLOG may be a natural choice of programming language

Requirements  4-6 short programming assignments0 – 50% (half of them must be submitted before the exam session, in order to be allowed to proceed, others are optional)  1 programming project (optional)0 – 30%  Exam (open book, optional)–20 – 20% (?)

Academic honesty You are expected to submit only your own work! Sanctions: Receiving a zero on the assignment (in no circumstances a resubmission will be allowed) No admission to the exam and no grade for the course

Textbooks Ivan Bratko PROLOG programming for artificial intelligence Pearson Range Extension Paul 2011 (4 th ed)

Textbooks Leon Sterling, Ehud Shapiro The art of Prolog MIT Press 1994 (2 nd ed)

Textbooks Richard A. O’Keefe The craft of Prolog MIT Press 1990 (reprint 2009)

Textbooks W.F. Clocksin, C.S.Mellish Programming in Prolog Springer Verlag 2003 (4 th /5 th ed) (5 th ed looks like just a reprint of 4 th ed from 2003)

Textbooks This one can be downloaded from the internet: http://www.ida.liu.se/~ulfni/lpp/ Ulf Nilsson and Jan Maluszynski Logic, Programming and Prolog (2ed) Previously published by John Wiley & Sons Ltd.

Textbooks Also available from the internet: http://bookboon.com/en/textbooks/it-programming/applications-of-prolog http://bookboon.com/en/textbooks/it-programming/prolog-techniques-applications-of-prolog Attila Csenki Applications of Prolog Prolog Techniques

Textbooks L.C.Paulson ML for working programmer Cambridge University Press, 1996

Prolog compilers/interpreters SWI Prologhttp://www.swi-prolog.org/

Prolog compilers/interpreters SWI Prolog Editor http://lakk.bildung.hessen.de/netzwerk/faecher/informatik /swiprolog/indexe.html An integrated environment that allows editing and running of Prolog programs + provides some debugging functionality. Http link also from SWI Prolog site.

Prolog compilers/interpreters SICSTUS Prolog http://www.sics.se/isl/sicstuswww/site/index.html

Workplan  PROLOG syntax4 formal (its quite simple...) special keywords and their semantics (also there is not that much...)  Computational model of PROLOG programs1  PROLOG interpreters - how to use them1  Program examples7 PROLOGish (often AI related) general (for training - to try to write thing that are hard in PROLOG)  Functional programming 2

Prolog – short history 1961Resolution method for predicate calculus (John Robinson) 1965ABSYS (Aberdeen System), ABSET (Aberdeen Set Language) (Michael Foster, Ted Ellock) 1971SYSTEMQ (Colmerauer, Kowalski), eventually it become Prolog 19741 st FORTRAN compiler for Prolog (David Warren) Was generally considered to be a very promising language up to early 1990s

Prolog is a ‘declarative’ language Clauses are statements about what is true about a problem, instead of instructions how to accomplish the solution. The PROLOG system uses the clauses to work out how to accomplish the solution by searching through the space of possible solutions. Not all problems have pure declarative specifications. Sometimes extralogical statements are needed.

What a program looks like % At the Zoo % elephant(george). elephant(mary). panda(chi_chi). panda(ming_ming). dangerous(X) :- big_teeth(X). dangerous(X) :- venomous(X). guess(X, tiger) :- stripey(X), big_teeth(X), isaCat(X). guess(X, koala) :- arboreal(X), sleepy(X). guess(X, zebra) :- stripey(X), isaHorse(X).

Example: Concatenate lists a and b list procedure cat(list a, list b) { list t = list u = copylist(a); while (t.tail != nil) t = t.tail; t.tail = b; return u; } In an imperative language: In a declarative language In a functional language: cat(a,b) if b = nil then a else cons(head(a),cat(tail(a),b)) cat([], Z, Z). cat([H|T], L, [H|Z]) :- cat(T, L, Z).

Prolog “pseudocodes” “Hand computation” Prolog program “Pseudocode” [Adapted from A.Csenki]