FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF...

47
FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší budoucnosti

Transcript of FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF...

Page 1: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU

2011 Jan JanoušekMI-FLP

Evropský sociální fondPraha & EU: Investujeme do vaší budoucnosti

Page 2: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Introduction

We have discussed that Common Lisp gathers three paradigms (styles) of programming:

Functional programming, which originated from the lambda calculus and is also the origin of Lisp. It is represented by functions, macros, symbolic rewritting and reduction style, recursion.

Imperative programming, which is represented by variables, blocks of a sequence of consecutive statements, iterative cycles.

Object-oriented programming, which is represented by OOP features, such as operators defclass and defmethod for example.

This lecture considers a good style of functional programming in Lisp.

Page 3: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

ON USING FUNCTIONS

Page 4: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Lisp – a collection of functions

Lisp is a collection of Lisp functions(, except for a small number of operators called special forms).

If you think of something you wish Lisp could do, you can write it yourself, and your new function will be treated just like the built-in ones.

Functions are Lisp objects.

Page 5: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Data type function

Lisp offers most of the data types one can find in other languages: integers and floating-point numbers, strings, arrays, structures, and so on.

The function is also a data type in Lisp! We can: create new ones at runtime, store them in variables and in structures, pass them as arguments to other functions, and return them as results.

Creating new functions at runtime turns out to be a routinely used Lisp programming technique.

Page 6: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Functional design

Programs should evolve instead of being developed by the old plan-and-implement method.

Functional programming means writing programs which work by returning values instead of by performing side-effects.Side-effects of functions are to be minimised.

The following example will show how functional programming differs from what you might do in another, imperative, language.

Page 7: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Example - a function to reverse lists

(defun bad-reverse (lst)

(let* ((len (length lst))

(ilimit (truncate (/ len 2))))

(do ((i 0 (1+ i))

(j (1- len) (1- j)))

((>= i ilimit))

(rotatef (nth i lst) (nth j lst)))))

The list is reversed by side-effects:> (setq lst ’(a b c))

(A B C)

> (bad-reverse lst)

NIL

> lst

(C B A)

Page 8: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Example, contd.

Function bad-reverse is far from good Lisp style and draws its callers away from the functional ideal.

The desired behaviour should be:> (setq lst ’(a b c))

(A B C)

> (good-reverse lst)

(C B A)

> lst

(A B C)

Page 9: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Example, contd.

(defun good-reverse (lst)

(labels ((rev (lst acc)

(if (null lst)

acc

rev (cdr lst) (cons (car lst) acc)))))

(rev lst nil)))

Generally, functional code looks fluid on the page, like definitions; imperative code looks solid and blockish, like C.

Page 10: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Example – built-in reverse function

Like good-reverse, the built-in reverse works by returning a value — it doesn’t touch its arguments. Operators like reverse are intended to be called for return values, not side-effects:

Instead of(reverse lst)

we need to write(setq lst (reverse lst)).

Page 11: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Example, contd.

Instead of building new list structure, bad-reverse operateson the original list. However, for efficiency operating on the original data structure is sometimes necessary.

For such cases, Lisp provides an O(n) destructive reversing function called nreverse.

Even destructive functions usually work by returning values!

You still can’t write (nreverse lst) in the middle of a function and assume that afterwards lst will be reversed.

Page 12: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Example, contd.

This is what happens in most Lisp implementations:> (setq lst ’(a b c))

(A B C)

> (nreverse lst)

(C B A)

> lst

(A)

To reverse lst, you have would have to set lst to the return value, as with plain reverse.

Page 13: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

General rules on side-effects

Only a few Lisp operators are intended to be called for side-effects. In general, the built-in operators are meant to be called for their return values. (Don’t be misled by names like sort, remove, or substitute.)

If you want side-effects, use setf on the return value.

Some side-effects are inevitable in general. However, having functional programming as an ideal doesn’t imply that programs should never have side effects. It just means that they should have no more than necessary.

Page 14: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Multiple return values

To make functional programming easier Lisp functions can return more than one value.

> (truncate 26.21875)

26

0.21875

The first return value is catched implicitly, catching all return values is done by using a multiple-value-bind.

> (= (truncate 26.21875) 26)

T

> (multiple-value-bind (int frac) (truncate 26.21875) (list int frac))

(26 0.21875)

Page 15: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Multiple return values, contd.

To return multiple values, we use the values operator:

> (defun powers (x)

(values x (sqrt x) (expt x 2)))

POWERS

> (multiple-value-bind (base root square) (powers 4)(list base root square))

(4 2.0 16)

Page 16: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Another explanation of the difference

A functional program tells you what it wants; an imperative program tells you what to do.

A functional program says “Return a list of a and the square of the first element of x:”

(defun fun (x)

(list ’a (expt (car x) 2)))

Page 17: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Another explanation of the difference

An imperative programs says “Get the first element of x, then square it, then return a list of a and the square:”

