coms w4115 programming languages & translators maria ayako taku mat2185@columbia.edu coms w4115...

Post on 11-Jan-2016

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

COMS W4115Programming Languages & Translators

Maria Ayako Takumat2185@columbia.edu

COMS W4115 - PLT Columbia University 1 April 24, 2013

Functional Programming Languagesand the Influence of Lambda Calculus

Lecture 23

Why Functional Programming?

COMS W4115 - PLT Columbia University 2 April 24, 2013

You may have heard that:

(1) functional programs are

very succinct and elegant,

but…

(2) they take forever to

write and understand

let xor p = match pwith (false, x) -> x | (true, x) -> not x;;

Simple XOR program:

Outline

COMS W4115 - PLT Columbia University 3 April 24, 2013

I. What is Functional Programming?

II. Lambda Calculus' Influence on Functional

Programming

III. Benefits of Functional Programming

IV. OCaml in Action: An Entire Interpreter in

3 slides

What is Functional Programming?

COMS W4115 - PLT Columbia University 4 April 24, 2013

• A "programming paradigm" that focuses on the evaluation of functions

• Programs are defined as recursive functions, where each takes a single input and outputs a single result.

• Recall lambda calculus nests functions in this manner. Ex: (+ (* 1 2) (- 4 3))

let f = let a = 1*2 in let b = 4-3 in a + b;;

In OCaml:

What is Functional Programming?

COMS W4115 - PLT Columbia University 5 April 24, 2013

• One important trademark of Functional Programming:

It avoids both changes in state and mutable data

These expressions are

not equal.

Conceptually, they

are more like this.

//F# //C#let a=42; int a=42;

//F# //C#let a=42; static int a(){

return 42;}

http://johnnycoder.com/blog/2009/04/20/functional-programming-part-1/

What is Functional Programming?

COMS W4115 - PLT Columbia University 6 April 24, 2013

Some Common (more or less) FP Languages:

• LISP• OCaml• Haskell• Erlang• F# (multi-paradigm)

So what's the value in functional programming?

Are its benefits worth taking the time to learn it and overcoming the (possibly) steep learning curve?

Outline

COMS W4115 - PLT Columbia University 7 April 24, 2013

I. What is Functional Programming?

II. Lambda Calculus' Influence on Functional

Programming

III. Benefits of Functional Programming

IV. OCaml in Action: An Entire Interpreter in

3 slides

Lambda Calculus' Influence

COMS W4115 - PLT Columbia University 8 April 24, 2013

• The syntax and theory of Functional Programming is highly influenced by lambda calculus.

• Knowledge of lambda calculus means that FP is easier to learn, understand, and use efficiently.

You might recognize some of the following FP syntax traits from lambda

calculus…

Lambda Calculus' InfluenceFunction Abstraction

COMS W4115 - PLT Columbia University 9 April 24, 2013

Lambda Calculus Recap

A function abstraction consists of four parts:

1. a lambda followed by

2. a single variable,

3. a period, and then

4. an expression

λx.expr

Lambda Calculus' InfluenceFunction Abstraction

COMS W4115 - PLT Columbia University 10 April 24, 2013

λx.exprLambda Calculus:

fun x -> expr;;OCaml:

f(x) = exprAlgebra:

Lambda Calculus' InfluenceFunction Abstraction

COMS W4115 - PLT Columbia University 11 April 24, 2013

is equivalent to

• Named Functions: OCaml strays from pure functional programming at times.

• The "let" command can be used to allow one to create named functions

fun x -> expr;;

let myFunc x = expr;;

The Let Command

Lambda Calculus' InfluenceFunction Abstraction

COMS W4115 - PLT Columbia University 12 April 24, 2013

# let fToc temp = (temp -. 32.0)/. 1.8;;# fToc 98.6;;- : float = 36.99999999

double fToc (double temp) { return (temp-32)/1.8;

}fToc(98.6);

An OCaml "let" example

Similar code

in Java

Lambda Calculus' InfluenceBeta Reductions

COMS W4115 - PLT Columbia University 13 April 24, 2013

Lambda Calculus Recap

A function application is evaluated via a beta reduction

