Docsity

Lecture Notes on Object Oriented Programming with OCaml | CMSC 330, Study notes of Programming Languages

Partial preview of the text

Download Lecture Notes on Object Oriented Programming with OCaml | CMSC 330 and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

Object Oriented Programming

with OCaml

OCaml Data

• So far, we’ve seen the following kinds of data:

– Basic types (int, float, char, string)

– Lists

  • (^) One kind of data structure
  • (^) A list is either [] or h::t, deconstructed with pattern matching

– Tuples

  • (^) Let you collect data together in fixed-size pieces

– Functions

• How can we build other data structures?

– Building everything from lists and tuples is awkward

Data Types, con't.

type shape =

Rect of float * float (* width * length ) | Circle of float ( radius *)

let x = [Rect (3.0, 4.0) ; Circle 3.0; Rect (10.0, 22.5)]

• What's the type of x?

• What's the type of x's first element?

x : shape list

shape

Data Types (cont’d)

• The arity of a constructor is the number of

arguments it takes

– A constructor with no arguments is nullary

– Constructors must begin with uppercase letter

type optional_int = None | Some of int

let add_with_default a = function None -> a + 42 | Some n -> a + n

add_with_default 3 None (* 45 ) add_with_default 3 (Some 4) ( 7 *)

NOTES

type int_option = None | Some of int;;

The OCaml compiler will warn of a function matching only Some … values and neglecting the None value:

let extract = function Some i -> i;;

Warning: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: None val extract : int_option -> int = This extract function then works as expected on Some … values:

extract (Some 3);;

  • : int = 3 but causes a Match_failure exception to be raised at run-time if a None value is given, as none of the patterns in the pattern match of the extract function match this value:

extract None;;

Exception: Match_failure ("", 5, -40).

Recursive Data Types

• We can build up lists this way:

– Note: Don’t have nice [1; 2; 3] syntax for this kind of

list

type 'a list = Nil | Cons of 'a * 'a list

let rec length l = function Nil -> 0 | Cons (_, t) -> 1 + (length t)

length (Cons (10, Cons (20, Cons (30, Nil))))

Exercise: A Binary Tree Data Type

• Write type bin_tree for ordered binary trees over int

• Implement the following

empty : bin_tree is_empty : bin_tree -> bool contains : int -> bin_tree -> bool insert : int -> bin_tree -> bin_tree equals : bin_tree -> bin_tree -> bool map : (int -> int) -> bin_tree -> bin_tree fold : ('a -> int -> 'a) -> 'a -> bin_tree -> 'a

• What about remove?

Creating a Module

module Shapes = struct type shape = Rect of float * float (* width * length ) | Circle of float ( radius *)

let area = function Rect (w, l) -> w *. l | Circle r -> r *. r *. 3.

let unit_circle = Circle 1. end;;

unit_circle;; (* not defined ) Shapes.unit_circle;; Shapes.area (Shapes.Rect (3.0, 4.0));; open Shapes;; ( import all names into current scope ) unit_circle;; ( now defined *)

Modularity and Abstraction

• Another reason for creating a module is so we

can hide details

– For example, we can build a binary tree module, but

we may not want to expose our exact representation

of binary trees

– This is also good software engineering practice

Module Signatures (cont’d)

• The convention is for signatures to be all capital

letters

– This isn't a strict requirement, though

• Items can be omitted from a module signature

– This provides the ability to hide values

• The default signature for a module hides nothing

Abstract Types in Signatures

• Now definition of shape is hidden

module type SHAPES = sig type shape val area : shape -> float val unit_circle : shape val make_circle : float -> shape val make_rect : float -> float -> shape end;;

module Shapes : SHAPES = struct ... let make_circle r = Circle r let make_rect x y = Rect (x, y) end

Note for Project 4

• You may wish to put the “nfa” type definition in

the signature to get better debugging information

from Ocaml

• REMOVE IT BEFORE YOU SUBMIT

.ml and .mli files

• General structure: put the signature in a foo.mli

file, the struct in a foo.ml file

– Use the same names

– Omit the sig...end and struct...end parts

– The OCaml compiler will make a Foo module from

these

• Similar to C file structure (.c/.h)

• Not implemented in project 4 for simplicity

Imperative OCaml

• There are three basic operations on memory:

  • (^) ref : 'a -> 'a ref
    • (^) Allocate an updatable reference
  • (^)! : 'a ref -> 'a
    • (^) Read the value stored in reference
  • (^) := : 'a ref -> 'a -> unit
    • (^) Write to a reference

let x = ref 3 (* x : int ref *) let y = !x x := 4

Comparison to L- and R-values

• Recall that in C/C++/Java, there’s a strong

distinction between l- and r-values

• A variable's meaning depends on where it appears

in an assignment

– On the right-hand side, it’s an r-value, and it refers to

the contents of the variable

– On the left-hand side, it’s an l-value, and it refers to the

location the variable is stored in