[m-users.] Calling type constructors from C

Zoltan Somogyi zoltan.somogyi at runbox.com
Wed Jul 10 19:19:11 AEST 2019



On Wed, 10 Jul 2019 09:46:31 +0100, emacstheviking <objitsu at gmail.com> wrote:
> Any clues in the documentation as to where I can find out how to do this?

If by "this" you mean "how to construct Mercury terms from C", the answer is:
there is no clue in the documentation, because this is not supported.

As I said a few days ago, there are a few C macros for accessing Mercury lists,
but that is all. Even those are there only because they are needed for the
implementation of other parts of the Mercury system. 

> It's just knowing what the "way" is?!?!

The standard way in cases like this is, where the C code may or may not succeed,
is to have two predicates.

The inner predicate is implemented in C, and returns two things:
the item you want to compute (in this case, a non-null pointer), and a bool that says
whether you succeeded. (The two values of bools in C are MR_TRUE and
MR_FALSE.)

The outer predicate calls the inner predicate, tests the bool, and returns either "no"
if the bool is no, or "yes(Item)" if the bool is yes. Crucially, the yes() wrapper is added
by Mercury code, not C code.

In general, the values passing through the Mercury /Cinterface fall into three categories:
(1) values of primitive types such as integers, floats, chars, strings, and bools;
(2) values of types defined in Mercury that are opaque to C, and (c) values of types
defined in C that are opaque to Mercury.

Zoltan.


More information about the users mailing list