A Theoreticians Programming Language Do we need... Function - - PowerPoint PPT Presentation

a theoretician s programming language
SMART_READER_LITE
LIVE PREVIEW

A Theoreticians Programming Language Do we need... Function - - PowerPoint PPT Presentation

A Theoreticians Programming Language Do we need... Function definition? Martin Henz From the Lambda Calculus to JavaScript A Theoreticians Programming Language Do we need... Function definition? granted! Martin Henz From the Lambda


slide-1
SLIDE 1

A Theoretician’s Programming Language

Do we need... Function definition?

Martin Henz From the Lambda Calculus to JavaScript

slide-2
SLIDE 2

A Theoretician’s Programming Language

Do we need... Function definition? granted!

Martin Henz From the Lambda Calculus to JavaScript

slide-3
SLIDE 3

A Theoretician’s Programming Language

Do we need... Function definition? granted! Function application?

Martin Henz From the Lambda Calculus to JavaScript

slide-4
SLIDE 4

A Theoretician’s Programming Language

Do we need... Function definition? granted! Function application? granted!

Martin Henz From the Lambda Calculus to JavaScript

slide-5
SLIDE 5

A Theoretician’s Programming Language

Do we need... Function definition? granted! Function application? granted! Functions with multiple parameters?

Martin Henz From the Lambda Calculus to JavaScript

slide-6
SLIDE 6

A Theoretician’s Programming Language

Do we need... Function definition? granted! Function application? granted! Functions with multiple parameters? no!

Martin Henz From the Lambda Calculus to JavaScript

slide-7
SLIDE 7

A Theoretician’s Programming Language

Do we need... Function definition? granted! Function application? granted! Functions with multiple parameters? no! Numbers?

Martin Henz From the Lambda Calculus to JavaScript

slide-8
SLIDE 8

A Theoretician’s Programming Language

Do we need... Function definition? granted! Function application? granted! Functions with multiple parameters? no! Numbers? no!

Martin Henz From the Lambda Calculus to JavaScript

slide-9
SLIDE 9

A Theoretician’s Programming Language

Do we need... Function definition? granted! Function application? granted! Functions with multiple parameters? no! Numbers? no! Conditionals?

Martin Henz From the Lambda Calculus to JavaScript

slide-10
SLIDE 10

A Theoretician’s Programming Language

Do we need... Function definition? granted! Function application? granted! Functions with multiple parameters? no! Numbers? no! Conditionals? no!

Martin Henz From the Lambda Calculus to JavaScript

slide-11
SLIDE 11

A Theoretician’s Programming Language

Do we need... Function definition? granted! Function application? granted! Functions with multiple parameters? no! Numbers? no! Conditionals? no! Recursive functions?

Martin Henz From the Lambda Calculus to JavaScript

slide-12
SLIDE 12

A Theoretician’s Programming Language

Do we need... Function definition? granted! Function application? granted! Functions with multiple parameters? no! Numbers? no! Conditionals? no! Recursive functions? no!

Martin Henz From the Lambda Calculus to JavaScript

slide-13
SLIDE 13

A Theoretician’s Programming Language

Do we need... Function definition? granted! Function application? granted! Functions with multiple parameters? no! Numbers? no! Conditionals? no! Recursive functions? no!

Martin Henz From the Lambda Calculus to JavaScript

slide-14
SLIDE 14

Some Examples

function square(x) { return x * x; } square(13);

Martin Henz From the Lambda Calculus to JavaScript

slide-15
SLIDE 15

Do we need multiple arguments?

function plus(x,y) { return x + y; } plus(5,7);

Martin Henz From the Lambda Calculus to JavaScript

slide-16
SLIDE 16

Do we need multiple arguments?

function plus(x,y) { return x + y; } plus(5,7); becomes function plus(x) { function plusx(y) { return x + y; } return plusx; } var plusfive = plus(5); plusfive(7);

Martin Henz From the Lambda Calculus to JavaScript

slide-17
SLIDE 17

Another Example

function power(x,y) { if (y === 0) return 1; return x * power(x,y-1); } power(2,4);

Martin Henz From the Lambda Calculus to JavaScript

slide-18
SLIDE 18

Do we need multiple arguments?

function power(x,y) { if (y === 0) return 1; return x * power(x,y-1); } power(2,4); translates to: function power(x) { return function(y) { if (y === 0) return 1; return x * power(x)(y-1); }; } power(2)(4);

Martin Henz From the Lambda Calculus to JavaScript

slide-19
SLIDE 19

Do we need numbers?

Representing 0 using Church numerals: function zero(f) { return function(x) { return x; } } zero(’something’)(’somethingelse’)

Martin Henz From the Lambda Calculus to JavaScript

slide-20
SLIDE 20

Do we need numbers?

Representing 1 using Church numerals: function one(f) { return function(x) { return f(x); } }

  • ne(function(x) { return x*2; })(4)

Martin Henz From the Lambda Calculus to JavaScript

slide-21
SLIDE 21

Do we need numbers?

Representing 2 using church numerals: function two(f) { return function(x) { return f(f(x)); } } two(function(x) { return x*2; })(4)

Martin Henz From the Lambda Calculus to JavaScript

slide-22
SLIDE 22

Getting the number back

function two(f) { return function(x) { return f(f(x)); } } function church2js(c) { return c(function(x) { return x+1; })(0); } church2js(two);

