| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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 |
|
#3
| |||
| |||
| 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. |
|
#4
| |||
| |||
| 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 |
|
#5
| |||
| |||
| 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 |
|
#6
| |||
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.