[mercury-users] data base interface

Fergus Henderson fjh at cs.mu.oz.au
Sat Oct 4 14:19:32 AEST 1997


Simon Taylor wrote:
> 
> Tom Howland wrote:
> > I wonder if there is a way to get Mercury to treat queries to an external
> > database as a nondeterminate predicate, and yet arrange things so that
> > cursors get cleaned up.
> 
> To treat an SQL database relation as a predicate, you would need to generate 
> interface (.int) files containing declarations for the predicates you want 
> to access.

Actually you would probably generate the declarations in a `.m' file,
and let the Mercury compiler generate the `.int' file for you.

> You would also need to generate procedures to interface to the 
> database to send the query and get the results.  [...]
> Since the C interface does not yet support non-deterministic procedures, 
> you would need some knowledge about the C code the compiler generates for 
> non-deterministic procedures to implement the interface.

Not necessarily.

You could use a deterministic interface that returns a list, or better yet
some lazy representation of a list (e.g. a database cursor), and then wrap
a simple Mercury predicate around this to turn it into a nondeterminstic
interface.

For example:

	:- interface.
	:- pred p(string::out) is nondet.

	:- implementation.
	p(X) :-
		db_get_cursor("p", Cursor),
		find_answers(Cursor, X).

	:- pred find_answers(cursor::in, string::out) is nondet.
	find_answers(Cursor, X) :-
		get_next_solution(Cursor, FirstX, NextCursor),
		( X = FirstX
		; find_answers(NextCursor, X)
		).

	% implemented using `pragma c_code':
	:- pred db_get_cursor(string::in, cursor::out) is det.
	:- pred get_next_solution(cursor::in, string::out, cursor::out) is det.

This is a slightly misleading interface, because the implementation
of these procedures will depend on the current state of the database.
However, if the database remains unchanged for the duration of the
program, that should not be a problem.

That just leaves the problem of cleaning up the database cursors.
I'm afraid there is no way of doing that in 0.7.
However, the latest beta-release does provide just what you need.
See the section on "function trailing" in the language reference
manual in the latest beta-release.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the users mailing list