[comp.lang.prolog] self-reproducing Prolog program With Variables

arun@cwruecmp.UUCP (05/05/87)

Reference a comment in my previous posting regarding
   Variables in a self-reproducing Prolog program.

Here is a s-r program (clone/0) generator. The program when run would
produce a prolog program that when run would reproduce itself
Variable for Variable, and blank for blank.
(a la Kernigham's Turing award lecture style)

%% -----
numbervars(Name, N, N1) :-
    var(Name),
    Temp is 65 + N,                    % 65 = 'A'
    name(Name, [Temp]),
    N1 is N +1.
numbervars(Term, N1, N2) :-
    nonvar(Term),
    functor(Term, Name, N),
    numbervars(0,N,Term,N1,N2).

numbervars(N,N,Term,N1,N1).
numbervars(I,N,Term,N1,N3) :-
    I < N,
    I1 is I +1,
    arg(I1,Term,Arg),
    numbervars(Arg,N1,N2),
    numbervars(I1,N,Term,N2,N3).

display_clause(X) :-
    numbervars(X,0,_),
    write(X),
    write('.'),
    nl,
    fail.
display_clause(X).

display_clauses(F/A) :-
    functor(X,F,A),
    clause(X,B),
    display_clause((X:-B)),
    fail.
display_clauses(_).

clone :-
    display_clauses(numbervars/3),
    display_clauses(numbervars/5),
    display_clauses(display_clause/1),
    display_clauses(display_clauses/1),
    display_clauses(clone/0).

%% ---
Ofcourse i take my words back regarding the the difficulty
(or impossibility) of writing a 'portable' s-r Prolog program
with variables.

The correctness of the above program is constrained by the
assumption that there exist no other clauses in the program
database of same functor and arity as in the program above.

=
arun lakhotia

PS: The program would not run on Prolog implementation that
    cannot readback 'write/1' output.