Termination Analysis Math Foundations of Computer Science.

Slides:



Advertisements
Similar presentations
PROOF BY CONTRADICTION
Advertisements

Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Disjunctive Normal Form CS 680: Formal Methods Jeremy Johnson.
© M. Winter COSC 4P41 – Functional Programming Testing vs Proving Testing –uses a set of “typical” examples, –symbolic testing, –may find errors,
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Basic properties of the integers
Induction and recursion
Recursive Definitions Rosen, 3.4 Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
Induction and recursion
Chapter 4: A Universal Program 1. Coding programs Example : For our programs P we have variables that are arranged in a certain order: Y 1 X 1 Z 1 X 2.
1.3 – AXIOMS FOR THE REAL NUMBERS. Goals  SWBAT apply basic properties of real numbers  SWBAT simplify algebraic expressions.
Propositional Calculus Math Foundations of Computer Science.
Propositional Calculus CS 680: Formal Methods in Verification Computer Systems Jeremy Johnson.
Equational Reasoning Math Foundations of Computer Science.
Deciding a Combination of Theories - Decision Procedure - Changki pswlab Combination of Theories Daniel Kroening, Ofer Strichman Presented by Changki.
The ACL2 Proof Assistant Formal Methods Jeremy Johnson.
A Brief Summary for Exam 1 Subject Topics Propositional Logic (sections 1.1, 1.2) –Propositions Statement, Truth value, Proposition, Propositional symbol,
Reading and Writing Mathematical Proofs
1 Introduction to Abstract Mathematics Chapter 4: Sequences and Mathematical Induction Instructor: Hayk Melikya 4.1- Sequences. 4.2,
Induction Schemes Math Foundations of Computer Science.
Second-Order Functions and Theorems in ACL2 Alessandro Coglio Workshop 2015 Kestrel Institute.
Module #13: Inductive Proofs Rosen 5 th ed., § inference of a generalized conclusion from particular instances 2. mathematical demonstration of the.
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
Copyright © Zeph Grunschlag, Induction Zeph Grunschlag.
Great Theoretical Ideas in Computer Science.
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
CompSci 102 Discrete Math for Computer Science March 1, 2012 Prof. Rodger Slides modified from Rosen.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Lecture 1 Overview Topics 1. Proof techniques: induction, contradiction Proof techniques June 1, 2015 CSCE 355 Foundations of Computation.
Advanced Formal Methods Lecture 4: Isabelle – Types and Terms Mads Dam KTH/CSC Course 2D1453, Some material from Paulson.
Classifications LanguageGrammarAutomaton Regular, right- linear Right-linear, left-linear DFA, NFA Context-free PDA Context- sensitive LBA Recursively.
Daniel Kroening and Ofer Strichman Decision Procedures An Algorithmic Point of View Deciding Combined Theories.
1 Melikyan/DM/Fall09 Discrete Mathematics Ch. 7 Functions Instructor: Hayk Melikyan Today we will review sections 7.3, 7.4 and 7.5.
Mathematical Induction
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.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Chapter 5. 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 we can reach.
Boolean Expression Evaluation CS 270: Math Foundations of CS Jeremy Johnson.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
1 Proving Properties of Recursive Functions and Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Chapter 1 Logic and proofs
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
1 Interactive Computer Theorem Proving CS294-9 October 19, 2006 Adam Chlipala UC Berkeley Lecture 9: Beyond Primitive Recursion.
1 Interactive Computer Theorem Proving CS294-9 September 7, 2006 Adam Chlipala UC Berkeley Lecture 3: Data structures and Induction.
1 Recursive Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Functional Programming in ACL2 Jeremy Johnson Kurt Schmidt Drexel University.
MATH 224 – Discrete Mathematics
CSCE 355 Foundations of Computation
Introduction to Logic for Artificial Intelligence Lecture 2
Propositional Calculus: Boolean Functions and Expressions
Chapter 3 The Real Numbers.
Induction and recursion
Disjunctive Normal Form
Propositional Calculus: Boolean Functions and Expressions
CSCE 355 Foundations of Computation
Proving Properties of Recursive Functions and Data Structures
Proving Properties of Recursive List Functions
Elementary Metamathematics
Induction and recursion
Simplification of Boolean Expressions
Disjunctive Normal Form
MA/CSSE 474 More Math Review Theory of Computation
Computer Security: Art and Science, 2nd Edition
Testing vs Proving Testing uses a set of “typical” examples,
Advanced Analysis of Algorithms
Mathematical Induction
Presentation transcript:

