CS 403: Programming Languages

Slides:



Advertisements
Similar presentations
ISBN Chapter 3 Describing Syntax and Semantics.
Advertisements

CSC212 Data Structure - Section FG Lecture 14 Reasoning about Recursion Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Proving the Correctness of Algorithms Algorithm Design and Analysis Week 2
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Describing Syntax and Semantics
Discrete Structures Chapter 5: Sequences, Mathematical Induction, and Recursion 5.2 Mathematical Induction I [Mathematical induction is] the standard proof.
C OURSE : D ISCRETE STRUCTURE CODE : ICS 252 Lecturer: Shamiel Hashim 1 lecturer:Shamiel Hashim second semester Prepared by: amani Omer.
Introduction to Algorithms Jiafen Liu Sept
Lecture 3.1: Mathematical Induction CS 250, Discrete Structures, Fall 2014 Nitesh Saxena Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag.
Problem 1.b Theorem 1: For all n≥0, the function call fac n evaluates to an answer n!: let rec fac n = if n
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
CSC201 Analysis and Design of Algorithms Asst.Proof.Dr.Surasak Mungsing Oct-151 Lecture 2: Definition of algorithm and Mathematical.
CS 403: Programming Languages Lecture 6 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS 403: Programming Languages Lecture 1 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Mathematical Induction I Lecture 4: Sep 16. This Lecture Last time we have discussed different proof techniques. This time we will focus on probably the.
CS 611: Lecture 6 Rule Induction September 8, 1999 Cornell University Computer Science Department Andrew Myers.
CS 603: Programming Language Organization Lecture 7 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
Reasoning about programs March CSE 403, Winter 2011, Brun.
CS 614: Theory and Construction of Compilers Lecture 7 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion.
Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!
CS 603: Programming Language Organization Lecture 8 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CS 403: Programming Languages Lecture 12 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS 603: Programming Language Organization Lecture 9 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Mathematical Induction I Lecture 5: Sep 20 (chapter of the textbook and chapter of the course notes)
CS 614: Theory and Construction of Compilers Lecture 8 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
CS 603: Programming Language Organization Lecture 6 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
1/29/02CSE460 - MSU1 Nondeterminism-NFA Section 4.1 of Martin Textbook CSE460 – Computability & Formal Language Theory Comp. Science & Engineering Michigan.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
1 Interactive Computer Theorem Proving CS294-9 September 7, 2006 Adam Chlipala UC Berkeley Lecture 3: Data structures and Induction.
Proof And Strategies Chapter 2. Lecturer: Amani Mahajoub Omer Department of Computer Science and Software Engineering Discrete Structures Definition Discrete.
Induction (chapter of the book and chapter of the notes)
Lecture 1.5: Proof Techniques
Proving the Correctness of Algorithms
CS 550 Programming Languages Jeremy Johnson
Dynamic Programming Sequence of decisions. Problem state.
Lecture 1 – Formal Logic.
Lecture 1.5: Proof Techniques
CS 326 Programming Languages, Concepts and Implementation
Reductions.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 2 Sets Slides are adopted from “Discrete.
CS 603: Programming Language Organization
Proving Properties of Recursive List Functions
Propositional Calculus: Boolean Algebra and Simplification
ICS 353: Design and Analysis of Algorithms
Copyright © Cengage Learning. All rights reserved.
Introduction CSE 373 Data Structures.
This Lecture Substitution model
CS 201 Fundamental Structures of Computer Science
ICS 353: Design and Analysis of Algorithms
Easy, Hard, Impossible! A Lecture in CE Freshman Seminar Series:
CS 583 Fall 2006 Analysis of Algorithms
ICS 353: Design and Analysis of Algorithms
Computer Security: Art and Science, 2nd Edition
October 6, 2011 Dr. Itamar Arel College of Engineering
Lecture 3.1: Mathematical Induction
Administrivia- Introduction
This Lecture Substitution model
Copyright © Cengage Learning. All rights reserved.
Administrivia- Introduction
This Lecture Substitution model
Kleene’s Theorem (Part-3)
Lecture 3.1: Mathematical Induction
ICS 353: Design and Analysis of Algorithms
Presentation transcript:

