Putting the O in OCaml CS 51 and CSCI E-51 March 27, 2014 To - - PowerPoint PPT Presentation

putting the o in ocaml
SMART_READER_LITE
LIVE PREVIEW

Putting the O in OCaml CS 51 and CSCI E-51 March 27, 2014 To - - PowerPoint PPT Presentation

Putting the O in OCaml CS 51 and CSCI E-51 March 27, 2014 To install the graphics library on your appliance: . curl -sL http://goo.gl/f8J4L6 | sh But first, a word about final projects Requirements: Teams of 2 to 4 Any language


slide-1
SLIDE 1

Putting the “O” in OCaml

CS 51 and CSCI E-51 March 27, 2014

To install the graphics library on your appliance:

curl -sL http://goo.gl/f8J4L6 | sh

.

slide-2
SLIDE 2

But first, a word about final projects

Requirements:

  • Teams of 2 to 4
  • Any language you please
  • Must be computationally interesting
  • Proposals due 4/4 (a week from tomorrow)
  • More details will be forthcoming…

2:1

slide-3
SLIDE 3

Some example projects

  • A document search suite and plagarism detector (OCaml)
  • A version control system, focusing on the rsync and compression algorithms

(Java)

  • Machine learning for games: genetic algorithm & neural network to find
  • ptimum play for a strategy game (Java)
  • A simplex solver with a complete matrix module (OCaml)
  • Collaborative filtering using five algorithms (OCaml)
  • Mini-WolframAlpha (OCaml)
  • Comparison of dictionary performance with 6 different data structures (Java)
  • SAT solver (OCaml)
  • Content-aware image resizing (Python)
  • Natural language parsing (Python)

3:1

slide-4
SLIDE 4

A crude GUI

type point = { x : int; y : int } type rect = { rect_ll : point; rect_width : int; rect_height : int } type circ = { circ_center : point; circ_radius : int } type text = { text_ll : point; text_value : string } type display_elt = Rectangle of rect | Circle of circ | Text of text type scene = display_elt list

4:1

slide-5
SLIDE 5

A crude GUI

type point = { x : int; y : int } type rect = { rect_ll : point; rect_width : int; rect_height : int } type circ = { circ_center : point; circ_radius : int } type text = { text_ll : point; text_value : string } type display_elt = Rectangle of rect | Circle of circ | Text of text type scene = display_elt list

4:2

slide-6
SLIDE 6

A crude GUI

type point = { x : int; y : int } type rect = { rect_ll : point; rect_width : int; rect_height : int } type circ = { circ_center : point; circ_radius : int } type text = { text_ll : point; text_value : string } type display_elt = Rectangle of rect | Circle of circ | Text of text type scene = display_elt list

4:3

slide-7
SLIDE 7

Rendering

let draw (elt : display_elt) : unit = match elt with | Rectangle r ->

.

fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c ->

.

fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t ->

.

moveto t.text_ll.x t.text_ll.y;

.

draw_string t.text_value let draw_scene : scene -> unit = List.iter ~f:draw

5:1

slide-8
SLIDE 8

Rendering

let draw (elt : display_elt) : unit = match elt with | Rectangle r ->

.

.

.

fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c ->

.

fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t ->

.

moveto t.text_ll.x t.text_ll.y;

.

draw_string t.text_value let draw_scene : scene -> unit = List.iter ~f:draw

5:2

slide-9
SLIDE 9

Rendering

let draw (elt : display_elt) : unit = match elt with | Rectangle r ->

.

fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c ->

.

.

.

fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t ->

.

moveto t.text_ll.x t.text_ll.y;

.

draw_string t.text_value let draw_scene : scene -> unit = List.iter ~f:draw

5:3

slide-10
SLIDE 10

Rendering

let draw (elt : display_elt) : unit = match elt with | Rectangle r ->

.

fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c ->

.

fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t ->

.

.

.

moveto t.text_ll.x t.text_ll.y;

.

draw_string t.text_value let draw_scene : scene -> unit = List.iter ~f:draw

5:4

slide-11
SLIDE 11

Rendering

