Bracket abstraction in the combinator system C`(K ... - Semantic Scholar

2 downloads 0 Views 164KB Size Report
algorithm introduces at most one combinator for each abstraction in the initial lambda term. .... nator bbc with reduction rule bbcXY ZU w X(Y (UZ)) and represent.
Bracket abstraction in the combinator system C`(K) Sabine Broda, Lus Damas LIACC, Universidade do Porto R. do Campo Alegre 823, 4150 Porto, Portugal Phone: 351 2 6001672 Fax: 351 2 6003654 e-mail: fsbb,[email protected]

Abstract

Translations from lambda calculi to combinatory logics can be used to avoid some implementational problems of the former systems. However, this scheme can only be ecient if the translation produces short output with a small number of combinators, in order to reduce the time and transient storage space spent during reduction of combinatory terms. In this paper we present a combinatory system and an abstraction algorithm, based on the original bracket abstraction operator of Schon nkel [9]. The algorithm introduces at most one combinator for each abstraction in the initial lambda term. This provides the system with quite good properties in terms of memory space as well as in terms of length of evaluations. We prove the correctness of the algorithm and establish some relations between the combinatory system and the lambda calculus.

1 Introduction Combinatory Logic may be regarded as an alternative notation for the Lambda Calculus, but without using bound variables. As such, systems of combinators avoid technical complications due to substitution and congruence that arise in implementations of systems of Lambda Calculus. In fact it is possible to translate any -term into a combinatory term using for this a practical algorithm for carrying out abstraction on combinatory terms. This algorithm was de ned by Schon nkel [9] in 1924. However, when the abstraction algorithm is used repeatedly, it tends to produce expressions of disastrous size with bad consequences in practical applications. First the large size of terms increases the storage space required to represent them and in addition to that there are usually too many combinators involved to mimick abstraction. For this reason the reduction of combinatory terms consists typically of a huge number of reduction steps that consume a lot of transient memory space. Since 1924 several attempts have been made (e.g., [1], [6], [10], [11]) to improve the initial algorithm in order to produce more compact combinatory terms. Besides these Curry [3] presented a simple, but very inecient, algorithm of theoretical importance. Abdali [1] as 1

well as Statman [10] and Kennaway & Sleep [6] de ned multiple abstraction operators which abstract on any number of variables simultaneously. However, these algorithms only produce compact output when the number of simultaneous abstractions performed is large. But as remarked by Turner ([11]), in practice the need to abstract on di erent variables arises at successive stages what makes it more convenient to have a de nition of simple abstraction which is well behaved under self-composition. Another method that is based on multiple abstraction operators is the use of supercombinators which were proposed by Hugues [5] (see also [8]). This approach is frequently used in implementations of functional programming languages and uses an extended notion of combinator. In fact, supercombinators are de ned by combinations which among their arguments may contain also other supercombinators, in contrast to regular combinators which are de ned by combinations of their arguments only. Turner (see [11] and [12]) proposed a method based on a single abstraction operator. This approach is probably one of the most adequate proposals for practical applications (compare with remarks in [7], [8] and [2]). His system is actually used to implement the high-level functional programming language SASL ([12]). Turner improved the algorithm given by Schon nkel [9]. He noticed that the combinators created during an abstraction usually appear at the same position in the resulting combinatory term or one of its subterms, namely at the position of K in KAB . This happens whenever a C , B or S is introduced. Turner prevents combinators in that position from raising the number of combinators in further abstractions by introducing three extra combinators C 0 , B 0 and S 0 of arity 4. These have similar behaviour as C , B and S , while skipping their rst argument and working directly on the last three. With this modi cation Turner was able to improve Schon nkel's algorithm, that for a lambda expression of size n produces in worst case combinatory terms of size O(n3 ). The output of Turner's algorithm is worst case of size O(n2). In this paper we present a combinator system and an abstraction algorithm with the following properties:  The system consists of an indexed family of recursively de ned combinators, that can be very easily implemented.  The algorithm introduces at most one indexed combinator for each abstraction that it performs. Consequently each lambda term is represented by a combinatory term of smaller or equal length.  The complexity of (the index of) this combinator depends, of course, on the size of the initial term, but this complexity does not propagate to further abstractions since the size of the term remains the same.  The complexity of the produced combinatory terms, i.e. the memory space needed to store them, is in the worst case O(n2) with, as for Turner's algorithm, a very small constant.  The number of times that redexes are searched for, reducted and restored, during the reduction of a combinatory term is less or equal to the number of -reduction steps in the corresponding reduction of the original lambda expression. This saves time and avoids the needless use of a lot of transient memory space. As an example consider the lambda expression M = xy:u(z (xv)(yv)): 2

