Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming.

Size: px
Start display at page:

Download "Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming."

Transcription

1 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, Bindings, Scope contd Dynamic Scope Recursion, Iteration, and Last Call Optimization Recursion Iteration and Tail Recursion Exceptions Chapter 2 Summary Summary Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (2/48)

2 Dynamic Scope In dynamically scoped languages, free identifiers are looked-up in the caller s environment rather than in the environment corresponding to lexical (textual) regions of code. Example (Dynamic scope in Emacs Lisp) (let ((x 1)) (defun showx () (message "x = %d" x)) (let ((x 2)) (showx))) The no-argument procedure showx has one free variable in its body, 1 which is given the value 1 in the lexically enclosing environment. When showx is called, it needs to lookup x, but it does that using the caller s environment, which now includes a binding of x to 2 thus, 2 is displayed. Try it! Open the program code/scope.el in emacs and execute it (C-x X-e). 1 Well, it has two: message is free as well. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (3/48) Dynamic Scope contd In some languages, we can achieve the effects of dynamic scoping with special constructs. Example (Lexical and dynamic scoping in Scheme) lexical scoping: dynamic scoping: (let ((x 1)) (define (showx) (display x)) (let ((x 2)) (showx)) ;; 1 (showx)) ;; 1 (let ((x 1)) (define (showx) (display x)) (fluid-let ((x 2)) (showx)) ;; 2 (showx)) ;; 1 let creates a new environment and introduces new bindings; 2 when showx (left) is called, x is looked up in the environment at its definition, where x equals 1. fluid-let does not create any environment, but rather temporarily modifies bindings in the enclosing environment; when showx is called (right), x is looked up as previously, but now it equals 2. 2 let in Scheme roughly corresponds to local in Oz. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (4/48)

3 Dynamic Scope contd In different languages similar constructs may have different effects, or have similar effects despite different mechanisms. Example (Dynamic scoping) Bash: x=hello showx() { echo $x; } test() { local x=dolly; showx; } test # dolly Perl: $x = "hello"; sub showx { print $x; } sub test { local $x = "dolly"; &showx; } &test # dolly &showx # hello In Bash, local introduces a local variable, but functions do not capture definition environments, and showx uses the binding for x available where it is called. In Perl, local temporarily modifies bindings in the enclosing environment, and showx within test looks-up the global x when it has the temporary value dolly. Try it! Execute the code in code/scope.sh and code/scope.pl. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (5/48) Lecture Outline Memory Management, Bindings, Scope contd Dynamic Scope Recursion, Iteration, and Last Call Optimization Recursion Iteration and Tail Recursion Exceptions Chapter 2 Summary Summary Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (6/48)

4 Recursion What is recursion? Recursion: see recursion. Recursion is a fundamental concept in mathematics, and in computer science in particular, in functional programming. A recursive procedure is one that calls itself within its own body. 3 A recursive computation is one in which a recursive procedure is called repetitively so that the result of each call deps on the result of the subsequent calls. In a recursive computation, the stack of activation records (the semantic stack in the abstract machine in Oz) grows with each nested call, and shrinks when nested calls are exited. 3 There are other variants of recursion, e.g., mutual recursion; see below. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (7/48) Recursion contd Recursive computations need appropriate stopping conditions otherwise, stack overflow is inevitable. Example (Recursive procedure) declare fun {Factorial N} if N < 2 then 1 else N * {Factorial N - 1} Factorial computes the factorial of its argument: given a non-negative integer n, it computes n! = n (n 1) Factorial is recursive there is a call to Factorial within Factorial s body. 4 Incidentally, this implementation computes incorrectly with negative integers as well. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (8/48)

5 Recursion contd If the nested call in a recursive procedure is not the last statement to be executed within the body, the procedure specifies a recursive computation. Let us see how the computation of {Factorial 3} proceeds we need to translate Factorial (and its application) into the kernel language. Example (Recursive procedure, quasi-kernel language) local Factorial N Result in Factorial = proc {$ N?Result} if N < 2 then Result = 1 else local NMinusOne NestedResult in NMinusOne = N - 1 {Factorial NMinusOne NestedResult} Result = N * NestedResult N = 3 {Factorial N Result} We shall visualize the steps using a slightly modified notation. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (9/48) Recursion contd Example (Application of a recursive procedure) 1. [ (local F N R in..., {}) ] { }... (nested declarations, sequence splitting) 5. [ (F = proc..., {F:v1, N:v2, R:v3}),... ] { v1, v2, v3 }... (binding F, sequence splitting) 8. [ ({F N R}, {F:v1, N:v2, R:v3}) ] { v1=(proc {$ N R}..., {F:v1}), v2=3, v3 } 9. [ (if N < 2..., {F:v1, N:v2, R:v3}) ] { v1=(proc {$ N R}..., {F:v1}), v2=3, v3 }... (evaluating the condition, nested declarations, sequence splitting) 15. [ ({F NM NR}, {F:v1, N:v2, R:v3, NM:v4, NR:v5}), (R = N * NR, {F:v1, N:v2, R:v3, NM:v4, NR:v5}) ] { v1=(...), v2=3, v3, v4=2, v5 } 16. [ (if N < 2..., {F:v1, N:v4, R:v5}), (R = N * NR, {F:v1, N:v2, R:v3, NM:v4, NR:v5}) ] { v1=(...), v2=3, v3, v4=2, v5 }... (evaluating the condition, nested declarations, sequence splitting) Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (10/48)

