Definable Functions in the Simply Typed λ-Calculus Mateusz Zakrzewski Instituteof Informatics, 7 Warsaw University, 0 Banacha 2, 02-097 Warsaw, Poland. 0 [email protected] 2 n a Abstract. It is a common knowledge that the integer functions defin- J able in the simply typed λ-calculus are exactly the extended polynomi- 4 als. This is indeed the case when one interprets integers over the type (p → p) → p → p where p is a base type and/or equality is taken as ] O β-conversion. It is commonly believed that the same holds for βη-equality and for integers represented overany fixed typeof theform L (τ →τ)→τ →τ. . s Inthispaperweshowthat thisopinion isnotquitetrue.Weprovethat c theclass of functions strictly definablein thesimply typedλ-calculusis [ considerablylargerthantheextendedpolynomials.Next,weinvestigate 1 which functionsbelong to that class. v 2 2 1 Introduction 0 1 We assume that all types are constructed from only one base type, which we 0 7 denote by o. Natural numbers are represented as Church numerals: 0=λfx.x, 0 1=λfx.fx, 2=λfx.f(fx), 3=λfx.f(f(fx)), ...Everytype that can be assigned / to all Church numerals is of the form (τ →τ)→τ →τ, for some τ. s c We say that a function f over a free algebra A, of arity k, is definable v: (or representable) in the untyped λ-calculus, if there exists a term E, such that Xi Eρ(a1)ρ(a2)...ρ(ak)is equal(up to β- or βη-conversion)to ρ(f(a1,a2,...,ak)) for all a ,a ,...,a ∈A. By ρ(a) we mean the term which represents a (as de- 1 2 k r fined in [4]) e.g. if A = N and a ∈ A then ρ(A) is a Church numeral. In typed a λ-calculi,thereareadditionalrequirementsthatmustbemetbyadefinablefunc- tion. We say that a function is non-uniformly definable, if Eρ(a )ρ(a )...ρ(a ) 1 2 k can be typed for all a ,a ,...,a . A function is skewly definable, if there are 1 2 k types τ ,τ ,...,τ , such that Eρ(a )ρ(a )...ρ(a ) can be typed for all a ,a , 1 2 k 1 2 k 1 2 ...,a , with type τ assigned to ρ(a ) (i = 1...k). If τ are all the same and k i i i equal to the type of Eρ(a )ρ(a )...ρ(a ), we say that E represents f strictly 1 2 k (or f is strictly definable). In the simply typed λ-calculus, functions over natural numbers that are strictlydefinablewithτ beingabasetype(oratypevariable)havebeencharac- terizedby Schwichtenberg(see [1]) and areknownas the extendedpolynomials. The resultof Schwichtenberghas been generalizedto arbitraryfree algebras[5]. Leivanthas shownthat, insimple types,everynon-uniformly definable function (over any algebra) is skewly definable. The exact characterizationof skewly de- finable functions is not known. It has been shown (see [4]) that when equality is taken as β-conversion, every function that is strictly definable is strictly de- finable with τ being a base type. It was also claimed in [4] that the same holds whenequalityistakenasβη-conversion.Surprisingly,asweshowbelow,itisnot trueevenforfunctions overnaturalnumbers.Inotherwords,thereexiststrictly definable functions that are not extended polynomials. This paper aims to investigate which functions are strictly definable over natural numbers, with arbitrary τ and βη-conversion. We define F as the class ofstrictlydefinablefunctionsandGasaclassthatcontainsextendedpolynomials and two additional functions (or, more precisely, two function schemas) and is closed under composition. We prove that for every function f ∈ G, there exists a type τ such that f is definable with numerals of type (τ → τ) → τ → τ. It follows that G is a subset of F. The proof yields an effective procedure that can be used to find τ and a term which represents f. We conjecture that G exactly characterizes strictly definable functions, i.e. G = F, and we gather some evidence for this conjecture proving, for example, thateveryskewlyrepresentablefiniterangefunctionisstrictlyrepresentableover (τ →τ) →τ →τ, for some τ. We also prove that all definable functions of the form: f(m,n ,n ) = if m ∈ A then m else m (for all A ⊆ N) are in G. 1 2 1 2 Finally, we give examples of very simple functions that are not definable. 1.1 Extended Polynomials Definition 1. The class of extended polynomials is the smallest class of func- tions over N which contains: 1. the constant functions: 0 and 1, 2. projections, 3. addition, 4. multiplication, 5. the function ifzero(n,m,p)=if n=0 then m else p, and is closed under composition. Addition, multiplication and ifzero can be represented by the terms: λnm.λfx.nf(mfx), λnm.λfx.n(mf)xandλnmp.λfx.n(λy.pfx)(mfx), respec- tively. For allτ, every k-aryextended polynomialcanbe strictly representedby a term of type ωk →ω where ω =(τ →τ)→τ →τ. τ τ τ 1.2 Beyond Extended Polynomials Consider the following term: E =λn.λfx. λa a ...a . 1 2 l (n(λy.λz z ...z .yz z ...z z )(λz z ...z .z )) 1 2 l 2 3 l 1 1 2 l 1 (x a a ...a ) 1 2 l (fx a a ...a ) 1 2 l (f(fx) a a ...a ) 1 2 l ... ((fl−1x) a a ...a ). 1 2 l If n is assigned a numeral N, the term (n(λy.λz z ...z .yz z ...z z )(λz z ...z .z )) 1 2 l 2 3 l 1 1 2 l 1 will evaluate (or reduce) to (λz z ...z .z ), where i is equal to (N modl)+1. 1 2 l i Therefore,E strictlyrepresentsthefunctiong(n)=nmodlandcanbeassigned the type ω →ω , where τ must be of the form αl →α, for some α. τ τ Proposition 2. For every extended polynomial f : N → N, there exists an m, such that for all n ,n ≥m, if n ≥n then f(n )≥f(n ). 1 2 1 2 1 2 Proof. Simple induction with respect to the structure of a polynomial. Proposition 2 states that every extended polynomial is non-decreasing for sufficiently large arguments. Clearly, it is not the case for g. It follows that g is not an extended polynomial. It is worth noting that for this example to work, weneedbothβη-conversionandnumeralsoftypeω withτ beingdifferentfrom τ the base type. 2 Strictly Definable Functions In this section, we define a class of functions and prove that all functions which are in that class are (strictly) definable. Definition 3. G is the smallestclass of functions which is closedunder compo- sition and contains: 1. extended polynomials, 2. the function fl(m,n ,n ,...,n )=n , where i=(mmodl)+1, 1 1 2 l i (for all l ≥2), 3. the function fl(m,n ,n )=if m≤l then n else n , (for all l≥1). 2 1 2 1 2 By F we denote the class of all functions that are strictly definable. Nota- tion (s)hM ,M ,...,M i means a tuple of length s, represented by the term 1 2 s λp.pM M ...M . By Π (s)(p), we denote the i-th element of the tuple p of 1 2 s i length s, represented by p(λx x ...x .x ). The function fl can be represented 1 2 s i 2 by the term: E =λmn n .λfx.λa a a .ifzero Gm then n fxa a a else n fxa a a , 1 2 1 2 3 1 1 2 3 2 1 2 3 where: ifzero x then y else z =x(λt.z)y, G=λm.Π (l+1)(mFP), 1 F =λp.hΠ (l+1)(p),Π (l+1)(p),...,Π (l+1)(p),succ Π (l+1)(p)i, 2 3 l+1 l+1 P =(l+1)h0,0,...,0i, succ=λn.λfx.f(nfx). The term G skewly represents the function g(m) = m−l. Therefore ifzero Gm then x else y returns the same value as if m≤l then x else y. Here is how E can be typed: ⊢E :ω3 →ω , τ τ τ =(αl+1 →α)→α, α=ωτ′. The type τ is the type of an (l+1)-tuple of numerals (hence α = ωτ′, for some τ′). To prove that all functions which are in G are definable, we will need more flexibility as to what types can be assigned to the terms which represent them. For example, if (for any s≥l+1) the function g is represented like this: G=λn.Π (s)(nFP), s−l F =λp.(s)hΠ (s)(p),Π (s)(p),...,Π (s)(p),succ Π (s)(p)i, 2 3 s s P =(s)h0,0,...,0i. then E can be assigned the type ω3 → ω with τ being the type of a tuple of τ τ length s. A flexible representationof fl is as follows: 1 E =λm.λn n ...n .λfx.λa.Π (s)(mFP), 1 2 l l where: F =λp.(s)hΠ (s)(p),Π (s)(p),...,Π(s)(p),Π (s)(p),Π (s)(p) 2 3 l 1 l+1 Π (s)(p),...,Π (s)(p)i, l+2 s P =(s)hM ,M ,...,M ,M ,X,X,...,Xi, 2 3 l 1 X =xa, ∀ M =n fxa. 1≤i≤l i i The term E, when applied to arguments, evaluates similarly to the term which represents the remainder modulo l (as given in the introduction). The main difference is that we can now use tuples of arbitrary length s≥l. We can assign the type ωl+1 →ω (with τ =(αs →α)→α) to E. τ τ It is easy to see that G can be equivalently defined as follows: Definition 4. G is the smallestclass of functions which is closedunder compo- sition and contains: 1. the constant functions: 0 and 1, 2. projections, 3. addition, 4. multiplication, 5. the function ifzero(n,m,p)=if n=0 then m else p, 6. the function fl(m,n ,n ,...,n ) = n , where i = (mmodl)+1, (for all 1 1 2 l i l ≥2), 7. the function fl(m,n ,n )=if m≤l then n else n , (for all l≥1). 2 1 2 1 2 Definition 5. G is the smallest class of functions such that for all k: 1. if ∀nf(n)=0 then f ∈G, 2. if ∀nf(n)=1 then f ∈G, 3. ∀ if ∀nf(n)=n then f ∈G, 1≤i≤k i 4. if g ,g ∈G and f(n)=g (n)+g (n), then f ∈G, 1 2 1 2 5. if g ,g ∈G and f(n)=g (n)·g (n), then f ∈G, 1 2 1 2 6. if g,h ,h ∈ G and f(n) = if g(n) = 0 then h (n) else h (n), then 1 2 1 2 f ∈G, 7. ∀ if g ∈G,∀ h ∈G and f(n)=h (n),wherej =(g(n)mod 2≤l≤k−1 1≤i≤l i j l)+1, then f ∈G, 8. ∀ if g,h ,h ∈G and f(n)=if g(n)≤l then h (n) else h (n), then l≥1 1 2 1 2 f ∈G, where n=n ,n ,...,n . 1 2 k Lemma 6. Definition 5 is yet another equivalent definition of G: Proof. Let G be the class specified by Definition 4 and let G be the class 1 2 specified by Definition 5. A simple induction on Definition 4 is enough to prove that G ⊆G . It is easy to see that G ⊆G . 1 2 2 1 Theorem 7. For all k-ary functions f ∈ G and for almost all s, there exists an E such that: 1. ∀n ,n ,...,n Eρ(n )ρ(n )...ρ(n )= ρ(f(n ,n ,...,n )), 1 2 k 1 2 k βη 1 2 k 2. ⊢E :ω k →ω , τ(s) τ(s) where τ(s) = (αs → α) → α with α = ω (i.e. τ(s) is the type of an s-tuple of o numerals). Proof. InductiononDefinition5.Herewethoroughlydiscussonecase(theother cases are similar). Suppose f(n) = g (n)+ g (n), where g ,g ∈ G. By the 1 2 1 2 induction hypothesis: For almost all s, there exists a G such that: 1 1. ∀n ,n ,...,n G ρ(n )ρ(n )...ρ(n )= ρ(g (n ,n ,...,n )), 1 2 k 1 1 2 k βη 1 1 2 k 2. ⊢G :ω k →ω . 1 τ(s) τ(s) For almost all s, there exists a G such that: 2 1. ∀n ,n ,...,n G ρ(n )ρ(n )...ρ(n )= ρ(g (n ,n ,...,n )), 1 2 k 2 1 2 k βη 2 1 2 k 2. ⊢G :ω k →ω . 2 τ(s) τ(s) Itfollowsthatforalmostalls,thefunctionf canberepresentedbytheterm E =λn.λfx.(G n)f(G nfx),whichissimilartothetermλn n .λfx.n f(n fx) 1 2 1 2 1 2 (the one used to represent addition), but there are two differences: 1. it takes k arguments of type ω instead of two, τ(s) 2. n is replaced with G n (i=1,2) in the part to the right of“λfx”. i i The terms needed in other cases can be obtained similarly. There is always only a finite number of tuple lengths for which E cannot be constructed. ⊓⊔ Corollary 8. G ⊆F. 3 Limitations of Definability InthissectionweshowsomepropertiesoftheclassG tonarrowthegapbetween thefunctionsthatareknowntobedefinableandthefunctionsthatareknownto be undefinable. We make extensive use of the following theorem (provedin [2]). Theorem 9. Forarbitrary M of type α, there existsa finitemodel M suchthat for every term N of type α: M = N if and only if M|=M =N. βη Lemma 10. Let M be a finite model, and let α = ω , for some τ. For every τ c ∈ M the function h(m,n ,n ) = if (Jρ(m)αKM = c) then n else n is ωτ 1 2 1 2 definable. Proof. LetlbethesmallestnumbersuchthatJρ(l+t)αKM =Jρ(l)αKM,forsome t≥1. Let t be the smallest such t. If c=Jρ(s)αKM, where s<l, then min h(m,n ,n )= 1 2 if (m=s) then n else n = 1 2 if m≤s then (if m≤s−1 then n else n ) else n . 2 1 2 The function h is a composition of functions which belong to G. Therefore h is in G. If c=Jρ(s)αKM, where s≥l, then h(m,n ,n )= 1 2 if m≡s (mod t ) then n else n . min 1 2 Again, h can be expressed as a composition of functions which belong to G. If c6=Jρ(s)αKM for all s∈N then h(m,n ,n )=n . Therefore h∈G. 1 2 2 Theorem 11. If f is of the form f(m,n ,n ) = if m ∈ A then n else n , 1 2 1 2 for some A⊆N, then f is not definable or f ∈G. Proof. Suppose f is definable. Therefore, g(m)= f(m,1,0) is also definable. It is clear that the following holds: f(m,n ,n )=ifzero g(m) then n else n . 1 2 2 1 Let G of type α → α be a term which represents g. The following is the result of applying Theorem 9 to ρ(0)α: 1. N = ρ(0)α if and only if M|=N =ρ(0) for all N of type α, βη 2. M is finite. Let B = {Jρ(m)KM | m ∈ N, JGKM · Jρ(m)KM = Jρ(0)αKM}. Then B = {b ,b ,...,b }, for some k ≥0. 1 2 k The function f can be expressed as follows: f(m,n ,n )= 1 2 if (Jρ(m)αKM =b ) then n else 1 2 if (Jρ(m)αKM =b ) then n else 2 2 ... if (Jρ(m)αKM =b ) then n else n . k 2 1 ItfollowsfromLemma10thatf isacompositionoffunctionswhichbelongtoG. Therefore f ∈G. ⊓⊔ The next theorem is a generalizationof Theorem 11. Theorem 12. Every skewly representable finite range function is strictly repre- sentable. Proof. Let f be a k-ary skewly representablefinite rangefunction, and let A be the range of f. The function f is equal to the function f′, which is defined as follows: f′(m)= if (f(m)=a ) then a else 1 1 if (f(m)=a ) then a else 2 2 ... if (f(m)=a ) then a else a , p−1 p−1 p where A={a ,a ,...,a }, 1 2 p m=m ,m ,...,m . 1 2 k The function f is a composition of functions of the form: h (m,n ,n )=if f(m)=a then n else n . i 1 2 i 1 2 We show that h ∈ G, for arbitrary 1 ≤ i ≤ p. Let E of type α → α → i 1 2 ··· →α → β be a term which skewly represents f. The following is the result k of applying Theorem 9 to ρ(a )β: i 1. N = ρ(a )β if and only if M|=N =ρ(a ) for all N of type β, βη i i 2. M is finite. Let B ={hJρ(m )α1KM,Jρ(m )α2KM,...,Jρ(m )αkKMi|m ,m ,...,m ∈N, 1 2 k 1 2 k JEKM·Jρ(m )α1KM·Jρ(m )α2KM·····Jρ(m )αkKM =Jρ(a )βKM}. 1 2 k i The set B consists of the arguments at which the value of f is equal to a , in i model M. The function h can be expressed as follows: i h (m,n ,n )=if Jρ(m)KM ∈B then n else n , i 1 2 1 2 where m=m ,m ,...,m , 1 2 k Jρ(m)KM =hJρ(m )α1KM,Jρ(m )α2KM,...,Jρ(m )αkKMi. 1 2 k ThesetBisfinite.ItfollowsfromLemma10thath isacompositionoffunctions i which belong to G. Therefore h ∈G. ⊓⊔ i Proposition 13. The predecessor function is not strictly definable. Proof. Suppose pred(n) = n − 1 is strictly definable. Let E be a term that represents pred. Therefore, subtraction can be skewly represented by the term λnm.mEn.Ontheotherhand,weknowthatsubtractionisnotskewlydefinable (see [3]). Proposition 14. Division by two is not strictly definable. Proof. Suppose f(n) = n div 2 is strictly definable. Let E be a term that strictly represents f. Let g be the function which is skewly represented by the term G = λnm.mEn of type α → β → α, where β = ω . The following is the α result of applying Theorem 9 to ρ(0)α: 1. N = ρ(0)α if and only if M|=N =ρ(0) for all N of type α, βη 2. M is finite. Let l be the smallest number such that Jρ(l+t)αKM = Jρ(l)αKM, for some t ≥ 1. Let t be the smallest such t. Similarly, let l′ be the smallest number min suchthatJρ(l′+t′)βKM =Jρ(l′)βKM,forsomet′ ≥1,andlett′ bethesmallest min such t′. On one hand, we have: JGKM·Jρ(l)αKM·Jρ(l′)βKM =JGKM·Jρ(l)αKM·Jρ(l′+l·t′ )βKM = min Jρ(g(l,l′+l·t′ ))αKM =Jρ(0)αKM. min On the other hand, we have: JGKM·Jρ(l)αKM·Jρ(l′)βKM =JGKM·Jρ(l+2l′ ·t )αKM·Jρ(l′)βKM = min Jρ(g(l+2l′ ·t ,l′))αKM 6=Jρ(0)αKM. min 4 Conclusion We have given examples of functions that are strictly definable and are not ex- tended polynomials. It is clear that there are many considerably different kinds of strict definability, depending on the type of numerals. There is also an unex- pecteddifferencebetweenβ-andβη-conversionregardingλ-definability.Acorol- laryofProposition13isthatfunctionswhicharestrictlydefinablewitharbitrary type of numerals form a proper subset of the set of skewly definable functions. Theorem 12 states that it is not the case for functions with finite range. We conjecture that G exactly characterizes strictly definable functions (or G =F). We have shownthat G ⊆F. The functions: fl and fl are compositions 1 2 of functions of the form if m ∈ A then n else n , for all l. It follows from 1 2 Theorem 11 that every strictly definable function of that form is already in G. References 1. Schwichtenberg, H.: Definierbare Funktionen im λ-Kalku¨l mit Typen. Archiv fu¨r Mathematische Logik und Grundlagenforschung, 17:113–114, 1976. 2. Statman,R.:Completeness,invarianceandλ-definability.JournalofSymbolicLogic, 47:17–26, 1982. 3. Fortune, S., Leivant, D., O’Donnell, M.: The expressiveness of simple and second- order typestructures. Journal of the ACM, 30:151-185, 1983. 4. Leivant, D.: Discrete polymorphism. Proceedings of the 1990 ACM Conference on LISP and Functional Programming, pages 288–297, 1990. 5. Zaionc, M.: λ-Definability on free algebras. Annals of Pure and Applied Logic, 51:279–300, 1992.