Programming Languages

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Dr. Philip Cannata 1 Programming Languages Genesis of Some Programming Languages (My kind of Fiction)
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
Foundations of Programming Languages: Introduction to Lambda Calculus
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Dr. Philip Cannata 1 Programming Languages. Dr. Philip Cannata 2 10 Java (Object Oriented) ACL2 (Propositional Induction) Algorithmic Information Theory.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
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.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
© 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.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Dr. Philip Cannata 1 Simple Lisp John McCarthy Alonzo Church David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan.
Dr. Philip Cannata 1 Programming Languages Chapter 14 – Functional Programming – Lisp.
Dr. Philip Cannata 1 Functions and Recursion Programming Languages.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
Introduction to the λ-Calculus and Functional Programming Languages
CS 550 Programming Languages Jeremy Johnson
Functional Programming
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Reminder About Functions
Lecture 6: Lambda Calculus
Lambda Calculus CSE 340 – Principles of Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
Programming Languages
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
More on Lambda Calculus
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lambda Calculus Revisited
Programming Language Concepts
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Is everyone signed up on piazza?
Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Lesson2 Lambda Calculus Basics
The Metacircular Evaluator
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
Programming Languages
The Metacircular Evaluator
Announcements Quiz 6 HW7 due Tuesday, October 30
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
The Metacircular Evaluator (Continued)
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
L Calculus.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Language semantics Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
topics interpreters meta-linguistic abstraction eval and apply
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:

Programming Languages

This Course Jython in Java High Level Languages Java (Object Oriented) 10 High Level Languages Java (Object Oriented) ACL2 (Propositional Induction) Algorithmic Information Theory (Information Compression and Randomness) - Kolmogorov Complexity Orc (Parallel Computing) GpH (Parallel Computing) RDF (Horn Clause Deduction, Semantic Web) This Course Jython in Java Relation

Relations and Functions A Relation is a subset of the cross-product of a set of domains. Functions: An n-ary relation R is a function if the first n-1 elements of R are the function’s arguments and the last element is the function’s results and whenever R is given the same set of arguments, it always returns the same results. [Notice, this is an unnamed function!].

A little Bit of Lambda Calculus – Lambda Expressions The function Square has R (the reals) as domain and range. Square : R  R Square(n) = n2 A lambda expression is a particular way to define a function: LambdaExpression  variable | ( M N) | (  variable . M ) M  LambdaExpression N  LambdaExpression E.g., (  x . x2 ) represents the Square function.

A little Bit of Lambda Calculus – Properties of Lambda Expressions In ( x . M), x is bound. Other variables in M are free. A substitution of N for all occurrences of a variable x in M is written M[x  N]. Examples: An alpha-conversion allows bound variable names to be changed. For example, alpha-conversion of λx.x might yield λy.y. A beta reduction (( x . M)N) of the lambda expression ( x . M) is a substitution of all bound occurrences of x in M by N. E.g., (( x . x2) 5) = 52

Lambda Calculus and Lambda Calculus in Python (sort of) Python Version Application x.x lambda x: x (lambda x: x)(4) (lambda x: x)(lambda x: x+2) f=(lambda x: x)(lambda x: x+2) f(4) s.(s s) lambda s: (s)(s) s=(lambda s: (s)(s)) (s)(lambda x: x) (s)(s) func.arg.(func arg) lambda func, arg: (func)(arg) (lambda func, arg: (func)(arg))((lambda x: x), 4) def identity = x.x def self_apply = s.(s s) def apply = func.arg.(func arg) Identity = lambda x: x self_apply = lambda s: (s)(s) apply = lambda func, arg: (func)(arg) def select_first = first.second.first def select_second =first.second.second select_first=lambda f, s: f select_second=lambda f, s: s (select_first)(3, 4) def cond= e1.e2.c.((c e1) e2) cond=lambda c, e1, e2: (c)(e1, e2) def true=select_first def false=select_second def not= x.(((cond false) true) x) Or def not= x.((x false) true) true=select_first false=select_second lnot=lambda x: (x)(false, true) (lnot)(true) ((lnot)(true))("true", "false") def and= x.y.(((cond y) false) x) Or def and= x.y.((x y) false) land=lambda x, y: (cond)(x, y, false) ((land)(true, true))("true", "false")  ((land)(true, false))("true", "false") def or= x.y.(((cond true) y) x) Or def or= x.y.((x true) y) lor=lambda x, y: (cond)(x, true, y)

