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.

Slides:



Advertisements
Similar presentations
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Advertisements

The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Class 39: Universality cs1120 Fall 2009 David Evans University of Virginia.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM.
Catriel Beeri Pls/Winter 2004/5 functional-language 1 Substitution Semantics of FL – a simple functional language FL is EL + (non-recursive) function creation.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from G. Klein, NICTA.
Foundations of Programming Languages: Introduction to Lambda Calculus
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
Type Systems and Object- Oriented Programming John C. Mitchell Stanford University.
Comp 205: Comparative Programming Languages Imperative Programming Languages Functional Programming Languages Semantics Other Paradigms Lecture notes,
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Lecture 20: λ Calculus λ Calculus & Computability Yan Huang Dept. of Comp. Sci. University of Virginia.
Class 37: Computability in Theory and Practice cs1120 Fall 2011 David Evans 21 November 2011 cs1120 Fall 2011 David Evans 21 November 2011.
1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
Chapter Fifteen: Functional Programming Languages Lesson 12.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
Logical and Functional Programming
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
CMSC 330: Organization of Programming Languages Operational Semantics.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L04Ext-1 September 21, 2006http:// Some more thoughts.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
CSE-321 Programming Languages Abstract Machine E POSTECH May 1, 2006 박성우.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
Chapter 2: Lambda Calculus
CS 550 Programming Languages Jeremy Johnson
Lambda Calculus CSE 340 – Principles of Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
More on Lambda Calculus
Comp 205: Comparative Programming Languages
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
FP Foundations, Scheme In Text: Chapter 14.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Announcements Quiz 6 HW7 due Tuesday, October 30
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSE S. Tanimoto Lambda Calculus
Carlos Varela Rennselaer Polytechnic Institute September 8, 2015
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Carlos Varela Rennselaer Polytechnic Institute September 10, 2019
Presentation transcript:

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 slides were originally created by Prof. Sungwoo Park at POSTECH.

2 What lies in the core of 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 ?

3 Candidates? booleans and if/then/else integers lists variables functions and function applications datatypes patterns structures functors...

4 Core of ML fun x -> e e 1 e 2 x

5  calculus

-Calculus Kangwon National University 임현승 Programming Languages

7 Outline Brief history Abstract syntax of the -calculus Operational semantics of the -calculus Substitutions Programming in the -calculus

Liebniz’s Ideal Gottfried Wilhelm von Leibniz ( ) 8 1)Create a “universal language” in which all possible problems can be stated. 2)Find a decision method to solve all the problem stated in the universal language.

9 Can one solve all problems formulated in the universal language? Need a formalization of the notion of “decidable” or “computable”

Two Models of Computation -Calculus (Church 1936) Turing machine (Turing 1937) 10

 Calculus (1936) Alonzo Church ( ) 11 Minimalist Three expression types: x [variables] x.e [anonymous function] e 1 e 2 [function application] Foundations of functional languages: Lisp, ML, Haskell, etc.

Turing Machine (1937) Alan Turing ( ) 12 A hypothetical device that manipulates symbols on a strip of tape according to a table of rules The first accepted definition of a general-purpose computer Foundations of imperative languages: Java, C/C++, C#, Fortran, Pascal, assembler languages, etc.

13  calculus is Turing-complete fun x -> e e 1 e 2 x Turing machine  calculus =

 Everywhere Not only in FPLs… C++ 11 C# Java 8 Javascript PHP Python Scala Apple Swift 14

15 Outline Brief history V Abstract syntax of the -calculus Operational semantics of the -calculus Substitutions Programming in the -calculus

16 Syntax for a Programming Language Concrete syntax program = string of characters specifies rules for parsing. –operator precedence –associativity –keywords, * (2 * 3) 1 + (2 * (3)) Abstract syntax abstracts away from details of parsing. focuses on the high-level structure of programs. suitable for studying the semantics

17 x –variable –z, s, t, f, arg, accum,... x. e – -abstraction – x = formal argument, e = body – fun x -> e e 1 e 2 –application –left-associative (as in OCaml): e 1 e 2 e 3 = (e 1 e 2 ) e 3 e 1 e 2 e 3  e 1 (e 2 e 3 ) Abstract Syntax of the -Calculus