When abstracting for the rst time Schon nkel's algorithm produces the term Bu(B (z (xv))(CIv)) which contains the successively introduced combinators B , B , C and I . We propose to join this sequence in one single combinator bbc with reduction rule bbcXY ZU w X (Y (UZ )) and represent B (u(B (z (xv))(CIv)) by bbcu(z (xv))v. Applying the same method during the next abstraction we obtain the following representation for M : cbbcv(bbc u)zv instead of

B (Bu)(C (BB (Bz (CIv )))(CIv )) which is the output of Schon nkel's algorithm or Turner's representation BB 0 u(C 0B (Bz (CIv))(CIv )): Now consider the reduction of Mxy in the di erent systems. In our approach the reduction is made in two steps, namely cbbcv(bbc u)zvxy 1w bbcu(z (xv))vy 1w u(z (xv)(yv)) instead of Turner's reduction that involves much more steps (or Schon nkel's which is obviously worse): B 0 Bu(C 0 B (Bz (CIv))(CIv))xy 1w Bu(C 0 B (Bz (CIv))(CIv)x)y 1w u(C 0B (Bz (CIv))(CIv )xy) 1w u(B (Bz (CIv)x)(CIv)y ) 1w u(Bz (CIv)x(CIvy)) 1w u(z (CIvx)(CIvy)) 1w u(z (Ixv)(CIvy)) 1w u(z (Ixv)(Iyv)) 1w u(z (xv)(Iyv)) 1w u(z (xv)(yv)):

In the next section we give the formal de nition of the combinatory system C`(K). Then, in section 3, we describe the translation algorithm and prove its correctness. Some further correspondences between C`(K) and the lambda calculus are given in section 4 and nally, in section 5, we give a prototype implementation in Prolog for the translation and reduction of lambda terms.

2 The combinatory system C`(K) In this section we give the formal description of the combinatory system

C`(K) and establish some notational conventions. De nition 2.1 The set of combinator indexes A is given by the following

rules:

 2A  2 A ) c  2 A; 3

 2 A ) b  2 A;  ; 2 A ) ( ; ) 2 A. 1

2

1

2

Here  denotes the empty word and  the concatenation operation. The set of combinators K is de ned by  K 2 K;  2 A )  2 K . Let V be a set of variables. Then we de ne the set of combinatory terms

C`(K) inductively,  x 2 V ) x 2 C`(K);   2 K )  2 C`(K);  X; Y 2 C`(K) ) XY 2 C`(K).

In what follows, we will denote C`(K)-terms by capital letters X; Y; Z; : : : using the abbreviation X1 X2 X3 : : :Xn for (: : : ((X1 X2 )X3 ) : : :). We de ne the degree of the combinator K by 1 and the degree of a combinator  by ] which is the number of c's and b's that appear in . A term of the form X1 : : :Xn+1 , where  is a combinator of degree n, is called a weak redex. The contraction of a redex in C`(K) is de ned by the following:

De nition 2.2 (Weak reduction) We de ne the contractum (R) of a C`(K)-redex R and the reduction relation w inductively by  (KXY ) = X ;  ( X ) = X ;  (c  XX : : :X] ) = (( X : : :X] ))X ;  (b  XX : : :X] ) = X (( X : : :X] ));  ( 1; 2 X : : :X] 1 Y : : :Y] 2 Z ) = (( 1 X : : :X] 1 Z )) (( 2 Y : : :Y] 2 Z ));  X  w Y , if Y is obtained from X by substitution of a subterm R, 1

