MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: prolog like search

  • To: mathgroup at
  • Subject: [mg76667] Re: [mg76621] prolog like search
  • From: Sseziwa Mukasa <mukasa at>
  • Date: Fri, 25 May 2007 06:42:54 -0400 (EDT)
  • References: <>

On May 24, 2007, at 6:27 AM, siewsk at wrote:

> I would like to do prolog like searches in Mathematica
> entities={ william , henry , diana , charles }
> Sonof(william,diana);
> Sonof(william,charles);
> Sonof(henry,diana);
> Sonof(henry,charles);
> Married(A,B) := Sonof(S,A) && Sonof(S,B) && A != B;
> PrologSearch(Married,entities)
> returns { Married(diana,charles), Married(charles,diana) }
> Any idea how this could be implemented in Mathematica?

It's been a while since I've looked at Prolog (and the second time  
I've heard it mentioned in the last 5 days) but if I recall  
correctly, Prolog does a depth first search with backtracking.  The  
function you want would search the tree of values of sonof (it's  
easier in Mathematica not to capitalize the names of your  
expressions, or at least the initial letter) and return the values of  
married for which it's true.  As I understand it Mathematica's  
pattern matcher does the depth first search, although I don't know  
how to emulate Prolog's cut, but you have to supply the implied sonof 
(x_,y_):=False for the other possible arguments of sonof.

The other tricky part is preventing the evaluator from evaluating  
married[a_,b_] to a boolean, I'll choose to return the expressions as  
string then.

The following works but requires much more work on the programmer's  
part than just doing it in Prolog:

entities={ william , henry , diana , charles };

married[a_,b_] :=
     Or@@( sonof[#,a] && sonof[#,b] && ToString[a] !=ToString[ b]&/ 


There's probably a more clever and generalized way to do this using  
Mathematica's inherent pattern matching capabilities suing MatchQ but  
I haven't had enough coffee to figure it out yet.



  • Prev by Date: Re: Re: Weird result in Mathematica 6
  • Next by Date: Re: Grid
  • Previous by thread: prolog like search
  • Next by thread: Re: prolog like search