[m-users.] Using type constructors in predicate heads

Zoltan Somogyi zoltan.somogyi at runbox.com
Mon Nov 8 00:45:18 AEDT 2021


2021-11-07 15:11 GMT+11:00 "Julien Fischer" <jfischer at opturion.com>:
>> > :- type snode
>>     --->    sexp(location, list(snode))
>>     ;       list(location, list(snode))
>>     ;       map(location, list(snode))
>>     ;       tk(location,string)
>>     ;       kw(location, string)
>>     ;       s1(location,string)
>>     ;       s2(location,string)

Julien answered your question as you asked it, but there is
something else you could do to achieve your goal that falls outside
the strict parameters of your question: changing the snode type
to create single structure holding all the info about sexp's.
Specifically, you could define a new type like this:

:- type sexp_info
    --->     sexp_info(location, list(snode)).

and then update the snode type like this:

:- type snode
    ---->    sexp(sexp_info)
    ;           ....

This way, after you have tested that a variable of type snode
is bound to sexp, you could pass an sexp_info to other predicates;
you wouldn't have to pass the whole snode.

I find this solution to be better when there is a lot of code
that needs to work with the info associated with one function symbol
(sexp in this case) that does not care about the other function symbols
in that type. Of course, I don't know whether that is the case in your application.

Zoltan.


More information about the users mailing list