[m-users.] Multi predicate of entire enum
Julien Fischer
jfischer at opturion.com
Mon Jul 20 15:27:16 AEST 2020
Hi,
On Sun, 19 Jul 2020, Ace wrote:
> So say I have some enum:
>
> `:- type ex ---> a; b; c.`
>
> And I want to generate a function similar to:
>
> ```
> :- pred ex_mul(ex::out) is multi.
> ex_mul(a).
> ex_mul(b).
> ex_mul(c).
> ```
>
> Any kind of standard library predicate I can use so I don't have to
> add a new case to the predicate for every instance of the enum?
You could use RTTI to implement such a predicate, although it will be more
expensive than than the simple form above, e.g. something like:
:- pred ex_mul(ex::out) is multi.
ex_mul(V) :-
TypeDesc = type_desc.type_of(V),
NumFunctors = construct.det_num_functors(TypeDesc),
( if
int.nondet_int_in_range(0, NumFunctors - 1, FunctorNum),
Univ = construct.construct(TypeDesc, FunctorNum, [])
then
univ.det_univ_to_type(Univ, V)
else
error("should not get here")
).
Julien.
More information about the users
mailing list