let draw (elt : display_elt) : unit = match elt with | Rectangle r ->

.

fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c ->

.

fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t ->

.

moveto t.text_ll.x t.text_ll.y;

.

.

.

draw_string t.text_value let draw_scene : scene -> unit = List.iter ~f:draw

5:5

slide-12
SLIDE 12

Rendering

let draw (elt : display_elt) : unit = match elt with | Rectangle r ->

.

fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c ->

.

fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t ->

.

moveto t.text_ll.x t.text_ll.y;

.

draw_string t.text_value let draw_scene : scene -> unit = List.iter ~f:draw

5:6

slide-13
SLIDE 13

— To Emacs —

slide-14
SLIDE 14

A different approach

type display_elt = { draw : unit -> unit } let rect (ll : point) (width : int) (height : int) : display_elt = { draw = fun () -> fill_rect ll.x ll.y width height } let circ (center : point) (radius : int) : display_elt = { draw = fun () -> fill_circle center.x center.y radius } let text (ll : point) (value : string) : display_elt = { draw = fun () -> moveto ll.x ll.y; draw_string value } type scene = display_elt list

7:1

slide-15
SLIDE 15

A different approach

type display_elt = { draw : unit -> unit } let rect (ll : point) (width : int) (height : int) : display_elt = { draw = fun () -> fill_rect ll.x ll.y width height } let circ (center : point) (radius : int) : display_elt = { draw = fun () -> fill_circle center.x center.y radius } let text (ll : point) (value : string) : display_elt = { draw = fun () -> moveto ll.x ll.y; draw_string value } type scene = display_elt list

7:2

slide-16
SLIDE 16

A different approach

type display_elt = { draw : unit -> unit } let rect (ll : point) (width : int) (height : int) : display_elt = { draw = fun () -> fill_rect ll.x ll.y width height } let circ (center : point) (radius : int) : display_elt = { draw = fun () -> fill_circle center.x center.y radius } let text (ll : point) (value : string) : display_elt = { draw = fun () -> moveto ll.x ll.y; draw_string value } type scene = display_elt list

7:3

slide-17
SLIDE 17

A different approach

type display_elt = { draw : unit -> unit } let rect (ll : point) (width : int) (height : int) : display_elt = { draw = fun () -> fill_rect ll.x ll.y width height } let circ (center : point) (radius : int) : display_elt = { draw = fun () -> fill_circle center.x center.y radius } let text (ll : point) (value : string) : display_elt = { draw = fun () -> moveto ll.x ll.y; draw_string value } type scene = display_elt list

7:4

slide-18
SLIDE 18

A different approach

type display_elt = { draw : unit -> unit } let rect (ll : point) (width : int) (height : int) : display_elt = { draw = fun () -> fill_rect ll.x ll.y width height } let circ (center : point) (radius : int) : display_elt = { draw = fun () -> fill_circle center.x center.y radius } let text (ll : point) (value : string) : display_elt = { draw = fun () -> moveto ll.x ll.y; draw_string value } type scene = display_elt list

7:5

slide-19
SLIDE 19

— To Emacs —

slide-20
SLIDE 20

This is OO

  • Objects

▶ Represented as OCaml record ▶ Collection of named values—instance variables ▶ Collection of operations—methods

Interfaces

An OCaml record type Instance variables are hidden All interaction is via methods

Extensible types

Easy to add new kinds of display elements Each object defines its own behavior But not so easy to add new methods…

9:1

slide-21
SLIDE 21

This is OO

  • Objects

▶ Represented as OCaml record ▶ Collection of named values—instance variables ▶ Collection of operations—methods

  • Interfaces

▶ An OCaml record type ▶ Instance variables are hidden ▶ All interaction is via methods

Extensible types

Easy to add new kinds of display elements Each object defines its own behavior But not so easy to add new methods…

9:2

slide-22
SLIDE 22

This is OO

  • Objects

▶ Represented as OCaml record ▶ Collection of named values—instance variables ▶ Collection of operations—methods

  • Interfaces

▶ An OCaml record type ▶ Instance variables are hidden ▶ All interaction is via methods

  • Extensible types