(defun imp (x)

(let (y sqr)

(setq y (car x))

(setq sqr (expt y 2))

(list ’a sqr)))

Note: In fact, the definition of imp is similar in form to themachine language code that most Lisp compilers would generate for fun.

Page 18: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Note on the difference

A programmer used to programming in an imperative language often conceives of programs in imperative terms, and can find it easier to write imperative programs than functional ones in the beginning.

This habit of mind is worth overcoming if you have a language that will let you.

Functional programs can be written with unusual speed, and at the same time, can be unusually reliable.

Page 19: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Informal parables of the difference in Lisp Beginning to use Lisp may be like stepping onto a skating

rink for the first time. It’s actually much easier to get around on ice than it is on dry land — if you use skates. Till then you will be left wondering what people see in this sport.

What skates are to ice, functional programming is to Lisp. Together the two allow you to travel more gracefully, with less effort.

Page 20: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

From imperative to functional style

There is a trick for transforming imperative programs into functional ones.

You can begin by applying this trick to finished code. Soon you will begin to anticipate yourself, and transform your code as you write it.

Soon after that, you will begin to conceive of programs in functional terms from the start.

Page 21: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

From imperative to functional style

An imperative program can be seen as a functional program turned inside-out.

Vice versa, to find the functional program implicit in our imperative one, we just turn it outside-in.

We show this method on the example of imp and funfunctions. (Note. The first thing we can notice in imp is the creation of variables y and sqr in the initial let. This is a sign that bad things are to follow - uninitialized variables are so rarely needed that they should generally be treated as a symptom of illness in the program.)

Page 22: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

From imperative to functional style

The method:

Let us go straight to the end of the function. What occurs last in an imperative program occurs outermost in a functional one.

So our first step is to grab the final call to list and begin stuffing the rest of the program inside it.

We continue by applying the same transformation repeatedly.

Page 23: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Example

Starting at the end, we replace: sqr with (expt y 2), yielding:

(list ’a (expt y 2)))

Then we replace y by (car x):

(list ’a (expt (car x) 2))

Now we can throw away the rest of the code, having stuffed it all into the last expression. In the process we removed the need for the variables y and sqr, so we can discard the let as well.

Page 24: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Case study: implementing nondeterminism, search problems

Page 25: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Simulation of Nondeterminism

A nondeterministic algorithm can be simulated by a deterministic one.

In functional programs, nondeterminism can be implemented straightforward by search with backtracking.

Two special operators are used: choose - takes a finite set of possible choices and returns

one element. fail - is used to influence the value returned by choose.

Page 26: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

futures

Consider example:(let ((x (choose ’(1 2 3))))

(if (odd? x)

(+ x 1)

x))

Three possible futures: 1. If choose returns 1, the computation will return 2. 2. If choose returns 2, the computation will return 2. 3. If choose returns 3, the computation will return 4.

Page 27: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

futures

Consider another example:(let ((x (choose ’(2 3))))

(if (odd? x)

(choose ’(a b))

x))

Three possible futures: 1. If choose returns 2, the computation will return 2. 2. If choose returns 3, the computation splits into two possible futures,

one in which a is returned, and one in which b is.

If choose is given a choice of several alternatives, each one is associated with a set of possible futures.

Page 28: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

The main concept of using choose and fail In nondeterministic algorithms we are allowed to say “choose

an element such that nothing we do later will result in a call to fail.” (Only a choose over zero alternatives is equivalent to a fail.)ie. We assume that In(let ((x (choose ’(1 2))))

(if (odd? x)

(fail)

x))

choose cannot select 1 and the expression as a whole is deterministic: it always returns 2.

Page 29: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

The main concept

In(let ((x (choose ’(1 2))))

(if (odd? x)

(let ((y (choose ’(a b))))

(if (eq? y ’a)

(fail)

y))

x))

The expression as a whole could return either b or 2.

Page 30: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

The main concept

Another example (discovering whether you have a known ancestor called Igor):

Function Ig(n)if name(n) = ‘Igor’

then return nelse if parents(n)

then return Ig(choose(parents(n)))else fail.

Here choose is to select the corresponding mother or father genealogical subtree so that Igor ancestor would be find (if exists).

Page 31: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

The main concept

The fail operator is used to influence the value returned by choose.

Any implementation of choose must simulate correct guessing by backtracking when it discovers mistakes, like a rat finding its way through a maze.

This backtracking can be done beneath the surface.

Once you have some form of choose and fail, you can write algorithms as if it really were possible to guess the correct choices.

Page 32: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Search

By using choose it is possible to write an algorithm to search some problem space just by writing an algorithm to traverse it.

Page 33: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Deterministic tree search

