Building Multi-Module Mercury Programs

末鹿安然 提交于 2019-12-24 02:07:42

问题


Q. What's a simple template for building a two-module mercury program? Module_1 defines and exports a simple function or predicate. Module_2 imports the function/predicate to compute a useful result and outputs the result.


回答1:


I would use the following approach, first define the module with the function(s) or predicate(s) or predicates you want to export (interface section):

% File: gcd.m

:- module gcd.

:- interface.
:- import_module integer.

:- func gcd(integer, integer) = integer.

:- implementation.

:- pragma memo(gcd/2).
gcd(A, B) = (if B = integer(0) then A else gcd(B, A mod B)).

The file using the exported function in the gcd module (gcd/2):

% File: test_gcd.m

:- module test_gcd.

:- interface.

:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.

:- import_module char.
:- import_module gcd.
:- import_module integer.
:- import_module list.
:- import_module std_util.
:- import_module string.

main(!IO) :-
    command_line_arguments(Args, !IO),
    ArgToInteger = integer.det_from_string `compose` list.det_index0(Args),

    A = ArgToInteger(0),
    B = ArgToInteger(1),

    Fmt = (func(Integer) = s(integer.to_string(Integer))),
    GCD = gcd(A, B),
    io.format("gcd(%s, %s) = %s\n", list.map(Fmt, [A, B, GCD]), !IO).

To compile and run on Windows (cmd.exe): Please note that mmc is also a Windows system command, so please use the Mercury environment provided by the Mercury distribution installer:

> mmc --use-grade-subdirs -m test_gcd
> test_gcd 12 4

To compile and run on Linux/MacOS/etc (any Bash-like shell):

$ mmc --use-grade-subdirs -m test_gcd
$ ./test_gcd 12 4



回答2:


I read the Mercury User's Guide and learned the following:

$ "mmc -f module_1.m module_2.m" % without the quotes

$ "mmake module_2.depend"

$ "mmake module_2"

It built an executable, module_2, which I executed

$ "./module_2"

and it worked properly. When all else fails, read the manual.



来源:https://stackoverflow.com/questions/26825338/building-multi-module-mercury-programs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!