18 Examples

19 Outline Brief history V Abstract syntax of the -calculus V Operational semantics of the -calculus Substitutions Programming in the -calculus

20 Semantics of Languages Answers "what is the meaning of a given program?" –ML has a formal semantics. –What about C? Three styles –denotational semantics –axiomatic semantics –operational semantics The 1990s saw the renaissance of operational semantics.

21 Operational Semantics Specifies how to transform a program into a value via a sequence of operations Program Value P2P2 operation PnPn... let rec fac = function 1 -> 1 | n -> n * fac (n - 1) in fac 4 24

22 Operational Semantics of -Calculus Specifies how to transform an expression into a value via a sequence of reductions Expr Value E2E2 reduction EnEn...

23 Values and Reductions

24 Reductions redex = reducible expression :  -reduction

25 _____ = Redex

26 _____ = Redex

27  Reduction Not Unique So we need a reduction strategy.

28  Reduction Not Unique So we need a reduction strategy.

29 Call-by-name Call-by-value

30 Call-by-name Call-by-value

31

32

33 Call-by-name Call-by-value Used in Haskell Lazy or non-strict functional languages The implementation uses call-by-need. Superb! Used in OCaml Eager or strict functional languages Superb! (fn x => 0)

34 Outline Brief history V Abstract syntax of the -calculus V Operational semantics of the -calculus V Substitutions Programming in the -calculus

35 Values and Reductions redex = reducible expression :  -reduction

36 Call-by-name Call-by-value

37 [e' / x] e Informally "substitute e' for every occurrence of x in e." Examples

38 Easy Cases First

39 Two Remaining Cases

40 First (stupid) attempt Second attempt But wait:

OCaml Analogy 41 let f = fun x -> fun x -> x ? fun x -> fun y -> y ≡ Example f 3 4 → ([3/x](fun x -> x)) 4 ≡ (fun x -> x) 4 → 4

42 Names of bound variables do not matter. Hence –for a fresh variable y, Bound Variables

43 One Remaining Case

44 A Naive Attempt An anomaly: something for y

45 Free Variables Variables that are bound nowhere FV(e) = set of free variables in e

46 Free Variables Remain Free From the point of view of an outside observer, a free variable remains free until it is explicitly replaced. outside observer ? variable capture

47 Capture-Avoiding Substitution What happens if –the free variable y is captured and becomes a bound variable. –To an outside observer, it suddenly disappears!

48 Substitution Completed

49 We have to rename bound variables as necessary. Capture-Avoiding Substitution in Action

50 Renaming bound variables when necessary Okay because the names of bound variables do not matter. Examples

51

52 Last case of substitution

53 Outline Brief history V Abstract syntax of the -calculus V Operational semantics of the -calculus V Substitutions V Programming in the -calculus

54 A boolean value –"Give me two options and I will choose one for you!" Syntactic sugar Booleans

55 Examples Under the call-by-name strategy,

56 Logical Operators

57 Natural Numbers A natural number n –has the capability to repeat a given process n times. –"Give me a function f and I will return f n = f o f... f o f "

58 Church Numerals

59 Addition Key observation:

60 Multiplication Key observation: Alternatively

61 Recursive Functions in -Calculus Plan of attack 1.Assume a recursive function construct 2.Rewrite it as an expression in the -calculus i.e., show that it is syntactic sugar. fun f x. e

62 Plan of attack 1.Assume a recursive function construct 2.Rewrite it as an expression in the -calculus i.e., show that it is syntactic sugar. Example: Factorial

63 fac to FAC

64 FAC produces a new function from f

65 Now we only need to find a fixed point of:

66 Fixed Point Fixed point of function f V such that V = f (V) Ex. f (x) = 2 - x fixed point of f = 1 1 = f (1)

67 Magic Revealed Fixed point combinator / Y combinator (call-by-value) fix F returns a fixed point of F.

68 Now we only need to find a fixed point of: Now we only need to compute:

69 Answer