▶ Easy to add new kinds of display elements ▶ Each object defines its own behavior

But not so easy to add new methods…

9:3

slide-23
SLIDE 23

This is OO

  • Objects

▶ Represented as OCaml record ▶ Collection of named values—instance variables ▶ Collection of operations—methods

  • Interfaces

▶ An OCaml record type ▶ Instance variables are hidden ▶ All interaction is via methods

  • Extensible types

▶ Easy to add new kinds of display elements ▶ Each object defines its own behavior ▶ But not so easy to add new methods…

9:4

slide-24
SLIDE 24

Datatypes versus objects

  • Datatypes are closed

▶ Hard to add new alternatives

(production)

▶ Easy to add new operations

(consumption)

  • Objects are open

▶ Easy to add new alternatives

(production)

▶ Hard to add new operations

(consumption)

10:1

slide-25
SLIDE 25

Datatypes versus objects

  • Datatypes are closed

▶ Hard to add new alternatives (production) ▶ Easy to add new operations (consumption)

  • Objects are open

▶ Easy to add new alternatives (production) ▶ Hard to add new operations (consumption)

10:2

slide-26
SLIDE 26

Other OO concepts

  • Classes

▶ Specify how to build objects ▶ Like our functions rect, circ, etc.

Inheritance (implementation reuse)

Base new classes on existing ones Can change or extend behavior

Subtyping (is not inheritance!)

Use an object where a smaller interface is expected C++, Java, and C# confuse subtyping with inheritance

11:1

slide-27
SLIDE 27

Other OO concepts

  • Classes

▶ Specify how to build objects ▶ Like our functions rect, circ, etc.

  • Inheritance (implementation reuse)

▶ Base new classes on existing ones ▶ Can change or extend behavior

Subtyping (is not inheritance!)

Use an object where a smaller interface is expected C++, Java, and C# confuse subtyping with inheritance

11:2

slide-28
SLIDE 28

Other OO concepts

  • Classes

▶ Specify how to build objects ▶ Like our functions rect, circ, etc.

  • Inheritance (implementation reuse)

▶ Base new classes on existing ones ▶ Can change or extend behavior

  • Subtyping

(is not inheritance!)

▶ Use an object where a smaller interface is expected

C++, Java, and C# confuse subtyping with inheritance

11:3

slide-29
SLIDE 29

Other OO concepts

  • Classes

▶ Specify how to build objects ▶ Like our functions rect, circ, etc.

  • Inheritance (implementation reuse)

▶ Base new classes on existing ones ▶ Can change or extend behavior

  • Subtyping (is not inheritance!)

▶ Use an object where a smaller interface is expected ▶ C++, Java, and C# confuse subtyping with inheritance

11:4

slide-30
SLIDE 30

The “O” in OCaml

class type display_elt =

  • bject

method draw : unit method get_color : color method set_color : color -> unit method translate : dx:int -> dy:int -> unit end class

. .

rect

. .

(ll : point) (width : int) (height : int) : display_elt =

  • bject

. .

val mutable ll = ll

. .

val mutable color = black

. .

method get_color = color

. .

method set_color c = color <- c

. .

method draw = set_color color;.

.

fill_rect ll.x ll.y width height method translate ~dx ~dy = ll <- translate_point ll ~dx ~dy end

12:1

slide-31
SLIDE 31

The “O” in OCaml

class type display_elt =

  • bject

method draw : unit method get_color : color method set_color : color -> unit method translate : dx:int -> dy:int -> unit end class

.

..

rect

.

(ll : point) (width : int) (height : int) : display_elt =

  • bject

.

val mutable ll = ll

.

val mutable color = black

.

method get_color = color

.

method set_color c = color <- c

.

method draw = set_color color;. fill_rect ll.x ll.y width height method translate ~dx ~dy = ll <- translate_point ll ~dx ~dy end

12:2

slide-32
SLIDE 32

The “O” in OCaml

class type display_elt =

  • bject

method draw : unit method get_color : color method set_color : color -> unit method translate : dx:int -> dy:int -> unit end class

