[m-rev.] for review: fix behaviour of bimap.from_assoc_list

Ian MacLarty maclarty at cs.mu.OZ.AU
Mon Mar 7 15:23:53 AEDT 2005


Julien Fischer wrote:
> For review by anyone.
> 
> Estimated hours taken: 0.5
> Branches: main, release
> 
> Change the implementation of bimap.from_assoc_list so that
> it throws an exception if the association list does not
> implicitly define a bijection.  At the moment it is possible
> to use it to create bimaps that are not bijections.
> 

It would be better if there were a version that just failed, since then 
the user wouldn't need to check the bijective property before calling 
bimap.from_assoc_list.  This would also be more consistent with other 
conversion predicates like string.to_int which fail if the conversion is 
not possible.  I suggest the version which throws an exception should 
have the usual "det_" prefix.

> library/bimap.m:
> 	Make bimap.from_assoc_list work only with association
> 	lists that implicitly define bijections.
> 
> Julien.
> 
> Index: library/bimap.m
> ===================================================================
> RCS file: /home/mercury1/repository/mercury/library/bimap.m,v
> retrieving revision 1.16.2.2
> diff -u -r1.16.2.2 bimap.m
> --- library/bimap.m	7 Mar 2005 03:02:25 -0000	1.16.2.2
> +++ library/bimap.m	7 Mar 2005 04:10:53 -0000
> @@ -136,7 +136,10 @@
>  :- func bimap__to_assoc_list(bimap(K, V)) = assoc_list(K, V).
> 
>  	% Convert an association list to a bimap.
> -	%
> +	% Throws an exception if the association list does not implicitly
> +	% define a bijection, i.e a key or value occurs multiple times in
> +	% the association list.
> +	%
>  :- pred bimap__from_assoc_list(assoc_list(K, V)::in, bimap(K, V)::out) is det.
>  :- func bimap__from_assoc_list(assoc_list(K, V)) = bimap(K, V).
> 
> @@ -332,10 +335,8 @@
>  bimap__to_assoc_list(bimap(Forward, _), L) :-
>  	map__to_assoc_list(Forward, L).
> 
> -bimap__from_assoc_list(L, bimap(Forward, Reverse)) :-
> -	map__from_assoc_list(L, Forward),
> -	assoc_list__reverse_members(L, L1),
> -	map__from_assoc_list(L1, Reverse).
> +bimap__from_assoc_list(L, Bimap) :-
> +	bimap__det_insert_from_assoc_list(L, bimap.init, Bimap).
> 
>  bimap__from_corresponding_lists(Ks, Vs, BM) :-
>  	assoc_list__from_corresponding_lists(Ks, Vs, L),
> 
> 
> --------------------------------------------------------------------------
> mercury-reviews mailing list
> post:  mercury-reviews at cs.mu.oz.au
> administrative address: owner-mercury-reviews at cs.mu.oz.au
> unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
> subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
> --------------------------------------------------------------------------
> 

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list