创建调用 OCaml 或 Haskell 的 GUI 桌面应用程序——这是一件傻事吗?

发布于 2024-08-30 06:04:01 字数 167 浏览 3 评论 0原文

在 Haskell 和 OCaml 中,都可以从 C 程序调用该语言。为 Windows、Mac 或 Linux 创建广泛使用此技术的本机应用程序有多可行?

(我知道有像 wxHaskell 这样的 GUI 库,但假设有人只想用外语编写应用程序逻辑的一部分。)

或者这是一个糟糕的主意?

In both Haskell and OCaml, it's possible to call into the language from C programs. How feasible would it be to create Native applications for either Windows, Mac, or Linux which made extensive use of this technique?

(I know that there are GUI libraries like wxHaskell, but suppose one wanted to just have a portion of your application logic in the foreign language.)

Or is this a terrible idea?

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

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

发布评论

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

评论(5

烟柳画桥 2024-09-06 06:04:01

嗯,主要的风险是,虽然设施存在,但它们没有经过充分的测试——没有很多应用程序这样做。从 C 调用 Haskell 应该不会有太多麻烦,看起来很简单:

http://www.haskell.org /haskellwiki/Calling_Haskell_from_C

我想说的是,如果有一些令人信服的理由使用 C 作为前端(例如,您有一个遗留应用程序)并且您确实需要一个 Haskell 库,或者出于其他原因想要使用 Haskell ,那么,是的,去做吧。主要风险在于没有很多人这样做,因此与调用其他方式相比,文档和示例更少。

Well, the main risk is that while facilities exist, they're not well tested -- not a lot of apps do this. You shouldn't have much trouble calling Haskell from C, looks pretty easy:

http://www.haskell.org/haskellwiki/Calling_Haskell_from_C

I'd say if there is some compelling reason to use C for the front end (e.g. you have a legacy app) and you really need a Haskell library, or want to use Haskell for some other reason, then, yes, go for it. The main risk is just that not a lot of people do this, so less documentation and examples than for calling the other way.

烟若柳尘 2024-09-06 06:04:01

您也可以将 OCaml 嵌入到 C 中(请参阅手册 ),尽管这并不像用 C 扩展 OCaml 那样常见。

You can embed OCaml in C as well (see the manual), although this is not as commonly done as extending OCaml with C.

丑丑阿 2024-09-06 06:04:01

我相信,即使 GUI 和逻辑都是用同一种语言编写的,最好的方法是运行两个进程,通过人类可读的、基于文本的协议(某种 DSL)进行通信。该架构也适用于您的情况。

优点是显而易见的:GUI是可拆卸和可替换的,自动化测试更容易,日志记录和调试更容易。

I believe that the best approach, even if both GUI and logic are written in the same language, is to run two processes which communicates via a human-readable, text-based protocol (a DSL of some sort). This architecture applies to your case as well.

Advantages are obvious: GUI is detachable and replaceable, automated tests are easier, logging and debugging are much easier.

演出会有结束 2024-09-06 06:04:01

我通过编译在 Haskell 外部调用的 haskell 共享库来广泛利用这一点。

通常涉及的任务是

  1. 创建正确的外部导出声明,
  2. 为您需要编组的任何数据类型创建可存储实例,
  3. 创建 C 结构(或您正在使用的语言中的结构)来读取此信息,
  4. 因为我不想手动初始化 haskell RTS,我将初始化/终止代码添加到库本身。 (dllmain in windows __attribute__ ((constructor)) on unix)
  5. 因为我不再需要它们中的任何一个,所以我创建了一个 .def 文件来隐藏所有闭包和 rts 函数,使其不出现在导出表(windows)中,
  6. 使用 GHC 来编译所有内容 这些任务相当

机械化且结构化,以至于您可以编写一些东西来自动化它们。事实上,我自己使用的是我创建的一个工具,它对您标记为导出的函数进行依赖项跟踪,它会将它们包装起来并为您编译共享库,同时为您提供 C/C++ 中的声明。

(不幸的是,这个工具还没有被破解,因为在我习惯这样做之前,我仍然需要修复和测试更多的东西)

工具可以在这里使用 http://hackage.haskell.org/package/Hs2lib-0.4.8

I make extensive use of this by compiling haskell shared libs that are called outside Haskell.

usually the tasks involved would be to

  1. create the proper foreign export declarations
  2. create Storable instances for any datatypes you need to marshal
  3. create the C structures (or structures in the language you're using) to read this information
  4. since I don't want to manually initialize the haskell RTS, i add initiallisation/termination code to the lib itself. (dllmain in windows __attribute__ ((constructor)) on unix)
  5. since I no longer need any of them, I create a .def file to hide all the closure and rts functions from being in the export table (windows)
  6. use GHC to compile everything together

These tasks are rather robotic and structured, to a point you could write something to automate them. Infact what I use myself to do this is a tool I created which does dependency tracing on functions you marked to be exported, and it'll wrap them up and compile the shared lib for you along with giving you the declarations in C/C++.

(unfortunately, this tool is not yet on hackage, because there is something I still need to fix and test alot more before I'm comfortable doing so)

Tool is available here http://hackage.haskell.org/package/Hs2lib-0.4.8

听风念你 2024-09-06 06:04:01

或者这是一个糟糕的主意吗?

这根本不是一个可怕的主意。但正如唐·斯图尔特指出的那样,这可能是一条少有人走的路。你当然可以以 Haskell 或 OCaml 的形式启动你的程序,然后让它从一开始就进行外部函数调用——我建议你以这种方式构建你的代码——但这并不能改变更多人调用的事实从 Haskell 到 C 比从 C 到 Haskell 好。 OCaml 也是如此。

Or is this a terrible idea?

It's not a terrible idea at all. But as Don Stewart notes, it's probably a less-trodden path. You could certainly launch your program as Haskell or OCaml, then have it do a foreign-function call right out of the starting gate—and I recommend you structure your code that way—but it doesn't change the fact that many more people call from Haskell into C than from C into Haskell. Likewise for OCaml.

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