Lambda Calculus Normal Order Substitution Normal Order (call-by-name Algol 60, lazy evaluation) v. Applicative Order (call-by-value, eager evaluation) In lambda calculus, if cond is defined as def cond= e1.e2.c.((c e1) e2), def and= x.y.(((cond y) false) x) is equivalent to def and= x.y.((x y) false) because: (((cond y) false) x) (((e1.e2.c.((c e1) e2) y) false) x) (c.((c y) false) x) ((x y) false) def or= x.y.(((cond true) y) x) def or= x. y.((x true) y) because: (((cond true) y) x) (((e1.e2.c.((c e1) e2) true) y) x) ((e2.c.((c true) e2) y) x) ((x true) y)

Lambda Calculus as Function Relations Remember I said a function is a relation written as follows (param1 param2 ... body)? If we restrict ourselves to functions that only take one argument, this would be (param body). Also, function application could be written as (param body)(arg). Using this, we can re-write the following lambda expressions and applications as follows: (λx.x λx.x) i.e., apply λx.x to itself (x x) (x x) (x x)  a function is returned (λs.(s s) λx.x) i.e., apply λs.(s s) to λx.x (s (s s)) (x x) ((x x) (x x)) (λs.(s s) λs.(s s)) i.e., apply λs.(s s) to itself (s (s s)) (s (s s)) (s (s s)) ((s (s s)) (s (s s)))  a function application is returned etc. ((λfunc.λarg.(func arg) λx.x) λs.(s s)) i.e., apply the "function application function" to λx.x and λs.(s s) (func (arg (func arg))) ((x x) (s (s s))) (arg ((x x) arg)) (s (s s)) ((x x) (s (s s))) (s (s s))  a function is returned So, in reality, the "function application function" which looks like it takes 2 arguments really is a function that consumes one argument and returns a function which consumes the second argument.

A little Bit of Lambda Calculus – Lambda Calculus Arithmetic def true = select_first def false = select_second def zero = λx.x def succ = λn.λs.((s false) n) def pred = λn.(((iszero n) zero) (n select_second)) def iszero = λn.(n select_first) one = (succ zero) (λn.λs.((s false) n) zero) λs.((s false) zero) two = (succ one) (λn.λs.((s false) n) λs.((s false) zero)) λs.((s false) λs.((s false) zero)) three = (succ two) (λn.λs.((s false) n) λs.((s false) λs.((s false) zero))) λs.((s false) λs.((s false) λs.((s false) zero))) (iszero zero) (λn.(n select_first) λx.x) (λx.x select_first) select_first For more but different details see Section 22.3 of the textbook. (iszero one) (λn.(n select_first) λs.((s false) zero) ) (λs.((s false) zero) select_first) ((select_first false) zero)

A little Bit of Lambda Calculus – Lambda Calculus Arithmetic ADDITION def addf = λf.λx.λy. if iszero y then x else f f (succ x)(pred y) def add = λx.λy. else addf addf (succ x)(pred y) add one two (((λx.λy. else addf addf (succ x)(pred y)) one) two) if iszero two then one else addf addf (succ one)(pred two) addf addf (succ one)(pred two) ((((λf.λx.λy else f f (succ x)(pred y)) addf) (succ one))(pred two)) if iszero (pred two) then (succ one) else addf addf (succ (succ one))(pred (pred two)) addf addf (succ (succ one)) (pred (pred two)) else f f (succ x)(pred y)) addf) (succ (succ one)))(pred (pred two))) if iszero (pred (pred two)) then (succ (succ one) else addf addf (succ (succ (succ one))) (pred (pred (pred two))) (succ (succ one)) three Multiplication def multf = λf.λx.λy. if iszero y then zero else add x (f x (pred y))) def recursive λf.(λs.(f (s s)) λs.(f (s s))) def mult = recursive multf = λx.λy else add x ((λs.(multf (s s)) λs.(multf (s s))) x (pred y)) Church-Turing thesis: no formal language is more powerful than the lambda calculus or the Turing machine which are both equivalent in expressive power.

