[mercury-users] Exchanging 'rules' between OCaml and Mercury

Paul Bone pbone at csse.unimelb.edu.au
Tue Jun 7 11:51:20 AEST 2011

On Tue, Jun 07, 2011 at 12:06:00AM +0200, Guillaume Yziquel wrote:
> Hi.
> I finally got OCaml and Mercury to cooperate. I'm now wondering what's
> the best way to exchange rather complex data structures between OCaml
> and Mercury.
> The main idea is that I have, in OCaml:
> -1- items (let's call them that way), which are indexed by long ints.
> -2- 'propositions' (i.e. OCaml discriminated union types) that expresses
> properties of the various items, and relations between items.
> -3- a handul of maps (tree-like purely functional data structures) that
> map an item to the propositions that refer to this item.
> Essentially, I have a parser written in OCaml (with Dypgen) that creates
> all these items, propositions, and maps, and I wish to feed them to some
> Mercury code.
> Right now I'm wondering what's the most reasonable way to exchange data
> between OCaml and Mercury. (By 'most reasonable', I mean nice, clean,
> readable code, not concerned about efficiency and stuff like unboxing
> yet). I'm mostly concerned about the maps.
> On one hand, it seems possible to use Mercury to use the OCaml maps by
> using foreign code and types the right from Mercury code, but this seems
> a bit cumbersome. I also see that Mercury libraries have interesting
> data structures readily available. So I'm also pondering the possibility
> of creating Mercury datastructures from OCaml. This would probably the
> cleanest solution, but I have little idea of the runtime issues
> involved, such as garbage collection.
> Another thing to consider is that items, propositions and maps are
> created incrementally by the parser, and that Mercury would be called
> quite frequently during this incremental parsing. (The datastructures
> involved are purely functional and immutable, though).
> So how would you do you live in harmony with the Mercury GC when
> building Mercury data structures from foreign code and then invoking
> Mercury code from foreign code on these data structures? Is there any
> example of such code lying around on the net?

If you want to pass the data back and forth you might want to choose a
particular format and then write code, possibly in both OCaml and Mercury that
reads and writes this 'canonical' format.

This first option can be extended.  You could write code that writes data
structures and readers/writers for either language when given a description of
the data structure.  To save you some time you could pick the Mercury format
for the canonical representation and take advantage of io.read and io.write -
OCaml probably also has an equivalent.  This approach also works very well for
network communication but might be overkill for your intended purpose.

Alternatively, if you just care about getting the data into Mercury, running
some query and getting a simple answer out.  Then I'd suggest exporting
predicates from Mercury that help you construct the data you like and calling
those from OCaml.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
URL: <http://lists.mercurylang.org/archives/users/attachments/20110607/98e86a39/attachment.sig>

More information about the users mailing list