+1

1

(

)

1

+1

1

+1

1

+1

1

1

1

1

which is a redex, by its contractum (R). We denote by w the re exive and transitive closure of 1w and by =w the symmetric closure of w .

3 The abstraction algorithm

De nition 3.1 The set of -terms  is de ned by the following rules  x 2 V ) x 2 ;  M; N 2  ) MN 2 ;  x 2 V and M 2  ) x:M 2 . Terms of  will be denoted by capital letters M; N; : : :. The abbreviation x1 : : :xn :M will stand for (x1 (: : : (xn :M ) : : :)). ! is the re exive and transitive closure of !1 , de ned by the well-known relation (x:M )N !1 M [N=x] 4

and = is the symmetric closure of ! . In what follows we consider terms modulo = so that we do not have to be concerned about N being free for x in M or not. Next we de ne the transformation ( )H that translates each M 2  into a combinatory term MH . Abusing on the notation we write X 2 Y if the term X is a subterm of Y , and X 62 Y otherwise.

De nition 3.2 The transformation ( )H is given as usual by ( )H :  ?! C`(K) x ; x MN ; MH NH x:M ;  x:MH

except for the abstraction algorithm  x:X which is de ned below. The abstraction algorithm  x:X for a C`(K)-term X consists of two

steps. First compute [x]X using Schon nkel's algorithm: [x] U = KU if x 62 U [x] Ux = U if x 62 U [x] x = I [x] UV = C ([x] U )V if x 2 U and x 62 V [x] UV = BU ([x] V ) if x 62 U and x 2 V [x] UV = S ([x] U )([x] V ) if x 2 U and x 2 V We de ne  x:X = [x]X if I , C , B and S are not present in [x]X . Otherwise let

 x:X =  X1 : : :Xn where = i([x]X ) and X1 ; : : :; Xn = r([x]X ). Note that [x]X contains extra constants if and only if it is of the form I , CY Z , BY Z or SY Z . For these terms we de ne a combinator index and the corresponding sequence of residuals by two quite simple algorithms: i(I ) =  i(CXY ) = c  i(X ) i(BXY ) = b  i(Y ) i(SXY ) = (i(X ); i(Y )) i(X ) = b otherwise. r(I ) r(CXY ) r(BXY ) r(SXY ) r(X )

= = = = =

 Y; r(X ) X; r(Y ) r(X ); r(Y ) X otherwise.

It is easy to verify the following:  if i([x]X ) is of degree n  0, then the sequence of residuals r([x]X ) has exactly n elements; 5

 if X 2 C`(K), then  x:X 2 C`(K); in particular  x:X does not contain any I , C , B or S .

Example: since and

and

 y:u(z (xv)(yv)) = bbcu(z (xv))v [y]u(z (xv)(yv)) = Bu(B (z (xv))(CIv));

i(Bu(B (z (xv))(CIv ))) = = = =

b  i(B (z (xv))(CIv)) bb  i(CIv) bbc  i(I ) bbc

r(Bu(B (z (xv))(CIv))) = = = =

u; r(B (z (xv))(CIv)) u; z (xv); r(CIv) u; z (xv); v; r(I ) u; z (xv); v:

Lemma 3.3 Let x be a variable and X 2 C`(K). Then, 1. x 62 FV ( x:X ); 2. ( x:X )x w X . Proof

1. Trivial. 2. First suppose that x 62 X . Then ( x:X )x = KXx w X . From now on let x 2 X . Note that in this case the rst equation of Schon nkel's algorithm will not be used during the computation of [x]X . If  x:X = [x]X , then X = Ux, x 62 U and ( x:X )x = Ux. Finally we prove by induction on n that, if X 2 C`(K), x 2 X , 0 = i([x]X ) and X1 ; : : :; Xn = r([x]X ), then ( X1 : : :Xn x) = X . If n = 0, then 0 =  and [x]X = I . Thus X = x and ( x) = x. Otherwise 0 = c  , 0 = b  or 0 = ( 1 ; 2). We prove the result for 0 = b  , which is the most complex of the three cases. In fact there are two possibilities: (i) If the index 0 = b  was created by application of the third equation of the algorithm, then [x]X = BUY . This means that X = UV , with x 62 U and x 2 V , Y = [x]V and X1 = U . On the other hand, there is i([x]V ) = and r([x]V ) = X2 ; : : :; Xn. From the induction hypothesis, we conclude ( X2 : : :Xn x) = V . Thus (b  X1 : : :Xn x) = X1 (( X2 : : :Xn x)) = UV . (ii) If 0 results from the last equation of the algorithm, then 0 = b and [x]X = X1 . Obviously, this means that [x]X is not of the form I , CUV , BUV or SUV . Thus, [x]X = X1 was created by the second equation of Schon nkel's algorithm. We conclude that X = X1 x, with x 62 X1 and (( x:X )x) = (b X1 x) = X1 x.  0

