Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!ames!oliveb!intelca!mipos3!omepd!littlei!ogcvax!schaefer From: schaefer@ogcvax.UUCP (Barton E. Schaefer) Newsgroups: comp.lang.prolog Subject: Re: self-reproducing programs Message-ID: <1272@ogcvax.UUCP> Date: Tue, 5-May-87 13:45:19 EDT Article-I.D.: ogcvax.1272 Posted: Tue May 5 13:45:19 1987 Date-Received: Sat, 9-May-87 01:34:12 EDT References: <430@ivax.doc.ic.ac.uk> Reply-To: schaefer@ogcvax.UUCP (Barton E. Schaefer) Organization: Oregon Graduate Center, Beaverton, OR Lines: 62 In article broome@brl.arpa (Paul Broome (CTAB) ) writes: >Here's a frivolous little program that removes itself. > >suicide :- > abolish(suicide,0). How about this one? The following predicate, invoked as ?- everyclause(everyclause(_,_),X). will remove itself from the database, binding X to a list of all its clauses in the process, then put itself back again. /* `everyclause(X,Y)' returns in Y a list of all clauses whose heads have the same functor and arity as X. `everyclause' works by calling the primitive `clauses', retracting the clause found, recurring to find the next clause, and then re-asserting the retracted clause. It cannot be used to test directly that a list contains every clause; its second argument must be a variable. */ /* Since `clause' fails with an error if called with a variable in its first argument, `everyclause' fails if it would have to make such a call. */ everyclause(X,Y) :- var(X), !, fail. /* The useful part of `everyclause'. */ everyclause(X,[]) :- not clause(X,_), !. everyclause(X,[Y|Z]) :- var(Y), !, functor(X,F,N), X =.. [F|N1], newvars(N1,N2), A =.. [F|N2], clause(A,C), Y = (A :- C), functor(B,F,N), clause(B,D), retract((B :- D)), ((everyclause(X,Z), !, asserta((B :- D))); (asserta((B :- D)), !)). newvars(X,X) :- atomic(X). newvars(X,Y) :- var(X). newvars((X1,Y1),(X2,Y2)) :- !, newvars(X1,X2), newvars(Y1,Y2). newvars([H1|R1],[H2|R2]) :- !, newvars(H1,H2), newvars(R1,R2). newvars(X,Y) :- X =.. [F|A], newvars(A,B), Y =.. [F|B]. -- Bart Schaefer Dept. of CS&E CSNET: schaefer@Oregon-Grad Oregon Graduate Center UUCP: {ihnp4,seismo,sun}!verdix \ 19600 NW Von Neumann Dr {hplabs,ucbvax,decvax}!tektronix !ogcvax!schaefer Beaverton, OR 97006