Programming Languages. Dr. Philip Cannata 1

Similar documents
Programming Languages!

Simple Lisp. Alonzo Church. John McCarthy. Turing. David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan

Name SOLUTIONS EID NOTICE: CHEATING ON THE MIDTERM WILL RESULT IN AN F FOR THE COURSE.

Functions and Recursion

CMSC 330: Organization of Programming Languages. Lambda Calculus

Functional Programming

CMSC 330: Organization of Programming Languages. Lambda Calculus

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

The Untyped Lambda Calculus

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

CMSC 330: Organization of Programming Languages

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Programming Languages. Dr. Philip Cannata 1

CMSC 330: Organization of Programming Languages

Introduction to the Lambda Calculus

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

An Introduction to Functions

Last class. CS Principles of Programming Languages. Introduction. Outline

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

fjyswan Dr. Philip Cannata 1

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Reminder About Functions

Functions and Recursion. Dr. Philip Cannata 1

Formal Systems and their Applications

Names and Types. standard hue names. Dr. Philip Cannata 1

The Untyped Lambda Calculus

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Fundamentals and lambda calculus

Lecture 5: The Untyped λ-calculus

Lambda Calculus. Lecture 4 CS /26/10

Lambda Calculus. Gunnar Gotshalks LC-1

Chapter 5: The Untyped Lambda Calculus

Functional Programming. Pure Functional Languages

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors.

dynamically typed dynamically scoped

Activity. CSCI 334: Principles of Programming Languages. Lecture 4: Fundamentals II. What is computable? What is computable?

CSC173 Lambda Calculus Lambda Calculus Evaluation (10 min) Evaluate this expression (β-reduce with normal-order evaluation):

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Lambda Calculus. Variables and Functions. cs3723 1

The Untyped Lambda Calculus

Introduction to the λ-calculus

Software Paradigms (Lesson 4) Functional Programming Paradigm

Type Systems Winter Semester 2006

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lambda Calculus. Lambda Calculus

Functional Programming. Pure Functional Languages

Chapter 5: The Untyped Lambda Calculus

A Quick Overview. CAS 701 Class Presentation 18 November Department of Computing & Software McMaster University. Church s Lambda Calculus

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

FP Foundations, Scheme

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Bindings & Substitution

CMSC 330: Organization of Programming Languages

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Lambda Calculus LC-1

The Untyped Lambda Calculus

Recursive Definitions, Fixed Points and the Combinator

Comp 311: Sample Midterm Examination

Organization of Programming Languages CS3200/5200N. Lecture 11

Introductory Example

Untyped Lambda Calculus

COP4020 Programming Assignment 1 - Spring 2011

CS 314 Principles of Programming Languages. Lecture 21

lambda calculus History 11/28/13 Jianguo Lu A formal system designed to inves:gate func:on defini:on, func:on applica:on, and recursion.

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

CIS 500 Software Foundations Fall September 25

1.3. Conditional expressions To express case distinctions like

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

Y-Combinator. Contents. 1 Introduction. Avi Hayoun, Hodai Goldman and Lior Zur-Lotan. November 24, Terms recap. 1.2 Y-Combinator notes

Concepts of programming languages

Functional Languages. Hwansoo Han

CMSC 330: Organization of Programming Languages

CMSC330. Objects, Functional Programming, and lambda calculus

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Costly software bugs that could have been averted with type checking

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

Programming Languages

Pure Lambda Calculus. Lecture 17

CSCC24 Functional Programming Scheme Part 2

CMSC 330: Organization of Programming Languages. Operational Semantics

Functional Programming Languages (FPL)

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

Functions & First Class Function Values

Untyped Lambda Calculus

CS 242. Fundamentals. Reading: See last slide

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Part I. Historical Origins

Lambda Calculus and Type Inference

CIS 500 Software Foundations Fall October 2

Transcription:

Programming Languages Dr. Philip Cannata 1

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

Relations and Functions Relations: 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!]. Dr. Philip Cannata 3

A little Bit of Lambda Calculus Lambda Expressions The function Square has R (the reals) as domain and range. Square : R R Square(n) = n 2 A lambda expression is a particular way to define a function: LambdaExpression variable ( M N) ( variable. M ) M LambdaExpression N LambdaExpression E.g., ( x. x 2 ) represents the Square function. Dr. Philip Cannata 4

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. x 2 ) 5) = 5 2 Dr. Philip Cannata 5

Lambda Calculus and Lambda Calculus in Python (sort of) x.x s.(s s) func. arg.(func arg) Lambda Calculus def identity = x.x def self_apply = s.(s s) def apply = func. arg.(func arg) def select_first = first. second.first def select_second = first. second.second def cond= e1. e2. c.((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) def and= x. y.(((cond y) false) x) Or def and= x. y.((x y) false) def or= x. y.(((cond true) y) x) Or def or= x. y.((x true) y) lambda x: x lambda s: (s)(s) Python Version lambda func, arg: (func)(arg) Identity = lambda x: x self_apply = lambda s: (s)(s) apply = lambda func, arg: (func)(arg) select_first=lambda f, s: f select_second=lambda f, s: s cond=lambda c, e1, e2: (c)(e1, e2) true=select_first false=select_second lnot=lambda x: (x)(false, true) land=lambda x, y: (cond)(x, y, false) lor=lambda x, y: (cond)(x, true, y) s=(lambda s: (s)(s)) (s)(lambda x: x) (s)(s) (lambda func, arg: (func)(arg))((lambda x: x), 4) (select_first)(3, 4) Application (lambda x: x)(4) (lambda x: x)(lambda x: x+2) f=(lambda x: x)(lambda x: x+2) f(4) (lnot)(true) ((lnot)(true))("true", "false") ((land)(true, true))("true", "false") ((land)(true, false))("true", "false") ((land)(true, true))("true", "false") ((land)(true, false))("true", "false") Dr. Philip Cannata 6

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) In lambda calculus, if cond is defined as def cond= e1. e2. c.((c e1) e2), def or= x. y.(((cond true) y) x) is equivalent to 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) Dr. Philip Cannata 7

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)) (x x) a function is returned (λ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)) ((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. Dr. Philip Cannata 8

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)) For more but different details see Section 22.3 of the textbook. 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 (iszero one) (λn.(n select_first) λs.((s false) zero) ) (λs.((s false) zero) select_first) ((select_first false) zero) Dr. Philip Cannata 9

A little Bit of Lambda Calculus Lambda Calculus Arithmetic def addf = λf.λx.λy. if iszero y then x else f f (succ x)(pred y) def add = λx.λy. if iszero y then x else addf addf (succ x)(pred y) add one two ADDITION (((λx.λy. if iszero y then x 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 if iszero y then x 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)) ((((λf.λx.λy if iszero y then x 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 if iszero y then zero 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. Dr. Philip Cannata 10

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) 30414093201713378043612608166064768844377641568960512000000000000 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 )))))) Dr. Philip Cannata 11

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. Dr. Philip Cannata 12

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))))) Dr. Philip Cannata 13

#lang plai Scheme for Textbook Chapter 3 (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))])) Dr. Philip Cannata 14

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 Dr. Philip Cannata 15

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 {x {+ 5 5}} {with {y {- x 3}} {+ y y}}} = {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}} = {+ 7 7} = 14 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). Dr. Philip Cannata 16