Programming Languages 2nd edition Tucker and Noonan

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Formal Semantics of Programming Languages 虞慧群 Topic 5: Axiomatic Semantics.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
Functional Design and Programming Lecture 11: Functional reasoning.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
CS 355 – Programming Languages
1 Languages. 2 A language is a set of strings String: A sequence of letters Examples: “cat”, “dog”, “house”, … Defined over an alphabet: Languages.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
CSE115/ENGR160 Discrete Mathematics 04/03/12 Ming-Hsuan Yang UC Merced 1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
1 Intro to Induction Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.
Fall 2004COMP 3351 Single Final State for NFA. Fall 2004COMP 3352 Any NFA can be converted to an equivalent NFA with a single final state.
1 Languages and Finite Automata or how to talk to machines...
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
CSE115/ENGR160 Discrete Mathematics 03/31/11
CMPT 225 Recursion-part 3. Recursive Searching Linear Search Binary Search Find an element in an array, return its position (index) if found, or -1 if.
Describing Syntax and Semantics
1 Section 3.4 Recursive Definitions. 2 Recursion Recursion is the process of defining an object in terms of itself Technique can be used to define sequences,
Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language.
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
Discrete Mathematics CS 2610 March 26, 2009 Skip: structural induction generalized induction Skip section 4.5.
Lecture 9. Arithmetic and geometric series and mathematical induction
CSE 311 Foundations of Computing I Lecture 16 Recursively Defined Sets and Structural Induction Spring
March 3, 2015Applied Discrete Mathematics Week 5: Mathematical Reasoning 1Arguments Just like a rule of inference, an argument consists of one or more.
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Joseph E. Hollingsworth
Type Safety Kangwon National University 임현승 Programming Languages.
Mathematical Induction Chapter 16 Language, Proof and Logic.
CSE 311 Foundations of Computing I Lecture 17 Structural Induction Spring
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
October 3, 2001CSE 373, Autumn Mathematical Background Exponents X A X B = X A+B X A / X B = X A-B (X A ) B = X AB X N +X N = 2X N 2 N +2 N = 2 N+1.
Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design.
Inductive Proofs and Inductive Definitions Jim Skon.
1 Discrete Mathematical Mathematical Induction ( الاستقراء الرياضي )
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
Functional Programming
11.7 – Proof by Mathematical Induction
Languages.
PROGRAMMING IN HASKELL
CSE 311 Foundations of Computing I
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Discrete Structures for Computer Science
Single Final State for NFA
Discrete Structures for Computer Science
Proving Properties of Recursive List Functions
CSE 311: Foundations of Computing
Programming Languages 2nd edition Tucker and Noonan
CSE15 Discrete Mathematics 04/26/17
This Lecture Substitution model
CSE 311: Foundations of Computing
Lecture 11 CS 1813 – Discrete Mathematics
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
This Lecture Substitution model
CSCE 314: Programming Languages Dr. Dylan Shell
Program correctness Axiomatic semantics
This Lecture Substitution model
Mathematical Induction
Copyright © Cengage Learning. All rights reserved.
Programming Languages 2nd edition Tucker and Noonan
Programming Languages 2nd edition Tucker and Noonan
Representations & Reasoning Systems (RRS) (2.2)
Presentation transcript:

Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming scientifically, it must be possible to specify the required properties of programs precisely. Formality is certainly not an end in itself. The importance of formal specifications must ultimately rest in their utility - in whether or not they are used to improve the quality of software or to reduce the cost of producing and maintaining software. J. Horning

Contents 18.1 Axiomatic Semantics 18.2 Formal Methods Tools: JML 18.3 Correctness of Object-Oriented Programs 18.4 Correctness of Functional Programs 18.4.1 Recursion and Induction 18.4.2 Examples of Structural Induction

18.4 Correctness of Functional Programs Pure functional programs are more accessible to correctness proofs than imperative or OO programs. Three major reasons: Pure functional programs are state-free (no assignment), Functions and variables mathematical ideas, and Recursion aligns well with proof by induction.

18.4.1 Recursion and Induction Consider the Haskell function: > fact n > | n == 1 = 1 -- fact.1 > | n > 1 = n*fact(n-1) -- fact.2 Suppose we want to prove that this function correctly computes the factorial. I.e., that it computes: fact(1) = 1 fact(n) = 12…(n-1)n when n>1

Induction proof of a recursive function The induction proof is straightforward. We use the definition of the function directly in the proof. Basis step: The function computes the correct result for n = 1, using line fact.1 of the definition. Induction step: Assume the hypothesis that the function computes the correct result for some n = k > 1. That is, it computes fact(k) = 12…(k-1)k. Then for n = k+1, it computes fact(k+1) = (k+1)*fact(k) using line fact.2 of the definition. Thus, it computes fact(k+1) = 12…(k- 1)k(k+1), which completes the induction step.

18.4.2 Examples of Structural Induction List concatenation and reversal: > cat [] ys = ys -- cat.1 > cat (x:xs) ys = x : (cat xs ys) -- cat.2 > rev [] = [] -- rev.1 > rev (x:xs) = cat (rev (xs)) [x] -- rev.2 Suppose we want to prove the following property about the relationship between cat and rev: rev (cat xs ys) = cat (rev ys) (rev xs) E.g., rev (cat “hello ” “world”) = cat (rev “world”) (rev “hello ”) = “dlrow olleh”

The Proof Basis step: rev (cat [] ys) = rev (ys) from cat.1 = cat (rev ys []) from rev.2 = cat (rev ys rev [] from rev.1 Induction step: Hypothesis: rev (cat xs ys) = cat (rev ys) (rev xs) rev (cat (x:xs) ys) = rev x : (cat xs ys) from cat.2 = rev (cat (xs ys) [x]) from rev.2 = cat (cat (rev ys) (rev xs)) [x] from hypothesis = cat (cat (rev ys) (rev xs)) [x] cat associativity* = cat (rev ys) (rev (x:xs)) from rev.2 *Note: associativity of cat needs to be proved separately.

List Length and Concatenation > len [] = 0 -- len.1 > len (x:xs) = 1 + (len xs) -- len.2 E.g., len [1,3,4,7] = 1 + len [3,4,7] = 1 + (1 + len [4,7]) = 1 + (1 + (1 + len [7])) = 1 + (1 + (1 + (1 + len []))) = 1 + (1 + (1 + (1 + 0))) = 4 Suppose we want to prove the following property about the relationship between len and cat: len (cat xs ys) = len xs + len ys

The Proof Basis step: len (cat [] ys) = len (ys) from cat.1 = 0 + len (ys) from arithmetic = len [] + len ys from len.1 Induction step: Hypothesis: len (cat xs ys) = len xs + len ys len (cat (x:xs) ys) = len x : (cat xs ys) from cat.2 = 1 + len (cat xs ys) from len.2 = 1 + len xs + len ys from hypothesis = len x:xs + len ys from len.2