Simple PROLOG issue: How do you test multiple quer

2019-06-19 00:17发布

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条回答
贼婆χ
2楼-- · 2019-06-19 00:58

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] []
| ?- 
查看更多
登录 后发表回答