Documentation of support for polymorphic pragma c code
Oliver Hutchison
ohutch at students.cs.mu.oz.au
Wed Jan 14 14:10:55 AEDT 1998
On Wed, 14 Jan 1998, Fergus Henderson wrote:
> On 14-Jan-1998, Oliver Hutchison <ohutch at students.cs.mu.oz.au> wrote:
> >
> > I notice that there does not seem to be any documentation about using
> > pragma c_code for polymorphic preds, but when I had a go at it I discovered
> > that the compiler does support this. Perhaps there is some uncommitted
> > documentation floating around?
>
> What's to document? It just works.
>
> > Now we have type layouts etc. this is a
> > very useful feature and unless the interface for this feature is unstable
> > it sould be documented.
>
> The interface is the same whether the predicate is monomorphic or polymorphic.
Not quite. If the pred is polymorphic c variables holding type infos for
each of the type variables will also be present. Is this the undocument
feature you mention below?
>
> If you want to get access to the type_infos, then as the documentation
> in the "Calling Mercury code from C" section says:
>
> ... `type_info' arguments can be obtained using the Mercury
> `type_of' function in the Mercury standard library module `std_util'.
>
> (Well, OK, I admit it --- there is an undocumented alternate way of obtaining
> them. However, using `type_of' works fine, and is arguably more elegant,
> so I thought it better not to document the alternative.)
>
> I suppose we could add a similar statement to the above documentation
> in the "Calling C code from Mercury" section. Perhaps something like
>
> Note that `pragma c_code' can be used for both polymorphic and
> ordinary (monomorphic) predicates. When defining polymorphic
> predicates, you may sometimes need access to the `type_info'
> values which provides information about the particular types
> passed. These can be obtained using the Mercury `type_of'
> function in the Mercury standard library module `std_util'.
> You can write a small wrapper predicate which calls `type_of'
> to obtain the type_info for its arguments and then passes the
> results to the predicate implemented using `pragma c_code'.
>
Well I guess that is ok but I don't really agree that it is a particularly
elegant way of doing it. IMHO adding the extra variables to the c_code is
the elegant/intuitive way. Using wrappers is simply forcing the programmer
to do what is already done! Given the following code (I think this is what
you have in mind?) :
:- pred fug(T1, T2).
:- mode fug(in, out) is det.
fug(A, B) :-
TypeInfo_for_T1 = type_of(A),
TypeInfo_for_T2 = type_of(B),
fug2(TypeInfo_for_T1, TypeInfo_for_T2, A, B).
:- pred fug2(type_info, type_info, T1, T2).
:- mode fug2(in, in, in, out).
:- pragma c_code(fug2(TypeInfo_for_T1::in, TypeInfo_for_T2::in, A::in,
B::in), "
do_some_stuff(TypeInfo_for_T1, TypeInfo_for_T2, A, B);
").
could be written
:- pred fug(T1, T2).
:- mode fug(in, out) is det.
:- pragma c_code(fug(A::in, B::in) ,"
do_some_stuff(TypeInfo_for_T1, TypeInfo_for_T2, A, B);
").
I know I like the second alternative. Also if you have a look at the
generated c code for each of the alternatives you will see that the second
way of doing it produces a lot less code. Oh and the first way produces a
bug in the generated c code too (The variable names TypeInfo_for_T? are
duplicated).
Oliver
More information about the developers
mailing list