• Aucun résultat trouvé

J-O-Caml (2)

N/A
N/A
Protected

Academic year: 2022

Partager "J-O-Caml (2)"

Copied!
34
0
0

Texte intégral

(1)

J-O-Caml (2)

jean-jacques.levy@inria.fr

pauillac.inria.fr/~levy/qinghua/j-o-caml

Qinghua, November 20

dimanche 10 janvier 2010

(2)

Plan of this class

compiling programs

list processing

pattern-matching

new data types

labeling algorithm

dimanche 10 janvier 2010

(3)

Compiling programs

prog1.ml

program text ocamlc

binary a.out

ocamlc -o prog1 produces prog1 (executable byte code)

ocamlc -c prog1.ml produces prog1.cmo (byte code)

ocamlopt -c prog1.ml produces prog1.cmx (binary)

also files prog1.cmi (compiled interfaces -- see later)

dimanche 10 janvier 2010

(4)

Printing

or if printing many times:

dimanche 10 janvier 2010

(5)

List processing

dimanche 10 janvier 2010

(6)

List processing

dimanche 10 janvier 2010

(7)

List processing

polymorphic types

‘a list (say ``alpha list’’) is type of list of any type, e.g. int list, bool list, ...

dimanche 10 janvier 2010

(8)

List processing

Iterators on lists (~ arrays), but no List.make

List.map f x

List.iter f x

List.fold_left f a x

List.fold_right f a x

dimanche 10 janvier 2010

(9)

Exercices on lists

dimanche 10 janvier 2010

(10)

Exercices on lists

Write mem a x which tests if a belongs to list x

Write exists p x which tests if predicate p is true for one element of x;

same forall p x which tests for all elements.

Arbitrary precision numbers can be implemented by lists (little endian or big endian style). Write addition and multiplication algorithms.

Conway sequence of lists starts with list [1]. Then next list is read from the previous one. Therefore [1; 1], [2; 1], [1; 2; 1; 1], [1; 1; 1;

2; 2; 1].... Print lists of Conway sequence. Is there any unlucky number ?

dimanche 10 janvier 2010

(11)

New data types

products

dimanche 10 janvier 2010

(12)

New data types

products

dimanche 10 janvier 2010

(13)

New data types

sums

dimanche 10 janvier 2010

(14)

New data types

sums

dimanche 10 janvier 2010

(15)

New data types

recursive types

dimanche 10 janvier 2010

(16)

New data types

recursive types

dimanche 10 janvier 2010

(17)

New data types

recursive types

recursive polymorphic types

dimanche 10 janvier 2010

(18)

New data types

recursive types

recursive polymorphic types

dimanche 10 janvier 2010

(19)

New data types

recursive polymorphic types (alternative definition for binary trees)

dimanche 10 janvier 2010

(20)

New data types

recursive polymorphic types (alternative definition for binary trees)

dimanche 10 janvier 2010

(21)

Caring about space

with an extra argument as an accumulator of the result

dimanche 10 janvier 2010

(22)

Combien d’objets dans une image?

Jean- Jacques L évy INRIA

dimanche 10 janvier 2010

(23)

Labeling

16 objects in this picture 1

1

2 3

4 5 6

7

8 9

10

11 12

13 14

dimanche 10 janvier 2010

(24)

Naive algorithm

1) choose an unvisited pixel

2) traverse all similar connected pixels 3) and restart until all pixel are visited

Very high complexity:

-

how to find an unvisited pixel ?

-

how organizing exploration of connected pixels (which direction?)

dimanche 10 janvier 2010

(25)

Algorithm

1) first pass

-

scan pixels left-to-right, top-to-bottom giving a new object id each time a new object is met

2) second pass

-

generate equivalences between ids due to new adjacent relations met during scan of pixels.

3) third pass

-

compute the number of equivalence classes

Complexity:

-

scan twice full image (linear cost)

-

try to efficiently manage equivalence classes (Union-Find by Tarjan)

dimanche 10 janvier 2010

(26)

Animation

with Polka system for animated algorithms

dimanche 10 janvier 2010

(27)

Exercise for next class

find an algorithm for the labeling algorithm

dimanche 10 janvier 2010

(28)

Equivalence classes

dimanche 10 janvier 2010

(29)

Equivalence classes

“Union-Find”

objects

• equivalences

• find the equivalence class of x

1

, x

2

, ... , x

n

x

p

• 3 operations:

-

- -

NEW(x ) FIND(x )

UNION(x , y )

new object

find canonical representative merge 2 equivalence classes

dimanche 10 janvier 2010

(30)

Equivalence classes

“Union-Find”

• with tree-like structure x

9

x

7

x

5

x

10

x

1

x

4

x

2

x

6

x

3

x

8

x

11

x

12

x

13

• given by the array of direct ancestors

7 9 9 7

9

0 2 11 1 2 8 8 13 8

ancestor

dimanche 10 janvier 2010

(31)

Equivalence classes

“Union-Find”

• unbalanced trees because of merges

-

UNION(x , y )

merge 2 equivalence classes

x

9

x

7

x

5

x

10

x

1

x

4

x

3

x

8

x

11

x

12

x

13

x

6

x

2

x

6

= x

13

dimanche 10 janvier 2010

(32)

Equivalence classes

“Union-Find”

• try to balance

-

UNION(x , y )

merge 2 equivalence classes

x

9

x

7

x

5

x

10

x

1

x

4

x

3

x

8

x

11

x

12

x

13

x

6

x

2

therefore keep height at each node opérations pour

O (log n)

FIND(x)

dimanche 10 janvier 2010

(33)

Equivalence classes

“Union-Find”

• compression of path toward canonical representative

-

FIND(x )

with side-effect on tree structure

x

9

x

7

x

5

x

10

x

1

x

4

x

6

x

2

x

9

x

7

x

5

x

10

x

1

x

4

x

6

x

2

FIND(x

4

)

dimanche 10 janvier 2010

(34)

Exercice for next class

find good primitives for bitmap graphics in Ocaml library

design the overall structure of the labeling program

dimanche 10 janvier 2010

Références

Documents relatifs

• Scheme, SML, Ocaml, Haskell are functional programming languages.. • they

• Scheme, SML, Ocaml, Haskell are functional programming languages. • they

• recursive polymorphic types (alternative definition for binary trees). New

• module Graphics is a portable graphics library (needs graphics.cma to be compiled as first argument of the ocamlc command). • module names ( List ) start with

• mutable values can’t have real polymorphic types (see below). • they are not considered as real values only values have true

• an colorful array of great number of colors to pick in to show final objects with distinct colors [take the one given on next slide]. jeudi 3

- scan pixels left-to-right, top-to-bottom giving a new object id each time a new object is met, can be done on dual-core with half image per core.. 2)

Our type system is built by starting with the concept of a finite partial map to describe the store and then using a combination of standard type constructors such as singleton,