[mercury-users] Converting large tables from prolog

Ralph Becket rbeck at microsoft.com
Fri Nov 26 03:22:27 AEDT 1999


In the reference manual section on implementation specific extensions, there
is a subsection on the construction of fact tables, which may be what you
want.
I believe they were intended for the sort of problem you describe.

A fact table is basically a way of optimising the representation of simple
tables.  Essentially, you put the table data (in Mercury) in some other
file,
say "foo_table.m", and then insert the following line in the host module:

:- pragma fact_table(foo/2, "foo_table.m").

These tables are optimised for rapid consultation.  The main restriction
here
is that fact table predicates may only have argument types of string, int or
float.  So you'll have to (a) find some mapping from tags to one of these
types and (b) recode your (presumably) one-to-one table foo/2 as a
one-to-many
table of the form foo/3.  That is, if

foo(String, [Tag1-Float1, Tag2-Float2, ..., TagN-FloatN]).

is a record in your current version of the table, you need to recode it as

foo(String, repn(Tag1), Float1).
foo(String, repn(Tag2), Float2).
...
foo(String, repn(TagN), FloatN).

where repn/1 is a function mapping tags to ints or strings.  Then, where in
your code you would previously have written

	foo(Str, ListOfPairs)

you'll now need to supply a predicate to consult the foo/3 table:

:- pred foo(string, list(pair(tag, float))).
:- mode foo(in, out) is cc_multi.

foo(Str, ListOfPairs) :-
	unsorted_solutions(
		(pred(tag(TagRep)-Float::out) is nondet :- foo(Str, TagRep,
Float)),
		ListOfPairs
	).

where tag/1 is a function mapping tag representations as ints (or strings)
back into their natural form.  Of course, if tags are already ints or
strings
then repn/1 and tag/1 are just the identity function.

If you want to avoid the cc_multi determinism (which you'll need to
propagate
up through your code) then you could use just plain solutions/2 rather than
unsorted_solutions/2 - the only difference being that solutions/2 sorts its
results and discards any duplicates.  You may or may not want to pay that
overhead.

Best of luck!

Ralph

> -----Original Message-----
> From: Richard Tucker [mailto:rit at cam.sri.com]
> Sent: 25 November 1999 15:37
> To: mercury-users at cs.mu.OZ.AU
> Subject: [mercury-users] Converting large tables from prolog
> 
> 
> 
> Hello from a new Mercury user!
> 
> I have a prolog program involving a large table of around 50000
> facts of the form: foo(string, list(pair(tag, float))). In prolog
> this is no problem: I just consult a file with them in, and if
> that takes too long I can save an image, which is then quite quick
> to load.
> 
> I wanted to try converting this to Mercury. Once the table is built,
> I don't need to add or remove entries, and I only ever want to use
> the mode foo(in, out), so it seems a map would be the obvious choice.
> I tried having the program build a map at by reading in all the facts
> from a file, but this is very slow -- it takes around 25 seconds on
> an ultrasparc, and it has to do it every time it starts up. I also
> tried getting the Mercury compiler to compile all 50000 clauses for
> the predicate foo, but it eventually ran out of memory. Lastly, I
> tried building a map from a file, then writing it out and attempting
> to read it back in again. This caused the detstack to overflow,
> and if I increase detstack-size then it runs out of memory.
> 
> So, what's the recommended thing to do?
> 
> Richard/
> 
> --------------------------------------------------------------
> ------------
> mercury-users mailing list
> post:  mercury-users at cs.mu.oz.au
> administrative address: owner-mercury-users at cs.mu.oz.au
> unsubscribe: Address: mercury-users-request at cs.mu.oz.au 
> Message: unsubscribe
> subscribe:   Address: mercury-users-request at cs.mu.oz.au 
> Message: subscribe
> --------------------------------------------------------------
> ------------
> 
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list