Returning multiple results?

Apr 5, 2011 at 6:37 PM

I've just started to play with Prolog.NET, so this may be obvious, but I don't see a way to do it, so I'll ask...  If a query can return multiple results, how are those results obtained?  For instance:

hello(world).
hello(bob).

:- hello(X)

should return two matches for X, and it does with GNU Prolog (it initially returns one match, but typing "a" returns the other match).  The Prolog.NET Workbench, and this code (modified from the one presented in the documentation):

            Program program = new Program();
            foreach (CodeSentence cs in Parser.Parse("hello(world).  hello(bob)."))
                program.Add(cs);

            foreach (CodeSentence cs in Parser.Parse(":-hello(X)"))
            {
                PrologMachine machine = PrologMachine.Create(program, new Query(cs));
                ExecutionResults results = machine.RunToSuccess();
                if (results == ExecutionResults.Success)
                    foreach (PrologVariable v in machine.QueryResults.Variables)
                        Console.WriteLine(v);
            }

only return "world" as a result.  How does one, ideally programmatically, execute a query such that each possible result is obtained?

 

Thanks!

Charles

Apr 5, 2011 at 7:14 PM

Ah - never mind.  I hadn't realized that I could rerun the machine and it would produce successive results, e.g.:

ExecutionResults results = ExecutionResults.None;
PrologMachine machine = PrologMachine.Create(program, new Query(cs));

while (results != ExecutionResults.Failure)
{
    results = machine.RunToSuccess();
    if (results == ExecutionResults.Success)
        foreach (PrologVariable v in machine.QueryResults.Variables)
            Console.WriteLine(v);
}

Thanks,

Charles

Apr 6, 2011 at 4:44 PM

[Sorry for my bad English]

Hello Charles,

As I know, you should use findall predicate for multiple results:

:-findall(X, hello(X),Y).

Result:

Y=[world,bob]

 

I hope this helps.

Warm regards,

Slava.