CS5205: Foundation in Programming Languages Lecture 1 : Overview

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

- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
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.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
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
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
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.
Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
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.
Lambda Calculus1 The Untyped Lambda-Calculus Hossein Hojjat University of Tehran Formal Methods Laboratory.
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
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.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
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.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
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 박성우.
CS5205Haskell1 CS5205: Foundations in Programming Languages Overview Lecturer : Chin Wei Ngan Office : S “Language.
CS5205Introduction1 CS5205: Foundation in Programming Languages Lecture 0 : Overview Lecturer : Chin Wei Ngan Office : COM2.
-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
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CSE 5317/4305 L12: Higher-Order Functions1 Functional Languages and Higher-Order Functions Leonidas Fegaras.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
CMSC 330: Organization of Programming Languages Operational Semantics.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
CS321 Functional Programming 2 © JAS The λ Calculus This is a formal system to capture the ideas of function abstraction and application introduced.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
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.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
CS5205: Foundation in Programming Languages Type Reconstruction
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lambda Calculus CSE 340 – Principles of Programming Languages
More on Lambda Calculus
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Lesson2 Lambda Calculus Basics
Announcements Quiz 6 HW7 due Tuesday, October 30
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
CSCE 314: Programming Languages Dr. Dylan Shell
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
CSE S. Tanimoto Lambda Calculus
Carlos Varela Rennselaer Polytechnic Institute September 8, 2015
Carlos Varela Rennselaer Polytechnic Institute September 10, 2019
Presentation transcript:

CS5205: Foundation in Programming Languages Lecture 1 : Overview “Language Foundation, Extensions and Reasoning Lecturer : Chin Wei Ngan Email : chinwn@comp.nus.edu.sg Office : S15 06-01 CS5205 Introduction

Course Objectives - graduate-level course with foundation focus - languages as tool for programming - foundations for reasoning about programs - explore various language innovations CS5205 Introduction

Course Outline Lecture Topics (13 weeks) Lambda Calculus Advanced Language (Haskell) http://www.haskell.org Type System for Lightweight Analysis http://www.cs.cmu.edu/~rwh/plbook/book.pdf Semantics Formal Reasoning – Separation Logic + Provers Language Innovations (paper readings) CS5205 Introduction

Administrative Matters - mainly IVLE - Reading Materials (mostly online): www.haskell.org Robert Harper : Foundations of Practical Programming Languages. Free PL books : http://www.cs.uu.nl/~franka/ref - Lectures + Assignments + Presentation + Exam - Assignment/Quiz (30%) - Paper Reading + Critique (25%) - Exam (45%) CS5205 Introduction

Paper Critique Focus on Language Innovations Select Topic (Week 3) 2-Page Summary (Week 5) Prepare Presentation (Week 7) Oral Presentation (Last 4 weeks) Paper Critique (Week 13) Possible Topics Concurrent and MultiCore Programming Software Transaction Memory GUI Programming with Array Testing with QuickCheck IDE for Haskell SELinks (OCaml) CS5205 Introduction

Advanced Language - Haskell Strongly-typed with polymorphism Higher-order functions Pure Lazy Language. Algebraic data types + records Exceptions Type classes, Monads, Arrows, etc Advantages : concise, abstract, reuse Why use Haskell ? cool & greater productivity CS5205 Introduction

Example - Haskell Program Apply a function to every element of a list. data List a = Nil | Cons a (List a) map f Nil = Nil map f (Cons x xs) = Cons (f x) (map f xs) map inc (Cons 1 (Cons 2 (Cons 3 Nil))) ==> (Cons 2 (Cons 3 (Cons 4 Nil))) a type variable type is : (a  b)  (List a)  (List b) CS5205 Introduction

Some Applications Hoogle – search in code library Darcs – distributed version control Programming user interface with arrows.. How to program multicore? map/reduce and Cloud computing Some issues to be studied in Paper Reading. CS5205 Introduction

Type System – Lightweight Analysis Abstract description of code + genericity Compile-time analysis that is tractable Guarantees absence of some bad behaviors Issues – expressivity, soundness, completeness, inference? How to use, design and prove type system. Why? detect bugs CS5205 Introduction