6 Recursion contd Example (Application of a recursive procedure contd) 22. [ ({F NM NR}, {F:v1, N:v4, R:v5, NM:v6, NR:v7}), (R = N * NR, {F:v1, N:v4, R:v5, NM:v6, NR:v7}), (R = N * NR, {F:v1, N:v2, R:v3, NM:v4, NR:v5}) ] { v1=(...), v2=3, v3, v4=2, v5, v6=1, v7 } 23. [ (if N < 2..., {F:v1, N:v6, R:v7}), (R = N * NR, {F:v1, N:v4, R:v5, NR:v7}), (R = N * NR, {F:v1, N:v2, R:v3, NM:v4, NR:v5}) ] { v1=(...), v2=3, v3, v4=2, v5, v6=1, v7 } 24. [ (R = 1, {F:v1, N:v6, R:v7}), (R = N * NR, {F:v1, N:v4, R:v5, NR:v7}), (R = N * NR, {F:v1, N:v2, R:v3, NM:v4, NR:v5}) ] { v1=(...), v2=3, v3, v4=2, v5, v6=1, v7 } 25. [ (R = N * NR, {F:v1, N:v4, R:v5, NR:v7}), (R = N * NR, {F:v1, N:v2, R:v3, NM:v4, NR:v5}) ] { v1=(...), v2=3, v3, v4=2, v5, v6=1, v7=1 } 26. [ (R = N * NR, {F:v1, N:v2, R:v3, NM:v4, NR:v5}) ] { v1=(...), v2=3, v3, v4=2, v5=2, v6=1, v7=1 } 27. [ ] { v1=(...), v2=3, v3=6, v4=2, v5=2, v6=1, v7=1 } Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (11/48) Iteration and Tail Recursion Examine the content of the semantic stack during an execution of the Factorial program: it grows linearly with each recursive application of Factorial (steps 8, 15, 22); it shrinks linearly when the stop condition (N < 2) is met and the postponed computations are resumed and completed (steps 24 27). The computation is recursive. But we can do better with an alternative design: 5 Instead of accumulating postponed operations on the stack, start the computation from the other. During the computation, the stack will be kept constant in size. This approach is commonly called iteration. 5 We could also do better with explicit state, but our current model does not support that. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (12/48)

7 Iteration and Tail Recursion contd We need to rewrite Factorial so that the recursive calls receive an additional argument, which keeps the partial result computed so far. Example (Iteration) declare fun {Factorial N} fun {Iterate Iterator Accumulator} if Iterator > N then Accumulator else {Iterate Iterator+1 Accumulator*Iterator} in {Iterate 2 1} When no more recursion is needed (Iterator > n), the result has been completely calculated, and can be returned without going back through a chain of postponed computations. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (13/48) Iteration and Tail Recursion contd To see how the computation proceeds on the abstract machine, we need a kernel version of the new Factorial. Example (Iteration, quasi-kernel language) local Factorial N Result in Factorial = proc {$ N?Result} local Iterate Iterator Accumulator in Iterate = proc {$ Iterator Accumulator} if Iterator > N then Result = Accumulator else local NextIterator UpdatedAccumulator in NextIterator = Iterator + 1 UpdatedAccumulator = Accumulator * Iterator {Iterate NextIterator UpdatedAccumulator} Iterator = 2 Accumulator = 1 {Iterate Iterator Accumulator} N = 3 {Factorial N Result} Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (14/48)

8 Iteration and Tail Recursion contd We will not go through the computation manually here, but you are free to try it out. Instead, we can summarize both computations as follows: Example (Recursive vs. iterative computation) recursive: iterative: {Factorial 4} 4 * {Factorial 3} 4 * (3 * {Factorial 2}) 4 * (3 * (2 * {Factorial 1})) 4 * (3 * (2 * 1)) 4 * (3 * 2) 4 * 6 24 {Factorial 4} {Iterate 2 1} {Iterate 3 2} {Iterate 4 6} {Iterate 5 24} 24 The recursive version accumulates postponed operations, the iterative version does not. In the recursive version, the stack grows linearly with input, in the iterative it is kept constant. 6 6 There are variations in stack size throughout both computations, but at each recursive call in the iterative version the stack has the same size. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (15/48) Iteration and Tail Recursion contd In both versions, each recursive call causes new variables to be allocated in the single assignment store. However, in the recursive version, all these local variables are reachable and thus persistent until the procedure reaches its stop condition, and the computation begins to fold back through the postponed operations; in the iterative version, once a recursive call is made some of the local variables are no longer reachable, and may thus be deallocated. Stack and store in recursive and iterative computations Given an input of size n, the stack and store sizes are: Recursive computation Iterative computation Stack size O(n) O(1) Store size O(n) O(1) Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (16/48)

