[m-users.] Generic predicate for enumeration types

Philippe de Rochambeau phiroc at free.fr
Sat May 15 03:44:44 AEST 2021


Hello,

I’ve found a way to implement my code:

:- module animals1b.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module string, list, int.

:- type kinds ---> cat; dog.
:- type pet
    --->    pet(
                name :: string,
                size :: int,
                kind :: kinds
            ).

:- func large_lower_bound = int.
large_lower_bound = 34.

:- func relative_size(pet) = string.
relative_size(pet(_, Size, cat)) = (if Size > large_lower_bound then "large cat" else "small cat").
relative_size(pet(_, Size, dog)) = (if Size > large_lower_bound then "large dog" else "small dog").

main(!IO) :-
    foldl(animals1b.print, Animals, !IO),
    Animals = [
        pet("Bill", 26, cat),
        pet("Harry", 27, cat),
        pet("Mary", 33, cat),
        pet("Steve", 31, cat),
        pet("Fido", 26, dog),
        pet("Fred", 33, dog),
        pet("Henri", 44, dog),
        pet("Jane", 55, dog),
        pet("Rover", 45, dog),
        pet("Tom", 68, dog)
    ].

:- pred print(pet::in, io::di, io::uo) is det.
print(P, !IO) :-
    io.format("%s is sized %d: it's a %s.\n",
        [s(P^name), i(P^size), s(relative_size(P))], !IO).






> Le 14 mai 2021 à 13:22, Philippe de Rochambeau <phiroc at free.fr> a écrit :
> 
> Hello,
> 
> I would like to create a predicate called small which works for the cat and dog types show below.
> 
> I’ve tried to create a generic predicate, but to no avail:
> 
> :- pred cat(T::out) is multi.
> cat(X) :- small(X).
> 
> 
> Any help would be much appreciated.
> 
> Many thanks.
> 
> Philippe
> 
> -----------------------------------------------------------------------------------
> 
> :- implementation.
> 
> :- type cat
>         --->  bill
>         ;     harry
>         ;     mary
>         ;     steve.
> 
> :- type dog
>         --->  fido
>         ;     fred
>         ;     henry
>         ;     jane
>         ;     rover
>         ;     tom.
> 
> :- pred cat(cat::out) is multi.
> cat(Cat) :- small(Cat).
> 
> :- pred dog(dog::out) is multi.
> dog(Dog) :- small(Dog).
> 
> :- pred small(cat).
> :- mode small(in) is semidet.
> :- mode small(out) is multi.
> small(steve).
> small(harry).

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20210514/99a7a29f/attachment.html>


More information about the users mailing list