Functional Design and Programming Lecture 11: Functional reasoning.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Introducing Formal Methods, Module 1, Version 1.1, Oct., Formal Specification and Analytical Verification L 5.
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
© M. Winter COSC 4P41 – Functional Programming Testing vs Proving Testing –uses a set of “typical” examples, –symbolic testing, –may find errors,
Formal Semantics of Programming Languages 虞慧群 Topic 5: Axiomatic Semantics.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Predicate Transformers
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
CS 355 – Programming 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.
CSE115/ENGR160 Discrete Mathematics 04/03/12 Ming-Hsuan Yang UC Merced 1.
VIDE Integrated Environment for Development and Verification of Programs.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
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
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
4/17/2017 Section 3.6 Program Correctness ch3.6.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
Mathematical Induction
Induction and recursion
Induction and recursion
Chapter 6 Mathematical Induction
1 Program Correctness CIS 375 Bruce R. Maxim UM-Dearborn.
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Overview of Formal Methods. Topics Introduction and terminology FM and Software Engineering Applications of FM Propositional and Predicate Logic Program.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Type Safety Kangwon National University 임현승 Programming Languages.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Lecture 4,5 Mathematical Induction and Fibonacci Sequences.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
ARTIFICIAL INTELLIGENCE [INTELLIGENT AGENTS PARADIGM] Professor Janis Grundspenkis Riga Technical University Faculty of Computer Science and Information.
ISBN Chapter 3 Describing Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
13 Aug 2013 Program Verification. Proofs about Programs Why make you study logic? Why make you do proofs? Because we want to prove properties of programs.
Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
Cs7100(Prasad)L18-9WP1 Axiomatic Semantics Predicate Transformers.
1 2/21/2016 MATH 224 – Discrete Mathematics Sequences and Sums A sequence of the form ar 0, ar 1, ar 2, ar 3, ar 4, …, ar n, is called a geometric sequence.
CMSC 330: Organization of Programming Languages Operational Semantics.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
Metalogic Soundness and Completeness. Two Notions of Logical Consequence Validity: If the premises are true, then the conclusion must be true. Provability:
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Formal Verification – Robust and Efficient Code Lecture 1
(Proof By) Induction Recursion
Advanced Algorithms Analysis and Design
CS2210:0001Discrete Structures Induction and Recursion
Chapter 3 The Real Numbers.
Programming Languages 2nd edition Tucker and Noonan
Mathematical Structures for Computer Science Chapter 1
Lecture 5 Floyd-Hoare Style Verification
Programming Languages 2nd edition Tucker and Noonan
Predicate Transformers
Testing vs Proving Testing uses a set of “typical” examples,
Advanced Analysis of Algorithms
Program correctness Axiomatic semantics
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

Functional Design and Programming Lecture 11: Functional reasoning

Literature  Paulson, chapter 6

Exercises  Paulson: , 6.9, 6.12

Overview  Induction principles  Program verification  Transformational programming

Logical Notation: Formulas

Free and Bound Variables  All variable occurrences in the scope of a  or  -binding are bound; e.g. n and m in  m  n. m > n.  All other variable occurrences in a formula are free.

Bound-variable renaming  Bound variables (variable binding plus all associated bound variable occurrences) can be renamed without changing the meaning of the formula.  Example:  m  n. m > n.  q  p. q > p.  m’  n’. m’ > n’.

Precedence of Connectives  The logical connectives bind weaker and weaker in the order given before.

Natural Numbers  Basis: (n = 0) 0 is a natural number.  Rule: (n -> n+1) If n is a natural number, then n+1 is a natural number.  Nothing else is a natural number. 0 basis 1 rule rulerule...

Mathematical Induction  We want to prove  n  N. P(n) by mathematical induction on n.  Basis: (n = 0) Prove P(0).  Induction: (n = m +1) Assume that P(m) holds. (Induction hypothesis) Show that P(m+1) holds.

Mathematical Induction... 0 basis P(0) 1 rule P(1) rulerule... P(3) P(4) P(5) P(6) P(7)... 2 P(2) rule

Example: Factorials fun fact 0 = 1 | fact n = n * fact (n-1) fun facti (0, p) = p | facti (n, p) = facti (n-1, n*p)

Example: Fibonacchi Numbers fun fib 0 = 0 | fib 1 = 1 | fib n = fib (n-1) + fib (n-2) fun itfib (1, prev, curr) = curr | itfib (n, prev, curr) = itfib (n-1, curr, prev + curr)

Complete Induction  We want to prove  n  N. P(n) by complete induction on n.  Induction: Assume that  k < n. P(k) holds. (Induction hypothesis) Show that P(n) holds.

Complete Induction... 0 P(0) 1 P(1) P(3) P(4) P(5) P(6) P(7)... 2 P(2) We may assume that P(0),..., P(n-1) have already been proved when proving P(n). (Consider n=7 above.)