Termination Analysis Math Foundations of Computer Science

Topics  Definitional principle  Soundness and termination  Proving termination and measure functions  Well ordering and natural numbers  Induction and termination  Recursively defined data structures  Undecidability of termination

Definitional Axiom (defunc f (x 1... x n ) :input-contract ic :output-contract oc body)  If the function f is admissible  Add definitional axiom for f: ic  [(f x 1... x n ) = body]  Add contract theorem for f: ic  oc

Definitional Principle (defunc f (x 1... x n ) :input-contract ic :output-contract oc body)  The function f is admissible  f is a new function (no other axioms about f)  x i ’s are distinct  body is a term, possibly using f, but with no free variables other than x i ’s  f is terminating  ic  oc is a theorem  body contracts hold under assumption of ic

Soundness and Global Variables (defunc f (x) :input-contract t :output-contract t y)  The definitional axiom for f leads to unsound logic  Substituting ((x 0) (y nil)) we get (f 0) = nil  Substituting ((x 0) (y t)) we get (f 0) = t  Which implies t = nil.

Soundness and Termination (defunc f(x) :input-contract (natp x) :output-contract (natp (f x)) (+ 1 (f x)))  The definitional axiom for f leads to unsound logic  (natp x)  x  x+1 [property of natural numbers]  (natp (f x))  (f x)  (+ 1 (f x)) [instantiate above]  (natp x)  (f x)  (+ 1 (f x)) [from ic  oc]  (natp x)  (f x) = (+ 1 (f x)) [from def axiom]  (natp x)  nil [from p  p = nil]

How do we Prove Termination  For recursive functions show that the “size” of the inputs get smaller and eventually must hit a base case  Size is defined to be a function to the natural numbers  Use the well ordering principle of the natural numbers to conclude that the number of recursive calls can not be infinite

Well Ordering of Natural Numbers  Any decreasing sequence of natural numbers is finite. I.E. it terminates  This implies that any non-empty set of natural numbers has a minimum element  Induction works as long as we have termination

sum (defunc sum (n) :input-contract (natp n) :output-contract (integerp (sum n)) (if (equal n 0) 0 (+ n (sum (- n 1)))))  The input to the recursive call (- n 1) is smaller than the input to sum and the decreasing sequence of natural numbers n, n-1, n-2,... must terminate (equal 0) after a finite number of steps  If (integerp n) this would not be guaranteed

