Article View: comp.lang.prolog
Article #2667self-reproducing Prolog program With Variables
From: arun
Date: Tue, 05 May 1987 03:44
Date: Tue, 05 May 1987 03:44
64 lines
2218 bytes
2218 bytes
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.
Technical Details
Message-ID:
<2090@cwruecmp.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!cbatt!cwruecmp!arunShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!cbatt!cwruecmp!arun From: arun@cwruecmp.UUCP Newsgroups: comp.lang.prolog Subject: self-reproducing Prolog program With Variables Message-ID: <2090@cwruecmp.UUCP> Date: Mon, 4-May-87 23:44:02 EDT Article-I.D.: cwruecmp.2090 Posted: Mon May 4 23:44:02 1987 Date-Received: Thu, 7-May-87 01:07:53 EDT Reply-To: arun@cwruecmp.UUCP (Arun Lakhotia) Distribution: world Organization: CWRU Dept. of Computer Engineering, Cleveland, Ohio Lines: 64