如何在相互递归类型中使用deRing show()?
让我们直接查看代码。
type symbol =
| JumpDes of int
| CallDes of func
| StarDes of exp (*here needs the definition of type exp*)
deriving (Show)
type exp =
| Const of const
| Symbol of symbol (*here needs the definition of type symbol*)
| Reg of reg
| Assist of assistop
| Ptr of ptraddr
| Label of string
deriving (Show)
我想使用软件包派生以打印有关两种类型的信息。根据其 document ,我只需要添加deriving(show)(show)< /代码>在我想打印的类型之后。但是我无法通过添加
和
这样的相互递归类型来定义相互递归类型:
type symbol =
| JumpDes of int
| CallDes of func
| StarDes of exp
deriving (Show)
and exp = (*note that it's line 240*)
| Const of const
| Symbol of symbol
| Reg of reg
| Assist of assistop
| Ptr of ptraddr
| Label of string
deriving (Show)
上面的编译代码会在下面获取错误:
File "type.ml", line 240, characters 16-17:
Parse error: [semi] expected after [str_item] (in [implem])
File "type.ml", line 1:
Error: Error while running external preprocessor
如果我想在相互递归类型上使用deading show(),该怎么办?感谢您的帮助!
Let's directly see the codes.
type symbol =
| JumpDes of int
| CallDes of func
| StarDes of exp (*here needs the definition of type exp*)
deriving (Show)
type exp =
| Const of const
| Symbol of symbol (*here needs the definition of type symbol*)
| Reg of reg
| Assist of assistop
| Ptr of ptraddr
| Label of string
deriving (Show)
I'd like to use package deriving to print info about the two types. According to its document, i just need to add deriving (Show)
after types i want to print. But i can not define mutually recursive types by adding and
like this:
type symbol =
| JumpDes of int
| CallDes of func
| StarDes of exp
deriving (Show)
and exp = (*note that it's line 240*)
| Const of const
| Symbol of symbol
| Reg of reg
| Assist of assistop
| Ptr of ptraddr
| Label of string
deriving (Show)
Compiling codes above will get error below:
File "type.ml", line 240, characters 16-17:
Parse error: [semi] expected after [str_item] (in [implem])
File "type.ml", line 1:
Error: Error while running external preprocessor
How should i do if i'd like to use deriving Show() on mutually recursive types? Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要得出漂亮打印功能,您可以使用 ppx_deriving 1 。首先,请确保您安装了它,
接下来,让我们根据您的输入创建一个示例项目,
我为您在问题中未提供的类型提供了一些别名。请注意,所有类型都必须指定
[@@ deriving Show]
。当类型是递归时,只需将和
视为type
。基本上,与您相同,但是我们需要使用ppx-syntax,例如[@@ deriving show]
而不是ocsigen。在程序结束时,有一个示例显示了如何使用生成的函数。有两种类型的函数,
show_foo
,将类型foo
的值转换为字符串,而漂亮打印的函数pp_foo
pp_foo prints 代码> foo 进入格式化器,由于不需要创建中间字符串,因此更快。打印机与%a
指定符一起使用,并进行两个参数,即打印机本身和要打印的值。因此,无需将括号放置,也是一个小的胜利。我在示例中使用了两个选项,以便您可以对比它们。最后,如何构建它?如果您使用
Dune
,则是示例Dune
文件,您可以自己创建
Dune
文件或使用以下命令(假设 在示例中
。
我们的程序 .. 向您的 ocamlbuild 调用,例如,
如果您使用其他构建系统,则会打印,
然后不要犹豫,我们将需要有关您的构建的更多信息系统或您项目的链接。如果您只是启动一个新项目,而不知道应该使用哪种构建系统,那么沙丘就是答案。
1)这不是唯一的选择。您还可以使用PPX_JANE的SEXP DERIVER PPX_SHOW,可能还有更多。
To derive pretty-printing functions you can use ppx_deriving1. First of all, make sure that you have it installed,
Next, let's create a sample project based on your input,
I put some aliases for the types that you didn't provide in your question. Notice that all the types had to specify
[@@deriving show]
. When a type is recursive, just treatand
astype
. Basically, the same as you did, but we need to use the ppx-syntax, e.g.,[@@deriving show]
not the Ocsigen one.At the end of the program, there is an example that shows how to use the generated functions. There are two types of function,
show_foo
which translates a value of typefoo
into the string, and the pretty-printing functionpp_foo
that printsfoo
into the formatter, which is faster since there is no need to create an intermediate string. The printers are used with the%a
specifier and take two arguments, the printer itself and the value to print. So there's no need to put parentheses, also a small win. I used both options in the example, so that you can contrast them.Finally, how to build it? If you're using
dune
, then here's the sampledune
file,You can create the
dune
file yourself or use the following command (assuming that our program is in theexample.ml
file),You can run example with,
If you're using
ocamlbuild
instead of dune, then just add-package ppx_deriving.show
to yourocamlbuild
invocation, e.g.,Both will print,
If you're using some other build system, then don't hesitate to ask, we will need more information about your build system or a link to your project. If you just starting a new project and don't know which build system you should use, then dune is the answer.
1)This is not the only option. You can also use ppx_jane's sexp deriver, ppx_show, and probably there are a few more.