Docsity

Programming Languages: OOP vs. Functional Programming and Stacks in Java and OCaml - Prof., Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 330: Organization of
Programming Languages
Objects vs. Functional
Programming
CMSC 330 2
OOP vs. FP
Object-oriented programming (OOP)
•
Computation as interactions between objects
•
Objects encapsulate mutable data (state)
Accessed / modified via object’s public methods
Functional programming (FP)
•
Computation as evaluation of functions
Mutable data used to improve efficiency
•
Higher-order functions implemented as closures
Closure = function + environment
CMSC 330 3
An Integer “Stack” Abstraction in Java
class Stack {
class Node {
Integer val; Node next;
Node(Integer v, Node n) { val = v; next = n; }
};
private Node theStack;
void push(Integer v) {
theStack = new Node(v, theStack);
}
Integer pop() {
if (theStack == null)
throw new NoSuchElementException();
Integer temp = theStack.val;
theStack = theStack.next;
return temp;
}
}
CMSC 330 4
A “Stack” Abstraction in OCaml
module type STACK =
sig
type 'a stack
val new_stack : unit -> 'a stack
val push : 'a stack -> 'a -> unit
val pop : 'a stack -> 'a
end
module Stack : STACK =
struct
type 'a stack = 'a list ref
let new_stack () = ref []
let push s x = s := (x::!s)
let pop s = match !s with
[] -> failwith "Empty stack"
| (h::t) -> s := t; h
end
CMSC 330 5
Another “Stack” Abstraction in OCaml
let new_stack () =
let this = ref [] in
let push x = this := (x::!this)
and pop () = match !this with
[] -> failwith "Empty stack"
| (h::t) -> this := t; h
in
(push, pop)
# let s = new_stack ();;
val s : ('_a -> unit) * (unit -> '_a) = (<fun>, <fun>)
# Pervasives.fst s 3;; (* applies 1
st
part of s to 3 *)
- : unit = ()
# Pervasives.snd s ();; (* applies 2
nd
part of s to () *)
- : int = 3
CMSC 330 6
Two OCaml Stack Implementations
1
st
implementation (OOP style)
•
Based on modules
•
Specifies methods for
Creating stack
Pushing value onto stack parameter
Popping value from stack parameter
2
nd
implementation (FP style)
•
Based on closures
•
Creating stack returns tuple containing
Closure for pushing value onto created stack
Closure for popping value from created stack
pf3

Partial preview of the text

Download Programming Languages: OOP vs. Functional Programming and Stacks in Java and OCaml - Prof. and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

Objects vs. Functional

Programming

CMSC 330 2

OOP vs. FP

Object-oriented programming (OOP)

  • Computation as interactions between objects
  • Objects encapsulate mutable data (state)  Accessed / modified via object’s public methods

Functional programming (FP)

  • Computation as evaluation of functions  Mutable data used to improve efficiency
  • Higher-order functions implemented as closures  Closure = function + environment

CMSC 330 3

An Integer “Stack” Abstraction in Java

class Stack { class Node { Integer val; Node next; Node(Integer v, Node n) { val = v; next = n; } }; private Node theStack; void push(Integer v) { theStack = new Node(v, theStack); } Integer pop() { if (theStack == null) throw new NoSuchElementException(); Integer temp = theStack.val; theStack = theStack.next; return temp; } }

CMSC 330 4

A “Stack” Abstraction in OCaml

module type STACK = sig type 'a stack val new_stack : unit -> 'a stack val push : 'a stack -> 'a -> unit val pop : 'a stack -> 'a end

module Stack : STACK = struct type 'a stack = 'a list ref let new_stack () = ref [] let push s x = s := (x::!s) let pop s = match !s with [] -> failwith "Empty stack" | (h::t) -> s := t; h end

CMSC 330 5

Another “Stack” Abstraction in OCaml

let new_stack () = let this = ref [] in let push x = this := (x::!this) and pop () = match !this with [] -> failwith "Empty stack" | (h::t) -> this := t; h in (push, pop)

let s = new_stack ();;

val s : ('_a -> unit) * (unit -> '_a) = (, )

Pervasives.fst s 3;; (* applies 1st^ part of s to 3 *)

  • : unit = ()

Pervasives.snd s ();; (* applies 2nd^ part of s to () *)

  • : int = 3

CMSC 330 6

Two OCaml Stack Implementations

1 st^ implementation (OOP style)

  • Based on modules
  • Specifies methods for  Creating stack  Pushing value onto stack parameter  Popping value from stack parameter

2 nd^ implementation (FP style)

  • Based on closures
  • Creating stack returns tuple containing  Closure for pushing value onto created stack  Closure for popping value from created stack

CMSC 330 7

Relating Objects and Closures

An object...

  • Is a collection of fields (data)
  • ...and methods (code)
  • When a method is invoked  Method has implicit this parameter that can be used to access fields of object

A closure...

  • Is a pointer to an environment (data)
  • ...and a function body (code)
  • When a closure is invoked  Function has implicit environment that can be used to access variables

CMSC 330 8

Relating Objects and Closures (cont.)

class C { int x = 0; void set_x(int y) { x = y; } int get_x() { return x; } }

let make () = let x = ref 0 in ( (fun y -> x := y), (fun () -> !x) )

x = 0

C c = new C(); c.set_x(3); int y = c.get_x();

x = ref 0

fun y -> x := y fun () -> !x

let (set, get) = make ();; set 3;; let y = get ();;

CMSC 330 9

Encoding Objects with Functions

We can apply this transformation in general

  • becomes
  • make ( ) is like the constructor
  • The closure environment contains the fields

class C { f1 ... fn; m1 ... mn; }

let make () = let f1 = ... ... and fn = ... in ( fun ... , (* body of m1 ) ... fun ..., ( body of mn *) )

Tuple containing closures

CMSC 330 10

Recall a Useful Higher-Order Function

Map applies an arbitrary function f

  • To each element of a list
  • And returns the resulting modified list

Can we encode this in Java?

  • Using object oriented programming
let rec map f = function
[] -> []
| (h::t) -> (f h)::(map f t)

CMSC 330 11

A Map Method for Stack

Problem – Write a map method in Java

  • Must pass a function into another function

Solution

  • Can be done using an object with a known method
  • Use interface to specify what method must be present

public interface Function { Integer eval(Integer arg); }

CMSC 330 12

A Map Method for Stack (cont.)

Examples

  • Two classes which both implement Function interface

class AddOne implements Function { Integer eval(Integer arg) { return new Integer(arg + 1); } }

class MultTwo implements Function { Integer eval(Integer arg) { return new Integer(arg * 2); } }