首页 > 解决方案 > OCaml 编译错误:语法错误:需要模块路径

问题描述

由于谷歌上没有关于此的内容,因此我打开了此问题。

我正在尝试编译此代码:

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

我不知道为什么会发生此错误。我没有包括Clock, EthernetArp因为错误已经出现了Random。您可以在此处查看随机签名:https ://github.com/mirage/mirage-random/blob/master/src/mirage_random.ml以及我在这里包含的实现https://github.com/mirage/mirage -随机标准库

标签: functional-programmingocaml

解决方案


首先,你有一个语法错误,函子应用程序应该写成:

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

推荐阅读