Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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....

like image 545
Mr Prolog Avatar asked Apr 08 '12 01:04

Mr Prolog


People also ask

How do you query in Prolog?

Prolog queries work by pattern matching. The query pattern is called a goal. If there is a fact that matches the goal, then the query succeeds and the listener responds with 'yes. ' If there is no matching fact, then the query fails and the listener responds with 'no.

What is a Prolog database?

Prolog is an elegant language for database queries. In fact if one constrains Prolog programs to use only atoms, integers and reals (no lists or complex terms) and disallows recursive definitions, one gets a database language that is equivalent to a powerful subset of SQL.

What is dynamic database in Prolog?

Dynamic database can change dynamically at execution time and are of two types. Type1: created at each execution. It grows, shrinks and is deleted at the end of program. ● This type of database is no longer available after program finishes its execution and is called working memory.


1 Answers

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] []
| ?- 
like image 115
CapelliC Avatar answered Nov 15 '22 10:11

CapelliC