Simple PROLOG issue: How do you test multiple queries against your Prolog database?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 21:22:47

问题


I have a Prolog database file (test_inserts.p) that I used to insert all my data.

I also have a Prolog query file (test_queries.pl) that has all of the Prolog queries I wrote up to receive specific information from my database.

I was wondering how to actually use the test_queries.pl queries against my test_inserts.p database file when using gprolog? I was hoping there would be a way to load both at the same time, and somehow be able to command which query to run, instead of having to re-type each query that I wanted to run....


回答1:


I've used initialization/1 ISO directive in test_queries.pl to get the effect you see at bottom.

test_queries.pl

test :-
        findall(_, (a(X,Y), format('~w ~w~n', [X,Y])), _).

:- initialization([test_inserts]).
:- initialization(test).

test_inserts.pl

a(X,Y) :- append(X,Y,[1,2,3]).

then call gprolog with --consult-file

gprolog --consult-file test_queries.pl
GNU Prolog 1.4.0
By Daniel Diaz
Copyright (C) 1999-2011 Daniel Diaz
compiling /home/carlo/test_queries.pl for byte code...
/home/carlo/test_queries.pl compiled, 5 lines read - 659 bytes written, 28 ms
compiling /home/carlo/test_inserts.pl for byte code...
/home/carlo/test_inserts.pl compiled, 2 lines read - 379 bytes written, 30 ms
[] [1,2,3]
[1] [2,3]
[1,2] [3]
[1,2,3] []
| ?- 


来源:https://stackoverflow.com/questions/10059701/simple-prolog-issue-how-do-you-test-multiple-queries-against-your-prolog-databa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!