[m-users.] Programming with advanced type system

Volker Wysk post at volker-wysk.de
Sat Feb 5 04:07:10 AEDT 2022


Hi!

I'm diving in Mercury's type system. :-) I have this type:

:- type target(P) --->
     t_wikipage(P)
   ; t_external(string)
   ; t_attachment(P, filename)
   ; t_dangling_wikipage(P)
   ; t_dangling_attachment(P, filename).


And this inst:

:- inst att == bound(  t_attachment(ground, ground)
                     ; t_dangling_attachment(ground, ground)).


Now I want to sort a list of values of type target(P), which have the inst
att. I have written a comparison predicate with this declaration:

:- pred compare_targets(
    target(P)::in(att), 
    target(P)::in(att), 
    comparison_result::out) 
is det <= pagename(P).


In module list, there is this declaration:

:- pred sort(comparison_pred(X)::in(comparison_pred), 
             list(X)::in,
             list(X)::out) 
is det.


The type and inst comparison_pred are defined in builtin as:

:- type comparison_pred(T) == pred(T, T, comparison_result).
:- inst comparison_pred(I) == (pred(in(I), in(I), out) is det).
:- inst comparison_pred == comparison_pred(ground).


Now, I want to sort a list, using my comparison predicate, but this won't
compile. I get a mode error from this code:

:- func testlist = list(target(tikipage)).
...
    list.sort(compare_targets, testlist, Sorted),


I think the reason is that in the declaration of sort/3, there the inst
comparison_pred/0 is used instead of comparison_pred/1. I think I would need
sort to be declared like this:

:- pred sort(comparison_pred(X)::in(comparison_pred(I)), 
             list(X)::in,
             list(X)::out) 
is det.


Am I right? Is there a particular reason for why list/3 is defined as it is?
This stuff is outgrowing me...


Cheers, Volker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.mercurylang.org/archives/users/attachments/20220204/ef389071/attachment.sig>


More information about the users mailing list