6

Proposition 3.4 ( x:X )Y behaves like a -redex, i.e. ( x:X )Y w X [Y=x]: Proof First note that X w Z ) X [Y=x] w Z [Y=x]

(1)

since this is true for each redex in X and since (AB )[Y=x] = (A[Y=x])(B [Y=x]). Now, ( x:X )Y = ( x:X )(x[Y=x]) = (( x:X )x)[Y=x] Lemma 3.3 1. w X [Y=x] from (1) and Lemma 3.3 2.  The following proposition shows that in contrast with traditional systems we have a more direct correspondence between weak reduction in C`(K) and -reduction in . In fact, for each weak reduction of the combinatory representation of a -term M there exists a -reduction of M whose translation leads to the same combinatory term. The reciprocal of this property is still false, in particular MH may be a weak normal form while M itself is not a normal form for -reduction.

Proposition 3.5 MH w Y ) 9N : M ! N and NH = Y: Proof (scheme)

Let X1 : : :Xn+1 be a redex in MH . As the abstraction algorithm does not create any new redexes (hint: show that if R is a redex in  x:PH , then R 2 PH ) we conclude that X1 : : :Xn+1 was constructed by an application, i.e. a subterm UV of M such that UH = X1 : : :Xn and VH = Xn+1 . Note that this term UH VH was not destructed during the further construction of MH since M necessarily contains no abstraction over the free variables of UV . Now prove by induction on the number of applications of the rule

 x:Xx = [x]Xx = X; x 62 X during the construction of UH , that U ! x:P , for some P 2  such that PH 6= Zx with x 62 Z and  x:PH = X1 : : :Xn . From the proof of Lemma3.3 it is easy to conclude that (X1 : : :Xn+1 ) = (( x:PH )VH ) = PH [VH =x]. On the other hand, one shows by induction on the structure of P that PH [VH =x] = (P [V=x])H . Thus for each reduction step in MH , there exists a -reduction for M (substitution of UV by P [V=x]) which leads to equality via ( )H (for this, note that every free variable of P [V=x] is a free variable of UV ). Now suppose that MH w Y . Then there exists a nite sequence MH = Y1 1w : : : 1w Yn = Y: Applying the previous to each of the steps we obtain the existence of N by transitivity (see gure 3). 

7

M = M1 ( )

!

M2

! : : : !

Mn = N

1w

1w : : : 1w Y2 Figure 3

Yn = Y

H

H

( )

( )

MH = Y1 



H



4 Further correspondence between C`(K) and 

Now we establish some other relations between the two calculi beginning with the de nition of the function ( ) which computes -representations for combinatory terms.

De nition 4.1 For X 2 C`(K) we de ne X by induction on X : (x) (AB ) (K ) ( ) (c  )

(b  ) (( 1 ; 2 ) )

x A B xy:x x:x x1 x2 : : :xn :Mx1; if ( ) = x2 : : :xn :M = x1 x2 : : :xn :x1M; if ( ) = x2 : : :xn :M = x1 : : :xm y1 : : :yn z:Mz (Nz ); if ( 1 ) = x1 : : :xm :M and ( 2 ) = y1 : : :yn :N

= = = = =

Note that the function ( ) is well de ned, i.e. 8X 2 C`(K) 91 M 2  such that (X ) = M . The following Lemmas can be proved by induction on n.

