[m-users.] Mercury to c# workflow?

Julien Fischer jfischer at opturion.com
Fri Jan 14 17:56:39 AEDT 2022


Hi David,

On Fri, 14 Jan 2022, David Epstein wrote:

> I'm still trying to understand the relationship between Mercury and C#. For example, CSharpProlog is a Prolog implementation
> written entirely in C#. After including it as a dependency in my project, I access it via a standard C# "using" statement. Then
> I can assert and retract clauses and query a live Prolog database after compilation, which can target any .NET platform
> including Android and iOS.

The relationship between Mercury and C# is this: the Mercury compiler
has backend that compiles Mercury into C# code.  You can use that
backend to compile Mercury code into  either .NET executables (or
excutable assemblies as Microsoft call them) or into DLLs that can be
called from C# (or any of the other .NET languages).  Your Mercury code
can make calls to C# code and likewise, your C# code can make calls
to Mercury code.

The details of how Mercury and C# interact are covered in sections of
the reference manual titled ``C# data passing conventions'' and
``Interfacing with C#''.  (There are some examples of how this is done
for C/C++ and Java in the samples directory of the source distribution,
but not for C# -- I will see about adding some C# specific examples.)

This has nothing to do with the ability assert and retract clauses like
in Prolog, which as others have mentioned, is not a capability that
Mercury was designed to support.  There are some alternatives mentioned
in the Prolog transition guide.

> An old Mercury flyer describes "the possibility to interoperate with
> code generated by any other compiler on the .NET platform."

That flyer describes an older code generator that compiled Mercury
directly to the Microsoft Intermediate Language (MSIL).  It is no
longer part of the current Mercury distribution.

> But, based on your previous answers, this is not like having a C#
> Prolog library, correct? There is no easy way to maintain a live
> (dynamic) Prolog database inside my application and target Android and
> iOS, right?

The approach I would use in this case is to create a Mercury library
that allows me to manipulate a database of clauses -- perhaps using
the metaprogramming facilities in the standard library's term and varset
modules -- and then export the relevant interface predicates to C#
(e.g. using pragma foreign_export for C#).  When compiled to C# the
resulting Mecury library can be called from a C# program.

Julien.


More information about the users mailing list