Specification with Separation Logic Is sorting algorithm correct? Any memory leaks? Any null pointer dereference? Any array bound violation? What is the your specification/contract? How to verify program correctness? Issues – mutation and aliasing Why? sw reliability CS5205 Introduction

Lambda Calculus Untyped Lambda Calculus Evaluation Strategy Techniques - encoding, extensions, recursion Operational Semantics Explicit Typing Introduction to Lambda Calculus: http://www.inf.fu-berlin.de/lehre/WS03/alpi/lambda.pdf http://www.cs.chalmers.se/Cs/Research/Logic/TypesSS05/Extra/geuvers.pdf Lambda Calculator : http://ozark.hendrix.edu/~burch/proj/lambda/download.html CS5205 Introduction

Untyped Lambda Calculus Extremely simple programming language which captures core aspects of computation and yet allows programs to be treated as mathematical objects. Focused on functions and applications. Invented by Alonzo (1936,1941), used in programming (Lisp) by John McCarthy (1959). CS5205 Introduction

Functions without Names Usually functions are given a name (e.g. in language C): int plusOne(int x) { return x+1; } …plusOne(5)… However, function names can also be dropped: (int (int x) { return x+1;} ) (5) Notation used in untyped lambda calculus: (l x . x+1) (5) CS5205 Introduction

Syntax x variable l x . t abstraction t t application In purest form (no constraints, no built-in operations), the lambda calculus has the following syntax. t ::= terms x variable l x . t abstraction t t application This is simplest universal programming language! CS5205 Introduction

Conventions Parentheses are used to avoid ambiguities. e.g. x y z can be either (x y) z or x (y z) Two conventions for avoiding too many parentheses: Applications associates to the left e.g. x y z stands for (x y) z Bodies of lambdas extend as far as possible. e.g. l x. l y. x y x stands for l x. (l y. ((x y) x)). Nested lambdas may be collapsed together. e.g. l x. l y. x y x can be written as l x y. x y x CS5205 Introduction

Scope An occurrence of variable x is said to be bound when it occurs in the body t of an abstraction l x . t An occurrence of x is free if it appears in a position where it is not bound by an enclosing abstraction of x. Examples: x y y. x y l x. x (identity function) (l x. x x) (l x. x x) (non-stop loop) (l x. x) y (l x. x) x CS5205 Introduction

Alpha Renaming Lambda expressions are equivalent up to bound variable renaming. e.g. l x. x =a l y. y l y. x y =a l z. x z But NOT: l y. x y =a l y. z y Alpha renaming rule: l x . E =a l z . [x a z] E (z is not free in E) CS5205 Introduction

Beta Reduction An application whose LHS is an abstraction, evaluates to the body of the abstraction with parameter substitution. e.g. (l x. x y) z !b z y (l x. y) z !b y (l x. x x) (l x. x x) !b (l x. x x) (l x. x x) Beta reduction rule (operational semantics): ( l x . t1 ) t2 !b [x a t2] t1 Expression of form ( l x . t1 ) t2 is called a redex (reducible expression). CS5205 Introduction

Evaluation Strategies A term may have many redexes. Evaluation strategies can be used to limit the number of ways in which a term can be reduced. An evaluation strategy is deterministic, if it allows reduction with at most one redex, for any term. Examples: - full beta reduction - normal order - call by name - call by value, etc CS5205 Introduction

Full Beta Reduction Any redex can be chosen, and evaluation proceeds until no more redexes found. Example: (lx.x) ((lx.x) (lz. (lx.x) z)) denoted by id (id (lz. id z)) Three possible redexes to choose: id (id (lz. id z)) Reduction: ! id (id (lz.z)) ! id (lz.z) ! lz.z ! CS5205 Introduction

Normal Order Reduction Deterministic strategy which chooses the leftmost, outermost redex, until no more redexes. Example Reduction: id (id (lz. id z)) ! id (lz. id z)) ! lz.id z ! lz.z ! CS5205 Introduction

Call by Name Reduction Chooses the leftmost, outermost redex, but never reduces inside abstractions. Example: id (id (lz. id z)) ! id (lz. id z)) ! lz.id z ! CS5205 Introduction