9 Iteration and Tail Recursion contd Recursive procedures that specify iterative processes are usually called tail-recursive, since the recursive call is the last one to be executed within the body. The technique that allows a tail-recursive procedure to return immediately instead of going back through a sequence of stacked call frames 7 is called last call optimization. In some languages (e.g., Oz), last call optimization is automatically applied to tail-recursive procedures. In some languages (e.g., C), last call optimization is applied or not, deping on how the program is compiled. In some languages (e.g., Java), there is no last call optimization. 7 Semantic statements in Oz. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (17/48) Iteration and Tail Recursion contd Try it! Compile and execute the two Oz programs code/recursive.oz and code/iterative.oz: $ ozc -x recursive.oz &&./recursive $ ozc -x iterative.oz &&./iterative Compare their behaviour and examine the source code. Try to run the programs in the Oz debugger: $ ozd recursive $ ozd iterative Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (18/48)

10 Iteration and Tail Recursion contd Try it! Compile and execute the C program code/tail.c, once without and once with last call optimization: $ gcc -g -O0 tail.c -o tail &&./tail $ gcc -g -O2 tail.c -o tail &&./tail Compare the behaviour and examine the source code. Run the code in a debugger and examine the stack: 8 $ gdb tail 8 Type start to start the program, s to step it, bt to examine the stack. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (19/48) Iteration and Tail Recursion contd Try it! Compile and execute the Java program code/tail.java: $ javac tail.java && java tail Observe its behaviour and examine the source code. Run the program in a Java debugger: 9 $ jdb tail 9 Type stop in tail.tail to set a breakpoint, run to start the program, step to step it, where to examine the stack. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (20/48)

11 Lecture Outline Memory Management, Bindings, Scope contd Dynamic Scope Recursion, Iteration, and Last Call Optimization Recursion Iteration and Tail Recursion Exceptions Chapter 2 Summary Summary Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (21/48) Exceptions If we a re lucky, our programs run as they should, receiving the expected input and producing the expected output. What if something goes wrong? Example (Summing up nodes in a tree) fun {Sum Tree} case Tree of leaf(n) then N [] tree(n Left Right) then N + {Sum Left} + {Sum Right} What if Tree is neither a leaf nor a tree record? What if N is not a number? Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (22/48)

12 Exceptions contd We can check for unusual situations and report them with special values. Example (Summing up nodes in a tree contd) fun {Sum Tree} case Tree of leaf(n) then if {Number.is N} then N else nan(n) 10 [] tree(n Left Right) then L = {Sum Left} R = {Sum Right} in if {Not {Number.is N}} then nan(n) elseif {Not {Number.is L}} then L elseif {Not {Number.is R}} then R else N + L + R else nat(tree) 11 Sum becomes somewhat complicated: we need to test each used value within Sum, and we also need to test the returned value outside of Sum. If we don t, the computation may crash inside or outside of Sum. 10 nan stands for not a number. 11 nat stands for not a tree. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (23/48) Exceptions contd The problem is further complicated when recursive procedures are used. Example (Summing up nodes in a tree contd) Tree = tree(0 tree(1 tree(2 leaf(4) leaf(5)) tree(6 leaf(7) leaf))) {Browse {Sum Tree}} There is a problem: one of the elements of the examined tree has incorrect structure. The nested call which gets leaf as input returns nat(leaf). This value is tested and returned multiple times until the initiall call to Sum finally returns. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (24/48)

13 Exceptions contd What we really want is a way to return the special value immediately, without reinvoking the postponed computations. Example (Summing up nodes in a tree contd) fun {Sum Tree} case Tree of leaf(n) then if {Number.is N} then N else return nan(n) immediately [] tree(n Left Right) then... else return nat(tree) immediately For this to work, we need to have a means for discarding semantic statements (call frames) from the semantic stack (the call stack). Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (25/48) Exceptions contd An early attempt at such non-local exits (backward jumps over multiple frames on the stack) uses continuations. Example (Apping lists in Scheme) (define (multi-app. lists) (call-with-current-continuation (lambda (continuation) (let multi-app ((lists lists)) (cond ((null? lists) ()) ((list? (car lists)) (app (car lists) (multi-app (cdr lists)))) (else (continuation (cons not-a-list (car lists))))))))) (multi-app (1 2 3) (4 5) (6)) ;; => ( ) (multi-app (1 2 3) ops! (6)) ;; => (not-a-list. ops) multi-app can be applied to any number of lists, and returns a concatenation of all the lists. If any of the arguments is not a list, multi-app calls the continuation and exits imemdiately with an error message. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (26/48)