Lemma 4.2 For every combinator  of degree n there is ( ) = x : : :xn :M , for some M 2  that contains no abstraction. 1

+1

Lemma 4.3 Let  be a combinator of degree n such that ( ) = x : : :xn :M . 1

If (X1 : : :Xn+1 ) = B , then (B ) = M [(X1 ) =x1; : : :; (Xn+1) =xn+1].

+1

Proposition 4.4 Let X; Y 2 C`(K), then: 1. X w Y ) X ! Y ; 2. X =w Y ) X = Y . Proof

1. It is sucient to show that for every combinator  of degree n and X1 ; : : :; Xn+1 2 C`(K), if X1 : : :Xn+1 1w Y , then (X1 : : :Xn+1 ) ! Y. In fact (X1 : : :Xn+1 ) = ( )(X1 ) : : : (Xn+1 ) de nition of ( ) = (x1 : : :xn+1 :M )(X1) : : : (Xn+1 ) Lemma 4.2 : ! M [(X1) =x1; : : :; (Xn+1) =xn+1] = (Y ) Lemma 4.3 8

2. X =w Y if and only if there is some combinatory term Z such that X w Z and Y w Z . From (1.) we conclude that X ! Z and Y ! Z . Thus, X = Y . 

Proposition 4.5 M 2  ) (MH ) =  M . Proof By induction on M . The cases for M = x and M = M M are trivial. Let M = x:P . Then ((x:P )H ) = ( x:PH ) = x:( x:PH ) x = x:(( x:PH )x) = x:(PH ) =  x:P

1

2

Def. 3.2 Def. 4.1 Lemma 4.4 and Lemma 3.3 induction hypothesis. 

Proposition 4.6 M 2  ) ((MH ) )H = MH . Proof (scheme) First, verify following the de nition of the abstraction algorithm that 8M 2  b 62 MH . Next, show by induction on n that for every combinator  of length n  2

and such that ( ) = x1 : : :xn+1:N there is  xn+1:N =  x1 : : :xn. Finally prove ((MH ) )H = MH by induction on MH : The cases MH = x, MH = K , MH =  , MH = c and X = Y Z are easy to verify. Suppose that MH =  of length n  2. From Lemma 4.2, we know that ( ) = x1 : : :xn+1 :N for some -free term N . Thus (( ))H = (x1 : : :xn+1 :N )H =  x1:( x2:(: : : xn:( xn+1:N ) : : :)) =  x1:(: : : xn :( x1 : : :xn) : : :) =  : 

Corollary 4.7 X 2 C`(K) and b 62 X ) (X )H = X .

4.1 Subsystems of C`(K)