Call by Value Reduction Chooses the leftmost, innermost redex whose RHS is a value; and never reduces inside abstractions. Example: id (id (lz. id z)) ! id (lz. id z) ! lz.id z ! CS5205 Introduction

Strict vs Non-Strict Languages Strict languages always evaluate all arguments to function before entering call. They employ call-by-value evaluation (e.g. C, Java, ML). Non-strict languages will enter function call and only evaluate the arguments as they are required. Call-by-name (e.g. Algol-60) and call-by-need (e.g. Haskell) are possible evaluation strategies, with the latter avoiding the re-evaluation of arguments. In the case of call-by-name, the evaluation of argument occurs with each parameter access. CS5205 Introduction

Programming Techniques in l-Calculus Multiple arguments. Church Booleans. Pairs. Church Numerals. Recursion. Extended Calculus CS5205 Introduction

Multiple Arguments Pass multiple arguments one by one using lambda abstraction as intermediate results. The process is also known as currying. Example: f = l(x,y).s f = l x. (l y. s) Application: f(v,w) (f v) w requires pairs as primitve types requires higher order feature CS5205 Introduction

Church Booleans Church’s encodings for true/false type with a conditional: true = l t. l f. t false = l t. l f. f if = l l. l m. l n. l m n Example: if true v w = (l l. l m. l n. l m n) true v w ! true v w = (l t. l f. t) v w ! v Boolean and operation can be defined as: and = l a. l b. if a b false = l a. l b. (l l. l m. l n. l m n) a b false = l a. l b. a b false CS5205 Introduction

Pairs Define the functions pair to construct a pair of values, fst to get the first component and snd to get the second component of a given pair as follows: pair = l f. l s. l b. b f s fst = l p. p true snd = l p. p false Example: snd (pair c d) = (l p. p false) ((l f. l s. l b. b f s) c d) ! (l p. p false) (l b. b c d) ! (l b. b c d) false ! false c d ! d CS5205 Introduction

Church Numerals Numbers can be encoded by: c0 = l s. l z. z c1 = l s. l z. s z c2 = l s. l z. s (s z) c3 = l s. l z. s (s (s z)) : CS5205 Introduction

Church Numerals Successor function can be defined as: Example: succ = l n. l s. l z. s (n s z) Example: succ c1 = ( n.  s.  z. s (n s z)) ( s.  z. s z)  l s. l z. s ((l s. l z. s z) s z) ! l s. l z. s (s z) succ c2 = l n. l s. l z. s (n s z) (l s. l z. s (s z)) ! l s. l z. s ((l s. l z. s (s z)) s z) ! l s. l z. s (s (s z)) CS5205 Introduction

Church Numerals Other Arithmetic Operations: plus = l m. l n. l s. l z. m s (n s z) times = l m. l n. m (plus n) c0 iszero = l m. m (l x. false) true Exercise : Try out the following. plus c1 x times c0 x times x c1 iszero c0 iszero c2 CS5205 Introduction

Recursion Some terms go into a loop and do not have normal form. Example: (l x. x x) (l x. x x) ! (l x. x x) (l x. x x) ! … However, others have an interesting property fix = λf. (λx. f (x x)) (λx. f (x x)) fix = l f. (l x. f (l y. x x y)) (l x. f (l y. x x y)) that returns a fix-point for a given functional. Given x = h x = fix h That is: fix h ! h (fix h) ! h (h (fix h)) ! … x is fix-point of h CS5205 Introduction

Example - Factorial We can define factorial as: fact = l n. if (n<=1) then 1 else times n (fact (pred n)) = (l h. l n. if (n<=1) then 1 else times n (h (pred n))) fact = fix (l h. l n. if (n<=1) then 1 else times n (h (pred n))) CS5205 Introduction

Example - Factorial Recall: fact = fix (l h. l n. if (n<=1) then 1 else times n (h (pred n))) Let g = (l h. l n. if (n<=1) then 1 else times n (h (pred n))) Example reduction: fact 3 = fix g 3 = g (fix g) 3 = times 3 ((fix g) (pred 3)) = times 3 (g (fix g) 2) = times 3 (times 2 ((fix g) (pred 2))) = times 3 (times 2 (g (fix g) 1)) = times 3 (times 2 1) = 6 CS5205 Introduction

