如何在 SML 中打印类型信息?

发布于 2024-07-17 07:57:01 字数 322 浏览 3 评论 0原文

在 SML 中执行命令后,将返回“it”,其中包含从命令返回的数据和类型。 例如:

false;  
val it = false : bool

假设我在一个程序中有一个绑定,如下所示:

val argsToOutput = 
  map (fn (Absyn.var_exp(n)) => 
         (lookupReference env n)) 
      exps

有没有一种方法可以以与打印“it”类似的方式打印“argsToOutput”(在程序中间)的值和数据类型?

After a command is executed in SML, "it" is returned which has the data and the type returned from the command. For example:

false;  
val it = false : bool

Let's say I have a binding in a program like so:

val argsToOutput = 
  map (fn (Absyn.var_exp(n)) => 
         (lookupReference env n)) 
      exps

Is there a way to print the value and datatype of "argsToOutput" (in the middle of the program) in a similar fashion to how "it" is printed out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

我做我的改变 2024-07-24 07:57:01

你可以这样做:

 val argsToOutput = (map (fn (Absyn.var_exp(n)) => 
                               (lookupReference env n)) exps)
 ...
 in
   (print (argsToOutput); 2 )
 end

你拥有 let 主体的值,而不是 2 。 print 可能会在 argsToOutput 上失败,但它会给你找到的类型,其中预期类型是字符串。

当我必须打印出数据类型的值时,我创建了一个 tostring() 函数来获取该数据类型,并将其转换为字符串。 它需要一些工作,因为它有一些嵌套的数据类型,但后来非常有价值,因为它适用于该类型的所有变量。

You could do:

 val argsToOutput = (map (fn (Absyn.var_exp(n)) => 
                               (lookupReference env n)) exps)
 ...
 in
   (print (argsToOutput); 2 )
 end

where instead of 2 you have the value of your let body. Odds are print will barf on argsToOutput, but it will give you the type found where the type expected is string.

When I have had to print out a datatype's value, I created a tostring() function that took the datatype, and transformed it into a string. It took some work because it had some nested datatypes, but was very valuable afterwards because it worked on all vars of that type.

↙温凉少女 2024-07-24 07:57:01

SML 是静态类型的,因此程序中任何值的类型在编译时都是静态已知的。 当你想“打印出来”时,我不确定你想要什么。

也许你自己看代码并不能弄清楚某个东西的类型是什么。 当您在解释器中直接定义顶层值或使用 use "whatever.sml"; 加载文件时,解释器将告诉您为该值推断的类型。

或者,如果您有一段代码,您可以将其(以及它所依赖的任何代码)发布在这里,我们可以帮助您找出其中特定值的类型。

如果要确保特定值是特定类型,可以使用类型保护:(expression : type)expression 相同,只不过在 type 期间检查它将确保该表达式是该类型。

如果您想以某种方式在运行时获取类型信息,我认为您不能,也不应该需要这样做,因为它在编译时就已经知道了。

SML is statically-typed, so the type of any value in the program is statically known at compile time. I'm not sure what you want when you want to "print it out".

Maybe you can't figure out by looking at the code yourself what the type of something is. When you define a value at the top level into the interpreter, either directly or if you load a file using use "whatever.sml";, the interpreter will tell you the type that is inferred for that value.

Alternately, if you have a piece of code, you can post it (and also any code it depends on) here and we can help you figure out what type a particular value in it has.

If you want to ensure that a particular value is a certain type, you can use type guards: (expression : type) is the same thing as expression, except that during type checking it will make sure that that expression is that type.

If you want to somehow get type information at runtime, I don't think you can, and you shouldn't need to, because it's already known at compile time.

风吹过旳痕迹 2024-07-24 07:57:01

有没有办法打印“argsToOutput”的值和数据类型(在程序中间)

否。它在顶层工作,因为编译器保留了编译阶段的类型。 它不适用于从函数体中提取的任意表达式。 但是,如果您足够幸运,拥有一个其值独立于函数的表达式,您可以将其取出并在顶层编写一个 val 绑定并获得您想要的:

- val thing = hd [1, 2, 3];
val thing = 1 : int

这是很差的安慰,因为它涵盖了一些感兴趣的案例。

我想您可能想搜索其他常见问题解答的答案:

  1. 如何对我的 ML 程序进行类型检查?
  2. 如何确定类型良好的 ML 程序中子表达式的类型?
  3. 在 ML 程序中,如何打印用于调试的值?

(答案很难看。)

Is there a way to print the value and datatype of "argsToOutput" (in the middle of the program)

No. It works at toplevel because the compiler retains the type from the compilation phase. It won't work on arbitrary expressions pulled out of function bodies. BUT if you are lucky enough to have an expression whose value is independent of the function, you can pull it out and write a val binding at top level and get what you wanted:

- val thing = hd [1, 2, 3];
val thing = 1 : int

This is poor consolation since it covers few cases of interest.

I think you may want to search for the answers to other FAQs:

  1. How can I get my ML programs to type-check?
  2. How can I be sure of the type of a subexpression in a well-typed ML program?
  3. In an ML program, how can I print a value for debugging?

(The answers are ugly.)

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