.

rect

. . .

(ll : point) (width : int) (height : int) : display_elt =

  • bject

.

val mutable ll = ll

.

val mutable color = black

.

method get_color = color

.

method set_color c = color <- c

.

method draw = set_color color;. fill_rect ll.x ll.y width height method translate ~dx ~dy = ll <- translate_point ll ~dx ~dy end

12:3

slide-33
SLIDE 33

The “O” in OCaml

class type display_elt =

  • bject

method draw : unit method get_color : color method set_color : color -> unit method translate : dx:int -> dy:int -> unit end class

.

rect

.

(ll : point) (width : int) (height : int) : display_elt =

  • bject

.

.

.

val mutable ll = ll

.

.

.

val mutable color = black

.

method get_color = color

.

method set_color c = color <- c

.

method draw = set_color color;. fill_rect ll.x ll.y width height method translate ~dx ~dy = ll <- translate_point ll ~dx ~dy end

12:4

slide-34
SLIDE 34

The “O” in OCaml

class type display_elt =

  • bject

method draw : unit method get_color : color method set_color : color -> unit method translate : dx:int -> dy:int -> unit end class

.

rect

.

(ll : point) (width : int) (height : int) : display_elt =

  • bject

.

val mutable ll = ll

.

val mutable color = black

.

.

.

method get_color = color

.

method set_color c = color <- c

.

method draw = set_color color;. fill_rect ll.x ll.y width height method translate ~dx ~dy = ll <- translate_point ll ~dx ~dy end

12:5

slide-35
SLIDE 35

The “O” in OCaml

class type display_elt =

  • bject

method draw : unit method get_color : color method set_color : color -> unit method translate : dx:int -> dy:int -> unit end class

.

rect

.

(ll : point) (width : int) (height : int) : display_elt =

  • bject

.

val mutable ll = ll

.

val mutable color = black

.

method get_color = color

.

.

.

method set_color c = color <- c

.

method draw = set_color color;. fill_rect ll.x ll.y width height method translate ~dx ~dy = ll <- translate_point ll ~dx ~dy end

12:6

slide-36
SLIDE 36

The “O” in OCaml

class type display_elt =

  • bject

method draw : unit method get_color : color method set_color : color -> unit method translate : dx:int -> dy:int -> unit end class

.

rect

.

(ll : point) (width : int) (height : int) : display_elt =

  • bject

.

val mutable ll = ll

.

val mutable color = black

.

method get_color = color

.

method set_color c = color <- c

.

.

.

method draw = set_color color;.

.

.

fill_rect ll.x ll.y width height method translate ~dx ~dy = ll <- translate_point ll ~dx ~dy end

12:7

slide-37
SLIDE 37

Another class

class type display_elt =

  • bject

method draw : unit method get_color : color method set_color : color -> unit method translate : dx:int -> dy:int -> unit end class circ (center : point) (radius : int) : display_elt =

  • bject

val mutable center = center val mutable color = black method get_color = color method set_color c = color <- c method draw = set_color color; fill_circle center.x center.y radius method translate ~dx ~dy = center <- translate_point center ~dx ~dy end

13:1

slide-38
SLIDE 38

— To Emacs —

slide-39
SLIDE 39

