obtaining all query results

This is a discussion on obtaining all query results within the PROLOG forums in Programming Languages category; I'm very new to Prolog, so please excuse me if this is not the proper venue for questions like this. I have a database against which I want to execute a query and return the list of all matching bindings. I'm using SWI-Prolog and am able to get the results that I want when running interactively. I would like to get the same results displayed on stdout without user interaction. Is this possible? For example, I have the database "fruit.pl": color(apple, red). color(cherry, red). color(orange, orange). color(grape, purple). I then do $ swipl -f fruit.pl ?- color(X, red). X = ...

Go Back   Application Development Forum > Programming Languages > PROLOG

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 11:25 PM
Michael Hendricks
Guest
 
Default obtaining all query results

I'm very new to Prolog, so please excuse me if this is not the proper
venue for
questions like this. I have a database against which I want to
execute a query
and return the list of all matching bindings. I'm using SWI-Prolog
and am able
to get the results that I want when running interactively. I would
like to get
the same results displayed on stdout without user interaction. Is
this
possible?

For example, I have the database "fruit.pl":

color(apple, red).
color(cherry, red).
color(orange, orange).
color(grape, purple).

I then do

$ swipl -f fruit.pl
?- color(X, red).
X = apple ;
X = cherry ;
No

which is great. I would like to be able to do something like this and
get the
following output without having to type the query at a prompt or press
enter:

$ some-command fruit.pl "color(X,red)."
X = apple ;
X = cherry ;
No

I've tried

$ swipl -f fruit.pl -g "color(X,red)."
$ swipl -f fruit.pl -t "color(X,red)."
$ swipl -o fruit -c fruit.pl && ./fruit

all without success. I've spent about an hour reading all the
documentation
that looked relevant, but haven't turned up anything that works.

Thanks for your help.

--
Michael
Reply With Quote
  #2  
Old 08-28-2008, 03:16 AM
Carlo Capelli
Guest
 
Default Re: obtaining all query results

Hi Micheal.
I'm using windows, and after I saved a copy of 'fruit.pl' to
c:\programmi\pl\bin i issued the command plcon (i think swipl have same
meaning)

C:\Programmi\pl\bin>plcon -g
"[fruit],(color(X,Y),write(X=Y),nl,fail;halt(0))"
% c:/documents and settings/carlo.capelli/pl.ini compiled 0.00 sec, 1,888
bytes
% fruit compiled 0.00 sec, 836 bytes
apple=red
cherry=red
orange=orange
grape=purple

The syntax [fruit] is a shortcut to consult(fruit). Keep the documentaion at
hand!

Bye Carlo

"Michael Hendricks" <mndrix@gmail.com> ha scritto nel messaggio
news:45aea55d-e7f1-4a9e-a6d7-826804125585@z6g2000pre.googlegroups.com...
> I'm very new to Prolog, so please excuse me if this is not the proper
> venue for
> questions like this. I have a database against which I want to
> execute a query
> and return the list of all matching bindings. I'm using SWI-Prolog
> and am able
> to get the results that I want when running interactively. I would
> like to get
> the same results displayed on stdout without user interaction. Is
> this
> possible?
>
> For example, I have the database "fruit.pl":
>
> color(apple, red).
> color(cherry, red).
> color(orange, orange).
> color(grape, purple).
>
> I then do
>
> $ swipl -f fruit.pl
> ?- color(X, red).
> X = apple ;
> X = cherry ;
> No
>
> which is great. I would like to be able to do something like this and
> get the
> following output without having to type the query at a prompt or press
> enter:
>
> $ some-command fruit.pl "color(X,red)."
> X = apple ;
> X = cherry ;
> No
>
> I've tried
>
> $ swipl -f fruit.pl -g "color(X,red)."
> $ swipl -f fruit.pl -t "color(X,red)."
> $ swipl -o fruit -c fruit.pl && ./fruit
>
> all without success. I've spent about an hour reading all the
> documentation
> that looked relevant, but haven't turned up anything that works.
>
> Thanks for your help.
>
> --
> Michael



Reply With Quote
  #3  
Old 08-28-2008, 03:25 AM
ronaldo
Guest
 
Default Re: obtaining all query results

If you used something like...

$ swipl -g "color(X,red), writeln(X), fail." -c fruit.pl

you should get a binary file called a.out. Then you will only have to
execute a.out to get the result you wanted. Take into account that
launching the main goal with -g does not make the interpreter inform
you of the values of instantiated variables once it founds a coherent
result for your query. It simply ends execution because query
successes. So you have to add some write stuff to manually print the
values.

I usually add a halt to make the execution kill the prolog interpreter
at the end, just similar to this

$ swipl -g "color(X, red), writeln(X), fail; halt." -c fruit.pl

I hope this helps.
Reply With Quote
  #4  
Old 08-28-2008, 10:01 AM
Michael Hendricks
Guest
 
Default Re: obtaining all query results

On Aug 28, 1:25*am, ronaldo <rona...@cheesetea.com> wrote:
> If you used something like...
>
> * $ swipl -g "color(X,red), writeln(X), fail." -c fruit.pl


Thank you Ronaldo and Carlo. That's exactly what I was looking for.
I hadn't thought about using "fail" that way to force Prolog to
backtrack and find the remaining solutions. It's very clever.

Thanks again.

--
Michael
Reply With Quote
  #5  
Old 08-28-2008, 10:22 AM
Antonio Maschio
Guest
 
Default Re: obtaining all query results

ronaldo wrote:
> If you used something like...
>
> $ swipl -g "color(X,red), writeln(X), fail." -c fruit.pl
>
> you should get a binary file called a.out. Then you will only have to
> execute a.out to get the result you wanted. Take into account that
> launching the main goal with -g does not make the interpreter inform
> you of the values of instantiated variables once it founds a coherent
> result for your query. It simply ends execution because query
> successes. So you have to add some write stuff to manually print the
> values.
>
> I usually add a halt to make the execution kill the prolog interpreter
> at the end, just similar to this
>
> $ swipl -g "color(X, red), writeln(X), fail; halt." -c fruit.pl
>
> I hope this helps.



The same may be obtained in gprolog with the command:
$ gprolog --entry-goal "[fruit]" --query-goal "color(X,Y), write(X=Y),
nl, fail ; halt"

(the final dot is not required). It works even under Windows.

-- Antonio
Reply With Quote
  #6  
Old 08-28-2008, 10:52 AM
Mats Carlsson
Guest
 
Default Re: obtaining all query results

On Aug 28, 4:22 pm, Antonio Maschio <tbinNOS...@NOSPAMlibero.it>
wrote:
> ronaldo wrote:
> > If you used something like...

>
> > $ swipl -g "color(X,red), writeln(X), fail." -c fruit.pl

>
> > you should get a binary file called a.out. Then you will only have to
> > execute a.out to get the result you wanted. Take into account that
> > launching the main goal with -g does not make the interpreter inform
> > you of the values of instantiated variables once it founds a coherent
> > result for your query. It simply ends execution because query
> > successes. So you have to add some write stuff to manually print the
> > values.

>
> > I usually add a halt to make the execution kill the prolog interpreter
> > at the end, just similar to this

>
> > $ swipl -g "color(X, red), writeln(X), fail; halt." -c fruit.pl

>
> > I hope this helps.

>
> The same may be obtained in gprolog with the command:
> $ gprolog --entry-goal "[fruit]" --query-goal "color(X,Y), write(X=Y), nl, fail ; halt"


Similarly with SICStus:

$ sicstus -l fruit --goal "color(X,Y), portray_clause(color(X,Y)),
fail; halt."

--Mats
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:48 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.