OCaml此函数适用于太多参数

otc*_*com 3 ocaml module

这只是我编写的一个简单程序,试图更好地理解模块.我正在尝试调用该toS函数,Id("a",Int)但似乎我可以编写类似这样的类型.可能是什么问题?

module Typ =
struct
  type typ = Int | Bool
end

module Symbol =
struct
  type t = string
end

module Ast =
struct
  type ast = Const of int * Typ.typ | Id of Symbol.t * Typ.typ
  let rec toS ast = match ast with Id(a,b) -> "a"
    |_->"b"
end


Ast.toS Id("a",Int)
Run Code Online (Sandbox Code Playgroud)

rgr*_*erg 6

您收到错误,因为您没有在函数应用程序中使用parens包围您的类型构造函数.但是,您还必须通过它们所定义的模块之外的完全限定名称来引用类型构造函数.即

Ast.toS (Ast.Id("a",Typ.Int))
Run Code Online (Sandbox Code Playgroud)

或者,您可以打开模块.但这被认为是不好的做法.即

open Id
open Typ
Ast.toS (Id("a",Int))
Run Code Online (Sandbox Code Playgroud)

  • 或者使用ocaml> = 3.12:__Ast.(toS(Id("a",Typ.Int)))__ (4认同)