Theory of Computer Science. D2.1 Introduction. Theory of Computer Science. D2.2 LOOP Programs. D2.3 Syntactic Sugar. D2.

Similar documents
Theory of Computer Science

Theory of Computer Science. Theory of Computer Science. D4.1 Introduction. D4.2 Basic Functions and Composition. D4.3 Primitive Recursion

Theory of Computer Science

Programs with infinite loops: from primitive recursive predicates to the arithmetic hierarchy

Elementary Recursive Function Theory

Static Program Analysis

This book is licensed under a Creative Commons Attribution 3.0 License

TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function

Lecture 5: The Halting Problem. Michael Beeson

Introduction to the Lambda Calculus

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

(a) Give inductive definitions of the relations M N and M N of single-step and many-step β-reduction between λ-terms M and N. (You may assume the

Lecture 7: Primitive Recursion is Turing Computable. Michael Beeson

Regular Languages (14 points) Solution: Problem 1 (6 points) Minimize the following automaton M. Show that the resulting DFA is minimal.

EDAA40 At home exercises 1

Reflection in the Chomsky Hierarchy

CHAPTER 4. COMPUTABILITY AND DECIDABILITY

The Turing Machine. Unsolvable Problems. Undecidability. The Church-Turing Thesis (1936) Decision Problem. Decision Problems

Material from Recitation 1

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

Programming Languages Third Edition

CDM Other Models. Klaus Sutner Carnegie Mellon Universality. Fall 2017

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Static Program Analysis

Context-Free Grammars

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

CAS 701 Presentation. Ackermann's Function. Qinglei Zhang, Nov. 20, 2008.

CS 275 Automata and Formal Language Theory. First Problem of URMs. (a) Definition of the Turing Machine. III.3 (a) Definition of the Turing Machine

Softwaretechnik. Lecture 03: Types and Type Soundness. Peter Thiemann. University of Freiburg, Germany SS 2008

COMPUTABILITY THEORY AND RECURSIVELY ENUMERABLE SETS

Recursive Functions. Recursive functions are built up from basic functions by some operations.

Rewriting. Andreas Rümpel Faculty of Computer Science Technische Universität Dresden Dresden, Germany.

Presented By : Abhinav Aggarwal CSI-IDD, V th yr Indian Institute of Technology Roorkee. Joint work with: Prof. Padam Kumar

MATH Iris Loeb.

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

Context-Free Grammars

COMP4418 Knowledge Representation and Reasoning

Course notes for Data Compression - 2 Kolmogorov complexity Fall 2005

Context-Free Grammars. Carl Pollard Ohio State University. Linguistics 680 Formal Foundations Tuesday, November 10, 2009

Finite Model Generation for Isabelle/HOL Using a SAT Solver

Elementary Recursive Function Theory

6.045J/18.400J: Automata, Computability and Complexity. Practice Quiz 2

Computability Theory

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

Computer Sciences Department

English for Computer Science

Computation Engineering Applied Automata Theory and Logic. Ganesh Gopalakrishnan University of Utah. ^J Springer

CSC-461 Exam #2 April 16, 2014

CS 242. Fundamentals. Reading: See last slide

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

A Characterization of the Chomsky Hierarchy by String Turing Machines

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Handout 9: Imperative Programs and State

Lecture T4: Computability

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Appendix Set Notation and Concepts

The Eval/Apply Cycle Eval. Evaluation and universal machines. Examining the role of Eval. Eval from perspective of language designer

Notes on Turing s Theorem and Computability

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

Recursively Enumerable Languages, Turing Machines, and Decidability

We ve studied the main models and concepts of the theory of computation:

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics

CSCC24 Functional Programming Scheme Part 2

Diagonalization. The cardinality of a finite set is easy to grasp: {1,3,4} = 3. But what about infinite sets?

ON THE GRAMMAR OF FIRST-ORDER LOGIC

Lecture 5: The Untyped λ-calculus

Lambda Calculus and Computation

if s has property P and c char, then c s has property P.

Linguistics and Philosophy 23: , Is Compositionality Formally Vacuous? Francis Jeffry Pelletier

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

CITS3211 FUNCTIONAL PROGRAMMING

Denotational Semantics. Domain Theory

Safe Stratified Datalog With Integer Order Does not Have Syntax

CSE 505: Concepts of Programming Languages

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

EXTENSIONS OF FIRST ORDER LOGIC

Semantics. A. Demers Jan This material is primarily from Ch. 2 of the text. We present an imperative

Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and

An Industrially Useful Prover

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Overview. CS389L: Automated Logical Reasoning. Lecture 6: First Order Logic Syntax and Semantics. Constants in First-Order Logic.

Static Program Analysis Part 1 the TIP language

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Inductive Types for Free

Formal Predicate Calculus. Michael Meyling

Introduction to Denotational Semantics. Class Likes/Dislikes Survey. Dueling Semantics. Denotational Semantics Learning Goals. You re On Jeopardy!

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Untyped Lambda Calculus

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

V1.0: Seth Gilbert, V1.1: Steven Halim August 30, Abstract. d(e), and we assume that the distance function is non-negative (i.e., d(x, y) 0).

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

Second-Order Type Systems

Definition: A context-free grammar (CFG) is a 4- tuple. variables = nonterminals, terminals, rules = productions,,

Warm-Up Problem. Let L be the language consisting of as constant symbols, as a function symbol and as a predicate symbol. Give an interpretation where

Untyped Lambda Calculus

Algorithms Exam TIN093/DIT600

Programming Language Concepts, cs2104 Lecture 04 ( )

Transcription:

Theory of Computer Science April 20, 2016 D2. LOOP- and WHILE-Computability Theory of Computer Science D2. LOOP- and WHILE-Computability Malte Helmert University of Basel April 20, 2016 D2.1 Introduction D2.2 LOOP Programs D2.3 Syntactic Sugar D2.4 WHILE Programs D2.5 Digression: the Ackermann Function D2.6 Summary M. Helmert (Univ. Basel) Theorie April 20, 2016 1 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 2 / 45 Overview: Computability Theory Further Reading (German) Computability Theory imperative models of computation: D1. Turing-Computability D2. LOOP- and WHILE-Computability D3. GOTO-Computability functional models of computation: D4. Primitive Recursion and µ-recursion D5. Primitive/µ-Recursion vs. LOOP-/WHILE-Computability undecidable problems: D6. Decidability and Semi-Decidability D7. Halting Problem and Reductions D8. Rice s Theorem and Other Undecidable Problems Post s Correspondence Problem Undecidable Grammar Problems Gödel s Theorem and Diophantine Equations Literature for this Chapter (German) Theoretische Informatik kurz gefasst by Uwe Schöning (5th edition) Chapter 2.3 Chapter 2.5 M. Helmert (Univ. Basel) Theorie April 20, 2016 3 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 4 / 45

Further Reading (English) D2. LOOP- and WHILE-Computability Introduction Literature for this Chapter (English) Introduction to the Theory of Computation by Michael Sipser (3rd edition) This topic is not discussed by Sipser! D2.1 Introduction M. Helmert (Univ. Basel) Theorie April 20, 2016 5 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 6 / 45 D2. LOOP- and WHILE-Computability Introduction Formal Models of Computation: LOOP/WHILE/GOTO D2. LOOP- and WHILE-Computability Introduction LOOP, WHILE and GOTO Programs: Basic Concepts Formal Models of Computation Turing machines LOOP, WHILE and GOTO programs primitive recursive and µ-recursive functions In this and the following chapter we get to know three simple models of computation (programming languages) and compare their power to Turing machines: LOOP programs today WHILE programs today GOTO programs next chapter LOOP, WHILE and GOTO programs are structured like programs in (simple) traditional programming languages use finitely many variables from the set {x 0, x 1, x 2,... } that can take on values in N 0 differ from each other in the allowed statements M. Helmert (Univ. Basel) Theorie April 20, 2016 7 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 8 / 45

D2. LOOP- and WHILE-Computability LOOP Programs D2. LOOP- and WHILE-Computability LOOP Programs LOOP Programs: Syntax Definition (LOOP Program) LOOP programs are inductively defined as follows: D2.2 LOOP Programs x i := x j + c is a LOOP program for every i, j, c N 0 (addition) x i := x j c is a LOOP program for every i, j, c N 0 (modified subtraction) If P 1 and P 2 are LOOP programs, then so is P 1 ;P 2 (composition) If P is a LOOP program, then so is LOOP x i DO P for every i N 0 (LOOP loop) German: LOOP-Programm, Addition, modifizierte Subtraktion, Komposition, LOOP-Schleife M. Helmert (Univ. Basel) Theorie April 20, 2016 9 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 10 / 45 D2. LOOP- and WHILE-Computability LOOP Programs LOOP Programs: Semantics D2. LOOP- and WHILE-Computability LOOP Programs LOOP Programs: Semantics Definition (Semantics of LOOP Programs) A LOOP program computes a k-ary function f : N k 0 N 0. The computation of f (n 1,..., n k ) works as follows: 1 Initially, the variables x 1,..., x k hold the values n 1,..., n k. All other variables hold the value 0. 2 During computation, the program modifies the variables as described on the following slides. 3 The result of the computation (f (n 1,..., n k )) is the value of x 0 after the execution of the program. German: P berechnet f Definition (Semantics of LOOP Programs) effect of x i := x j + c: The variable x i is assigned the current value of x j plus c. All other variables retain their value. German: P berechnet f M. Helmert (Univ. Basel) Theorie April 20, 2016 11 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 12 / 45

D2. LOOP- and WHILE-Computability LOOP Programs LOOP Programs: Semantics D2. LOOP- and WHILE-Computability LOOP Programs LOOP Programs: Semantics Definition (Semantics of LOOP Programs) effect of x i := x j c: The variable x i is assigned the current value of x j minus c if this value is non-negative. Otherwise x i is assigned the value 0. All other variables retain their value. Definition (Semantics of LOOP Programs) effect of P 1 ; P 2 : First, execute P 1. Then, execute P 2 (on the modified variable values). German: P berechnet f German: P berechnet f M. Helmert (Univ. Basel) Theorie April 20, 2016 13 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 14 / 45 D2. LOOP- and WHILE-Computability LOOP Programs LOOP Programs: Semantics D2. LOOP- and WHILE-Computability LOOP Programs LOOP-Computable Functions Definition (Semantics of LOOP Programs) effect of LOOP x i DO P : Let m be the value of variable x i at the start of execution. The program P is executed m times in sequence. Definition (LOOP-Computable) A function f : N k 0 p N 0 is called LOOP-computable if a LOOP program that computes f exists. German: f ist LOOP-berechenbar Note: non-total functions are never LOOP-computable. (Why not?) German: P berechnet f M. Helmert (Univ. Basel) Theorie April 20, 2016 15 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 16 / 45

D2. LOOP- and WHILE-Computability LOOP Programs LOOP Programs: Example Example (LOOP program for f (x 1, x 2 )) LOOP x 1 DO LOOP x 2 DO x 0 := x 0 + 1 D2.3 Syntactic Sugar Which (binary) function does this program compute? M. Helmert (Univ. Basel) Theorie April 20, 2016 17 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 18 / 45 Syntactic Sugar or Essential Feature? Example: Syntactic Sugar We investigate the power of programming languages and other computation formalisms. Rich language features help when writing complex programs. Minimalistic formalisms are useful for proving statements over all programs. conflict of interest! Idea: Use minimalistic core for proofs. Use syntactic sugar when writing programs. Example (syntactic sugar) We propose five new syntax constructs (with the obvious semantics): x i := x j for i, j N 0 x i := c for i, c N 0 x i := x j + x k for i, j, k N 0 IF x i 0 THEN P for i N 0 IF x i = c THEN P for i, c N 0 Can we simulate these with the existing constructs? German: syntaktischer Zucker M. Helmert (Univ. Basel) Theorie April 20, 2016 19 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 20 / 45

Example: Syntactic Sugar Example: Syntactic Sugar Example (syntactic sugar) x i := x j for i, j N 0 Simple abbreviation for x i := x j + 0. Example (syntactic sugar) x i := c for i, c N 0 Simple abbreviation for x i := x j + c, where x j is a fresh variable, i.e., an otherwise unused variable that is not an input variable. (Thus x j must always have the value 0 in all executions.) M. Helmert (Univ. Basel) Theorie April 20, 2016 21 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 22 / 45 Example: Syntactic Sugar Example: Syntactic Sugar Example (syntactic sugar) x i := x j + x k for i, j, k N 0 Abbreviation for: x i := x j ; LOOP x k DO x i := x i + 1 Analogously we will also use the following: x i := x j x k x i := x j + x k c x m + d etc. Example (syntactic sugar) IF x i 0 THEN P for i N 0 Abbreviation for: x j := 0; LOOP x i DO x j := 1 ; LOOP x j DO P where x j is a fresh variable. M. Helmert (Univ. Basel) Theorie April 20, 2016 23 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 24 / 45

Example: Syntactic Sugar Can We Be More Minimalistic? Example (syntactic sugar) IF x i = c THEN P for i, c N 0 Abbreviation for: x j := 1; x k := x i c; IF x k 0 THEN x j := 0 ; x k := c x i ; IF x k 0 THEN x j := 0 ; IF x j 0 THEN P where x j and x k are fresh variables. We see that some common structural elements such as IF statements are unnecessary because they are syntactic sugar. Can we make LOOP programs even more minimalistic than in our definition? Simplification 1 Instead of x i := x j + c and x i := x j c it suffices to only allow the constructs x i := x j, x i := x i + 1 and x i := x i 1. Why? M. Helmert (Univ. Basel) Theorie April 20, 2016 25 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 26 / 45 Can We Be More Minimalistic? D2. LOOP- and WHILE-Computability WHILE Programs We see that some common structural elements such as IF statements are unnecessary because they are syntactic sugar. Can we make LOOP programs even more minimalistic than in our definition? Simplification 2 The construct x i := x j can be omitted because it can be simulated with other constructs: LOOP x i DO x i := x i 1 ; LOOP x j DO x i := x i + 1 D2.4 WHILE Programs M. Helmert (Univ. Basel) Theorie April 20, 2016 27 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 28 / 45

D2. LOOP- and WHILE-Computability WHILE Programs WHILE Programs: Syntax D2. LOOP- and WHILE-Computability WHILE Programs WHILE Programs: Semantics Definition (WHILE Program) WHILE programs are inductively defined as follows: x i := x j + c is a WHILE program for every i, j, c N 0 (addition) x i := x j c is a WHILE program for every i, j, c N 0 (modified subtraction) If P 1 and P 2 are WHILE programs, then so is P 1 ;P 2 (composition) If P is a WHILE program, then so is WHILE x i 0 DO P for every i N 0 (WHILE loop) Definition (Semantics of WHILE Programs) The semantics of WHILE programs is defined exactly as for LOOP programs. effect of WHILE x i 0 DO P : If x i holds the value 0, program execution finishes. Otherwise execute P. Repeat these steps until execution finishes (potentially infinitely often). German: WHILE-Programm, WHILE-Schleife M. Helmert (Univ. Basel) Theorie April 20, 2016 29 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 30 / 45 D2. LOOP- and WHILE-Computability WHILE Programs WHILE-Computable Functions D2. LOOP- and WHILE-Computability WHILE Programs WHILE-Computability vs. LOOP-Computability Definition (WHILE-Computable) A function f : N k 0 p N 0 is called WHILE-computable if a WHILE program that computes f exists. German: f ist WHILE-berechenbar Theorem Every LOOP-computable function is WHILE-computable. The converse is not true. WHILE programs are therefore strictly more powerful than LOOP programs. German: echt mächtiger M. Helmert (Univ. Basel) Theorie April 20, 2016 31 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 32 / 45

D2. LOOP- and WHILE-Computability WHILE Programs WHILE-Computability vs. LOOP-Computability D2. LOOP- and WHILE-Computability WHILE Programs WHILE-Computability vs. LOOP-Computability Proof. Part 1: Every LOOP-computable function is WHILE-computable. Given any LOOP program, we construct an equivalent WHILE program, i. e., one computing the same function. To do so, replace each occurrence of LOOP x i DO P with x j := x i ; WHILE x j 0 DO x j := x j 1; P where x j is a fresh variable.... Proof (continued). Part 2: Not all WHILE-computable functions are LOOP-computable. The WHILE program x 1 := 1; WHILE x 1 0 DO x 1 := 1 computes the function Ω : N 0 p N 0 that is undefined everywhere. Ω is hence WHILE-computable, but not LOOP-computable (because LOOP-computable functions are always total). M. Helmert (Univ. Basel) Theorie April 20, 2016 33 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 34 / 45 D2. LOOP- and WHILE-Computability Digression: the Ackermann Function D2. LOOP- and WHILE-Computability Digression: the Ackermann Function LOOP vs. WHILE: Is There a Practical Difference? D2.5 Digression: the Ackermann Function We have shown that WHILE programs are strictly more powerful than LOOP programs. The example we used is not very relevant in practice because our argument only relied on the fact that LOOP-computable functions are always total. To terminate for every input is not much of a problem in practice. (Quite the opposite.) Are there any total functions that are WHILE-computable, but not LOOP-computable? M. Helmert (Univ. Basel) Theorie April 20, 2016 35 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 36 / 45

D2. LOOP- and WHILE-Computability Digression: the Ackermann Function Ackermann Function: History D2. LOOP- and WHILE-Computability Digression: the Ackermann Function Ackermann Function David Hilbert conjectured that all computable total functions are primitive recursive (1926). We will see what this means in Chapter D4. Wilhelm Ackermann refuted the conjecture by supplying a counterexample (1928). The counterexample was simplified by Rózsa Péter (1935). here: simplified version Definition (Ackermann function) The Ackermann function a : N 2 0 N 0 is defined as follows: a(0, y) = y + 1 for all y 0 a(x, 0) = a(x 1, 1) for all x > 0 a(x, y) = a(x 1, a(x, y 1)) for all x, y > 0 German: Ackermannfunktion Note: the recursion in the definition is bounded, so this defines a total function. (Why?) M. Helmert (Univ. Basel) Theorie April 20, 2016 37 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 38 / 45 D2. LOOP- and WHILE-Computability Digression: the Ackermann Function Table of Values D2. LOOP- and WHILE-Computability Digression: the Ackermann Function Computability of the Ackermann Function y = 0 y = 1 y = 2 y = 3 y = k a(0, y) 1 2 3 4 k + 1 a(1, y) 2 3 4 5 k + 2 a(2, y) 3 5 7 9 2k + 3 a(3, y) 5 13 29 61 2 k+3 3 Theorem The Ackermann function is WHILE-computable, but not LOOP-computable. (Without proof.) a(4, y) 13 65533 2 65536 2 2 3 2 265536 3 2 }{{} k+3 3 M. Helmert (Univ. Basel) Theorie April 20, 2016 39 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 40 / 45

D2. LOOP- and WHILE-Computability Digression: the Ackermann Function Computability of the Ackermann Function: Proof Idea D2. LOOP- and WHILE-Computability Summary proof idea: WHILE-computability: show how WHILE programs can simulate a stack (essentially: push/pop with encode/decode from Chapter D4) dual recursion by using a stack WHILE program is easy to specify no LOOP-computability: show that there is a number k for every LOOP program such that the computed function value is smaller than a(k, n), if n is the largest input value proof by structural induction; use k = program length Ackermann function grows faster than every LOOP-computable function D2.6 Summary M. Helmert (Univ. Basel) Theorie April 20, 2016 41 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 42 / 45 D2. LOOP- and WHILE-Computability Summary Summary: LOOP and WHILE Programs D2. LOOP- and WHILE-Computability Summary Summary: Comparing Models of Computation two new models of computation for numerical functions: LOOP programs and WHILE programs closer to typical programming languages than Turing machines general approach to compare power of formalisms: How can features be used to simulate other features (cf. syntactic sugar, minimalistic formalisms)? How can one formalism simulate the other formalism? M. Helmert (Univ. Basel) Theorie April 20, 2016 43 / 45 M. Helmert (Univ. Basel) Theorie April 20, 2016 44 / 45

D2. LOOP- and WHILE-Computability Summary Power of LOOP vs. WHILE We now know: WHILE programs are strictly more powerful than LOOP programs. WHILE-, but not LOOP-computable functions: simple example: function that is undefined everywhere more interesting example (total function): Ackermann function, which grows too fast to be LOOP-computable How do LOOP and WHILE programs relate to Turing machines? next chapter M. Helmert (Univ. Basel) Theorie April 20, 2016 45 / 45