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
.
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
To install the graphics library on your appliance:
curl -sL http://goo.gl/f8J4L6 | sh
.
Requirements:
2:1
(Java)
3:1
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
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
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
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
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
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
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
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
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
— To Emacs —
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
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
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
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
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
— To Emacs —
▶ 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
▶ Represented as OCaml record ▶ Collection of named values—instance variables ▶ Collection of operations—methods
▶ 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
▶ Represented as OCaml record ▶ Collection of named values—instance variables ▶ Collection of operations—methods
▶ An OCaml record type ▶ Instance variables are hidden ▶ All interaction is via methods
▶ Easy to add new kinds of display elements ▶ Each object defines its own behavior
But not so easy to add new methods…
9:3
▶ Represented as OCaml record ▶ Collection of named values—instance variables ▶ Collection of operations—methods
▶ An OCaml record type ▶ Instance variables are hidden ▶ All interaction is via methods
▶ Easy to add new kinds of display elements ▶ Each object defines its own behavior ▶ But not so easy to add new methods…
9:4
▶ Hard to add new alternatives
(production)
▶ Easy to add new operations
(consumption)
▶ Easy to add new alternatives
(production)
▶ Hard to add new operations
(consumption)
10:1
▶ Hard to add new alternatives (production) ▶ Easy to add new operations (consumption)
▶ Easy to add new alternatives (production) ▶ Hard to add new operations (consumption)
10:2
▶ 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
▶ Specify how to build objects ▶ Like our functions rect, circ, etc.
▶ 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
▶ Specify how to build objects ▶ Like our functions rect, circ, etc.
▶ Base new classes on existing ones ▶ Can change or extend behavior
(is not inheritance!)
▶ Use an object where a smaller interface is expected
C++, Java, and C# confuse subtyping with inheritance
11:3
▶ Specify how to build objects ▶ Like our functions rect, circ, etc.
▶ Base new classes on existing ones ▶ Can change or extend behavior
▶ Use an object where a smaller interface is expected ▶ C++, Java, and C# confuse subtyping with inheritance
11:4
class type display_elt =
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 =
. .
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
class type display_elt =
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 =
.
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
class type display_elt =
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 =
.
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
class type display_elt =
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 =
.
.
.
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
class type display_elt =
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 =
.
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
class type display_elt =
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 =
.
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
class type display_elt =
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 =
.
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
class type display_elt =
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 =
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
— To Emacs —
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
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
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
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
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
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
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
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
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
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
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
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
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
— To Emacs —