14 Exceptions contd Another low-level approach to handling exceptional situations is to save the state of the execution environment (e.g., the stack and the static memory), and perform an explicit jump to that state (reconstruct the memory content) if something goes wrong. Example (Integer division in C) int divide(int a, int b) { if (0 == b) longjmp(target, 1); // jump if /0 else return a/b; } int main() { if (!setjmp(target)) // jump to here printf("%i/%i = %i\n", 1, 2, divide(1,2)); // fine printf("%i/%i = %i\n", 1, 0, divide(1,0)); // ops, jump! printf("%i/%i = %i\n", 2, 1, divide(2,1)); // never reached else { puts ("cannot divide by zero"); // jump action }... // proceed Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (27/48) Exceptions contd Most modern programming languages provide a convenient abstraction for handling pathological situations exceptions. Special syntactic constructs are used to set handlers for arriving exceptions (e.g., catch, rescue, except); generate exceptions (e.g., throw, raise); perform cleanup actions (e.g., finally, ensure). Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (28/48)

15 Exceptions contd Example (Exceptions) fun {Length List} case List of nil then 0 [] _ Tail then {Length Tail} + 1 else raise nal(list) List = a b c try {Browse {Length List}} catch nal(message) then {Browse "Length -- cannot process a non-list object: "#Message} Length recursively calculates the lenght of a list. If the input is not a list, an exception is thrown. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (29/48) Exceptions contd To implement exceptions, we need to ext the kernel language, both syntactically and sematically. The syntax of exception statements statement ::=... try statement catch id then statement raise id Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (30/48)

16 Exceptions contd Semantics of the try statement The semantic statement is: The execution rule is: (try statement 1 catch id then statement 2, E) 1. Push onto the stack the semantic statement (catch id then statement 2, E) 2. Push onto the stack the semantic statement ( statement 1, E) Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (31/48) Exceptions contd Semantics of the raise statement The semantic statement is: The execution rule is: (raise id, E) 1. If the semantic stack is empty, stop stop and report an uncaught exception. 2. Else pop the first semantic statement from the stack. If it is not a catch statement, go to step The popped statement is (catch id c then statement c, E c ) Push onto the stack the semantic statement ( statement c, E c + { id c E( id ) Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (32/48)

17 Exceptions contd Semantics of the catch statement The semantic statement is: The execution rule is: (catch id then statement, E) Do not do anything. (The catch statement is equivalent to the skip statement.) Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (33/48) Exceptions contd Example (Exceptions, quasi-kernel language) local Exception Message in try Exception = exception(message) Message = message raise Exception catch Exception then case Exception of exception() then {Browse Message} else raise Exception The catch clause pattern-matches the exception raised in the try clause; if it did not match, it would be re-raised. Try to execute the program manually, or use the Oz debugger The Oz debugger does not simulate the abstract machine exactly as specified in CTMCP. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (34/48)

18 Exceptions contd Note: Code enclosed within a try block is not executed as a transaction: as with complex (nested) unification, any bindings made until an exception is raised remain in effect at the time and after the exception is handled. Example (Non-transactional behaviour of try blocks) local A B C in try A = 1 B = 2 raise B C = 3 catch E then {Browse E} {Browse A#B#C} What will be displayed? Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (35/48) Exceptions contd Example (Non-transactional behaviour of try blocks contd) 1. [ (local A in..., {}) ] { } 2. [ (local B in..., {A:v1}) ] { v1 } 3. [ (local C in..., {A:v1, B:v2}) ] { v1, v2 } 4. [ (try... {Browse A#B#C}, {A:v1, B:v2, C:v3}) ] { v1, v2, v3 } 5. [ (try..., {A:v1, B:v2, C:v3}), ({Browse A#B#C}, {A:v1, B:v2, C:v3}) ] { v1, v2, v3 } 6. [ (A=1 B=2 raise B C=3, {A:v1, B:v2, C:v3}), (catch E..., {A:v1, B:v2, C:v3}), ({Browse A#B#C}, {A:v1, B:v2, C:v3}) ] { v1, v2, v3 } Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (36/48)

19 Exceptions contd Example (Non-transactional behaviour of try blocks contd) 7. [ (A=1, {A:v1, B:v2, C:v3}), (B=2 raise B C=3, {A:v1, B:v2, C:v3}), (catch E..., {A:v1, B:v2, C:v3}), ({Browse A#B#C}, {A:v1, B:v2, C:v3}) ] { v1, v2, v3 } 8. [ (B=2 raise B C=3, {A:v1, B:v2, C:v3}), (catch E..., {A:v1, B:v2, C:v3}), ({Browse A#B#C}, {A:v1, B:v2, C:v3}) ] { v1=1, v2, v3 } 9. [ (B=2, {A:v1, B:v2, C:v3}), (raise B C=3, {A:v1, B:v2, C:v3}), (catch E..., {A:v1, B:v2, C:v3}), ({Browse A#B#C}, {A:v1, B:v2, C:v3}) ] { v1=1, v2, v3 } 10. [ (raise B C=3, {A:v1, B:v2, C:v3}), (catch E..., {A:v1, B:v2, C:v3}), ({Browse A#B#C}, {A:v1, B:v2, C:v3}) ] { v1=1, v2=2, v3 } Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (37/48) Exceptions contd Example (Non-transactional behaviour of try blocks contd) 11. [ (raise B, {A:v1, B:v2, C:v3}), (C=3, {A:v1, B:v2, C:v3}), (catch E {Browse E}, {A:v1, B:v2, C:v3}), ({Browse A#B#C}, {A:v1, B:v2, C:v3}) ] { v1=1, v2=2, v3 } 12. [ ({Browse E}, {A:v1, B:v2, C:v3, E:v2}), ({Browse A#B#C}, {A:v1, B:v2, C:v3}) ] { v1=1, v2=2, v3 } displayed in the browser window [ ({Browse A#B#C}, {A:v1, B:v2, C:v3}) ] { v1=1, v2=2, v3 } 14. 1#2#_ displayed in the browser window [ ] { v1=1, v2=2, v3 } Note: The variables v 1 and v 2 remained bound to their values after the raise and catch statements had been executed; v 3 remained unbound. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (38/48)

20 Exceptions contd In the practical language, we can use a syntactic extensions that allow us to use raise with value expressions, not just identifiers; pattern-match exceptions directly in the catch clause; use multiple catch clauses with different patterns. Example (Pattern-matching catch, multiple handlers) try... catch exception(message) then... [] error#[message Cause] then [] AnyOtherException then... raise whatever#you#wish Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (39/48) Exceptions contd Multiple catch clauses with arbitrary patterns are equivalent to a sequence of nested catch and case statements, with a final raise statement if needed. Example try... catch exception(message) then... [] error#[message Cause] then... above: practical language right: kernel language try... catch Ex then case Ex of exception(1:message) then... else case Ex of # (1:Type 2:Desc) then case Type of error() then case Desc of (1:Message 2:Rest) then case Rest of (1:Cause 2:nil) then... else raise Ex else raise Ex else raise Ex else raise Ex Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (40/48)

21 Exceptions contd So what are exceptions? Example (Exceptions in Ada) code/exception.adb with text_io; procedure main is funnybunny : exception; begin begin raise funnybunny; exception when funnybunny => Text_IO.Put_Line("funnybunny caught!"); ; main; In some languages (e.g., Ada), exceptions are values of the built-in type exception. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (41/48) Exceptions contd Example (Exceptions in C++) code/exception.cpp #include <iostream> class FunnyBunny { }; int main() { try { throw FunnyBunny(); } catch (FunnyBunny fb) { std::cout << "funnubunny caught!" << std::l; } return 0; } In some languages (e.g., C++), objects of any class can be thrown as exceptions. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (42/48)

22 Exceptions contd Example (Exceptions in Java) code/exception.java public class exception { } public static void main(string[] args) { try { throw new Throwable() {}; } catch (Throwable t) { System.out.println("funnybunny caught!"); } } In some languages (e.g., Java), exceptions are objects of a class derived from some specific class in the built-in hierarchy (e.g., exting Exception or implementing Throwable). Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (43/48) Exceptions contd Example (Procedures as exceptions in Oz) try raise proc {$ Message} {Browse Message} catch Exception then {Exception funnybunny} In some languages (e.g., Oz), any value can be raised as an exception. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (44/48)

23 Exceptions contd Example (Exceptions in Perl) code/exception.pl #!/usr/bin/perl sub divide { my ($a, $b) my $result; eval { $result = $a/$b; }; die "cannot divide by zero!" if $@; return $result; } print &divide(1,0); In some languages (e.g., Perl), we can execute code within a nested evaluation loop, and test for error conditions to act appropriately. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (45/48) Lecture Outline Memory Management, Bindings, Scope contd Dynamic Scope Recursion, Iteration, and Last Call Optimization Recursion Iteration and Tail Recursion Exceptions Chapter 2 Summary Summary Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (46/48)

24 Chapter 2 Summary 2.1 Defining practical programming language: syntax (EBNF grammars), semantics. 2.2 The single assignment store: defining the abstract machine. 2.3 Kernel language: defining the syntax of the declarative sequential kernel language. 2.4 Kernel language semantics: rules for execution of semantic statements. 2.5 Memory management: tail recursion, garbage collection. 2.6 From kernel language to practical language: syntactic sugar, linguistic abstraction. 2.7 Exceptions: extension of the kernel language. 2.8 Advanced topics: functional languages, unification, typing.... and lecture handouts. Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (47/48) Lecture Outline Memory Management, Bindings, Scope contd Dynamic Scope Recursion, Iteration, and Last Call Optimization Recursion Iteration and Tail Recursion Exceptions Chapter 2 Summary Summary Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming. (48/48)

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011 Lecture 5: Declarative Programming. The Declarative Kernel Language Machine September 12th, 2011 1 Lecture Outline Declarative Programming contd Dataflow Variables contd Expressions and Statements Functions

More information

Lecture 6: The Declarative Kernel Language Machine. September 13th, 2011

Lecture 6: The Declarative Kernel Language Machine. September 13th, 2011 Lecture 6: The Declarative Kernel Language Machine September 13th, 2011 Lecture Outline Computations contd Execution of Non-Freezable Statements on the Abstract Machine The skip Statement The Sequential

More information

Functional Programming. Pure Functional Programming

Functional 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 information

Principles 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 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 information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 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 information

Lecture 4: The Declarative Sequential Kernel Language. September 5th 2011

Lecture 4: The Declarative Sequential Kernel Language. September 5th 2011 Lecture 4: The Declarative Sequential Kernel Language September 5th 2011 1 Lecture Outline Syntactic Analysis of Programs contd Scanning contd The Declarative Kernel Language Introduction Syntax of the

More information

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming Closures Mooly Sagiv Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming Summary 1. Predictive Parsing 2. Large Step Operational Semantics (Natural) 3. Small Step Operational Semantics

More information

Programming Language Concepts, cs2104 Lecture 04 ( )

Programming Language Concepts, cs2104 Lecture 04 ( ) Programming Language Concepts, cs2104 Lecture 04 (2003-08-29) Seif Haridi Department of Computer Science, NUS haridi@comp.nus.edu.sg 2003-09-05 S. Haridi, CS2104, L04 (slides: C. Schulte, S. Haridi) 1

More information

CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

More information

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Notes on Higher Order Programming in Scheme. by Alexander Stepanov by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction - it allows us to implement ADT (abstact data types) in a very special way. The issue of

More information

Run-time Environments - 2

Run-time Environments - 2 Run-time Environments - 2 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Outline of the Lecture n What is run-time

More information

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming Closures Mooly Sagiv Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming t ::= x x. t t t Call-by-value big-step Operational Semantics terms variable v ::= values abstraction x.

More information

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing

More information

Don t write anything in the field marked Eventuell ekstra ID. For each subtask, choose the answer you think is the most correct one.

Don t write anything in the field marked Eventuell ekstra ID. For each subtask, choose the answer you think is the most correct one. Contact persons under the exam: Per Holager, tlf 99 61 78 36 Ole Edsberg tlf 95 28 15 86 Language: English EXAM IN COURSE TDT4165 PROGRAMMING LANGUAGES Saturday May 15th, 2004, 0900 1300 No printed or

More information

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

More information

Chapter 1. Fundamentals of Higher Order Programming

Chapter 1. Fundamentals of Higher Order Programming Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme

More information

Control in Sequential Languages

Control in Sequential Languages CS 242 2012 Control in Sequential Languages Reading: Chapter 8, Sections 8.1 8.3 (only) Section 7.3 of The Haskell 98 Report, Exception Handling in the I/O Monad, http://www.haskell.org/onlinelibrary/io-13.html

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level

More information

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling Course Name: Advanced Java Lecture 5 Topics to be covered Exception Handling Exception HandlingHandlingIntroduction An exception is an abnormal condition that arises in a code sequence at run time A Java

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

More information

Object Oriented Programming Exception Handling

Object Oriented Programming Exception Handling Object Oriented Programming Exception Handling Budditha Hettige Department of Computer Science Programming Errors Types Syntax Errors Logical Errors Runtime Errors Syntax Errors Error in the syntax of

More information

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Tail 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 information

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements Programming Languages Third Edition Chapter 9 Control I Expressions and Statements Objectives Understand expressions Understand conditional statements and guards Understand loops and variation on WHILE

More information

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G. Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

More information

Lecture 21: Relational Programming II. November 15th, 2011

Lecture 21: Relational Programming II. November 15th, 2011 Lecture 21: Relational Programming II November 15th, 2011 Lecture Outline Relational Programming contd The Relational Model of Computation Programming with choice and Solve Search Strategies Relational

More information

Introduction to Scheme

Introduction to Scheme How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

More information

Functional Programming Languages (FPL)

Functional 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 information

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 CS 314, LS, LTM: Functional Programming 1 Scheme A program is an expression to be evaluated (in

More information

Functional Programming. Pure Functional Languages

Functional 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 information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

The role of semantic analysis in a compiler

The 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 information

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Semantic 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 information

Declarative Computation Model

Declarative Computation Model Declarative Computation Model Kernel language semantics revisited (VRH 2.4.5) From kernel to practical language (VRH 2.6) Exceptions (VRH 2.7) Carlos Varela RPI March 19, 2012 Adapted with permission from:

More information

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Semantic 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 information

Functional Programming. Pure Functional Languages

Functional 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 information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0)) SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if

More information

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7 Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

More information

To figure this out we need a more precise understanding of how ML works

To figure this out we need a more precise understanding of how ML works Announcements: What are the following numbers: 74/2/70/17 (2:30,2:30,3:35,7:30) PS2 due Thursday 9/20 11:59PM Guest lecture on Tuesday 9/25 o No RDZ office hours next Friday, I am on travel A brief comment

More information

NOTE: Answer ANY FOUR of the following 6 sections:

NOTE: 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 information

Program Correctness and Efficiency. Chapter 2

Program Correctness and Efficiency. Chapter 2 Program Correctness and Efficiency Chapter 2 Chapter Objectives To understand the differences between the three categories of program errors To understand the effect of an uncaught exception and why you

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 15: Review and Functional Programming Zheng (Eddy) Zhang Rutgers University March 19, 2018 Class Information Midterm exam forum open in Sakai. HW4 and

More information

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments Programming Languages Third Edition Chapter 10 Control II Procedures and Environments Objectives Understand the nature of procedure definition and activation Understand procedure semantics Learn parameter-passing

More information

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1 A Third Look At Java Chapter Seventeen Modern Programming Languages, 2nd ed. 1 A Little Demo public class Test { public static void main(string[] args) { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]);

More information

COP4020 Programming Assignment 1 - Spring 2011

COP4020 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 information

Lambda Calculus. Gunnar Gotshalks LC-1

Lambda Calculus. Gunnar Gotshalks LC-1 Lambda Calculus LC-1 l- Calculus History Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of l-calculus is equivalent in power to

More information

Exceptions and Design

Exceptions and Design Exceptions and Exceptions and Table of contents 1 Error Handling Overview Exceptions RuntimeExceptions 2 Exceptions and Overview Exceptions RuntimeExceptions Exceptions Exceptions and Overview Exceptions

More information

MIDTERM EXAMINATION - CS130 - Spring 2005

MIDTERM 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 information

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals INF4820: Algorithms for Artificial Intelligence and Natural Language Processing Common Lisp Fundamentals Stephan Oepen & Murhaf Fares Language Technology Group (LTG) August 30, 2017 Last Week: What is

More information

CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

More information

Lecture08: Scope and Lexical Address

Lecture08: Scope and Lexical Address Lecture08: Scope and Lexical Address Free and Bound Variables (EOPL 1.3.1) Given an expression E, does a particular variable reference x appear free or bound in that expression? Definition: A variable

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

Programming Languages Third Edition. Chapter 7 Basic Semantics

Programming Languages Third Edition. Chapter 7 Basic Semantics Programming Languages Third Edition Chapter 7 Basic Semantics Objectives Understand attributes, binding, and semantic functions Understand declarations, blocks, and scope Learn how to construct a symbol

More information

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Spring 2015 1 Handling Overloaded Declarations Two approaches are popular: 1. Create a single symbol table

More information

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter 6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

More information

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

More information

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Modern Programming Languages. Lecture LISP Programming Language An Introduction Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation

More information

UNIT 3

UNIT 3 UNIT 3 Presentation Outline Sequence control with expressions Conditional Statements, Loops Exception Handling Subprogram definition and activation Simple and Recursive Subprogram Subprogram Environment

More information

TAIL RECURSION, SCOPE, AND PROJECT 4 11

TAIL RECURSION, SCOPE, AND PROJECT 4 11 TAIL RECURSION, SCOPE, AND PROJECT 4 11 COMPUTER SCIENCE 61A Noveber 12, 2012 1 Tail Recursion Today we will look at Tail Recursion and Tail Call Optimizations in Scheme, and how they relate to iteration

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Java lecture (10.1) Exception Handling 1 Outline Exception Handling Mechanisms Exception handling fundamentals Exception Types Uncaught exceptions Try and catch Multiple catch

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 6a Andrew Tolmach Portland State University 1994-2017 Iteration into Recursion Any iteration can be written as a recursion, e.g. while (c) {e Scala is equivalent

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional 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 information

Organization of Programming Languages CS3200/5200N. Lecture 11

Organization 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 information

Pierce Ch. 3, 8, 11, 15. Type Systems

Pierce Ch. 3, 8, 11, 15. Type Systems Pierce Ch. 3, 8, 11, 15 Type Systems Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using

More information

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige CSC 308 2.0 System Development with Java Exception Handling Department of Statistics and Computer Science 1 2 Errors Errors can be categorized as several ways; Syntax Errors Logical Errors Runtime Errors

More information

Semantic Analysis. Compiler Architecture

Semantic Analysis. Compiler Architecture Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Source Compiler Architecture Front End Scanner (lexical tokens Parser (syntax Parse tree Semantic Analysis

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Design 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 information

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CSE 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 information

SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 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 information

Putting the fun in functional programming

Putting the fun in functional programming CM20167 Topic 4: Map, Lambda, Filter Guy McCusker 1W2.1 Outline 1 Introduction to higher-order functions 2 Map 3 Lambda 4 Filter Guy McCusker (1W2.1 CM20167 Topic 4 2 / 42 Putting the fun in functional

More information

LECTURE 3. Compiler Phases

LECTURE 3. Compiler Phases LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Week 13 - Part 2 Thomas Wies New York University Review Last lecture Scala Outline Today: Exceptions Sources for today s lecture: PLP, ch. 8.5 Exceptions

More information

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong: Exception Handling Run-time errors The exception concept Throwing exceptions Handling exceptions Declaring exceptions Creating your own exception Ariel Shamir 1 Run-time Errors Sometimes when the computer

More information

CS 3 Introduction to Software Engineering. 3: Exceptions

CS 3 Introduction to Software Engineering. 3: Exceptions CS 3 Introduction to Software Engineering 3: Exceptions Questions? 2 Objectives Last Time: Procedural Abstraction This Time: Procedural Abstraction II Focus on Exceptions. Starting Next Time: Data Abstraction

More information

A LISP Interpreter in ML

A 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 information

Recursion & Iteration

Recursion & 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 information

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C School of Electrical and Computer Engineering Cornell University revision: 2018-09-03-15-59 1 Statements, Syntax, Semantics, State

More information

LECTURE 18. Control Flow

LECTURE 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 information

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong: Exception Handling Run-time errors The exception concept Throwing exceptions Handling exceptions Declaring exceptions Creating your own exception 22 November 2007 Ariel Shamir 1 Run-time Errors Sometimes

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 3a Andrew Tolmach Portland State University 1994-2016 Formal Semantics Goal: rigorous and unambiguous definition in terms of a wellunderstood formalism (e.g.

More information

LECTURE 16. Functional Programming

LECTURE 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 information

Lambda Calculus see notes on Lambda Calculus

Lambda Calculus see notes on Lambda Calculus Lambda Calculus see notes on Lambda Calculus Shakil M. Khan adapted from Gunnar Gotshalks recap so far: Lisp data structures basic Lisp programming bound/free variables, scope of variables Lisp symbols,

More information

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1 Review Last week Control Structures Selection Loops 2 Outline Subprograms Calling Sequences Parameter Passing

More information

Syntax Errors; Static Semantics

Syntax Errors; Static Semantics Dealing with Syntax Errors Syntax Errors; Static Semantics Lecture 14 (from notes by R. Bodik) One purpose of the parser is to filter out errors that show up in parsing Later stages should not have to

More information

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Anatomy 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 information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

More information

Functions and Recursion. Dr. Philip Cannata 1

Functions and Recursion. Dr. Philip Cannata 1 Functions and Recursion Dr. Philip Cannata 1 10 High Level Languages This Course Java (Object Oriented) Jython in Java Relation ASP RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata 2 let transformation,

More information

Lambda Calculus LC-1

Lambda Calculus LC-1 Lambda Calculus LC-1 λ- Calculus History-1 Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of λ-calculus is equivalent in power

More information

CS 415 Midterm Exam Fall 2003

CS 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 information

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far Lecture Outline Operational Semantics of Cool Lecture 13 COOL operational semantics Motivation Notation The rules Prof. Aiken CS 143 Lecture 13 1 Prof. Aiken CS 143 Lecture 13 2 Motivation We must specify

More information

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Compiler Theory. (Semantic Analysis and Run-Time Environments) Compiler Theory (Semantic Analysis and Run-Time Environments) 005 Semantic Actions A compiler must do more than recognise whether a sentence belongs to the language of a grammar it must do something useful

More information

Chapter 7 Control I Expressions and Statements

Chapter 7 Control I Expressions and Statements Chapter 7 Control I Expressions and Statements Expressions Conditional Statements and Guards Loops and Variation on WHILE The GOTO Controversy Exception Handling Values and Effects Important Concepts in

More information

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures More Examples Lecture 24: More Scala CSC 131 Fall, 2014 Kim Bruce MyList, MyArrayList, SinglyLinkedList - Val vs var - Can create Array[T] (unlike Java), though need implicit ClassManifest - foreach(f)

More information

About this exam review

About this exam review Final Exam Review About this exam review I ve prepared an outline of the material covered in class May not be totally complete! Exam may ask about things that were covered in class but not in this review

More information

PL Revision overview

PL Revision overview PL Revision overview Course topics Parsing G = (S, P, NT, T); (E)BNF; recursive descent predictive parser (RDPP) Lexical analysis; Syntax and semantic errors; type checking Programming language structure

More information

Concepts Introduced in Chapter 7

Concepts Introduced in Chapter 7 Concepts Introduced in Chapter 7 Storage Allocation Strategies Static Stack Heap Activation Records Access to Nonlocal Names Access links followed by Fig. 7.1 EECS 665 Compiler Construction 1 Activation

More information