mathematical foundations supplemental material – not on exam

18
MATHEMATICAL FOUNDATIONS SUP PLEM ENTAL MA TERI A L – NOT ON EXAM

Upload: julian-robinson

Post on 18-Dec-2015

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

MATHEMAT

ICAL

FOUNDAT

IONS

SU

PP

L EM

EN

T AL M

AT

ER

I AL –

NO

T O

N E

XA

M

Page 2: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

LAMBDA C

ALCULU

S

Page 3: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

FROM WIKIPEDIA

Lambda calculus (also written as λ-calculus) is a formal system in mathematical logic and computer science for expressing computation based on function abstraction and application using variable binding and substitution.

In other words, this formalizes the concept of a function applied to arguments, so that we can reason about it

Name derives from the Greek letter lambda (λ) used to denote binding a variable in a function.

http://en.wikipedia.org/wiki/Lambda_calculus

Page 4: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

WHY? COMPUTABILITY THEORY

Computability is the ability to solve a problem in an effective manner.

Alonzo Church used lambda calculus to formalize the concept of effective computability.

Lambda calculus has been shown to have computationally equivalent power to Turing computability (Church-Turing thesis)

Lambda calculus is considered by some to be the world’s first programming language, although it is really intended to model computation rather than to describe algorithms.

Stack, Environment, Control, Dump (SECD) machine proposed as an abstract machine intended as a target for functional programming language compilers.*

LISP and other functional programming languages essentially implement the calculus.

*We now know that JavaScript stores objects as hashes. This knowledge may allow us to reason about behavior. This tells us that FP is tied to a stack model of implementation.

Page 5: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

MAIN IDEAS

Lambda calculus is a simple notation for functions and application

Main ideas:

• Applying a function to an argument

• Forming functions by abstraction

What is a function?

• Extensional view: sets of ordered pairs. (1,1),(2,4),(3,9),(4,16)• What is this function?

• Intensional view: rules of computation• How would you express the “rule” for this computation?

From http://plato.stanford.edu/entries/lambda-calculus/

Page 6: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

LANGUAGE SYNTAX*

Lambda expressions are composed (the language alphabet) of

• variables v1, v2, ..., vn, ...

• the abstraction symbols lambda 'λ' and dot '.'

• parentheses ( )

The set of lambda expressions, Λ, can be defined inductively:

• If x is a variable, then x ∈ Λ

• If x is a variable and M ∈ Λ, then (λx.M) ∈ Λ

• If M, N ∈ Λ, then (M N) ∈ Λ

Instances of rule 2 are known as abstractions and instances of rule 3 are known as applications. NOTE: M and N are terms/expressions… think of as a function. Beyond our scope to fully define (often done in grad-level theory course).

• Slightly different notations can be found, this is from wikipedia

• http://www.cs.yale.edu/homes/hudak/CS201S08/lambda.pdf

Page 7: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

MONADS

Page 8: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

WHAT IS A MONAD?

From:

http://en.wikipedia.org/wiki/Monads_in_functional_programming

http://stackoverflow.com/questions/44965/what-is-a-monad

Monad is a structure that represents computations defined as sequences of steps

A type with a monad structure defines what it means to chain operations

Ex: [x*2 | x<-[1..10], odd x] operation returns a list following operations performed on every item of the list

Page 9: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

MONADIC TYPE

A monad consists of a type constructor M and two operations, bind and return

return takes a plain value and uses the constructor to place it in a monadic container, thus creating a monadic value

bind does the reverse: extracts the value from the container and passes it to the next function in the pipeline

What does this sound like?

Page 10: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

USE IN FUNCTIONAL LANGUAGES

• Purely functional programs can use monads to structure procedures that include sequenced operations like those found in structured programming

• Many common programming concepts can be described in terms of a monad structure, including side effects such as input/output, variable assignment, exception handling, parsing, non-determinism, concurrency, and continuations.

http://en.wikipedia.org/wiki/Monad_(functional_programming)