(define (descent n1 n2)

(if (eq? n1 n2)

(list n2)

(let ((p (try-paths (kids n1) n2)))

(if p (cons n1 p) #f))))

(define (try-paths ns n2)

(if (null? ns)

#f

(or (descent (car ns) n2)

(try-paths (cdr ns) n2))))

Page 34: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Nondeterministic tree search using choose

(define (descent n1 n2)

(cond ((eq? n1 n2) (list n2))

((null? (kids n1)) (fail))

(else (cons n1 (descent (choose (kids n1)) n2)))))

Nondeterminism allows the programmer to ignore the details of finding a path.

It is written on the assumption that choose has chosen a kid with the desired properties.

Page 35: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Implementation of choose and fail

Page 36: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Desired behaviour

> (defun do2 (x)

(choose (+ x 2) (* x 2) (expt x 2)))

DO2

> (do2 3)

5

> (fail)

6

> (fail)

9

> (fail)

@

Page 37: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Depth-first search using stack

The variable *paths* is used as a stack to store paths which have not yet been followed.

When the computation reaches a choose expression with several alternatives, the first alternative is evaluated, and the remaining choices are stored on *paths*.

If the program later on encounters a fail, the last stored choice will be popped off *paths* and restarted.

Depth-first search

Page 38: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

choose

(defparameter *paths* nil)

(defconstant failsym ’@)

(defmacro choose (&rest choices)

(if choices

‘(progn

,@(mapcar #’(lambda (c)

‘(push #’(lambda () ,c) *paths*))

(reverse (cdr choices)))

,(car choices))

’(fail)))

Page 39: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

fail

(defun fail ()

(if *paths*

(funcall (pop *paths*))

failsym))

Page 40: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

What is pushed onto *path*

Notice that what is pushed onto *paths* is a closure – function over the choice to be saved, locking in all the bindings of the lexical variables referred to within it. For example, the expression

(let ((x 2))

(choose

(+ x 1)

(+ x 100)))

is macroexpanded to:(let ((x 2))

(progn

(push #’(lambda () (+ x 100))

*paths*)

(+ x 1)))

Page 41: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Choose with binding variables

(defmacro choose-bind (var choices &body body)

‘(cb #’(lambda (,var) ,@body) ,choices))

(defun cb (fn choices)

(if choices

(progn

(if (cdr choices)

(push #’(lambda () (cb fn (cdrchoices)))

*paths*))

(funcall fn (car choices)))

(fail)))

Page 42: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Choose with binding variables

> (choose-bind x ’(marrakesh strasbourg vegas)

(format nil "Let’s go to ~A." x))

"Let’s go to MARRAKESH."

> (fail)

"Let’s go to STRASBOURG."

Page 43: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Note on Prolog

This searching of problem space is very similar to Prolog, see our further lectures, which focuse on logic programming!

Page 44: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

NA ZÁVĚR– DALŠÍ POZNÁMKY K POUŽÍVÁNÍ FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V PRAXI (česky)

Page 45: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Lisp a funkcionální programování dá člověku nový pohledna principy programovacích jazyků který Vás pozitivněovlivní i když se pravděpodobně budete živit jinýmprogramovacím jazykem.

Aplikační oblast nejvíce v oboru umělé inteligence. V mainstreamových programovacích jazycích se objevují

funkcionální rysy: nový připravovaný standard C++0x, C# 4.0 a knihovna Linq (lambda funkce, typová inference -

odvození typů - i když to není zrovna vlastnost Lispu ale Haskellua F#),

diskutuje se o zařazení lambda funkcí do nadcházející verze Javy, Wolfram Mathematica - nepoužívá přímo Lisp ale její jazyk má

"Lisp feeling„.

Page 46: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Praktické aplikace

Lisp: skriptovací jazyk v Emacsu, Gimpu a Autocadu společnost Orbitz - prohledávání rozsáhlého stavového prostoru

při hledání nejlevnější kombinace letenek (http://www.franz.com/success/customer_apps/data_mining/itast

ory.lhtml) prototyp první verze Microsoft .NET CLR (virtuální stroj pro .NET)

byl napsán v Lispu - Lisp je vhodný jazyk pro výzkumnéprogramování

Page 47: FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ · PDF fileFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 7. PRINCIPY A TECHNIKY FUNKCIONÁLNÍHO PROGRAMOVÁNÍ V LISPU 2011 Jan

Praktické aplikace

Další funkcionální programovací jazyky:

Investiční bankovnictví - finanční matematické modely využívají skládánía vyhodnocování matematických funkcí a tudíž je to přirozená aplikačníoblast pro funkcionální programování Společnost Barclays Capital - z veřejně diskutovaných projektů je to například propojení funkcionálníhoprogramovacího jazyka s aplikací Microsoft Excel:

http://cufp.org/videos/fmd-functional-development-excel Společnost Jane Street Capital - programuje automatické burzovní

obchodníky v jazyce OCaml (high frequency trading bots). SpolečnostCitrix - využila funkcionální jazyk Ocaml k vývoji virtualizačníhoprostředí XenServer

(http://cufp.galois.com/2008/slides/MadhavapeddyAnil.pdf)