SML 中的函子结构扩展和多重归属

发布于 2024-11-29 07:55:56 字数 232 浏览 1 评论 0原文

标准机器学习中是否有任何方法可以使函子输出一个结构,该结构具有传入结构的所有功能以及任何新功能。

同理,是否可以进行多重归属?在上述情况下,它会立即有用,因为您可以将仿函数的输出归因于原始结构的签名和指定新功能的另一个签名。

我理解做这样的事情的含义,以及为什么这可能是一个坏主意。目前,我只是在函子输出中保留传入结构的副本 - 但这意味着您有一个长链“Foo.Bar.func”来访问基本功能。

谢谢

Is there any way in Standard ML to make a functor output a structure which has all of the functionality of the passed in structure, plus any new functionality.

In a similar way, is it possible to do multiple ascription? In the case of the above it would be immediately useful because you could ascribe the output of the functor to both the signature of the original structure and another signature which specifies the new functionality.

I understand the implications of doing such a thing, and why it might be a bad idea. Currently I've just been keeping a copy of the passed in structure within the functor output - but this means you have a long chain of "Foo.Bar.func" to access the base functionality.

Thanks

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

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

发布评论

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

评论(2

假装爱人 2024-12-06 07:55:56

假设我想为“TestUp”签名。有没有办法做到这一点而不将“TEST”的内容复制到新签名中?

如果我正确理解您的问题,那么您正在寻找 include 关键字,该关键字会将先前签名的定义包含到新签名中,从而使用先前的定义扩展签名。

signature TEST_EXT =
sig
  include TEST

  val beep1 : meep -> unit
end

functor TestUp_EXT(T : TEST) : TEST_EXT =
struct
  open T

  fun localFun s = beep (10, s)
  val beep1 = localFun

end


structure Test2_EXT = TestUp_EXT (Test);

Test2_EXT.beep (5, "EXT: Hi");
Test2_EXT.beep1 "Hi";

print (Int.toString (Test2.rand ()) ^ "\n");


(* This will fail as the signature doesn't define this function,
however as seen the function can easily be used within the functor as
expected *)
(* Test2_EXT.localFun "Hi"; *)

Say I wanted to make a signature for "TestUp". Is there any way to do this without duplicating the contents of the "TEST" into a new signature?

If I understand your question correctly then you are looking for the include keyword, which will include the definition of a previous signature into a new and thus extending the signature with the previous definitions.

signature TEST_EXT =
sig
  include TEST

  val beep1 : meep -> unit
end

functor TestUp_EXT(T : TEST) : TEST_EXT =
struct
  open T

  fun localFun s = beep (10, s)
  val beep1 = localFun

end


structure Test2_EXT = TestUp_EXT (Test);

Test2_EXT.beep (5, "EXT: Hi");
Test2_EXT.beep1 "Hi";

print (Int.toString (Test2.rand ()) ^ "\n");


(* This will fail as the signature doesn't define this function,
however as seen the function can easily be used within the functor as
expected *)
(* Test2_EXT.localFun "Hi"; *)
原来是傀儡 2024-12-06 07:55:56

您可以使用 open 将结构的内容放入当前范围。如果在另一个结构(或函子)中使用,它会做我相信你想要的事情。

可以在这里看到一个例子:

signature TEST =
sig
    type meep;
    val beep : int * meep -> unit;
end;

structure Test : TEST =
struct
    type meep = string

    fun beep (0, _) = ()
      | beep (n, s) = (print (s^"\n"); beep (n-1, s));
end;

functor TestUp (T : TEST) =
struct
    open T

    fun rand () = 4
end;

structure Test2 = TestUp (Test);

Test.beep (5, "Hello");

Test2.beep (5, "Hi");

print (
    Int.toString (Test2.rand ()) ^ "\n"
);

You can use open to bring the contents of a structure into the current scope. If used inside another structure (or functor), it'll do what I believe it is you want.

An example can be seen here:

signature TEST =
sig
    type meep;
    val beep : int * meep -> unit;
end;

structure Test : TEST =
struct
    type meep = string

    fun beep (0, _) = ()
      | beep (n, s) = (print (s^"\n"); beep (n-1, s));
end;

functor TestUp (T : TEST) =
struct
    open T

    fun rand () = 4
end;

structure Test2 = TestUp (Test);

Test.beep (5, "Hello");

Test2.beep (5, "Hi");

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