By inspection of the translation functions ( )H and ( ) one notes that the system C`(K), which can be denoted by

fK g [ f g 2A; includes several subsystems which correspond exactly to -subsystems of particular interest. As such f g 2A corresponds to Curry's I -calculus [3] and if A0 is de ned by   2 A0  2 A0 ) c  2 A 0  2 A0 ) b  2 A 0 then fK g [ f g 2A corresponds to the linear lambda calculus as de ned in [4]. 0

9

5 A prototype implementation In this section we give a prototype implementation in Prolog for the translation and reduction of lambda terms. Instead of using a recursive implementation of the combinators we choose to give them new names in order of appearance and store them together with their reduction rules. Thus, similar to the supercombinator approach, every lambda term yields a set of combinator de nitions plus a combinatory expression to be evaluated. Note that the memory space, stemming both from term size and combinator allocation, which is necessary to store the combinatory representation of a lambda term of size O(n) is in worst case of size O(n2 ). :- op(400,yfx,'@'). execute(M,Result):- reset, translate(M,X), reduce(X,Result,0,N). reset:- abolish(red/2), (recorded(comb,_,R),erase(R),fail; recorda(comb,0,_)). reduce(A,C,N,N1):l_reduce(A,B,N,N2), (A=B -> r_reduce(B,C,N2,N1); reduce(B,C,N2,N1)). l_reduce(k@X@Y,X,N,N1):- !, N1 is N+1. l_reduce(A@B,C,N,N1):- !, (red(A@B,C),N1 is N+1 ; l_reduce(A,A1,N,N1), C=A1@B). l_reduce(A,A,N,N). r_reduce(A@B,A1@B1,N,N1):- !, r_reduce(A,A1,N,N2), reduce(B,B1,N2,N1). r_reduce(A,A,N,N). translate(M@N,M1@N1):- !,translate(M,M1),translate(N,N1). translate(lambda(X,M),Exp):- !, translate(M,M1),abstr(X,M1,M2),comp(M2,Exp). translate(CV,CV). abstr(X,X,i):-!. abstr(X,Y@Z,T):- !,abstr(X,Y,Y1),abstr(X,Z,Z1),opt(s@Y1@Z1,T). abstr(X,CV,k@CV). opt(s@(k@X)@(k@Y),k@(X@Y)):-!. opt(s@(k@X)@i,X):-!. opt(s@(k@X)@Y,b@X@Y):-!. opt(s@X@(k@Y),c@X@Y):-!. opt(s@X@Y,s@X@Y). comp(A,Exp):- (A=i;A=c@Y@Z;A=b@Y@Z;A=s@Y@Z),!, new_comb(C),term(C,A,Exp),ind(A,List,Res), make_term([C|List],[],Cterm),asserta(red(Cterm,Res)). comp(A,A). new_comb(C):- recorded(comb,C,Ref),erase(Ref), D is C+1,recorda(comb,D,_). make_term([X|R],R,X):-!.

10

make_term(List,R,A@X):- !,make_term(List,[X|R],A). term(A,i,A):-!. term(A,c@X1@X2,Exp):- !,term(A@X2,X1,Exp). term(A,b@X1@X2,Exp):- !,term(A@X1,X2,Exp). term(A,s@X1@X2,Exp):- !,term(A,X1,E1),term(E1,X2,Exp). term(A,X,A@X). ind(i,[A],A):-!. ind(b@_@Y,[A|R],A@E):- !,ind(Y,R,E). ind(c@X@_,[A|R],E@A):- !,ind(X,R,E). ind(s@X@Y,R,E1@E2):- !,ind(X,R1,E1),ind(Y,R2,E2),last(R1,A), last(R2,A),join(A,R1,R2,R). ind(_,[A,B],A@B). last([X],X):-!. last([X|R],Y):- last(R,Y). join(A,[A],L,L):-!. join(A,[X|R],L,[X|T]):- join(A,R,L,T).

References [1] S. Abdali. An abstraction algorithm for combinatory logic. Journal of Symbolic Logic, 41:222{224, 1976. [2] H. Barendregt. Functional programming and lambda calculus. Handbook of Theoretical Computer Science, pages 322{63, 1990. [3] H. Curry and R. Feys. Combinatory Logic Vol. 1. North-Holland, Amsterdam, 1958. [4] J. R. Hindley. BCK-combinators and linear -terms have types. Theoretical Computer Science, 64:97{105, 1989. [5] J. Hugues. Supercombinators, a new implementation method for applicative languages. Proc. ACM Symp. on Lisp and Functional Programming, pages 1{10, 1982. [6] J. R. Kennaway and M. R. Sleep. Counting director strings. University of East Anglia, Norwich, 1984. [7] J. Mulder. Complexity of combinatory code. Preprint 389, University Utrecht, Department of Mathematics, August 1985. [8] S. Peyton Jones. The implementation of functional programming languages. Prentice-Hall, 1987. [9] M. Schon nkel. U ber die Bausteine der mathematischen Logik. Mathematische Annalen, 92:305{316, 1924. [10] R. Statman. On translating lambda terms into combinators; the basis problem. pages 378{382. IEEE, 1986. Proceedings of the LICS'86. [11] D. Turner. Another algorithm for bracket abstraction. Journal of Symbolic Logic, 44:267{270, 1979. [12] D. Turner. A new implementation technique for applicative languages. Software-Practice and Experience, 9:31{49, 1979.

11