mercury - 构建多模块 Mercury 程序

标签 mercury

问。用于构建两个模块的 Mercury 程序的简单模板是什么? Module_1 定义并导出一个简单的函数或谓词。 Module_2 导入函数/谓词来计算有用的结果并输出结果。

最佳答案

我将使用以下方法,首先使用以下方法定义模块 您要导出的函数或谓词或谓词(接口(interface)部分):

% 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 的 shell)上编译和运行:

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

关于mercury - 构建多模块 Mercury 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825338/

相关文章:

mercury - 如何检查变量是否在 Mercury 中实例化

Mercurial 安装

haskell - 除了 Monads 之外,还有哪些其他方式可以在纯函数式语言中处理状态?

mercury - Mercury 是否支持代数谓词?

使用本地模块时 Mercury "undefined reference"编译错误

mercury - 如何显示长整数列表? ( mercury 语言)

types - 类型为 Mercury 等逻辑编程语言带来什么好处?