[mercury-users] Fw: Questions on Mercury in .NET, please help.

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Dec 3 17:14:13 AEDT 2002


Jing Yang <jyang at cs.umn.edu> wrote:
> I'm currently working on a project which involves looking into
> your Mercury compilation to .NET CLR. We're wondering whether
> it is possible that a C# program can call a Mercury program in
> .NET?

Yes.

The details of this direction of the Mercury/C# interface are not yet
officially documented in the Mercury language reference manual and may be
subject to change in future versions of Mercury.  So if you write C# code
which calls Mercury, it is possible that it may need to change to work
with future versions of the Mercury implementation.  But any such changes
are likely to be minor and if it does turn out to be necessary to modify
the your code to work with new versions of the Mercury implementation,
I expect that such changes will be easy to make.

(The details of how to call C# from Mercury *are* documented in the
Mercury language reference manual.)

> If the answer is yes, would you please let me know how it
> works, and is there any published technical paper on this I can
> read? Or is there any related online documentation available on
> this?

The simplest way to call Mercury code from C# code is to compile the
Mercury code to a .NET DLL (e.g. using `mmake --grade il foo.dll'), run
ILDASM on the generated DLL to view the interface, and write C# code which
calls that interface. 

At this level, the technique is the same as for interfacing C# with
any other language implementation that generates the .NET CLR IL.
(It's not documented in the Mercury documentation since it's not
specific to Mercury!)  You need to pass the name of the DLL that you
are interfacing as an option to the C# compiler; such options should be
documented in your C# compiler's documentation.  The overall approach
of storing meta-data which describes the interface in the DLL file is
described in Microsoft's documentation for .NET.

The drawback with this approach is that the C# code that you write will
need to depend on some details of the Mercury implementation such as the
way in which Mercury names get mangled when converted to MSIL.

Another alternative which works in the current (release-of-the-day)
implementation of Mercury is to use `pragma export' to export the
Mercury procedures.  For example:

	:- pragma export(list__append(in, in, out), "list_append").

This is the same construct described in the "C interface" chapter of
the Mercury reference manual.  In our current implementation, this also
works for the .NET back-end, with the semantics being slightly different
(it exports to C# rather than C).  However, in a future version of the
Mercury compiler I would like change the semantics for this syntax to be
the same on the .NET back-end as they are on the C back-end (i.e. export
to C), and use a different syntax for exporting to C#.  That's the main
reason why this feature is not documented in the Mercury language
reference manual.

Using `pragma export' in your Mercury code allows you to specify the
.NET name used, avoiding the need to depend on the details of the
current Mercury implementation's name mangling.  Apart from that,
the technique is exactly the same as the method described earlier.
The mapping from Mercury data types to C# data types is documented
in the Mercury language reference manual.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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