Enriching the Calculus We can add constants and built-in primitives to enrich l-calculus. For example, we can add boolean and arithmetic constants and primitives (e.g. true, false, if, zero, succ, iszero, pred) into an enriched language we call lNB: Example: l x. succ (succ x) 2 lNB l x. true 2 lNB CS5205 Introduction

Formal Treatment of Lambda Calculus Let V be a countable set of variable names. The set of terms is the smallest set T such that: x 2 T for every x 2 V if t1 2 T and x 2 V, then l x. t1 2 T if t1 2 T and t2 2 T, then t1 t2 2 T Recall syntax of lambda calculus: t ::= terms x variable l x.t abstraction t t application CS5205 Introduction

Free Variables The set of free variables of a term t is defined as: FV(x) = {x} FV(l x.t) = FV(t) \ {x} FV(t1 t2) = FV(t1) [ FV(t2) CS5205 Introduction

Substitution Works when free variables are replaced by term that does not clash: [x a l z. z w] (l y.x) = (l y. l z. z w) However, problem if there is name capture/clash: [x a l z. z w] (l x.x) ¹ (l x. l z. z w) [x a l z. z w] (l w.x) ¹ (l w. l z. z w) CS5205 Introduction

Formal Defn of Substitution [x a s] x = s if y=x [x a s] y = y if y¹x [x a s] (t1 t2) = ([x a s] t1) ([x a s] t2) [x a s] (l y.t) = l y.t if y=x [x a s] (l y.t) = l y. [x a s] t if y¹ x Æ y FV(s) [x a s] (l y.t) = [x a s] (l z. [y a z] t) if y¹ x Æ y 2 FV(s) Æ fresh z CS5205 Introduction

Syntax of Lambda Calculus Term: t ::= terms x variable l x.t abstraction t t application Value: v ::= value l x.t abstraction value CS5205 Introduction

Call-by-Value Semantics premise t1 ! t’1 t1 t2 ! t’1 t2 (E-App1) conclusion t2 ! t’2 v1 t2 ! v1 t’2 (E-App2) (l x.t) v ! [x a v] t (E-AppAbs) CS5205 Introduction

Call-by-Name Semantics t1 ! t’1 t1 t2 ! t’1 t2 (E-App1) (l x.t) t2 ! [x a t2] t (E-AppAbs) CS5205 Introduction

Getting Stuck Evaluation can get stuck. (Note that only values are l-abstraction) e.g. (x y) In extended lambda calculus, evaluation can also get stuck due to the absence of certain primitive rules. (l x. succ x) true ! succ true ! CS5205 Introduction

Boolean-Enriched Lambda Calculus Term: t ::= terms x variable l x.t abstraction t t application true constant true false constant false if t then t else t conditional Value: v ::= value l x.t abstraction value true true value false false value CS5205 Introduction

Key Ideas Exact typing impossible. if <long and tricky expr> then true else (l x.x) Need to introduce function type, but need argument and result types. if true then (l x.true) else (l x.x) CS5205 Introduction

Simple Types The set of simple types over the type Bool is generated by the following grammar: T ::= types Bool type of booleans T ! T type of functions ! is right-associative: T1 ! T2 ! T3 denotes T1 ! (T2 ! T3) CS5205 Introduction

Implicit or Explicit Typing Languages in which the programmer declares all types are called explicitly typed. Languages where a typechecker infers (almost) all types is called implicitly typed. Explicitly-typed languages places onus on programmer but are usually better documented. Also, compile-time analysis is simplified. CS5205 Introduction

Explicitly Typed Lambda Calculus t ::= terms … l x : T.t abstraction v ::= value l x : T.t abstraction value T ::= types Bool type of booleans T ! T type of functions CS5205 Introduction

Examples l x:Bool . x (l x:Bool . x) true true if false then (l x:Bool . True) else (l x:Bool . x) CS5205 Introduction