Transcript Monads

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
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.
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/
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
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
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?
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)
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
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.
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) return
3. 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
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
CONTINUED
#data dependency and manually passing
state around
$state0 = 0; #initial state
($a,$state1) = some_func($x, $state0);
And here is how you would normally do
it with global variables in a strict
language...
#Mutable state -- global variable
our $state = 0;
($b,$state2) = some_func($y, $state1);
$a = some_func($x);
($c,$state3) = some_func($z, $state2);
$b = some_func($y);
$c = some_func($z);
sub some_func {
my $state = $_[1];
blah; blah;
blah; blah;
$state++;
return (blah, $state+1);
}
sub some_func {
return blah;
}
What other “stateless” environment do you know?
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!)
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
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