最近接触了Julia,觉得Julia的类型系统很强大。以前写过Haskell,就想把Haskell的东西搬过来,看看Julia能不能实现。
因为Julia中有类型变量,所以今天试着写了一个ST单子(其实我就想看一下Julia的类型变量到底和Haskell是不是一样,类型变量会不会逃逸出来),不想触发了一个编译器错误,代码是这样的:
struct STMonad{A,B}
function STMonad()
new{S,Ref{S}}() where S
end
end
function runST(stm::STMonad{S,T},pho::T) where S where T
//一些其他东西,不影响
end
function userRunST(pho::T) where T
stm = STMonad()
runST(stm::STMonad{S,T},pho::T) where S
end
不管这个代码有没有实现ST单子(好吧,我承认应该是没有),主要思想就是STMonad内部构造器创造了一个类型为 {S,Ref{S}} 的STMonad值,这个值由runST运行,同时runST还要接受一个类型为T的值,可以推导出T为Ref{S}的类型,但是S是一个不定的类型变量,所以类型S就逃逸了出来,所以应该是不安全的。
总之,在构造STMonad的实例时(就是调用STMonad()的时候),编译器报错了,但是我不清楚到底是什么原因导致的,也不知道之前有没有人报告过这个错误:
Please submit a bug report with steps to reproduce this fault, and any error messages that follow (in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x6b5cf117 -- jl_new_structv at /home/Administrator/buildbot/worker/package_win64/build/src\datatype.c:774
in expression starting at no file:0
jl_set_nth_field at /home/Administrator/buildbot/worker/package_win64/build/src\datatype.c:889 [inlined]
jl_new_structv at /home/Administrator/buildbot/worker/package_win64/build/src\datatype.c:781
Type at .\REPL[9]:3
userRunST at .\REPL[16]:2
unknown function (ip: 000000001D94153A)
jl_fptr_trampoline at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:1829
jl_apply_generic at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:2182
do_call at /home/Administrator/buildbot/worker/package_win64/build/src\interpreter.c:324
eval_value at /home/Administrator/buildbot/worker/package_win64/build/src\interpreter.c:428
eval_stmt_value at /home/Administrator/buildbot/worker/package_win64/build/src\interpreter.c:363 [inlined]
eval_body at /home/Administrator/buildbot/worker/package_win64/build/src\interpreter.c:682
jl_interpret_toplevel_thunk_callback at /home/Administrator/buildbot/worker/package_win64/build/src\interpreter.c:799
unknown function (ip: FFFFFFFFFFFFFFFE)
unknown function (ip: 00000000112B2F8F)
unknown function (ip: FFFFFFFFFFFFFFFF)
jl_toplevel_eval_flex at /home/Administrator/buildbot/worker/package_win64/build/src\toplevel.c:787
jl_toplevel_eval_in at /home/Administrator/buildbot/worker/package_win64/build/src\builtins.c:622
eval at .\boot.jl:319
jl_apply_generic at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:2182
eval_user_input at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\REPL\src\REPL.jl:85
macro expansion at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\REPL\src\REPL.jl:117 [inlined]
#28 at .\task.jl:259
jl_fptr_trampoline at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:1829
jl_apply_generic at /home/Administrator/buildbot/worker/package_win64/build/src\gf.c:2182
jl_apply at /home/Administrator/buildbot/worker/package_win64/build/src\julia.h:1536 [inlined]
start_task at /home/Administrator/buildbot/worker/package_win64/build/src\task.c:268
Allocations: 3927324 (Pool: 3926665; Big: 659); GC: 7
对Julia也不是特别熟悉,不过既然出Bug了那应该就不只是我的问题了。不知道有没有能够实现一下这个STMonad,感觉Julia的类型系统很强,应该做得到。
新人第一次问问题,请各位高手多加体谅与帮助!