CS 403: Programming Languages Lecture 8 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Announcement Graduate School Preview Day Thursday, September 25 Starts 3PM Ferguson Theater Breakout sessions 4:15–5 Engineering 307 Ferguson Reception 5–5:30 in North Ferguson Lobby More information on flyer Lecture 8 ©2003 Joel Jones

Outline Questions Answers to last time’s exercises Induction and Recursion Reading for next time Lecture 8 ©2003 Joel Jones

Questions Lecture 8 ©2003 Joel Jones

Problems (sumsquares n) (list1 x) (list2 x y) (list3 x y z) (atom? x) (equal l1 l2) – We’ll see this later Lecture 8 ©2003 Joel Jones

Why recursion and induction? Another tool for thinking about problems Easier to reason about recursive programs (!) Certain data structures are naturally recursive Pair Up: • What data structures are naturally recursive? Lecture 8 ©2003 Joel Jones

Induction and Recursion “When programming recursively, think inductively” Terms/Concepts Precondtion or assumption Postcondition or guarantee Base case Recursive or inductive case Lecture 8 ©2003 Joel Jones

Exponentiation Version 1 If n ≥ 0, then exp n evaluates to 2n Pair Up: • What is the precondition? • What is the postcondition? ; p. 227 (217) (define exp (lambda (n) (if (= n 0) 1 (* 2 (exp (- n 1)))))) Lecture 8 ©2003 Joel Jones

Exponentiation Version 1 (cont.) If n ≥ 0, then exp n evaluates to 2n ; p. 227 (217) (define exp (lambda (n) (if (= n 0) 1 (* 2 (exp (- n 1)))))) Does this code satisfy the specification? Pair Up: • Give an inductive proof. (Reminder of inductive proofs: prove the base case, prove the inductive case) Lecture 8 ©2003 Joel Jones

Exponentiation Version 1 (cont.) Running time as a function of n T(0) = O(1) T(n + 1) = O(1) + T(n) So T(n) = O(n) Lecture 8 ©2003 Joel Jones

Exponentiation Version 2 Can we do better? Yes! ; p. 229 (219) (define square (lambda (n) (* n n))) (define double (lambda (n) (+ n n))) (define fast_exp (lambda (n) (if (= n 0) 1 (if (= 0 (mod n 2)) (square (fast_exp (div n 2))) (double (fast_exp (- n 1))))))) Does this code satisfy the specification. Yes, and we can prove it using complete induction, a form of mathematical induction in Which we may prove that n > 0 has a desired property by assuming not only that the predecessor has it, but that all preceeding numbers have it, and arguing that therefore n must have it. Here’s how it is done. For n = 0, the argument is exactly as before. Suppose, then, that n > 0. If n is even, the value of exp n Is the result of squaring the value of exp (n / 2). Inductively this Value is 2^(n/2), so squaring it yields 2^(n/2) * 2^(n/2) = 2^(n/2) = 2^n, as required. If, on the hand, n is odd, the value is the result of doubling exp (n - 1). Inductively the latter value is 2^(n - 1), so Doubling it yields 2^n Lecture 8 ©2003 Joel Jones

Exponentiation Version 2 (cont.) Given T(0) = O(1) T(2n) = O(1) + T(n) T(2n + 1) = O(1) + T(2n) = O(1) + T(n) So T(n) = O(lg n) Lecture 8 ©2003 Joel Jones

But who cares? The general pattern of base case and recursive case carries through to many scheme functions And to handling recursive data structures in other languages (define f (lambda (n) (if (basecase? n) (dobase n) (combine (current n) (f (reduce n))))) Lecture 8 ©2003 Joel Jones

Example of induction on lists (equal l1 l2) Pair Up: • Use the pattern above (or a more complicated version) to write equal on two lists) Lecture 8 ©2003 Joel Jones

Reading & Questions for Next Class Online version of the book “Introduction to ML Programming” chapters 25 and 26, at:http://www-2.cs.cmu.edu/~rwh/smlbook/online.pdf I have placed corresponding Scheme code on web site Lecture 8 ©2003 Joel Jones