CDM Primitive Recursion 2 Properties of PR
Total Page:16
File Type:pdf, Size:1020Kb
1 Primitive Recursive Functions CDM Primitive Recursion 2 Properties of PR 3 Coding Klaus Sutner 4 *Digression: G¨odel’s β Function Carnegie Mellon University Spring 2021 Computability, Definition 2 Knee-Jerk Reaction 3 Computable means: can be done, in principle, by a standard We need a formal definition of computability that digital computer. is easy to understand and apply, and This sounds good to anyone who has ever written and executed a matches our intuitive notion of computability. program; sadly, there are lots of problems with this approach. First, the hedge “in principle” means you really have to abstract away There are many plausible approaches, we’ll start with a model for from a concrete physical device (time, space, mass, energy, . ). arithmetic functions that dates back to the 19th century and is quite straightforward. Additionally, it is more palatable that machine models to Then there is the question which operating system, which programming those used to program. language, which compiler? These typically have no clear semantics, so what exactly are we defining? But Why? 4 Math versus CS 5 Aka computability versus algorithms. Can’t we get away with an informal, wishy-washy definition? Implementation details are usually of little interest in mathematics, it only matters whether a function is computable or not. Computability is a Yes and No, but mostly: No. central foundational issue, but does not require detailed analysis. Informal is typically good enough for positive results: such-and-such CS is a bit different here, computability alone is typically of interest only thing is computable. as a very first step (e.g., when one establishes the decidability of some problem), to be followed by a careful effort to streamline the But for negative results we need real foundations: such-and-such problem computations (so as to keep resource bounds low). fails to be computable, or fails to be computable given particular resources (complexity theory). As we now understand, the latter type of question can be breathtakingly difficult. This second step leads into the realm of algorithms, which should not be confused with computable functions. Arguably, the concept of algorithm is much more complicated and currently only ill-defined notion. We’ll focus on computability. Computable Arithmetic Functions 6 Primitive Recursion 7 The main idea behind our first model is quite straightforward for anyone who has every used a modern programming language: we will define a For the time being we consider only one data type: the natural numbers function f : N Nn N by N. The corresponding functions are called arithmetic functions or number × → theoretic functions: Some examples are familiar to any kindergartener: addition, multiplication, squaring, roots, exponentiation and so on. defining f(0, y) explicitly, and n f : N N → defining f(x + 1, y) in terms of f(x, y). We introduce a model of computation that is designed to work particularly well with these, no input/output coding is required. This should produce computable functions: we can either compute f(n, y), f(n 1, y), f(n 2, y) . top-down (slightly complicated, − − requires a recursion stack), or we can compute bottom-up f(0, y), For the time being, all our functions will be total. f(1, y), f(2, y) . This requires no more than a loop. Later we will see more complicated forms of recursion. Details: Primitive Recursive Functions 8 Composition 9 m n Given functions gi : N N for i = 1, . , n , h : N N , we define a → → new function f : Nm N by composition as follows: → Interestingly, G¨odel encountered the problem of defining computable f(x) = h(g1(x), . , gn(x)) functions working on his seminal incompleteness theorem. He introduced a class of “very simple,” easily describable functions, that are now called primitive recursive functions. Notation: we write Comp[h, g , . , g ] or simply h (g , . , g ) inspired 1 n ◦ 1 n by the the well-known special case m = 1: Recursion is the key idea, but we need a few more ingredients such as composition and projections. (h g)(x) = h(g(x)). ◦ It will always be crystal clear that our functions are intuitively computable. It is clear that computability is closed wrto composition: output can be re-used as input. Digression: Arity 10 Bureaucracy: Projections 11 Unfortunately, composition by itself is not quite enough. When dealing with functions or relations, it is sometimes convenient to be able to express the arity as part of the notation used. Suppose we have a binary version add of addition, and want to define a ternary version. No problem: n (3) (2) (2) We will use a superscript ( ) for this purpose: add (x, y, z) = add (x, add (y, z)) f (n) n a function of arity But, this is not allowed according to our definition of composition; just try. We need a simple auxiliary tool, so-called projections: (n) In particular write ca for the n-ary constant map x a. 7→ n n n P : P (x , . , xn) = xi (0) i N N i 1 We will call ca a hard constant. → where 1 i n for the projections. ≤ ≤ Describing Functions 12 Clones 13 A clone or function algebra is a collection of arithmetic functions that contains all projections and is closed under composition. Now we can write (3) (2) 3 (2) 3 3 More generally, for any set A, define the collection of all finitary add = add (P1 , add (P2 ,P3 )) ◦ ◦ functions over A as F = (An A) A → n 0 [≥ Note that no variables are needed in this notation system. Definition In general, we will prefer the informal notation, but you should know how A clone (over A) is a subset FA that contains all projections and is C ⊆ to use projections to write formally correct terms. closed under composition. For example, all projections form a clone, as do all arithmetic functions. Nullary Functions 14 Nullary Composition 15 Note that we allow hard constants, null-ary functions in A0 A. → m m Recall composition: f (n), g( ), i [n], produces f (g , . , g ) F( ) i ∈ ◦ 1 n ∈ A We will write f() or f( ) when we evaluate such functions. where n, m 0. ∗ ≥ In the literature, you will also find clones without null-ary functions It is worthwhile to consider the special case where f or the gi are nullary. F(+) = (An A) Case: n = 0 C ⊆ A → n>0 (m) [ Then for m 1 we have cf( ) . ≥ ∗ ∈ C Case: m = 0 This is mostly a technical detail, but one should be aware of the issue. (0) Then for n 1 we have ca where a = f(g ( ), . , g ( )). ≥ ∈ C 1 ∗ n ∗ Actually, this is exactly the kind of pesky detail that makes programming quite so difficult. Generating Clones 16 Rectypes 17 To get something more interesting, we need to consider clones that are generated by This is a perfect example of a recursive data type (rectype), one of the fundamental concepts in TCS. We have certain basic functions , and/or F closed under additional operations Op. a collection of atoms (indecomposable items), and a collection of constructors that can be applied to build more We write complicated, decomposable objects. clone( ; Op) F for the least clone containing and closed under Op. Because of this inductive structure we can perform inductive arguments, F both to establish properties and to define operations. For example, clone(; ) consists just of all projections. Basic Arithmetic Functions 18 Closure Operations: Primitive Recursion 19 When dealing with natural numbers, it is natural (duh) to have Given h : Nn+2 N and g : Nn N we define a new function → → f : Nn+1 N by → Constant zero 0 f(0, y) = g(y) Successor function S : N N , S(x) = x + 1 → f(x + 1, y) = h(x, f(x, y), y) (0) Here constant 0 is meant to be the hard constant c0 (but recall the Write Prec[h, g] for this function. comment on nullary composition from above). This is a rather spartan set of built-in functions, but as we will see it’s all we need. Needless to say, these functions are trivially computable. Definition A function is primitive recursive (p.r.) if it lies in the clone generated by In fact, it is hard to give a reasonable description of the natural numbers zero, successor; and closed under primitive recursion: clone(0,S; Prec). without them (unless you are a set theorist). Two Views 20 Example: Factorials 21 The standard definition of the factorial function uses recursion like so: f(0) = 1 f(x + 1) = (x + 1) f(x) bureaucracy basic operator · To write the factorial function in the form f = Prec[h, g] we need atom projections zero, successor - g : N0 N, g() = 1 → constructors composition - prim. rec. 2 h : N N, h(u, v) = (u + 1) v → · g is none other than S 0 and h is multiplication combined with the ◦ successor function: f = Prec[mult (S P 2,P 2),S 0] ◦ ◦ 1 2 ◦ Digging Down 22 And Addition? 23 We have used addition, which can in turn be defined by yet another To get multiplication we use another recursion: recursion. mult(0, y) = 0 add(0, y) = y mult(x + 1, y) = add(mult(x, y), y) add(x + 1, y) = S(add(x, y)) (1) Note that the 0 here technically stands for the unary version c0 to We write x and y for simplicity, we really should use projections. conform with the specs for primitive recursion. Since S is a basic function, we now have a complete, inductive proof that (n) This is not an issue, since we know that our clone contains all ca , n 0.