【发布时间】:2015-01-05 16:00:32
【问题描述】:
问。什么是构建两模块水星计划的简单模板? Module_1 定义并导出一个简单的函数或谓词。 Module_2 导入函数/谓词以计算有用的结果并输出结果。
【问题讨论】:
标签: mercury
问。什么是构建两模块水星计划的简单模板? Module_1 定义并导出一个简单的函数或谓词。 Module_2 导入函数/谓词以计算有用的结果并输出结果。
【问题讨论】:
标签: mercury
我会使用下面的方法,首先用 要导出的函数或谓词或谓词(接口部分):
% 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)).
gcd模块中使用导出函数的文件(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).
在 Windows 上编译和运行 (cmd.exe): 请注意 mmc 也是一个 Windows 系统命令,所以请使用 Mercury 分发安装程序提供的 Mercury 环境:
> mmc --use-grade-subdirs -m test_gcd
> test_gcd 12 4
在 Linux/MacOS/etc(任何 Bash-like shell)上编译和运行:
$ mmc --use-grade-subdirs -m test_gcd
$ ./test_gcd 12 4
【讨论】:
我阅读了 Mercury 用户指南并了解了以下内容:
$ "mmc -f module_1.m module_2.m" % 不带引号
$ "mmake module_2.depend"
$ "mmake module_2"
它构建了一个可执行文件 module_2,我执行了它
$ "./module_2"
它工作正常。当所有其他方法都失败时,请阅读手册。
【讨论】:
mmc 编译器的自动依赖检测更容易利用--make 标志,正如我在回答中所展示的那样。