Comparison to Java (1/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public class Shape {

. .

protected Point pos;

. .

protected Color color;

. .

public Shape(Point p) { pos = p; }

. .

public Color getColor() { return color; }

. .

public void setColor(Color c) { color = c; }

. .

public void translate(int dx, int dy) { pos = pos.translate(dx, dy) } }

15:1

slide-40
SLIDE 40

Comparison to Java (1/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public class Shape {

.

protected Point pos;

.

protected Color color;

.

public Shape(Point p) { pos = p; }

.

public Color getColor() { return color; }

.

public void setColor(Color c) { color = c; }

.

public void translate(int dx, int dy) { pos = pos.translate(dx, dy) } }

15:2

slide-41
SLIDE 41

Comparison to Java (1/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public class Shape {

.

.

.

protected Point pos;

.

.

.

protected Color color;

.

public Shape(Point p) { pos = p; }

.

public Color getColor() { return color; }

.

public void setColor(Color c) { color = c; }

.

public void translate(int dx, int dy) { pos = pos.translate(dx, dy) } }

15:3

slide-42
SLIDE 42

Comparison to Java (1/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public class Shape {

.

protected Point pos;

.

protected Color color;

.

.

.

public Shape(Point p) { pos = p; }

.

public Color getColor() { return color; }

.

public void setColor(Color c) { color = c; }

.

public void translate(int dx, int dy) { pos = pos.translate(dx, dy) } }

15:4

slide-43
SLIDE 43

Comparison to Java (1/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public class Shape {

.

protected Point pos;

.

protected Color color;

.

public Shape(Point p) { pos = p; }

. . .

public Color getColor() { return color; }

. . .

public void setColor(Color c) { color = c; }

.

.

.

public void translate(int dx, int dy) { pos = pos.translate(dx, dy) } }

15:5

slide-44
SLIDE 44

Comparison to Java (2/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public static class Rect

. .

extends Shape

. .

implements DisplayElt{

. .

private final int width, height; public

. .

Rect(Point ll, int w, int h) {

. .

super(ll);

. .

width = w; height = h; } public void draw() { gc.setColor(color); gc.fillRect(pos.x, pos.y, width, height); } }

16:1

slide-45
SLIDE 45

Comparison to Java (2/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public static class Rect

.

extends Shape

.

implements DisplayElt{

.

private final int width, height; public

.

Rect(Point ll, int w, int h) {

.

super(ll);

.

width = w; height = h; } public void draw() { gc.setColor(color); gc.fillRect(pos.x, pos.y, width, height); } }

16:2

slide-46
SLIDE 46

Comparison to Java (2/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public static class Rect

.

.

.

extends Shape

.

implements DisplayElt{

.

private final int width, height; public

.

Rect(Point ll, int w, int h) {

.

super(ll);

.

width = w; height = h; } public void draw() { gc.setColor(color); gc.fillRect(pos.x, pos.y, width, height); } }

16:3

slide-47
SLIDE 47

Comparison to Java (2/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public static class Rect

.

extends Shape

.

.

.

implements DisplayElt{

.

private final int width, height; public

.

Rect(Point ll, int w, int h) {

.

super(ll);

.

width = w; height = h; } public void draw() { gc.setColor(color); gc.fillRect(pos.x, pos.y, width, height); } }

16:4

slide-48
SLIDE 48

Comparison to Java (2/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public static class Rect

.

extends Shape

.

implements DisplayElt{

.

.

.

private final int width, height; public

.

Rect(Point ll, int w, int h) {

.

super(ll);

.

width = w; height = h; } public void draw() { gc.setColor(color); gc.fillRect(pos.x, pos.y, width, height); } }

16:5

slide-49
SLIDE 49

Comparison to Java (2/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public static class Rect

.

extends Shape

.

implements DisplayElt{

.

private final int width, height; public

.

.

.

Rect(Point ll, int w, int h) {

.

super(ll);

.

width = w; height = h; } public void draw() { gc.setColor(color); gc.fillRect(pos.x, pos.y, width, height); } }

16:6

slide-50
SLIDE 50

Comparison to Java (2/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public static class Rect

.

extends Shape

.

implements DisplayElt{

.

private final int width, height; public

.

Rect(Point ll, int w, int h) {

.

.

.

super(ll);

.

width = w; height = h; } public void draw() { gc.setColor(color); gc.fillRect(pos.x, pos.y, width, height); } }

16:7

slide-51
SLIDE 51

Comparison to Java (2/2)

public interface DisplayElt { void draw(); Color getColor(); void setColor(Color c); void translate(int dx, int dy); } public static class Rect

.

extends Shape

.

implements DisplayElt{

.

private final int width, height; public

.

Rect(Point ll, int w, int h) {

.

super(ll);

.

.

.

width = w; height = h; } public void draw() { gc.setColor(color); gc.fillRect(pos.x, pos.y, width, height); } }

16:8

slide-52
SLIDE 52

— To Emacs —