mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-18 19:03:45 +00:00
Estimated hours taken: 10 Allow modules to be put in source files whose names do not directly match their the module names. When looking for the source for a module such as `foo:bar:baz', search for it first in `foo.bar.baz.m', then in `bar.baz.m', and finally in `baz.m'. compiler/prog_io.m: Change prog_io__read_module so that it returns the name of the module read, as determined by the `:- module' declaration. Add predicate `check_module_has_expected_name', for checking that this name matches what was expected. compiler/modules.m: Add read_mod_from_file, for reading a module given the file name, and generated_file_dependencies, for generating the dependencies of a module given the file name. (As opposed to the module name.) Change read_mod and read_mod_ignore_errors so that they search for `.m' files as described above, and return the name of the source file read. Also improve the efficiency of read_dependencies slightly: when reading in `.int' files, there's no need to call split_into_submodules, because we generate a seperate `.int' file for each submodule anyway. compiler/mercury_compile.m: Change the handling of command-line arguments. Arguments ending with `.m' are assumed to be file names, and other arguments are assumed to be module names. For file names, call read_mod_from_file instead of read_mod. compiler/handle_options.m: Change help message to reflect the above change to the semantics of command-line arguments. compiler/intermod.m: compiler/trans_opt.m: Fix a bug: call prog_io__read_opt_file instead of prog_io__read_module. doc/user_guide.texi: Document the above change to the semantics of command-line arguments. Update the "libraries" chapter to reflect our support for nested modules. tests/*/*.m: tests/*/*.exp: Fix a few incorrect module names in `:- module' declarations.
102 lines
1.6 KiB
Mathematica
102 lines
1.6 KiB
Mathematica
:- module queens_lib.
|
|
|
|
:- interface.
|
|
|
|
:- import_module io.
|
|
|
|
:- pred main(io__state, io__state).
|
|
:- mode main(di, uo) is cc_multi.
|
|
|
|
:- implementation.
|
|
|
|
:- import_module list, int.
|
|
|
|
main -->
|
|
( { data(Data), queen(Data, Out) } ->
|
|
print_list(Out)
|
|
;
|
|
io__write_string("No solution\n")
|
|
).
|
|
|
|
:- pred data(list(int)).
|
|
:- mode data(out) is det.
|
|
|
|
:- pred queen(list(int), list(int)).
|
|
:- mode queen(in, out) is nondet.
|
|
|
|
:- pred qperm(list(T), list(T)).
|
|
:- mode qperm(in, out) is nondet.
|
|
|
|
:- pred qdelete(T, list(T), list(T)).
|
|
:- mode qdelete(out, in, out) is nondet.
|
|
|
|
:- pred safe(list(int)).
|
|
:- mode safe(in) is semidet.
|
|
|
|
:- pred nodiag(int, int, list(int)).
|
|
:- mode nodiag(in, in, in) is semidet.
|
|
|
|
data([1,2,3,4,5]).
|
|
|
|
queen(Data, Out) :-
|
|
qperm(Data, Out),
|
|
safe(Out).
|
|
|
|
qperm([], []).
|
|
qperm([X|Y], K) :-
|
|
qdelete(U, [X|Y], Z),
|
|
K = [U|V],
|
|
qperm(Z, V).
|
|
|
|
qdelete(A, [A|L], L).
|
|
qdelete(X, [A|Z], [A|R]) :-
|
|
qdelete(X, Z, R).
|
|
|
|
safe([]).
|
|
safe([N|L]) :-
|
|
nodiag(N, 1, L),
|
|
safe(L).
|
|
|
|
nodiag(_, _, []).
|
|
nodiag(B, D, [N|L]) :-
|
|
NmB is N - B,
|
|
BmN is B - N,
|
|
( D = NmB ->
|
|
fail
|
|
; D = BmN ->
|
|
fail
|
|
;
|
|
true
|
|
),
|
|
D1 is D + 1,
|
|
nodiag(B, D1, L).
|
|
|
|
:- pred print_list(list(int), io__state, io__state).
|
|
:- mode print_list(in, di, uo) is det.
|
|
|
|
print_list(Xs) -->
|
|
(
|
|
{ Xs = [] }
|
|
->
|
|
io__write_string("[]\n")
|
|
;
|
|
io__write_string("["),
|
|
print_list_2(Xs),
|
|
io__write_string("]\n")
|
|
).
|
|
|
|
:- pred print_list_2(list(int), io__state, io__state).
|
|
:- mode print_list_2(in, di, uo) is det.
|
|
|
|
print_list_2([]) --> [].
|
|
print_list_2([X|Xs]) -->
|
|
io__write_int(X),
|
|
(
|
|
{ Xs = [] }
|
|
->
|
|
[]
|
|
;
|
|
io__write_string(", "),
|
|
print_list_2(Xs)
|
|
).
|