Reverse Conversion in Prolog

145 views Asked by At

Would like to do the follow reverse conversion from SKI expressions to lambda expressions:

L[I] = λx.x

L[K] = λx.λy.x

L[S] = λx.λy.λz.(x z (y z))

L[(E₁ E₂)] = (L[E₁] L[E₂])

The conversion need not involve any beta-reduction. But I would nevertheless like to do a special beta-reduction. Whenever there is a linear redex or a unit redex:

 (λx.E₁)E₂      x occurs at most once in E₁

I want to reduce it to:

 E₁[x/E₂]

This seems to be a safe reduction in the sense, that it doesn't make the redex any larger, it only moves the position of E₂ or even eliminates E₂ if x doesn't occur. Respectively does a rename. Example:

 L[S(S(K(S(KS)K))S)(KK)] = λx.λy.λz.xzy 

Any Prolog implementation around?

2

There are 2 answers

0
AudioBubble On

Here is an implementation based on deBruijn indexes, and based on the Prolog answers here . The first part, translating L[_] from wikipedia into deBruijn indexes gives:

lambda(P*Q, W) :-
   lambda(P, R),
   lambda(Q, S),
   simplify(R, S, W).
lambda('I', b(0)).
lambda('K', b(b(1))).
lambda('S', b(b(b(2*0*(1*0))))).

The new predicates in the linear simplification are simplify/2, subst2/4, notin/2 and oncein/2. shift/4 is exactly as in the Prolog answers here. The predicate subst2/4 in contrast to subst/4 calls simplify/2 to build application terms:

simplify(b(R), _, S) :- notin(R, 0), !,
   shift(R, 0, -1, S).
simplify(b(R), S, T) :- oncein(R, 0), !,
   subst2(R, 0, S, T).
simplify(R, S, R*S).

subst2(P*Q, J, D, W) :- !,
   subst2(P, J, D, R),
   subst2(Q, J, D, S),
   simplify(R, S, W).
subst2(b(P), J, D, b(R)) :- !,
   K is J+1,
   subst2(P, K, D, R).
subst2(J, K, _, J) :- integer(J), J < K, !.
subst2(0, 0, D, D) :- !.
subst2(J, J, D, R) :- integer(J), !,
   shift(D, 0, J, R).
subst2(J, _, _, K) :- integer(J), !, K is J-1.
subst2(I, _, _, I).

The predicates notin/2 and oncein/2 split the "at most once" into "not" and "exactly once":

notin(P*Q, J) :- !,
   notin(P, J),
   notin(Q, J).
notin(b(P), J) :- !,
   K is J+1,
   notin(P, K).
notin(J, K) :- integer(J), !,
   J =\= K.
notin(_, _).

oncein(P*Q, J) :-
   notin(P, J), !,
   oncein(Q, J).
oncein(P*Q, J) :- !,
   oncein(P, J),
   notin(Q, J).
oncein(b(P), J) :- !,
   K is J+1,
   oncein(P, K).
oncein(J, J).

This is the question problem solved:

?- lambda('S'*('S'*('K'*('S'*('K'*'S')*'K'))*'S')*('K'*'K'), X).
X = b(b(b(2*0*1)))

The example doesn't completely converse with this bracket abstraction here:

?- unlambda(b(b(b(2*0*1))), X).
X = 'S'*('S'*('K'*'S')*('S'*('K'*'K')*'S'))*('K'*'K')
0
Isabelle Newbie On

A fairly direct translation of the specification into a representation with logical variables:

combinator_lambda(i, lambda(X, X)).
combinator_lambda(k, lambda(X, lambda(_Y, X))).
combinator_lambda(s, lambda(X, lambda(Y, lambda(Z,
                        apply(apply(X, Z), apply(Y, Z)))))).
combinator_lambda(C1 * C2, Lambda) :-
    combinator_lambda(C1, L1),
    combinator_lambda(C2, L2),
    simplify(apply(L1, L2), Lambda).
    
simplify(Term, Simple) :-
    (   nonvar(Term),
        Term = apply(X, Y)
    ->  simplify(X, SimpleX),
        simplify(Y, SimpleY),
        (   nonvar(SimpleX),
            SimpleX = lambda(V, Exp),
            term_atmostonce(Exp, V)
        ->  apply(SimpleX, SimpleY, XY),
            simplify(XY, Simple)
        ;   Simple = apply(SimpleX, SimpleY) )
    ;   nonvar(Term),
        Term = lambda(V, Exp)
    ->  simplify(Exp, SimpleExp),
        Simple = lambda(V, SimpleExp)
    ;   Simple = Term ).
    
term_atmostonce(Term, NoOccurrence) :-
    term_variables(Term, Variables),
    forall(member(Var, Variables), Var \== NoOccurrence),
    !.
term_atmostonce(Term, Singleton) :-
    term_singletons(Term, Singletons),
    member(Var, Singletons),
    Var == Singleton,
    !.
    
apply(lambda(V, E), V, E).

(term_singletons is an SWI-Prolog utility.)

?- combinator_lambda(s*(s*(k*(s*(k*s)*k))*s)*(k*k), Lambda), numbervars(Lambda). 
Lambda = lambda(A, lambda(B, lambda(C, apply(apply(A, C), B)))).