如何在相互递归类型中使用deRing show()?

发布于 2025-01-22 09:40:15 字数 1321 浏览 2 评论 0原文

让我们直接查看代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2025-01-29 09:40:15

要得出漂亮打印功能,您可以使用 ppx_deriving 1 。首先,请确保您安装了它,

opam install ppx_deriving

接下来,让我们根据您的输入创建一个示例项目,

type func = string [@@deriving show]
type const = int [@@deriving show]
type reg = int [@@deriving show]
type assistop = int [@@deriving show]
type ptraddr = int [@@deriving show]

type symbol =
  | JumpDes of int
  | CallDes of func
  | StarDes of exp
[@@deriving show]

and exp =
  | Const of const
  | Symbol of symbol
  | Reg of reg
  | Assist of assistop
  | Ptr of ptraddr
  | Label of string
[@@deriving show]


let () =
  let input = StarDes (Const 1) in
  Format.printf "%s\n%a@\n" (show_symbol input) pp_symbol input

我为您在问题中未提供的类型提供了一些别名。请注意,所有类型都必须指定[@@ deriving Show]。当类型是递归时,只需将视为type。基本上,与您相同,但是我们需要使用ppx-syntax,例如[@@ deriving show]而不是ocsigen。

在程序结束时,有一个示例显示了如何使用生成的函数。有两种类型的函数,show_foo,将类型foo的值转换为字符串,而漂亮打印的函数pp_foo pp_foo prints 代码> foo 进入格式化器,由于不需要创建中间字符串,因此更快。打印机与%a指定符一起使用,并进行两个参数,即打印机本身和要打印的值。因此,无需将括号放置,也是一个小的胜利。我在示例中使用了两个选项,以便您可以对比它们。

最后,如何构建它?如果您使用Dune,则是示例Dune文件,

(executable
 (name example)
 (preprocess
  (pps ppx_deriving.show)))

您可以自己创建Dune文件或使用以下命令(假设 在示例中

dune init executable example --ppx=ppx_deriving.show

dune exec ./example.exe

我们的程序 .. 向您的 ocamlbuild 调用,例如,

ocamlbuild -package ppx_deriving.show example.native
./example.native

如果您使用其他构建系统,则会打印,

(Example.StarDes (Example.Const 1))
(Example.StarDes (Example.Const 1))

然后不要犹豫,我们将需要有关您的构建的更多信息系统或您项目的链接。如果您只是启动一个新项目,而不知道应该使用哪种构建系统,那么沙丘就是答案。


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,

opam install ppx_deriving

Next, let's create a sample project based on your input,

type func = string [@@deriving show]
type const = int [@@deriving show]
type reg = int [@@deriving show]
type assistop = int [@@deriving show]
type ptraddr = int [@@deriving show]

type symbol =
  | JumpDes of int
  | CallDes of func
  | StarDes of exp
[@@deriving show]

and exp =
  | Const of const
  | Symbol of symbol
  | Reg of reg
  | Assist of assistop
  | Ptr of ptraddr
  | Label of string
[@@deriving show]


let () =
  let input = StarDes (Const 1) in
  Format.printf "%s\n%a@\n" (show_symbol input) pp_symbol 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 treat and as type. 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 type foo into the string, and the pretty-printing function pp_foo that prints foo 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 sample dune file,

(executable
 (name example)
 (preprocess
  (pps ppx_deriving.show)))

You can create the dune file yourself or use the following command (assuming that our program is in the example.ml file),

dune init executable example --ppx=ppx_deriving.show

You can run example with,

dune exec ./example.exe

If you're using ocamlbuild instead of dune, then just add -package ppx_deriving.show to your ocamlbuild invocation, e.g.,

ocamlbuild -package ppx_deriving.show example.native
./example.native

Both will print,

(Example.StarDes (Example.Const 1))
(Example.StarDes (Example.Const 1))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文