Which occurs when an actual value is substituted for a variable

Examples:

(λx.xzx)y → [y/x]xzx = yzy

(λx.+ 1 x)7 → + 1 7 = 8

Layman's Terms: (λx.expr1)expr2 means "replace every instance of x in expr1 with expr2"

Lambda Calculus' InfluenceBeta Reductions

COMS W4115 - PLT Columbia University 14 April 24, 2013

(λx.expr1)expr2Lambda Calculus:

(fun x ->expr1)expr2;;OCaml:

(fun x ->x*x)5;;(* Evaluates to 5*5 = 25 *)

Example Usage:

Lambda Calculus' InfluenceBeta Reductions

COMS W4115 - PLT Columbia University 15 April 24, 2013

(fun x ->expr1)expr2;;

let x = 5 in x*x;;(* Evaluates to 5*5=25*)

Example Usage:

The Let Command

let x = expr2 in expr1;;

Is semantically equivalent to:

The "let" command can once again be used to perform similar functionality to the "fun" command.

Outline

COMS W4115 - PLT Columbia University 16 April 24, 2013

I. What is Functional Programming?

II. Lambda Calculus' Influence on Functional

Programming

III. Benefits of Functional Programming

IV. OCaml in Action: An Entire Interpreter in

3 slides

Benefits of Functional Programming

COMS W4115 - PLT Columbia University 17 April 24, 2013

A few benefits in a nutshell:

1. Succinct Code

2. Encourages disciplined and logical thinking

3. Lazy Evaluation

4. Higher Level Functions

Example:List.fold_left (fun s e ->s + e) 0 [42; 17; 120];;

5. No Side Effects & Referential Transparency

Benefits of Functional ProgrammingNo Side Effects

COMS W4115 - PLT Columbia University 18 April 24, 2013

Side Effect:

a function or expression has a side effect if it modifies state or has an observable interaction with the "outside world."

Examples:• Modify global/static

variables• Modify arguments• Write data to a display or

file

Functional programs contain no assignment statements. So variables, once given a value, never change.

Benefits of Functional ProgrammingNo Side Effects

COMS W4115 - PLT Columbia University 19 April 24, 2013

Simple Side Effect Example in Java

static int x = 5;static void changeX (int a){

x = a;}changeX(4); // This function has the side effect

// of changing the value of global var x

Benefits of Functional ProgrammingReferential Transparency

COMS W4115 - PLT Columbia University 20 April 24, 2013

Referential Transparency: An expression (e.g., function) is referentially transparent if it always produces the same output for the same input.

Lack of side effects results in referential transparency.

Because of this behavior, a variable/expression can always be replaced with its value without changing the behavior of a program if that language has Referential Transparency.

Benefits of Functional ProgrammingReferential Transparency

COMS W4115 - PLT Columbia University 21 April 24, 2013

Lack of Referential Transparency in Java

static int x = 10;static int add(int a){

return a + x;}add(1); // returns 11x = 0;add(1); // returns 10, even though same input. NOT RT!

Why are Referential Transparency and Lack of Side Effects Good?

COMS W4115 - PLT Columbia University 22 April 24, 2013

• Elimination of bugs• Debugging in general is

easier• Independence of

evaluation order• Safe reuse of

subprograms• Safe multithreading

Why are Referential Transparency and Lack of Side Effects Good?

COMS W4115 - PLT Columbia University 23 April 24, 2013

Easier to Compile – It's not only people that benefit from being able to better predict and understand a program's behavior

Better Compiling – optimization via:– Memoization– Parallelization– Common Subexpression Elimination

Easier and Better Compiling

Outline

COMS W4115 - PLT Columbia University 24 April 24, 2013

I. What is Functional Programming?

II. Lambda Calculus' Influence on Functional

Programming

III. Benefits of Functional Programming

IV. OCaml in Action: An Entire Interpreter in

3 slides

Implementing an Interpreter in OCaml

COMS W4115 - PLT Columbia University 25 April 24, 2013

• We will build a simple desk calculator to perform integer arithmetic.

• You recall the basic block diagram of a simple interpreter:

Lexer ParserAST

Walkertokens AST

Input