Structural Induction: Lists  We want to prove  l  a list. P(l) by structural induction on l.  Basis: (l = nil) Prove P(nil).  Induction: (l = x :: xs) Assume that P(xs) holds. (Induction hypothesis) Show that P(x :: xs) holds.

List functions fun nlength nil = 0 | nlength (x::xs) = 1 + nlength xs fun ys = ys | (x :: ys = x :: ys) fun nrev nil = nil | nrev (x::xs) = (nrev [x] fun revAppend (nil, ys) = ys | revAppend (x::xs, ys) = revAppend (xs, x::ys)

Structural Induction: Trees  We want to prove  t  a tree. P(t) by structural induction on t.  Basis: (t = Lf) Prove P(Lf).  Induction: (t = Br(x, t1, t2)) Assume that P(t1) and P(t2) hold. (Induction hypotheses) Show that P(Br(x, t1, t2)) holds.

Tree functions fun size Lf = 0 | size (Br (v, t1, t2)) = 1 + size t1 + size t2 fun depth Lf = 0 | depth (Br (v, t1, t2)) = 1 + Int.max (depth t1, depth t2)

Transformational Programming  Goal: Transforming specifications into programs that are guaranteed to be correct by construction.  Method: Apply transformation rules on functional programs (specifications) that are guaranteed to preserve their semantics (correctness).

Transformational Programming: Example fun preorder Lf = nil | preorder (Br (x, t1, t2)) = preorder preorder t2 Idea: Find function preorder’ such that preorder’(t, l) = preorder l for all trees t and lists l.

(Formal) Program Verification  Specification: Precise description of result of program execution.  Verification: Did we build the system right? Proof or test that a program satisfies the given specification.  Validation: Did we build the right system? Test that program has the intended behavior.

(Formal) Program Verification...  Ingredients: Program: The program at hand Specification: What properties should the program satisfy to be considered correct? Assumptions: What properties about the programming language, hardware, context, etc., are assumed to hold?

Input/Output Relations  Many specifications can be expressed as input/output (I/O) predicates together with a domain of inputs for which the predicate must be satisfied.  Example: Predicate: P(t, l) l = preorder t Domain: a tree (all a trees, for arbitrary a)

Total Program Correctness  A functional program f is totally correct wrt. I/O specification (P, X) if: for all x in X, P(x, f(x))  Example: for all t in a tree, preorder’ (t, nil) = preorder t

Partial Program Correctness  A functional program f is partially correct wrt. I/O specification (P, X) if: for all x in X, if f(x) terminates then P(x, f(x)).  Example: for all t in a tree, if undef(t) terminates then undef(t) = preorder t where fun undef (t: a tree) = undef t.

Program Correctness: Sorting  Correctness predicate and domain: sorted(l, l’) ordered(l’) bag(l) = bag(l’) t list, where t is totally ordered by operation bool  Example: forall l in t list, sorted(l, tmergesort l).

Sorting... fun ordered nil = true | ordered [x] = true | ordered (x::y::ys) = x <= y andalso ordered (y::ys) bag(l) = the multiset consisting of all the elements in list l.

Sorting... fun split k nil = (nil, nil) | split 0 l = (nil, l) | split k (x :: xs) = let val (l1, l2) = split (k-1) xs in (x :: l1, l2) end

Sorting... fun merge (nil, ys) = ys | merge (xs, nil) = xs | merge (x::xs, y::ys) = if x <= y then x :: merge(xs, y::ys) else y :: merge(x::xs, ys)

Sorting... fun mergesort nil = nil | mergesort [x] = [x] | mergesort xs = let val k = length xs div 2 val (l1, l2) = split k xs in merge (mergesort l1, mergesort l2) end

Value of Specification  Specification requires making requirements explicit (and thus thinking about the job, before beginning with something or other)  Specifications may pinpoint: ambiguities inconsistencies incompleteness

Limitations of Specification  Not all problems can be specified completely and rigorously (e.g., where specification and validation interact)  Specifications may be counterproductive (e.g., premature or excessive formalization, not human readable and/or with no tool support)  Specification may be wrong or incomplete.

Value of Verification  Verification of simple properties can pinpoint and eliminate very costly errors (e.g. type checking).  Verification provides hard assurances under explicit assumptions.  Verification by construction (e.g., transformational programming) provides concrete design methods.

Limits of Verification  Full verification may be extraordinarily more complex than the programs and their specifications.  Manual verification is tedious and error- prone.

Correctness Driven Program Design  Start with (mathematical) specification.  Refine/transform step-by-step specification to efficient program.  Verify that each step is correct.

Correctness Driven Program Design: Logical basis  Imperative programming: Floyd-Hoare logic Weakest preconditions (Dijkstra)  Functional programming: Lambda calculus Type theory Fold/unfold transformations (Burstall, Darlington)