Mercury 中不受限制的变量名称声明

发布于 2024-12-21 15:21:22 字数 409 浏览 1 评论 0原文

我想在 Mercury 中声明一种数据类型,它可以具有可变数量的值和名称。例如:

type goal ---> pick; give; come.

具有三个变量/值。

我想要这样的东西:

type myplayer ---> de value declaration here.

变量的数量不受限制或固定。

因此,我可以使用 myplayer 来声明诸如 v1、v2、v3 和 v4 之类的值/变量。 第二次我可以使用它来声明类似以下内容:a、b、c、d、e 、z、aa、ab 和 az

值的数量不受限制,名称也不固定。

我是水星的新人。

I would like to declare a data type in Mercury that can have a variable number of values and names. For instance :

type goal ---> pick; give; come.

has three variables/values.

I want something like:

type myplayer ---> de value declaration here.

That's the number of variables are not restricted or fixed.

So I can use myplayer to declare values/variables like v1, v2, v3 and v4. The second time I can use it to declare something like: a, b, c, d, e, z, aa, ab and az.

The number of the values are not restricted and the names are also not fixed.

I am new in Mercury.

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

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

发布评论

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

评论(3

手心的海 2024-12-28 15:21:22

正如其他人所说,这在水星上根本不可能——这是故意的。

不过,如果您想要一个表达 v1 v2 v3... 等的类型,您可能想要的是:

:- type my_type
    ----> v(int).

:- func name(my_type) = string.

name(v(Num)) = formst("v%d", [i(Num)]).

该类型表达所有整数的 v,并且函数名称可用于“漂亮打印”此类型的值。

As others have said, this is simply impossible in Mercury - which is deliberate.

What you might want though, if you want a type that expresses: v1 v2 v3... etc is:

:- type my_type
    ----> v(int).

:- func name(my_type) = string.

name(v(Num)) = formst("v%d", [i(Num)]).

The type expresses v of all integers, and the function name can be used to 'pretty-print' values of this type.

情域 2024-12-28 15:21:22

你直接要求的,根本无法完成。给定

:- type question
        --->    truth(string, bool)
        ;       blank(string, string)
        ;       multiple(string, string, list(string)).

其他类型的问题只能通过在定义的位置扩展此类型并重新编译模块来添加 - 并进行许多其他更改,因为以前的确定性代码

answer(truth(_, yes)) = "true".
answer(truth(_, no)) = "false".
answer(blank(_, A)) = A.
answer(multiple(_, A, _)) = A.

在给定新问题类型时会失败。在编译时被告知您未能更新程序以反映添加“选择所有正确答案”类型的问题是您拥有问题类型的一个很好的部分原因,而不是说字符串列表 [["狐狸漂亮吗?", "true"], ["绿狐狸是____", "可爱!", "假", "虐待动物的证据"]] 您的题库。

你所要求的事情是无法完成的。然而,您真正想做的事情(您认为“类型数量可变”将是一种有用的手段)肯定可以通过其他方式来完成。我不知道那是什么方式,因为我无法从你的问题中看出你为什么要这样做。也许您会受益于阅读 受歧视的工会类型类语言参考。

What you directly ask for, simply cannot be done. Given

:- type question
        --->    truth(string, bool)
        ;       blank(string, string)
        ;       multiple(string, string, list(string)).

additional kinds of questions can only be added by extending this type where it is defined, and recompiling the module - and making a lot of other changes, too, as previously deterministic code like

answer(truth(_, yes)) = "true".
answer(truth(_, no)) = "false".
answer(blank(_, A)) = A.
answer(multiple(_, A, _)) = A.

would fail when given your new question type. Being told at compile-time where you've failed to update your program to reflect the addition of a "pick-all-the-right-answers" type of question is a good part of the reason you have a question type at all, instead of say lists of strings [["Are foxes pretty?", "true"], ["Green foxes are ____", "adorable!", "fake", "evidence of animal cruelty"]] for your question bank.

What you ask for cannot be done. However, what you actually want to do -- the end to which you thought 'variable numbers of types' would be an helpful means -- can surely be accomplished in some other way. I can't tell what way that is, as I can't tell why you wanted to do this from your question. Maybe you'd benefit from reading over discriminated unions or typeclasses in the language reference.

一花一树开 2024-12-28 15:21:22

据我了解这个问题。你想要一些类似 Prolog 的行为。即没有类型化谓词。在静态类型系统中,您始终可以通过自己处理来实现此类行为。很多年前我在Turbo Prolog中看到过这样的例子(他们用Turbo/Visual Prolog实现了ISO prolog)。

考虑类似的事情(我不确定它是否正确):

:- type any_type ---> atom_value(string)
                 ;    number_value(int)
                 ;    struct_value(any_type, list(any_type)).

guess(atom_value("v1")).
guess(atom_value("a")).
guess(atom_value("aa")).
guess(number_value(42)).
guess(struct_value(atom_value("pair"), [number_value(3), number_value(4)])).

As far as I understand this question. You want some Prolog-like behavior. I.e without typed predicates. In statically typed system you always can achieve such behavior by handling that by yourself. A lot of time ago I saw such example in Turbo Prolog (they implemented ISO prolog in terms of Turbo/Visual Prolog).

Consider something like (I'm not sure it is correct):

:- type any_type ---> atom_value(string)
                 ;    number_value(int)
                 ;    struct_value(any_type, list(any_type)).

guess(atom_value("v1")).
guess(atom_value("a")).
guess(atom_value("aa")).
guess(number_value(42)).
guess(struct_value(atom_value("pair"), [number_value(3), number_value(4)])).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文