Martin Henz From the Lambda Calculus to JavaScript

slide-23
SLIDE 23

Can you do the reverse?

Wanted: Define a function js2church that takes a JediScript number as argument and returns its Church numeral?

Martin Henz From the Lambda Calculus to JavaScript

slide-24
SLIDE 24

Multiplication

function times(x) { return function(y) { return function(f) { return x(y(f)); } } } function two(f) { return function(x) { return f(f(x)); } }

Martin Henz From the Lambda Calculus to JavaScript

slide-25
SLIDE 25

Multiplication

function three(f) { return function(x) { return f(f(f(x))); } } function church2js(c) { return c(function(x) { return x+1; })(0); } church2js(times(two)(three));

Martin Henz From the Lambda Calculus to JavaScript

slide-26
SLIDE 26

Conditionals

Conditional statements if (20 < 10) { return 5; } else { return 7; } Conditional expressions (20 < 10) ? 5 : 7

Martin Henz From the Lambda Calculus to JavaScript

slide-27
SLIDE 27

Do we need conditionals?

Idea Represent booleans with functions The function “true” function True(x) { return function(y) { return x; } }

Martin Henz From the Lambda Calculus to JavaScript

slide-28
SLIDE 28

Do we need conditionals?

Idea Represent booleans with functions The function “false” function False(x) { return function(y) { return y; } }

Martin Henz From the Lambda Calculus to JavaScript

slide-29
SLIDE 29

Do we need conditionals?

Conditional in JediScript True ? 5 : 7; Conditional using Encoding True(5)(7);

Martin Henz From the Lambda Calculus to JavaScript

slide-30
SLIDE 30

Factorial using Conditional Expressions

function factorial(x) { return (x === 0) ? 1 : x * factorial(x - 1); } factorial(5);

Martin Henz From the Lambda Calculus to JavaScript

slide-31
SLIDE 31

Step 1: Eliminate Recursive Call

function F(f) { return function(x) { return (x === 0) ? 1 : x * f(x - 1); }; }

Martin Henz From the Lambda Calculus to JavaScript

slide-32
SLIDE 32

Step 2: Find a Fix-Point Function (aka Y-Combinator)

We need a function Y with the following properties: Y(F) ≡ F(Y(F))

Martin Henz From the Lambda Calculus to JavaScript

slide-33
SLIDE 33

Step 2: A Y-Combinator

function (f) { return (function (x) { return f(function(y) { return x(x)(y); }); }) (function (x) { return f(function(y) { return x(x)(y); }); }); }

Martin Henz From the Lambda Calculus to JavaScript

slide-34
SLIDE 34

The Pure (Untyped) Lambda Calculus

As a sublanguage of JediScript, the Lambda Calculus looks like this: L ::= id | (L)(L); | function(id) { return L; }

Martin Henz From the Lambda Calculus to JavaScript

slide-35
SLIDE 35

So: Why don’t we program using the Lambda Calculus?

Answer Other design goals are equally important!

Martin Henz From the Lambda Calculus to JavaScript

slide-36
SLIDE 36

So: Why don’t we program using the Lambda Calculus?

Answer Other design goals are equally important! Some design goals for full JavaScript Expressive Easy to learn Convenient to use

Martin Henz From the Lambda Calculus to JavaScript

slide-37
SLIDE 37

So: Why don’t we program using the Lambda Calculus?

Answer Other design goals are equally important! Some design goals for full JavaScript Expressive Easy to learn Convenient to use At the expense of...

Martin Henz From the Lambda Calculus to JavaScript

slide-38
SLIDE 38

So: Why don’t we program using the Lambda Calculus?

Answer Other design goals are equally important! Some design goals for full JavaScript Expressive Easy to learn Convenient to use At the expense of... simplicity!

Martin Henz From the Lambda Calculus to JavaScript

slide-39
SLIDE 39

Lambda Calculus: Some History

Introduced by Alonzo Church in 1930s as a minimal formal system for recursion theory Later found to be equivalent to other computing frameworks (Church-Turing thesis) Used extensively in programming language theory and theoretical computer science

Martin Henz From the Lambda Calculus to JavaScript

slide-40
SLIDE 40

Summary

Simplicity is an important and highly useful driving force behind science and engineering

Martin Henz From the Lambda Calculus to JavaScript

slide-41
SLIDE 41

Summary

Simplicity is an important and highly useful driving force behind science and engineering Enables insights that would otherwise remain lost in a thicket of details

Martin Henz From the Lambda Calculus to JavaScript

slide-42
SLIDE 42

Summary

Simplicity is an important and highly useful driving force behind science and engineering Enables insights that would otherwise remain lost in a thicket of details In practice, simplicity competes with other goals; keep it in mind when thinking about complex systems

Martin Henz From the Lambda Calculus to JavaScript

slide-43
SLIDE 43

Homework

Write a lambda expression EXP such that lambda2jediscript((EXP)(jediscript2lambda(6))); will result in the factorial of 6 when entered in the Web Console

  • f Firefox.

You will need: conditionals using the shown encoding your implementation of lambda2jediscript and jediscript2lambda the Y combinator shown above addition, multiplication predecessor “n - 1” is the hardest

Martin Henz From the Lambda Calculus to JavaScript