论坛首页 编程语言技术论坛

用 haskell 扩展 ruby

浏览 6378 次
精华帖 (6) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-06-12  
makedll.rb 还有改进余地,使用 hsFFI.h 和生成的 .h 可以更准确的产生 interface。
不改睡觉了……
0 请登录后投票
   发表时间:2009-06-12   最后修改:2009-06-12
night_stalker 写道
哇咔咔,修改后的版本自动产生 interface.rb, 还是 Ruby 方便吧?



这里还没牵涉C和C++的那些古怪结构和方法啊,这种是最简单的,花点时间都可以写出自动版。


比如SDK里经常看到的双输出,或者三输出的格式,返回值是boolean,参数里还有多个带有out的输出。还有一些特别奇怪的Struct结构,这些才是头疼的地方。


这个东东用C#写就比较信手了

[dllimport("xxx.dll)]
public extern static int add(int a ,int b);


几乎和C就是一样的,一次声明,到处使用。


ghc最神奇的地方是它可以调整stack?? 我用editbin修改C#的stack,怎么也到不了它那种让人发指的地步。(比如上次算梅森数)。

打成dll之后,这个好处就没了吧。。要是ghc能调用xml-rpc就好
0 请登录后投票
   发表时间:2009-06-12   最后修改:2009-06-12
ruby DL 声明的函数签名和 C 是完全一致的,再用上头文件的类型定义,可以做得很准……
比起 swig,至少这样生成的 interface 是人类可读的……

ray_linn 写道
打成dll之后,这个好处就没了吧。。要是ghc能调用xml-rpc就好


makedll.rb 修改,可以添加 RTS 选项了。
http://www.haskell.org/ghc/docs/latest/html/users_guide/runtime-control.html
引用
4.14.7. “Hooks” to change RTS behaviour

GHC lets you exercise rudimentary control over the RTS settings for any given program, by compiling in a “hook” that is called by the run-time system. The RTS contains stub definitions for all these hooks, but by writing your own version and linking it on the GHC command line, you can override the defaults.

Owing to the vagaries of DLL linking, these hooks don't work under Windows when the program is built dynamically.

The hook ghc_rts_optslets you set RTS options permanently for a given program. A common use for this is to give your program a default heap and/or stack size that is greater than the default. For example, to set -H128m -K1m, place the following definition in a C source file:

char *ghc_rts_opts = "-H128m -K1m";

Compile the C file, and include the object file on the command line when you link your Haskell program.

These flags are interpreted first, before any RTS flags from the GHCRTS environment variable and any flags on the command line.

You can also change the messages printed when the runtime system “blows up,” e.g., on stack overflow. The hooks for these are as follows:

void OutOfHeapHook (unsigned long, unsigned long)

    The heap-overflow message.
void StackOverflowHook (long int)

    The stack-overflow message.
void MallocFailHook (long int)

    The message printed if malloc fails.

For examples of the use of these hooks, see GHC's own versions in the file ghc/compiler/parser/hschooks.c in a GHC source tree.


譬如设了 -K10m,用以下简单代码 + 任务管理器可以明显看到占用内存不同
i = gets
require 'interface.rb'
i = gets



xml-rpc 这种基本的库应该是有的 ...
http://www.haskell.org/haxr/
http://www.haskell.org/haxr/haskell-xml-rpc.pdf

haskell 的文档和 paper 相当的丰富和高质量 
SPJ 和梅姐这帮牛人,有微软养着,每天过着神仙一般的生活,看来是闲得要命 ……
0 请登录后投票
   发表时间:2009-06-12   最后修改:2009-06-12
总的看起来还是C#的方便些,最方便最快捷的当然是managed C++的IJW。对于简单的东西,也可以code generate.

ruby的win32api看起来就很笨重。。。


我觉得把haskell放在另一个进程中更方便,通过一个mini xml rpc server进行通讯,这是我经常在python里干的事情。
0 请登录后投票
   发表时间:2009-06-19  
楼主的帖子好有意思,拜读了。

我想继续问一下楼主,haskell自然是强悍的,不过ruby本身受haskell影响,有不少functional programming的特制,这种情况下是否真的有必要写haskell extension?我的意思是效率不好的部分我们可以写c extension,而haskell作为functional language的优点很多都可以在ruby内消化。楼主是否能举一两个例子来证明一下haskell extension在某些情况下,就算极端一点也不要紧,是比c extension有优势的?
0 请登录后投票
   发表时间:2009-06-19   最后修改:2009-06-19
haskell 是静态强类型而且带 GC,编译器优化也很 nb,另外不用考虑边界条件,方便表达复杂的算法。
C 是弱类型的,而且单元测试不好写,就算写了测试,也不一定保证代码正确。而 haskell 基本能跑就已经很大程度上保证了正确性 ……

譬如这段给两个用户的首选项评分的算法(它是写在一个 module 中让数据集 extend 的):
  # euclidean distance score
  def eds p1, p2
    # preferences of p1 and p2
    p1p, p2p = self[p1], self[p2]
    (p1p.keys & p2p.keys).inject 0 do |acc, key|
      acc + (p1p[key] - p2p[key]) ** 2
    end
  end


如果写成 C 会很痛苦 …… 写成 Haskell 就容易一些:
uniq :: Eq a => [a] -> [a]
uniq = foldr (\x acc -> if elem x acc then acc else (x:acc)) []

eds prefs p1 p2 = sum $ map squarediff all_keys
	where
		kv l k = [v | (k,v) <- l] !! 0
		pref1 = prefs `kv` p1
		pref2 = prefs `kv` p2
		squarediff k = ((pref1 `kv` k) - (pref2 `kv` k)) ^ 2
		all_keys = uniq $ (map fst pref1) ++ (map fst pref2)


另外还有些其它好处,像事务内存、Parser、socket 通信等功能,用 C 是不容易做的 ……
还有就是现在一些论文喜欢用 haskell 描述算法 ……
0 请登录后投票
   发表时间:2009-06-19  
非常有说服力,再次感谢。

感觉现在haskell差不多快成functional programming的de facto standard了,我只对scheme有粗浅了解,不知道这样的基础想要深入了解haskell需要多少时间?

不真正了解functional language总觉得基础知识缺了一块
0 请登录后投票
   发表时间:2009-06-19  
check 写道
非常有说服力,再次感谢。

感觉现在haskell差不多快成functional programming的de facto standard了,我只对scheme有粗浅了解,不知道这样的基础想要深入了解haskell需要多少时间?

不真正了解functional language总觉得基础知识缺了一块


怎么才算深入了解呢…… 我从初了解到现在两个多月,连 template 都不会用,但不影响用 haskell 解决一些问题。
指望马上用它做功利的事情基本行不通,至少我没见过现在国内有招 haskell 的 ……

推荐有趣的learn you a haskell for greate good,详尽文档参考请到 haskell.org 和 hoogle.com,还有 wikibooks 。

另外推荐 Simon Peyton Jones 的博客。还有,学点数学也是有帮助的。

中文资料似乎不多,期待一下 Real World Haskell 中文版?
0 请登录后投票
   发表时间:2009-06-24   最后修改:2009-06-24
稍微翻了一下,ms haskshell可以编译成c libray,当然对C#来说更福音的是COM server....不过没有好的ide做这事太麻烦了点。
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics