[m-users.] Programming with advanced type system

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


Am Freitag, dem 04.02.2022 um 18:07 +0100 schrieb Volker Wysk:
> 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...

I think I'm right. I have taken the source code of sort/3 from the MMC
source code library/list.m and changed the declaration the way I've
described. Now it works.

(I don't understand, however, why it must stay the same for the
hosort/6 predicate, which does the actual work...)


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/be1693ac/attachment.sig>


More information about the users mailing list