Files
mercury/tests/hard_coded/one_member.m
Zoltan Somogyi 2bd7c5ee3e Rename X's aux modules as X_helper_N in hard_coded.
tests/hard_coded/*.m:
    Rename modules as mentioned above.

    In a few cases, where the main module's name itself had a suffix,
    such as "_mod_a" or "_main", remove that suffix. This entails
    renaming the .exp file as well. (In some cases, this meant that
    the name of a helper module was "taken over" by the main module
    of the test case.)

    Update all references to the moved modules.

    General updates to programming style, such as

    - replacing DCG notation with state var notation
    - replacing (C->T;E) with (if C then T else E)
    - moving pred/func declarations to just before their code
    - replacing io.write/io.nl sequences with io.write_line
    - replacing io.print/io.nl sequences with io.print_line
    - fixing too-long lines
    - fixing grammar errors in comments

tests/hard_coded/Mmakefile:
tests/hard_coded/Mercury.options:
    Update all references to the moved modules.

    Enable the constant_prop_int test case. The fact that it wasn't enabled
    before is probably an accident. (When constant_prop_int.m was created,
    the test case was added to a list in the Mmakefile, but that list
    was later removed due to never being referenced.)

tests/hard_coded/constant_prop_int.{m,exp}:
    Delete the calls to shift operations with negative shift amounts,
    since we have added a compile-time error for these since the test
    was originally created.
2023-06-16 08:33:22 +02:00

129 lines
3.4 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
:- module one_member.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module list.
:- import_module solutions.
:- type set_ctree234(T)
---> ct(int, set_tree234(T))
where equality is one_member.equal,
comparison is one_member.compare_total_order.
:- type set_tree234(T)
---> empty
; two(T, set_tree234(T), set_tree234(T))
; three(T, T, set_tree234(T), set_tree234(T), set_tree234(T))
; four(T, T, T, set_tree234(T), set_tree234(T),
set_tree234(T), set_tree234(T)).
main(!IO) :-
Set = ct(4, two("two", two("one", empty, empty),
three("three", "four", empty, empty, empty))),
solutions(one_member(Set), Solns),
io.write(Solns, !IO),
io.nl(!IO).
:- pred one_member(set_ctree234(T)::in, T::out) is nondet.
one_member(Set, Item) :-
promise_equivalent_solution_sets [Item] (
arbitrary [Tree] Set = ct(_, Tree),
do_one_member(Tree, Item)
).
:- pred do_one_member(set_tree234(T)::in, T::out) is nondet.
do_one_member(empty, _) :- fail.
do_one_member(two(E0, T0, T1), E) :-
(
E = E0
;
do_one_member(T0, E)
;
do_one_member(T1, E)
).
do_one_member(three(E0, E1, T0, T1, T2), E) :-
(
E = E0
;
E = E1
;
do_one_member(T0, E)
;
do_one_member(T1, E)
;
do_one_member(T2, E)
).
do_one_member(four(E0, E1, E2, T0, T1, T2, T3), E) :-
(
E = E0
;
E = E1
;
E = E2
;
do_one_member(T0, E)
;
do_one_member(T1, E)
;
do_one_member(T2, E)
;
do_one_member(T3, E)
).
:- pred equal(set_ctree234(T)::in, set_ctree234(T)::in) is semidet.
equal(SetA, SetB) :-
promise_equivalent_solution_sets [] (
arbitrary [SizeA, TreeA] SetA = ct(SizeA, TreeA),
arbitrary [SizeB, TreeB] SetB = ct(SizeB, TreeB),
SizeA = SizeB,
do_to_sorted_list(TreeA, [], ListA),
do_to_sorted_list(TreeB, [], ListB),
ListA = ListB
).
:- pred compare_total_order(comparison_result::uo,
set_ctree234(T)::in, set_ctree234(T)::in) is det.
compare_total_order(Result, SetA, SetB) :-
promise_equivalent_solution_sets [Result] (
arbitrary [SizeA, TreeA] SetA = ct(SizeA, TreeA),
arbitrary [SizeB, TreeB] SetB = ct(SizeB, TreeB),
( if SizeA = SizeB then
do_to_sorted_list(TreeA, [], ListA),
do_to_sorted_list(TreeB, [], ListB),
compare(Result, ListA, ListB)
else
compare(Result, SizeA, SizeB)
)
).
:- pred do_to_sorted_list(set_tree234(T)::in, list(T)::in, list(T)::out)
is det.
do_to_sorted_list(empty, L, L).
do_to_sorted_list(two(E0, T0, T1), L0, L) :-
do_to_sorted_list(T1, L0, L1),
do_to_sorted_list(T0, [E0 | L1], L).
do_to_sorted_list(three(E0, E1, T0, T1, T2), L0, L) :-
do_to_sorted_list(T2, L0, L1),
do_to_sorted_list(T1, [E1 | L1], L2),
do_to_sorted_list(T0, [E0 | L2], L).
do_to_sorted_list(four(E0, E1, E2, T0, T1, T2, T3), L0, L) :-
do_to_sorted_list(T3, L0, L1),
do_to_sorted_list(T2, [E2 | L1], L2),
do_to_sorted_list(T1, [E1 | L2], L3),
do_to_sorted_list(T0, [E0 | L3], L).