(arithmetic expression)

Output

(evaluated arithmetic expression)

Implementing an Interpreter in OCaml

COMS W4115 - PLT Columbia University 26 April 24, 2013Courtesy of Stephen Edwards' PLT Lecture Slides: http://www.cs.columbia.edu/~sedwards/classes/2012/w4115-fall/ocaml.pdf

{ open Parser }rule token = parse [’ ’ ’\t’ ’\r’ ’\n’] { token lexbuf }

| ’+’ { PLUS }| ’-’ { MINUS }| ’*’ { TIMES }| ’/’ { DIVIDE }| [’0’-’9’]+ as lit { LITERAL (int_of_string lit) }| eof { EOF }

type operator = Add | Sub | Mul | Divtype expr = Binop of expr * operator * expr | Lit of int

scanner.mll

ast.mli

Implementing an Interpreter in OCaml

COMS W4115 - PLT Columbia University 27 April 24, 2013

parser.mly

%{ open Ast %}%token PLUS MINUS TIMES DIVIDE EOF%token <int> LITERAL

%left PLUS MINUS%left TIMES DIVIDE

%start expr%type <Ast.expr> expr%%

expr: expr PLUS expr { Binop($1, Add, $3) } | expr MINUS expr { Binop($1, Sub, $3) } | expr TIMES expr { Binop($1, Mul, $3) } | expr DIVIDE expr { Binop($1, Div, $3) } | LITERAL { Lit($1) }

Implementing an Interpreter in OCaml

COMS W4115 - PLT Columbia University 28 April 24, 2013

AST Walker:

calc.ml

open Astlet rec eval = function Lit(x) -> x | Binop (e1, op, e2) -> let v1 = eval e1 and v2 = eval e2 in match op with Add -> v1 + v2 | Sub -> v1 - v2 | Mul ->v1 * v2 | Div -> v1 / v2

let _ = let lexbuf = Lexing.from_channel stdin in let expr = Parser.expr Scanner.token lexbuf in let result = eval expr in print_endline (string_of_int result)

Quick Side Note:Pattern Matching in OCaml

COMS W4115 - PLT Columbia University 29 April 24, 2013

Pattern Matching a Parameter (function…)

Pattern Matching a Value (match…with)

Main difference is how the pattern matcher receives

its value to match

function param1 -> expr1 | param2 -> expr2 | param3 -> expr3

match expr with param1 -> expr1 | param2 -> expr2 | param3 -> expr3

Quick Side Note:Pattern Matching in OCaml

COMS W4115 - PLT Columbia University 30 April 24, 2013

If you recall the XOR program from the beginning of this lecture, you should now be able to understand how this works.

Hint: Variables, such as "x" will match anything and are

bound to a value when the pattern matches.

let xor p = match pwith (false, x) -> x | (true, x) -> not x;;

Compiling the Interpreter

COMS W4115 - PLT Columbia University 31 April 24, 2013

$ ocamllex scanner.mll # create scanner.ml8 states, 267 transitions, table size 1116 bytes$ ocamlyacc parser.mly # create parser.ml and parser.mli$ ocamlc –c ast.mli # compile AST types$ ocamlc –c parser.mli # compile parser types$ ocamlc –c scanner.ml # compile the scanner$ ocamlc –c parser.ml # compile the parser$ ocamlc –c calc.ml # compile the interpreter$ ocamlc –o calc parser.cmo scanner.cmo calc.cmo$ ./calc2 * 3 + 4 * 526$

So Why Functional Programming?

COMS W4115 - PLT Columbia University 32 April 24, 2013

It's hard to get to compile, but once it compiles, it works

-Unknown PLT Student

References

COMS W4115 - PLT Columbia University 33 April 24, 2013

• Edwards, Stephen. “An Introduction to Objective Caml.” http://www.cs.columbia.edu/~sedwards/classes/2012/w4115-fall/ocaml.pdf

• Huges, John. “Why Functional Programming Matters.” Research Topics in Functional Programming, Addison-Wesley, 1990: pp 17-42

• “Advantages of Functional Programming.” http://c2.com/cgi/wiki?AdvantagesOfFunctionalProgramming

top related