Functions and Recursion. Dr. Philip Cannata 1
|
|
- Blaze Jordan
- 6 years ago
- Views:
Transcription
1 Functions and Recursion Dr. Philip Cannata 1
2 10 High Level Languages This Course Java (Object Oriented) Jython in Java Relation ASP RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata 2
3 let transformation, differed substitution and closures, and interpretation in FAE let transformation: (let ((A B)) C) == ((lambda (A) C) B) A B C A B (let (( x 3)) (let ((f (lambda (y) (+ x y)))) (f 4)) ((lambda (x) ((lambda (f) (f 4)) (lambda (y) (+ x y)))) 3) (app (fun 'x [app (fun 'f [app (id 'f) (num 4)]) (fun 'y (add (id 'x) (id 'y)))]) (num 3)) (app arg arg (app arg arg2 Differed substitution and closures: (asub 'f (closurev 'y (add (id 'x) (id 'y)) (asub 'x (numv 3) (mtsub))) (asub 'x (numv 3) (mtsub))) Interpretation: (interp (app (id 'f) (num 4)) (asub 'f (closurev 'y (add (id 'x) (id 'y)) (asub 'x (numv 3) (mtsub))) (asub 'x (numv 3) (mtsub)))) (numv 7) Dr. Philip Cannata 3 C
4 Eduardo Saenz (anon. to classmates) (1 day ago) - If our AST does not have let class nodes, then when our interpreter visits every node of the AST, making the environment of differed substitutions along the way, our environment will only have closures? You only do: (asub x 3 (mtsub)) when you encounter a let, and since our parser converts lets to lambdas we'll never see this type of differed substitution in our environment; only closures in our environment. Is this logic correct? Philip Cannata (Instructor) (Just now) - This is a good observation but try to understand the following three cases that can occur and see if you can distinguish when deferred substitution should be done in each of them and when function application should be done: > (parse '(with (f (fun (x) x)) 5)) (app (fun 'f (num 5)) (fun 'x (id 'x))) > (interp (parse '(with (f (fun (x) x)) 5)) (mtsub)) (numv 5) > (parse '(with (f (fun (x) x)) (f 5))) (app (fun 'f (app (id 'f) (num 5))) (fun 'x (id 'x))) > (interp (parse '(with (f (fun (x) x)) (f 5))) (mtsub)) (numv 5) > (parse '(with (f (fun (x) x)) ((fun (y) (+ y 2)) 5))) (app (fun 'f (app (fun 'y (add (id 'y) (num 2))) (num 5))) (fun 'x (id 'x))) > (interp (parse '(with (f (fun (x) x)) ((fun (y) (+ y 2)) 5))) (mtsub)) (numv 7) Dr. Philip Cannata 4
5 Static scoping: Static and Dynamic Scoping (interp (parse '{with {x 5 {f 4) (asub 'f (closurev 'y (add (id 'x) (id 'y)) (asub 'x (numv 3) (mtsub))) (asub 'x (numv 3) (mtsub)))) (numv 7) Dynamic Scoping: (interp (parse '{with {x 5 {f 4) (asub 'f (closurev 'y (add (id 'x) (id 'y)) (asub 'x (numv 3) (mtsub))) (asub 'x (numv 3) (mtsub)))) (numv 9) Think about this expression for both Static and Dynamic Scoping: (let ((z 3)) (let ((d 3) (f (lambda (x) x))) (let ((z 27)) (let ((z 3) (a 5) (x (lambda (x y) (+ x (+ y z))))) (let ((z 9) (a 7)) (x z a)))))) Dr. Philip Cannata 5
6 PLAI Chapters 4, 5 and 6 Chapter 6, Pages 41 & 42 first-order Functions are not values in the language. They can only be defined in a designated portion of the program, where they must be given names for use in the Page remainder 27 - " of the program. The functions in F1WAE are of this nature, which explains the 1 in the name of the language. higher-order Functions can return other functions as values. first-class Functions are values with all the rights of other values. In particular, they can be supplied as the value of arguments to functions, returned by functions as answers, and stored in data structures. Dr. Philip Cannata 6
7 $ java crono.crono (let ((z 17)) (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z a)))) Evaluating: (let ((z 17)) (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z a)))) empty Evaluating: (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z a))) Evaluating: (\ (x y) (- x (+ y z))) Result: (\ (x y) (- x (+ y z))) [] Evaluating: (let ((z 10) (a 5)) (x z a)) z: 3 x: (\ (x y) (- x (+ y z))) [] Evaluating: (x z a) z: 10 x: (\ (x y) (- x (+ y z))) [] Evaluating: (- x (+ y z)) y: 5 x: 10 Evaluating: (+ y z) y: 5 x: 10 Result: 22 Result: -12 Result: -12 Result: -12 Result: -12 Result: -12 In Crono, \ mean lambda Dr. Philip Cannata 7
8 In CronoOptions.java set public static boolean ENVIRONMENT_DYNAMIC = true; Run ant to rebuild crono. $ java crono.crono (let ((z 17)) (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z a)))) Evaluating: (let ((z 17)) (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z a)))) empty Evaluating: (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z a))) Evaluating: (\ (x y) (- x (+ y z))) Result: (\ (x y) (- x (+ y z))) [] Evaluating: (let ((z 10) (a 5)) (x z a)) z: 3 x: (\ (x y) (- x (+ y z))) [] Evaluating: (x z a) z: 10 x: (\ (x y) (- x (+ y z))) [] Evaluating: (- x (+ y z)) y: 5 z: 10 x: 10 Evaluating: (+ y z) y: 5 z: 10 x: 10 Result: 15 Result: -5 Result: -5 Result: -5 Result: -5 Result: -5 Dr. Philip Cannata 8
9 (let ((z 17)) (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z (x 0 0))))) Evaluating: (let ((z 17)) (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z (x 0 0))))) empty Evaluating: (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z (x 0 0)))) Evaluating: (\ (x y) (- x (+ y z))) Result: (\ (x y) (- x (+ y z))) [] Evaluating: (let ((z 10) (a 5)) (x z (x 0 0))) z: 3 x: (\ (x y) (- x (+ y z))) [] Evaluating: (x z (x 0 0)) z: 10 x: (\ (x y) (- x (+ y z))) [] Evaluating: (x 0 0) z: 10 x: (\ (x y) (- x (+ y z))) [] Evaluating: (- x (+ y z)) y: 0 x: 0 Evaluating: (+ y z) y: 0 x: 0 Result: 17 Result: -17 Result: -17 Evaluating: (- x (+ y z)) y: -17 x: 10 Evaluating: (+ y z) y: -17 x: 10 Result: 0 Result: 10 Result: 10 Result: 10 Result: 10 Result: 10 Notice function application here. Dr. Philip Cannata 9
10 (let ((z 17)(i 22)) (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z (x 0 ((\ (x) x) i)))))) Evaluating: (let ((z 17) (i 22)) (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z (x 0 ((\ (x) x) i)))))) empty Evaluating: (let ((z 3) (a 5) (x (\ (x y) (- x (+ y z))))) (let ((z 10) (a 5)) (x z (x 0 ((\ (x) x) i))))) Evaluating: (\ (x y) (- x (+ y z))) Result: (\ (x y) (- x (+ y z))) [, ] Evaluating: (let ((z 10) (a 5)) (x z (x 0 ((\ (x) x) i)))) z: 3 x: (\ (x y) (- x (+ y z))) [, ] Evaluating: (x z (x 0 ((\ (x) x) i))) z: 10 x: (\ (x y) (- x (+ y z))) [, ] Evaluating: (x 0 ((\ (x) x) i)) z: 10 x: (\ (x y) (- x (+ y z))) [, ] Evaluating: ((\ (x) x) i) z: 10 x: (\ (x y) (- x (+ y z))) [, ] Evaluating: (\ (x) x) z: 10 x: (\ (x y) (- x (+ y z))) [, ] Result: (\ (x) x) [,, z: 10, x: (\ (x y) (- x (+ y z)))] Result: 22 Evaluating: (- x (+ y z)) y: 22 x: 0 Evaluating: (+ y z) y: 22 x: 0 Result: 39 Result: -39 Result: -39 Evaluating: (- x (+ y z)) y: -39 x: 10 Evaluating: (+ y z) y: -39 x: 10 Result: -22 Result: 32 Result: 32 Result: 32 Result: 32 Result: 32 Dr. Philip Cannata 10
11 10 High Level Languages This Course Java (Object Oriented) Jython in Java Relation ASP RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata 11
12 Definitions An argument is an expression that appears in a function application/ call. A parameter is an identifier that appears in a function definition/ declaration. In the code on the right the call A(a, b) has arguments a and b. The function declaration A has parameters x and y. Parameter and Arguments int h, i; void B(int w) { int j = 1, k = 2; i = 2*w; w = w+1; printf("in B - w, j, k, h, i: %d, %d, %d, %d, %d\n", w, j, k, h, i); void A(int x, int y) { float i = 1.1, j = 2.2; B(h); printf("in A - x, y, i, j, h: %d, %d, %f, %f, %d\n", x, y, i, j, h); int main() { int a, b; h = 5; a = 3; b = 2; A(a, b); printf("in Main a, b, h, i: %d, %d, %d, %d\n", a, b, h, i); Dr. Philip Cannata 12
13 By value - Compute the value of the argument at the time of the call and assign that value to the parameter. So passing by value doesn t normally allow the called function to modify an argument s value. By reference - Compute the address of the argument at the time of the call and assign it to the parameter. Passing by value allows the called function to modify an argument s value. By value-result By name Parameter Passing Mechanisms Dr. Philip Cannata 13
14 int h, i; void B(int w) { int j = 1, k = 2; i = 2*w; w = w+1; Pass by Value printf("in B - w, j, k, h, i: %d, %d, %d, %d, %d\n", w, j, k, h, i); void A(int x, int y) { float i = 1.1, j = 2.2; B(h); printf("in A - x, y, i, j, h: %d, %d, %f, %f, %d\n", x, y, i, j, h); int main() { int a, b; h = 5; a = 3; b = 2; A(a, b); printf("in Main a, b, h, i: %d, %d, %d, %d\n", a, b, h, i); int h, i; void B(int *w) { int j = 1, k = 2; i = 2*(*w); *w = *w + 1; Pass by Reference printf("in B - w, j, k, h, i: %d, %d, %d, %d, %d\n", w, j, k, h, i); void A(int *x, int *y) { float i = 1.1, j = 2.2; B(&h); printf("in A - x, y, i, j, h: %d, %d, %f, %f, %d\n", x, y, i, j, h); int main() { int a, b; h = 5; a = 3; b = 2; A(&a, &b ); printf("in Main a, b, h, i: %d, %d, %d, %d\n", a, b, h, i); $./a In B - w, j, k, h, i: 6, 1, 2, 5, 10 In A - x, y, i, j, h: 3, 2, , , 5 In Main a, b, h, i: 3, 2, 5, 10 $./a In B - w, j, k, h, i: , 1, 2, 6, 10 In A - x, y, i, j, h: , , , , 6 In Main a, b, h, i: 3, 2, 6, 10 Dr. Philip Cannata 14
15 Pass by Value-Results Pass by value at the time of the call and/or copy the result back to the argument at the end of the call also called copy-in-copy-out. Pass by Name Textually substitute the argument for every instance of its corresponding parameter in the function body. Originated with Algol 60 (Jensen s device), but was dropped by Algol s successors -- Pascal, Ada, Modula. real procedure Sum(j, lo, hi, Ej); x[j]*j value lo, hi; integer j, lo, hi; real Ej; begin real S; S := 0; for j := lo step 1 until hi do S := S + Ej; Sum := S end; Exemplifies late binding, since evaluation of the argument is delayed until its occurrence in the function body is actually executed. Associated with lazy evaluation in functional languages (e.g., Haskell). Dr. Philip Cannata 15
16 This can be thought of as a stack with cons s on it. (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (cons 5 '())))))) Recursive Functions Exemplified by foldr in lisp (letrec ((f (lambda (l) (if (null? l) '() (cons (car l) (f (cdr l))))))) (f '( ))) '( ) (letrec ((f (lambda (v l) (if (null? l) v (cons (car l) (f v (cdr l))))))) (f '() '( ))) '( ) (letrec ((f (lambda (f1 v l) (if (null? l) v (f1 (car l) (f f1 v (cdr l))))))) (f cons '() '( ))) '( ) f == foldr If f1 == cons, foldr is the identity function for a list. It s the same as (cons 1 (cons 2 (cons 3( cons 4 (cons 5 (cons 6 '())))))) Dr. Philip Cannata 16 (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (cons 5 '())))))) Here the stack is upside down
17 Recursive Functions Exemplified by foldl in lisp Nothing goes on the stack in this case. (letrec ((f (lambda (f1 v l) (if (null? l) v (f f1 (car l) (cdr l)))))) (f cons '() '( ))) 6 (letrec ((f (lambda (f1 v l) (if (null? l) v (f f1 (f1 (car l) v) (cdr l)))))) (f cons '() '( ))) '( ) f == foldl If f1 == cons, foldl reverses the list. foldl is tail-recursive because nothing goes on the stack. It s the same as (cons 6 (cons 5 (cons 4 ( cons 3 (cons 2 (cons 1 '())))))) Dr. Philip Cannata 17
18 Recursive Functions A function that can call itself, either directly or indirectly, is a recursive function. $ cat test.c int factorial (int n) { int i; if (n < 2) { printf("factorial returning 1\n"); return 1; else { i = n * factorial(n-1); printf("factorial returning %d\n", i); return i; int main() { printf("factorial(3) returns: %d\n", factorial(3)); $./a factorial returning 1 factorial returning 2 factorial returning 6 factorial(3) returns: 6 Dr. Philip Cannata 18
19 Runtime Stack A stack of activation records: An activation record is a block of information associated with each function call, which includes: parameters and local variables Return address Each new call pushes an activation record, and each completing call pops the topmost one. So, the topmost record is the most recent call, and the stack has all active calls at any run-time moment. Dr. Philip Cannata 19
20 Runtime Stack for Functions Program int h, i;! void B(int w) {! int j, k;! i = 2*w;! w = w+1;!! void A(int x, int y) {! bool i, j;! B(h);!! int main() {! int a, b;! h = 5; a = 3; b = 2;! A(a, b);!! parameters and local variables Return address Saved registers Temporary variables Return value Dr. Philip Cannata 20
21 Calling function: factorial BasePtr = 3, printing runtime stack n: 3 answer: null number: 3 Calling function: factorial BasePtr = 5, printing runtime stack n: 2 n: 3 answer: null number: 3 Calling function: factorial BasePtr = 7, printing runtime stack n: 1 n: 2 n: 3 answer: null number: 3 Calling function: factorial BasePtr = 9, printing runtime stack n: 0 n: 1 n: 2 n: 3 answer: null number: 3 Hmm Runtime Stack for Factorial 3 int factorial(int n) { if(n < 1) { return 1; else { return n * factorial(n - 1); int main() { int number, answer; number = 3; answer = factorial(number); print(answer); Exiting function: factorial BasePtr = 9, printing runtime stack n: 0 return#factorial: 1 n: 1 n: 2 n: 3 answer: null number: 3 Exiting function: factorial BasePtr = 7, printing runtime stack return#factorial: 1 n: 1 return#factorial: 1 n: 2 n: 3 answer: null number: 3 Exiting function: factorial BasePtr = 5, printing runtime stack return#factorial: 1 n: 2 return#factorial: 2 n: 3 answer: null number: 3 Exiting function: factorial BasePtr = 3, printing runtime stack return#factorial: 2 n: 3 return#factorial: 6 answer: null number: 3 Dr. Philip Cannata 21
Functions and Recursion
Programming Languages Functions and Recursion Dr. Philip Cannata 1 10 High Level Languages This Course Jython in Java Java (Object Oriented) ACL2 (Propositional Induction) Relation Algorithmic Information
More informationReminder About Functions
Reminder About Functions (let ((z 17)) (let ((z 3) (a ) (x (lambda (x y) (- x (+ y z))))) (let ((z 0) (a )) (x z a)))) int h, i; void B(int w) { int j, k; i = 2*w; w = w+1; void A(int x, int y) { bool
More information22c:111 Programming Language Concepts. Fall Functions
22c:111 Programming Language Concepts Fall 2008 Functions Copyright 2007-08, The McGraw-Hill Company and Cesare Tinelli. These notes were originally developed by Allen Tucker, Robert Noonan and modified
More informationknown as non-void functions/methods in C/C++/Java called from within an expression.
FUNCTIONS 1 OUTLINE Basic Terminology Function Call and Return Parameters Parameter Passing Mechanisms Activation Records Recursive Functions Run Time Stack Function Declaration and Call in Clite Completing
More informationHigher-Order Functions
Higher-Order Functions 1 Why Functions as Values Abstraction is easier with functions as values abstract over add and sub cases filter, map, etc. What are objects? Callbacks? Separate deffun form becomes
More informationNames and Types. standard hue names. Dr. Philip Cannata 1
Names and Types standard hue names Dr. Philip Cannata 1 10 High Level Languages This Course Jython in Java Java (Object Oriented) ACL2 (Propositional Induction) Relation Algorithmic Information Theory
More informationCS 345. Functions. Vitaly Shmatikov. slide 1
CS 345 Functions Vitaly Shmatikov slide 1 Reading Assignment Mitchell, Chapter 7 C Reference Manual, Chapters 4 and 9 slide 2 Procedural Abstraction Can be overloaded (e.g., binary +) Procedure is a named
More informationCost of Substitution
Cost of Substitution (interp {with {x 1} {with {y 2} {+ 100 {+ 99 {+ 98... {+ y x}}}}}} ) (interp {with {y 2} {+ 100 {+ 99 {+ 98... {+ y 1}}}}} ) (interp {+ 100 {+ 99 {+ 98... {+ 2 1}}}} ) With n variables,
More informationFall 2012 CS345 Project Requirements and Suggestions (Version 4)
Fall 2012 CS345 Project Requirements and Suggestions (Version 4) Languages developed or modified for your project should have the following properties unless there is a good reason not to (e.g., you re
More informationName EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )
CS 345 Spring 2010 Midterm Exam Name EID 1. [4 Points] Circle the binding instances in the following expression: (calc (parse '+ with x + 5 5 with y - x 3 + y y z ) ) 2. [7 Points] Using the following
More informationProject 2: Scheme Interpreter
Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same
More informationImplementing Recursion
Implementing Recursion Shriram Krishnamurthi 2003-09-19 We ve seen that there are (at least two, fairly distinct ways of representing environments To implement recursive environments, we need to provide
More informationHigher-Order Functions (Part I)
Higher-Order Functions (Part I) 1 Why Functions as Values Abstraction is easier with functions as values abstract over add and sub cases filter, map, etc. What are objects? Callbacks? Separate deffun form
More informationTail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)
CMSC 330: Organization of Programming Languages Tail Calls A tail call is a function call that is the last thing a function does before it returns let add x y = x + y let f z = add z z (* tail call *)
More informationTyped Recursion {with {mk-rec : (((num -> num) -> (num -> num)) -> (num -> num)) {fun {body : ((num -> num) -> (num -> num))} {{fun {fx :
Recursion {with {mk-rec {fun {body} {{fun {fx} {fx fx}} {fun {fx} {{fun {f} {body f}} {fun {x} {{fx fx} x}}}}}}} {with {fib {mk-rec {fun {fib} {fun {n} {if0 n 1 {if0 {- n 1} 1 {+ {fib {- n 1}} {fib {-
More informationName SOLUTIONS EID NOTICE: CHEATING ON THE MIDTERM WILL RESULT IN AN F FOR THE COURSE.
CS 345 Fall TTh 2012 Midterm Exam B Name SOLUTIONS EID NOTICE: CHEATING ON THE MIDTERM WILL RESULT IN AN F FOR THE COURSE. 1. [5 Points] Give two of the following three definitions. [2 points extra credit
More informationFunctional Programming - 2. Higher Order Functions
Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class
More informationFunctional Programming. Pure Functional Programming
Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).
More informationPrinciples of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004
Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004 CS 314, LS,BR,LTM: Scope and Memory 1 Review Functions as first-class objects What can you do with an integer?
More informationfjyswan Dr. Philip Cannata 1
fjyswan Dr. Philip Cannata 1 10 High Level Languages This Course Jython in Java Java (Object Oriented) Relation ASP RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata 2 Dr. Philip Cannata 3 fjyswan
More informationCS 415 Midterm Exam Spring 2002
CS 415 Midterm Exam Spring 2002 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Good Luck! Score Fortran Algol 60 Compilation Names, Bindings, Scope Functional Programming
More informationEvery language has its own scoping rules. For example, what is the scope of variable j in this Java program?
Lexical Binding There are two ways a variable can be used in a program: As a declaration As a "reference" or use of the variable Scheme has two kinds of variable "declarations" -- the bindings of a let-expression
More informationComp 311: Sample Midterm Examination
Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief
More informationcs173: Programming Languages Midterm Exam
cs173: Programming Languages Midterm Exam Fall 2002 Please read this page of instructions before you turn the page! This exam is worth 181 points. We will assign partial credit to partial responses, provided
More informationcs173: Programming Languages Final Exam
cs173: Programming Languages Final Exam Fall 2002 Please read this page of instructions before you turn the page! This exam is worth 102 points. We will assign partial credit to partial responses, provided
More informationPrinciples of Programming Languages 2017W, Functional Programming
Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.
More informationMIDTERM EXAMINATION - CS130 - Spring 2005
MIDTERM EAMINATION - CS130 - Spring 2005 Your full name: Your UCSD ID number: This exam is closed book and closed notes Total number of points in this exam: 231 + 25 extra credit This exam counts for 25%
More informationLecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.
Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)
More informationCMSC 330: Organization of Programming Languages. Operational Semantics
CMSC 330: Organization of Programming Languages Operational Semantics Notes about Project 4, Parts 1 & 2 Still due today (7/2) Will not be graded until 7/11 (along with Part 3) You are strongly encouraged
More informationCOP4020 Programming Assignment 1 - Spring 2011
COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to
More informationDiscussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism
Type Joseph Spring Discussion Languages and Type Type Checking Subtypes Type and Inheritance and 7COM1023 Programming Paradigms 1 2 Type Type denotes the kind of values that programs can manipulate: Simple
More informationCS 275 Name Final Exam Solutions December 16, 2016
CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need
More informationThreads. {seqn {spawn EXPR 1 } {spawn EXPR 2 }} Runs EXPR 1 and EXPR 2 in any order, even interleaved with each other
Sequential Programs So far, the language that we ve implemented is deterministic. Running a program multiple times (or computing things slightly more quickly or slowly) does not change the result of the
More informationSimple Lisp. Alonzo Church. John McCarthy. Turing. David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan
Alonzo Church John McCarthy Simple Lisp David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan Turing Dr. Philip Cannata 1 Simple Lisp See the class website for a pdf
More informationRunning FAE Programs Natively
Running FAE Programs Natively So far: explained various language constructs by using Scheme Explaining Scheme? Certain parts explained using simpler parts of Scheme Leftover parts explained by reduction
More informationState. Substitution relies on an identifer having a fxed value
Part 1 1 State Substitution relies on an identifer having a fxed value {let {[x 5]} {let {[f {lambda {y} {+ x y}}]}... {f 1}}} = {let {[f {lambda {y} {+ 5 y}}]}... {f 1}} because x cannot change In Plait,
More informationThe Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis
The Compiler So Far Overview of Semantic Analysis Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) Lexical analysis Detects inputs with illegal tokens Parsing Detects inputs with ill-formed
More informationComputer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing
Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible
More informationFunctional Programming. Big Picture. Design of Programming Languages
Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics
More informationProgramming Languages. Dr. Philip Cannata 1
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
More informationProgramming Languages
Programming Languages Tevfik Koşar Lecture - XIII March 2 nd, 2006 1 Roadmap Functional Languages Lambda Calculus Intro to Scheme Basics Functions Bindings Equality Testing Searching 2 1 Functional Languages
More informationCOP4020 Programming Languages. Functional Programming Prof. Robert van Engelen
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts
More informationRecursion & Iteration
Recursion & Iteration York University Department of Computer Science and Engineering 1 Overview Recursion Examples Iteration Examples Iteration vs. Recursion Example [ref.: Chap 5,6 Wilensky] 2 Recursion
More informationCS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018
CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018 Taken from assignments by Profs. Carl Offner and Ethan Bolker Part 1 - Modifying The Metacircular Evaluator
More informationCSE413 Midterm. Question Max Points Total 100
CSE413 Midterm 05 November 2007 Name Student ID Answer all questions; show your work. You may use: 1. The Scheme language definition. 2. One 8.5 * 11 piece of paper with handwritten notes Other items,
More informationPrinciples of Programming Languages
Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,
More informationCMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics
Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End
More informationTypes and Type Inference
CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of
More informationWellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS
Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS This document contains solutions to the problems on the final exam review problems posted earlier except for
More informationCSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8
Question 1. (12 points) For each of the following, what value is printed? (Assume that each group of statements is executed independently in a newly reset Scheme environment.) (a) (define x 1) (define
More informationThe role of semantic analysis in a compiler
Semantic Analysis Outline The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors
More informationA LISP Interpreter in ML
UNIVERSITY OF OSLO Department of Informatics A LISP Interpreter in ML Mandatory Assignment 1 INF3110 September 21, 2009 Contents 1 1 Introduction The purpose of this assignment is to write an interpreter,
More informationSemantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far
Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Statically vs. Dynamically typed languages
More informationScheme as implemented by Racket
Scheme as implemented by Racket (Simple view:) Racket is a version of Scheme. (Full view:) Racket is a platform for implementing and using many languages, and Scheme is one of those that come out of the
More informationProgramming Languages. Dr. Philip Cannata 1
Programming Languages Dr. Philip Cannata 0 High Level Languages This Course Java (Object Oriented) Jython in Java Relation ASP RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata Dr. Philip Cannata
More informationFunctional Programming Languages (FPL)
Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional
More informationStack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.
STACK Stack ADT 2 A stack is an abstract data type based on the list data model All operations are performed at one end of the list called the top of the stack (TOS) LIFO (for last-in first-out) list is
More informationHigh Level Languages. Java (Object Oriented) This Course. Jython in Java. Relation. ASP RDF (Horn Clause Deduction, Semantic Web) Dr.
10 High Level Languages This Course Java (Object Oriented) Jython in Java Relation ASP RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata 1 Dr. Philip Cannata 2 Programming Languages Lexical
More informationCSCI312 Principles of Programming Languages!
CSCI312 Principles of Programming Languages! Scope Xu Liu ! 4.1 Syntactic Issues! 4.2 Variables! 4.3 Scope! 4.4 Symbol Table! 4.5 Resolving References! 4.6 Dynamic Scoping! 4.7 Visibility! 4.8 Overloading!
More informationCS 415 Midterm Exam Fall 2003
CS 415 Midterm Exam Fall 2003 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Questions will be graded on quality of answer. Please supply the best answer you can to
More informationDesign Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8
Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns
More informationLECTURE 18. Control Flow
LECTURE 18 Control Flow CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear in a program text. Selection (or alternation): a
More informationAn Introduction to Functions
Chapter 4 An Introduction to Functions Through the agency of with, we have added identifiers and the ability to name expressions to the language. Much of the time, though, simply being able to name an
More informationSemantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End
Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors
More informationTypes, Type Inference and Unification
Types, Type Inference and Unification Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Cornell CS 6110 Summary (Functional Programming) Lambda Calculus Basic ML Advanced ML: Modules, References,
More informationCS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find
CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS
More informationCS4215 Programming Language Implementation
CS4215 Programming Language Implementation You have 45 minutes to complete the exam. Use a B2 pencil to fill up the provided MCQ form. Leave Section A blank. Fill up Sections B and C. After finishing,
More informationOrganization of Programming Languages CS3200/5200N. Lecture 11
Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages
More informationLecture 8: Recursion and Iteration. Exceptions. Declarative Programming.
Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. September 26th, 2010 Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (1/48) Lecture Outline Memory Management,
More informationNOTE: Answer ANY FOUR of the following 6 sections:
A-PDF MERGER DEMO Philadelphia University Lecturer: Dr. Nadia Y. Yousif Coordinator: Dr. Nadia Y. Yousif Internal Examiner: Dr. Raad Fadhel Examination Paper... Programming Languages Paradigms (750321)
More informationBox-and-arrow Diagrams
Box-and-arrow Diagrams 1. Draw box-and-arrow diagrams for each of the following statements. What needs to be copied, and what can be referenced with a pointer? (define a ((squid octopus) jelly sandwich))
More informationSummer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define
CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationFunctional Programming. Pure Functional Languages
Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure
More informationFunctional Programming. Pure Functional Languages
Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure
More informationLecture 16: Static Semantics Overview 1
Lecture 16: Static Semantics Overview 1 Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing Produces trees Detects & eliminates ill-formed parse trees Static semantic analysis
More informationNon-Functional Procedures
Functional Programs So far, the language that we ve implemented is purely functional A function produces the same result every time for the same arguments Real programming languages do not behave this
More informationCS 415 Midterm Exam Spring SOLUTION
CS 415 Midterm Exam Spring 2005 - SOLUTION Name Email Address Student ID # Pledge: This exam is closed note, closed book. Questions will be graded on quality of answer. Please supply the best answer you
More information;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for
Page 1/11 (require (lib "trace")) Allow tracing to be turned on and off (define tracing #f) Define a string to hold the error messages created during syntax checking (define error msg ""); Used for fancy
More informationFunctional Programming and Haskell
Functional Programming and Haskell Tim Dawborn University of Sydney, Australia School of Information Technologies Tim Dawborn Functional Programming and Haskell 1/22 What are Programming Paradigms? A programming
More informationFunctional Programming
Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used
More informationParsing Scheme (+ (* 2 3) 1) * 1
Parsing Scheme + (+ (* 2 3) 1) * 1 2 3 Compiling Scheme frame + frame halt * 1 3 2 3 2 refer 1 apply * refer apply + Compiling Scheme make-return START make-test make-close make-assign make- pair? yes
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 18: Functional Programming Zheng (Eddy) Zhang Rutgers University April 9, 2018 Review: Defining Scheme Functions (define ( lambda (
More informationImplementing Subprograms
1 Implementing Subprograms CS 315 Programming Languages Pinar Duygulu Bilkent University CS315 Programming Languages Pinar Duygulu The General Semantics of Calls and Returns 2 The subprogram call and return
More informationChapter 15. Functional Programming Languages
Chapter 15 Functional Programming Languages Copyright 2009 Addison-Wesley. All rights reserved. 1-2 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages
More informationCS558 Programming Languages
CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and
More informationLogic - CM0845 Introduction to Haskell
Logic - CM0845 Introduction to Haskell Diego Alejandro Montoya-Zapata EAFIT University Semester 2016-1 Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester
More informationFunctional Languages. Hwansoo Han
Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm
More informationMidterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1
Midterm 2 Solutions 1. [20 points] Consider the language that consist of possibly empty lists of the identifier x enclosed by parentheses and separated by commas. The language includes { () (x) (x,x) (x,x,x)
More informationThe results for a few specific cases below are indicated. allequal ([1,1,1,1]) should return true allequal ([1,1,2,1]) should return false
Test 1 Multiple Choice. Write your answer to the LEFT of each problem. 4 points each 1. Which celebrity has not received an ACM Turing Award? A. Alan Kay B. John McCarthy C. Dennis Ritchie D. Bjarne Stroustrup
More informationScheme Quick Reference
Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete
More informationUNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.
UNIVERSITY OF TORONTO Faculty of Arts and Science Midterm Sample s CSC324H1 Duration: 50 minutes Instructor(s): David Liu. No Aids Allowed Name: Student Number: Please read the following guidelines carefully.
More informationTypes and Type Inference
Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on the course homepage Outline General discussion
More informationLECTURE 16. Functional Programming
LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative
More informationAnatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?
Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator
More informationCPSC 311, 2010W1 Midterm Exam #2
CPSC 311, 2010W1 Midterm Exam #2 2010/11/02 Page 1 of 18 CPSC 311, 2010W1 Midterm Exam #2 Name: Q1: 20 Student ID: Q2: 20 Signature (required; indicates agreement with rules below): Q3: 20 Q4: 20 Q5: 20
More informationProgramming Languages. Dr. Philip Cannata 1
Programming Languages Dr. Philip Cannata 0 High Level Languages This Course Jython in Java Java (Object Oriented) ACL (Propositional Induction) Relation Algorithmic Information Theory (Information Compression
More informationLecture 15 CIS 341: COMPILERS
Lecture 15 CIS 341: COMPILERS Announcements HW4: OAT v. 1.0 Parsing & basic code generation Due: March 28 th No lecture on Thursday, March 22 Dr. Z will be away Zdancewic CIS 341: Compilers 2 Adding Integers
More informationCPS 506 Comparative Programming Languages. Programming Language Paradigm
CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming
More informationCS 11 Haskell track: lecture 1
CS 11 Haskell track: lecture 1 This week: Introduction/motivation/pep talk Basics of Haskell Prerequisite Knowledge of basic functional programming e.g. Scheme, Ocaml, Erlang CS 1, CS 4 "permission of
More informationCSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018
CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018 Deliverables: Your code (submit to PLC server). A13 participation survey (on Moodle, by the day after the A13 due date). This is
More information