【问题标题】:OCaml compiling Error: Syntax error: module path expectedOCaml 编译错误:语法错误:需要模块路径
【发布时间】:2020-08-13 01:33:20
【问题描述】:

由于谷歌上没有这方面的内容,我打开了这个问题。

我正在尝试编译这段代码:

module Random: Mirage_random.S = struct 
  include Mirage_random_stdlib
end

module Ipv4: Static_ipv4.Make(Random, Clock, Ethernet, Arp) = struct
  include Static_ipv4
end

但我明白了:

root@66f08fd7c55b:/workspaces/ocaml_env/mirage-tcpip/examples/raw_ip_tcp_example# dune build raw_ip_tcp_example.exe
Entering directory '/workspaces/ocaml_env/mirage-tcpip'
File "examples/raw_ip_tcp_example/raw_ip_tcp_example.ml", line 44, characters 36-37:
44 | module Ipv4: Static_ipv4.Make(Random, Clock, Ethern
                                         ^
Error: Syntax error: module path expected.

你可以在这里看到static_ipv4文件https://github.com/mirage/mirage-tcpip/blob/master/src/ipv4/static_ipv4.mli#L17

我不知道为什么会发生此错误。我没有包括ClockEthernetArp,因为错误已经在Random 上。你可以在这里看到随机签名:https://github.com/mirage/mirage-random/blob/master/src/mirage_random.ml 和我在这里包含的实现https://github.com/mirage/mirage-random-stdlib

【问题讨论】:

    标签: functional-programming ocaml


    【解决方案1】:

    我根本不了解 Mirage,但通常 Make 是一个函子。即,它将模块映射到模块。但是你在模块类型的句法位置调用。

    我希望有更多这样的东西:

    module Ipv4 = Static_ipv4.Make(. . .)
    

    如果这没有帮助,我深表歉意。

    【讨论】:

    • 谢谢,我要试试。但是如果我真的想创建一个覆盖函数的模块呢?这就是我制作module Ipv4: Static_ipv4.Make(...) 的原因。我认为那是不可能的吗?我需要创建moduleIpv4Base = Static_ipv4.Make(...),然后创建module Ipv4: moduleIpv4Base = struct ...
    【解决方案2】:

    首先,你有一个语法错误,应该编写仿函数应用程序:

    Static_ipv4.Make(Random)(Clock)(Ethernet)(Arp)
    

    然后你有一个错误:Static_ipv4.Make(Random)(Clock)(Ethernet)(Arp) 是一个模块表达式,而不是一个模块类型。此外,尚不清楚您是否甚至需要签名约束。简单写

    module Ipv4 = struct
      include Static_ipv4
      let more = 0
    end
    

    如果您想制作 Static_ipv4 模块的扩展版本,则可以使用。

    但也许,您想在函子结果中添加一些函数? 在这种情况下,您可以使用:

    module Ipv4 = struct
      include Static_ipv4.Make(Random)(Clock)(Ethernet)(Arp)
      let an_new_and_shiny_function = ()
    end
    

    如果你真的想强制类型相同,你需要重用仿函数结果的签名:

    module Ipv4: sig
      include Mirage_protocols.IP with type ipaddr = Ipaddr.V4.t
      val connect : ip:(Ipaddr.V4.Prefix.t * Ipaddr.V4.t) -> ?gateway:Ipaddr.V4.t ->
      end
     = struct
      include Static_ipv4.Make(Random)(Clock)(Ethernet)(Arp)
      let an_new_and_shiny_function.
    end
    

    【讨论】: