Path: utzoo!attcan!uunet!clyde.concordia.ca!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!udel!burdvax!finin@prc.unisys.com From: finin@prc.unisys.com (Tim Finin) Newsgroups: comp.lang.prolog Subject: Re: Duplicate Solutions Message-ID: <13167@burdvax.PRC.Unisys.COM> Date: 15 Mar 90 14:35:17 GMT References: <11500023@hpldola.HP.COM> Sender: news@PRC.Unisys.COM Reply-To: finin@prc.unisys.com (Tim Finin) Organization: Unisys Center for Advanced Information Technology Lines: 34 In article <11500023@hpldola.HP.COM>, patch@hpldola (Pat Chkoreff) writes: >Occasionally I have written a predicate that yields duplicate solutions, and >I've wondered how to make each solution occur uniquely. For example, I have >a multigraph: ... > >Usually, the existence of duplicate solutions makes me change either my data >model or my algorithms to avoid them. But in this example, my data model is >straightforward, joined/2 seems like a natural thing to have, and duplicate >solutions bother me (I'm not sure why, though). What would you do to avoid >duplicate solutions, or would you even bother? You are correct, I think, in looking to change your data model and/or algorithm to avoid generating duplicate solutions when they are not appropriate. Other than that, there is no good way to do this in a pure logic programming language. In practice, this is usually done with a cut or some related operator, such as if-then-else. You might use one of the following approaches, for example: joined(X,Y) :- edge(_,X,Y),!. joined(X,Y) :- edge(_,X,Y) -> true ; fail. joined(X,Y) :- once(edge(_,X,Y)) once(P) :- P,!. All of these have problems, however, if you call joined/2 with uninstantiated arguments and expect it to generate a stream of answers. To support this use, you could use bagof/setof (or the equivalent) or, as you suggest, revise your representation scheme. The latter is preferable in my mind. -- Tim Finin finin@prc.unisys.com Center for Advanced Information Technology 215-648-2840, 215-648-2288 (fax) Unisys, PO Box 517, Paoli, PA 19301 215-386-1749 (home)