【问题标题】:Building Multi-Module Mercury Programs构建多模块 Mercury 程序
【发布时间】:2015-01-05 16:00:32
【问题描述】:

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

【问题讨论】:

    标签: mercury


    【解决方案1】:

    我会使用下面的方法,首先用 要导出的函数或谓词或谓词(接口部分):

    % 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
    

    【讨论】:

    • 您应该可以在 Windows 上使用“mmc”命令。 (我们鼓励这样做)。
    • 谢谢@PaulBone,我改写了答案。
    【解决方案2】:

    我阅读了 Mercury 用户指南并了解了以下内容:

    $ "mmc -f module_1.m module_2.m" % 不带引号

    $ "mmake module_2.depend"

    $ "mmake module_2"

    它构建了一个可执行文件 module_2,我执行了它

    $ "./module_2"

    它工作正常。当所有其他方法都失败时,请阅读手册。

    【讨论】:

    • 我不得不说用户指南在这种方法中有点过时了,根据我作为 Mercury 初学者的经验,使用 mmc 编译器的自动依赖检测更容易利用--make 标志,正如我在回答中所展示的那样。
    • 塞巴斯蒂安是正确的。 mmake 已被弃用,当我们引入新功能时,我们不会将它们添加到 mmake。 mmc -f 没有被弃用,但它仅在您的文件名与您的模块名称不匹配时有用,或者当模块放置在当前目录以外的目录中时。谢谢。
    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 2018-01-06
    • 2013-09-08
    • 2012-06-23
    • 2020-06-20
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多