app (defunc app (a b) :input-contract (and (listp a) (listp b)) :output-contract (and (listp (app a b)) (if (endp a) b (cons (first a) (app (rest a) b))))  This is a terminating function. Define the size of l to be (len l).  (len l) is a natural number  (len (rest l)) < (len l)  Implies len must eventually equal zero, i.e. l = nil

Measure Functions  Technically we measure size with a measure function  A measure function m for the function f  m is an admissible function defined over the parameters of f  m has the same input contract as f  The output contract for m is (natp (m … ))  For every recursive call, m applied to the arguments decreases, under the conditions that led to the recursive call.

Example Measure function (defunc app (a b) :input-contract (and (listp a) (listp b)) :output-contract (and (listp (app a b)) (if (endp a) b (cons (first a) (app (rest a) b)))) (defunc m (x y) :input-contract (and (listp x) (listp y)) :output-contract (natp (m x y)) (len x)) (< (m (rest x) y) (m x y)) since (< (len (rest x)) (len x))

Induction Depends on Termination  Show that the induction scheme for a non- terminating function can lead to unsoundness even when the definitional axiom does not  Alternative proof for the induction principle that shows “termination” front and center

General Induction Scheme (defunc foo (x 1... x n ) :input-contract ic :output-contract oc (cond (t 1 c 1 ) (t 2 c 2 )... (t m c m ) (t c m+1 )))  None of the c i ’s should have ifs in them  If c i has a recursive call to foo, it is called a recursive case otherwise a base case.

General Induction Scheme  Case 1 = t 1  Case 2 = t 2   t 1  …  Case i = t i   t 1     t i-1  …  Case m+1 = t   t 1     t m  If c i is a recursive case with R i calls to foo with the jth call, 1  j  R i, obtained by the substitution (foo x 1... x n )| s ij

General Induction Scheme  To prove  prove the following   ic    [ic  Case i ]    For all c i ’s that are base cases  [ic  Case i   1  i  R i  | s ij ]    For all c i ’s that are recursive cases

Induction Scheme for Non-terminating Function (defunc f (x) :input-contract t :output-contract t (f x))  The definitional axiom, i.e. (f x) = (f x) is ok  The induction scheme for f is unsound  (not t)    nil    t  t          t  Using this scheme we can derive  for any   In particular, we can derive nil

Induction Scheme over Naturals  Every terminating function gives rise to an induction scheme 1.(not (natp n)) ⇒  2.(natp n) ∧ (equal n 0) ⇒  3.(natp n) ∧ (not (equal n 0)) ∧  | ((n n-1)) ⇒   (1) and (2) are base cases and (3) is the induction hypothesis  More powerful than case analysis since you can use assume the induction hypothesis

Proof by Contradiction  Assume the conclusion is false and show that that leads to a contradiction.   1     n     1     n    F  Proof.  A  B  C  A   C   B (show this is valid)  Apply to (  1     n )  T  

Why does Induction Work?  Suppose we prove the three cases in the induction scheme but  is not valid.  Let S be the set of ACL2 objects for which  is false. By (1) and (2), S is a set of natural numbers not equal to 0.  Since S is a set of natural numbers it has a smallest element s  0 for which  | ((n s)).  This implies by (3) that  | ((n s-1)) is false and s-1  S which is a contradiction

Non-terminating Function (defunc f (x) :input-contract t :output-contract f (f x))  The induction scheme associated with f leads to unsoundness (i.e. we can derive nil)

Termination for Recursively Defined Data Structures  For recursively defined data structures like lists, trees, expression trees, etc. we can use the number of constructors for the size  Number of cons’s for lists  Number of nodes for trees  Number of +’s, -’s, *’s and /’s for expression trees

23 Boolean Expressions  BExpr :=  Constant: T|F [t | nil]  Variable [symbol]  Negation:  BExpr [(not BExpr)]  And: BExpr  BExpr [(and BExpr BExpr)  Or: BExpr  Bexpr [(or BExpr BExpr)]

Predicate (defunc booleanexprp (expr) :input-contract t :output-contract (booleanp (booleanexprp expr)) (cond ( (is-constant expr) t ) ( (is-variable expr) t ) ( (is-not expr) (booleanexprp (op1 expr)) ) ( (is-or expr) (and (booleanexprp (op1 expr)) (booleanexprp (op2 expr))) ) ( (is-and expr) (and (booleanexprp (op1 expr)) (booleanexprp (op2 expr))) ) ( t nil) )

Evaluation (defunc bool-eval (expr env) :input-contract (and (booleanexprp expr) (environmentp env) (all-variables-defined expr env)) :output-contract (booleanp (bool-eval expr env)) (cond ( (is-constant expr) expr ) ( (is-variable expr) (lookup expr env) ) ( (is-not expr) (not (bool-eval (op1 expr) env)) ) ( (is-or expr) (or (bool-eval (op1 expr) env) (bool-eval (op2 expr) env)) ) ( (is-and expr) (and (bool-eval (op1 expr) env) (bool-eval (op2 expr) env)) ) )

Measure Function (defun m (expr env) :input-contract (and (booleanexprp expr) (environmentp env) (all-variables-defined expr env)) :output-contract (natp (m expr env)) (cond ( (is-constant expr) 0 ) ( (is-variable expr) 0 ) ( (is-not expr) (+ 1 (m (op expr) env)) ) ( (is-or expr) (+ 1 (m (op1 expr) env) (m (op2 expr) env)) ) ( (is-and expr) (+ 1 (m (op1 expr) env) (m (op2 expr) env)) ) ))

Halting Problem  We can prove that many functions terminate  In general determining if an arbitrary function will terminate is undecidable  What about the following function? (defun 3np1 (n) (cond ((equal n 1) 1 ) ((evenp n) (3np1 (/ n 2)) ) ((oddp n) (3np1 (+ (* 3 n) 1)) ) ))