MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM.

Slides:



Advertisements
Similar presentations
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Principles of programming languages 1: Introduction (with a simple language) Isao Sasano Department of Information Science and Engineering.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Class 39: Universality cs1120 Fall 2009 David Evans University of Virginia.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
CPSC 411, Fall 2008: Set 12 1 CPSC 411 Design and Analysis of Algorithms Set 12: Undecidability Prof. Jennifer Welch Fall 2008.
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Programming Languages Structure
Programming Language Theory Leif Grönqvist The national Graduate School of Language Technology (GSLT) MSI.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
David Evans CS200: Computer Science University of Virginia Computer Science Class 31: Universal Turing Machines.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
GENERAL CONCEPTS OF OOPS INTRODUCTION With rapidly changing world and highly competitive and versatile nature of industry, the operations are becoming.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Class 37: Computability in Theory and Practice cs1120 Fall 2011 David Evans 21 November 2011 cs1120 Fall 2011 David Evans 21 November 2011.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
Functional Programming in Scheme and Lisp.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Albert Gatt LIN3021 Formal Semantics Lecture 4. In this lecture Compositionality in Natural Langauge revisited: The role of types The typed lambda calculus.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
Chapter 3 Part II Describing Syntax and Semantics.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Logical and Functional Programming
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
CMSC 330: Organization of Programming Languages Operational Semantics.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Functional Programming
Operational Semantics of Scheme
Mathematical Foundations
Functional Programming
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Introduction to Computer Programming
Important Concepts from Clojure
Important Concepts from Clojure
Lesson 2 Programming constructs – Algorithms – Scratch – Variables Intro.
Theory of Computation Turing Machines.
Coding Concepts (Basics)
6.001 SICP Variations on a Scheme
L Calculus.
Important Concepts from Clojure
CSE 341 Lecture 11 b closures; scoping rules
CSE S. Tanimoto Lambda Calculus
Representations & Reasoning Systems (RRS) (2.2)
Presentation transcript:

MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM

LAMBDA CALCULUS

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.

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

LANGUAGE SYNTAX* Lambda expressions are composed (the language alphabet) of variables v 1, v 2,..., v n,... the abstraction symbols lambda 'λ' and dot '.' parentheses ( ) The set of lambda expressions, Λ, can be defined inductively: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

MONADS

WHAT IS A MONAD? From: 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.

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.

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.

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 =  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.

#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; $state++; return blah; } CONTINUED 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:

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