Files
mercury/tests/hard_coded/myset.m
Julien Fischer 5d0acda798 Delete obsolete predicates from builtin module.
library/builtin:
    Delete the promise_only_solution/1 and promise_only_solution_io/4. Both
    have have been marked as obsolete since 2015.

    Also delete the non-public impure versions of those, get_one_solution/1
    and get_one_solution_io/4. Implementing the pure versions was the only
    use of these.

compiler/hlds_goal.m:
    Delete a reference to promise_only_solution in a comment.

tests/declarative_debugger/trust.exp:
tests/declarative_debugger/trust.inp:
tests/declarative_debugger/trust_1.m:
     Replace a call to promise_only_solution/1; this does simplify this test
     a little, but does not affect what the trust_1 module was testing, namely
     the user-defined comparison on the type exported by that module.

tests/declarative_debugger/exceptions.m:
tests/hard_coded/myset.m:
tests/hard_coded/user_compare.m:
tests/valid_seq/intermod_nested_module_bug2.m:
extras/curs/samples/nibbles.m:
     Replace calls to the now deleted predicates.
2022-04-13 15:09:21 +10:00

98 lines
2.2 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
:- module myset.
:- interface.
:- import_module list.
:- import_module io.
:- type set(T).
% convert list to set
:- func set(list(T)) = set(T).
% convert set to list
:- pred set_to_list(set(T)::in, list(T)::out) is cc_multi.
:- func set_to_sorted_list(set(T)) = list(T).
% empty set
:- func {} = set(T).
:- pred is_empty(set(T)::in) is semidet.
% singleton set
:- func {T} = set(T).
% union of two sets
:- func set(T) + set(T) = set(T).
:- mode in + in = out is det.
% union of an element and a set
:- func [T | set(T)] = set(T).
% :- mode [in | in] = out is det.
:- mode [out | out] = in is cc_nondet.
:- pred print_myset_rep(set(T)::in, io::di, io::uo) is cc_multi.
:- implementation.
:- import_module bool.
:- import_module require.
:- type set(T)
---> set_rep(list(T))
where equality is set_equal.
:- pred set_equal(set(T)::in, set(T)::in) is semidet.
set_equal(Set1, Set2) :-
set_to_sorted_list(Set1) = set_to_sorted_list(Set2).
set(List) = set_rep(List).
set_to_list(set_rep(List), List).
set_to_sorted_list(Set) = Sorted :-
promise_equivalent_solutions [Sorted] (
Set = set_rep(Unsorted),
list.sort(Unsorted, Sorted)
).
{} = set_rep([]).
{X} = set_rep([X]).
is_empty(Set) :-
promise_equivalent_solutions [Empty] (
Set = set_rep(List),
Empty = (if List = [] then yes else no)
),
Empty = yes.
Set1 + Set2 = Union :-
promise_equivalent_solutions [Union] (
Set1 = set_rep(List1),
Set2 = set_rep(List2),
list.append(List1, List2, UnionList),
Union = set_rep(UnionList)
).
% [Element | set_rep(Rest)] = set_rep([Element | Rest]).
[Element | set_rep(Rest)] = UnionSet :-
( if is_empty(UnionSet) then
fail
else
UnionSet = set_rep(UnionList),
( if UnionList = [Element1 | Rest1] then
Element = Element1,
Rest = Rest1
else
error("unexpected non-empty list")
)
).
print_myset_rep(set_rep(List), !IO) :-
print("set_rep(", !IO),
print(List, !IO),
print(")", !IO).