Page 11: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

MONADS VS CLOSURES

Closures, as the word tends to be used, are just functions (or blocks of code, if you like) that you can treat like a piece of data and pass to other functions, etc. (the "closed" bit is that wherever you eventually call it, it behaves just as it would if you called it where it was originally defined).

A monad is (roughly) more like a context in which functions can be chained together sequentially, and controls how data is passed from one function to the next.

http://stackoverflow.com/questions/790719/what-is-the-difference-between-a-monad-and-a-closure

Page 12: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

ANOTHER VIEW, SAME SOURCE

A "closure" is an object comprising 1) a function, and 2) the values of its free variables where it's constructed.

A "monad" is a class of functions that can be composed in a certain way, i.e. by using associated bind and return higher-order function operators, to produce other functions.

And also:

I think monads are a little more complicated than closures because closures are just blocks of code that remember something from the point of their definitions and monads are a construct for "twisting" the usual function composition operation.

Page 13: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

MONADS AND RUBY

Monads can be done in any language that supports closures and anonymous functions• anonymous subs (Perl), lambda (Python), blocks (Ruby)

Monads consist of: 1. A container type.2. The operation wrap: puts a single value in an instance of the container;

Haskell calls this (confusingly) return3. The operation pass: extracts the values from the container and filters them

through a function (we’ll use a block); Haskell calls this “bind” (spelt >>=)

What good are monads? They let you chain pass operations together to make little computational pipelines, with rules of your choosing. They don’t manipulate values themselves — that’s the job of the blocks (functions) you plumb together using the monad.

http://moonbase.rydia.net/mental/writings/programming/monads-in-ruby/00introduction.html

Page 14: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

MONADS AND PERL

In Haskell, you don't specify the order of execution, but that doesn't mean that there isn't one. All data dependencies need to be met. Think of a system of simultaneous equations...

X = 1 + 1 Y = 2 * 3 Z = X + Y

Both X and Y have to be computed before Z can be evaluated. You can think of lazy functions being like file handles or sockets that block until their answer is known. Pretend that all functions X,Y and Z are executing parallel, but Z is blocked until both X and Y are available.

http://web.archive.org/web/20080515195640/http://sleepingsquirrel.org/monads/monads.html

Page 15: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

#data dependency and manually passing state around

$state0 = 0; #initial state

($a,$state1) = some_func($x, $state0);

($b,$state2) = some_func($y, $state1);

($c,$state3) = some_func($z, $state2);

sub some_func {

my $state = $_[1];

blah; blah;

return (blah, $state+1);

}

And here is how you would normally do it with global variables in a strict language...

#Mutable state -- global variable

our $state = 0;

$a = some_func($x);

$b = some_func($y);

$c = some_func($z);

sub some_func {

blah; blah;

$state++;

return blah;

}

CONTINUED

What other “stateless” environment do you know?

Page 16: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

CONTINUED

Essentially a monad is a hidden data structure (Fig. 1) which automatically passes state around for us. We could manually pass state into functions and pass it back out again like we've seen, but this is tedious and boring. Monads supposed to be a way of letting the machines do the work for us.

These hidden data structures are composed of closures (also know as anonymous subroutines).

(site has lots more!)

Page 17: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

CURRYING AND PERL

In perl, we have to perform currying by hand. Here's an example of a function which takes one curried value.

sub add { $_[0] + $_[1] };

sub curried_add

{

$arg1 = $_[0];

return sub { add($arg1, $_[0]) }; # note that $_[0] will be first arg when curried_add is called

}

$plus5 = curried_add(5);

print $plus5->(6); # eleven

print curried_add(3)->(4); # seven

Still from: http://web.archive.org/web/20080515195640/http://sleepingsquirrel.org/monads/monads.html

Page 18: MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

MATHEMATICAL DEFINITION

From wikipedia:

branch of mathematics called category theory

monad is triple: functor + 2 natural transformations

functional programming monads correspond to strong monads in category theory