A little Bit of Lambda Calculus – Y Combinator in Scheme Are these really the same? (letrec ((factorial (lambda (N) (if (= N 0) 1 (* N (factorial (- N 1)))) ))) (factorial 50))  30414093201713378043612608166064768844377641568960512000000000000 ----------------------------------------------------------------------------------------------------------------------------------------- ( ( ( lambda (X) ( (lambda (procedure) (X (lambda (arg) ((procedure procedure) arg)))) ( lambda (procedure) (X (lambda (arg) ((procedure procedure) arg))) ) ) ) (lambda (func-arg) (lambda (n) (if (zero? n) 1 (* n (func-arg (- n 1)))))) ) 50) Y Combinator (red) which is applied to a function (blue) For more details see Section 22.4 of the textbook. (define make-recursive-procedure (lambda (p) ((lambda (f ) (f f )) (lambda (f ) (p (f f ))))))

Programming Language Concepts Chapter 3 Substitution Definition 8 (Substitution, take 4) To substitute identifier i in e with expression v, replace all non-binding identifiers in e having the name i with the expression v, except within nested scopes of i. Finally, we have a version of substitution that works. A different, more succint way of phrasing this definition is Definition 9 (Substitution, take 5) To substitute identifier i in e with expression v, replace all free instances of i in e with v.

Develop Substitution for the Following Expressions Start with schema from Chapter 2 Get the following expressions to work: (subst 'x (num 1) (num 2)) (subst 'x (num 1) (id 'x)) (subst 'x (num 1) (id 'y)) (subst 'x (num 1) (add (id 'x) (id 'x))) (subst 'x (num 1) (with 'y (num 2) (id 'x))) (subst 'x (num 1) (with 'y (num 2) (add (id 'x) (id 'y)))) (subst 'x (num 1) (with 'y (id 'x) (add (id 'x) (id 'y)))) (subst 'x (num 1) (with 'x (id 'x) (id 'x))) (cal (subst 'x (num 1) (with 'y (add (num 2) (id 'x)) (add (id 'y)(id 'x))))) (calc (subst 'x (num 1) (with 'y (add (num 2) (id 'x)) (add (id 'y)(id 'x)))))

Scheme for Textbook Chapter 3 #lang plai (define-type AE [num (n number?)] [add (lhs AE?) (rhs AE?)] [sub (lhs AE?) (rhs AE?)] [id (i symbol?)] [with (i symbol?) (v AE?) (e AE?)]) (define (subst i v e) (type-case AE e [num (n) e] [add (l r) (add (subst i v l) (subst i v r))] [sub (l r) (sub (subst i v l) (subst i v r))] [id (i^) (if (symbol=? i i^) v e)] [with (i^ v^ e^) (if (symbol=? i i^) (with i^ (subst i v v^) e^) (with i^ (subst i v v^) (subst i v e^)))])) (define (calc an-ae) (type-case AE an-ae [num (n) n] [add (l r) (+ (calc l) (calc r))] [sub (l r) (- (calc l) (calc r))] [id (i) (error 'calc "free id")] [with (i v e) (calc (subst i v e))]))

Programming Language Concepts Chapters 1 and 2 Concepts: Concrete Sytax Abstract Syntax Token (Terminal) Non-Terminal BNF s-expression Parse Interpret (calc) Chapter 3 Concepts: Identifier Substitution Binding Instance Bound Identifier Free Instance (Free Identifier) Scope Eager Evaluation Lazy Evaluation

Programming Language Concepts Eager and Lazy Evaluation We began this material motivating the introduction of with: as a means for eliminating redundancy. Let’s revisit this sequence of substitutions (skipping a few intermediate steps): {with {x {+ 5 5}} {with {y {- x 3}} {+ y y}}} = {with {x 10} {with {y {- x 3}} {+ y y}}} = {with {y {- 10 3}} {+ y y}} = {with {y 7} {+ y y}} = {+ 7 7} = 14 Couldn’t we have also written it this way? = {with {y {- {+ 5 5} 3}} {+ y y}} = {+ {- {+ 5 5} 3} {- {+ 5 5} 3}} = {+ {- 10 3} {- {+ 5 5} 3}} = {+ {- 10 3} {- 10 3}} = {+ 7 {- 10 3}} In the top example, we invoke calc before substitution (because the result of calc is what we supply as an argument to subst). This model of substitution is called eager: we “eagerly” reduce the named expression to a value before substituting it. This is in contrast to the second example of reductions above, which we call lazy, wherein we reduce the named expression to a value only when we need to (such as at the application of an arithmetic primitive).