tag:blogger.com,1999:blog-7094652.post3532878487315634840..comments2024-03-23T14:36:09.980+00:00Comments on Neil Mitchell's Blog (Haskell etc): Calling Haskell from RNeil Mitchellhttp://www.blogger.com/profile/13084722756124486154noreply@blogger.comBlogger20125tag:blogger.com,1999:blog-7094652.post-1342464705253099302016-08-12T14:41:11.602+01:002016-08-12T14:41:11.602+01:00I've just figured out that there's no need...I've just figured out that there's no need of Foreign.R to output a vector, by using pokeArray instead of poke. But this requires to know in advance the length of the vector. Anonymoushttps://www.blogger.com/profile/15747466627951469933noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-70254649109748750252016-08-12T13:10:24.687+01:002016-08-12T13:10:24.687+01:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/15747466627951469933noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-67128819608854124932016-08-11T21:17:05.145+01:002016-08-11T21:17:05.145+01:00Thanks Stéphane - it's excellent when people g...Thanks Stéphane - it's excellent when people go on to share their gained knowledge. Nice article!Neil Mitchellhttps://www.blogger.com/profile/13084722756124486154noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-80961535081567936902016-08-11T13:56:17.522+01:002016-08-11T13:56:17.522+01:00Dear Neil, please take a look at my blog post. I w...Dear Neil, please take a look at <a href="http://stla.github.io/stlapblog/posts/FloatExpansionHaskell.html" rel="nofollow">my blog post</a>. I would never have achieved this work without you and Alex. Thanks !Anonymoushttps://www.blogger.com/profile/15747466627951469933noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-53593271073768035012016-08-10T09:43:52.232+01:002016-08-10T09:43:52.232+01:00I have found a way to return a vector: https://gro...I have found a way to return a vector: https://groups.google.com/forum/#!topic/haskellr/6M0Ar1DfJJQAnonymoushttps://www.blogger.com/profile/15747466627951469933noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-57189438286956100302016-08-08T20:31:07.986+01:002016-08-08T20:31:07.986+01:00Stéphane: No idea I'm afraid, but you might co...Stéphane: No idea I'm afraid, but you might consider https://hackage.haskell.org/package/inline-r instead.Neil Mitchellhttps://www.blogger.com/profile/13084722756124486154noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-27138950672305674322016-08-08T13:27:32.285+01:002016-08-08T13:27:32.285+01:00Thank you. I managed to make it work with Ubuntu. ...Thank you. I managed to make it work with Ubuntu. Do you know how to deal with a function whose output is a list ?Anonymoushttps://www.blogger.com/profile/15747466627951469933noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-64063253906496118742014-07-24T19:15:06.489+01:002014-07-24T19:15:06.489+01:00Alex: Thanks for sharing, I've updated the pos...Alex: Thanks for sharing, I've updated the post to point people at your comments.Neil Mitchellhttps://www.blogger.com/profile/13084722756124486154noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-24081301635482795272014-07-24T04:29:47.062+01:002014-07-24T04:29:47.062+01:00PS Here's the actual compiler command I ended ...PS Here's the actual compiler command I ended up using:<br /><br />ghc -shared -fPIC -dynamic -lHSrts-ghc7.8.3 SumRoots.hs StartEnd.c -o SumRoots.dylib<br /><br />Note "dylib" instead of "dll" - it's just the Mac equivalent of "dylib", according to <a href="https://ghc.haskell.org/trac/ghc/wiki/SharedLibraries?version=19" rel="nofollow">this wiki page</a>.Alex Davisnoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-89151243538302925332014-07-24T04:23:24.117+01:002014-07-24T04:23:24.117+01:00For anybody trying this on Mac OS X Mavericks, her...For anybody trying this on Mac OS X Mavericks, here's what I had to do to get it working.<br /><br />1. More flags to the compiler, just like Dominik found. I wish I had noticed his comment before figuring this out on my own. But, you need these flags:<br />-fPIC: generate "position independent code". Whatever that is, it's necessary for shared libraries outside of Windows, according to <a href="http://www.haskell.org/ghc/docs/6.4.1/html/users_guide/options-phases.html" rel="nofollow">this page of the GHC manual</a>.<br />-dynamic: Access Haskell libraries dynamically at runtime, instead of compiling them into the final result. This is because the libraries were not compiled with the -fPIC flag, according to <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/using-shared-libs.html#idp12506320" rel="nofollow">this page of the GHC manual</a>.<br />-lHSrts-ghc7.8.3: I imagine you should substitute in your own GHC version here. This links your final product to the Haskell run-time system. I find no mention in the user manual that this is necessary, but on my computer it's necessary, or you get "Symbol not found" errors.<br /><br />That was enough for my friend Shea, but even after successfully compiling I was still getting errors. On my computer I get segmentation faults from SumRoots.hs as written. The problem, it turns out, is the use of Int instead of CInt: "n" is read in wrong, and the computer looks for too long of "xs" and gets a segfault. On my computer I need to use CInt. Here's the complete code, using CInt as well as CDouble: <br /><br />-- SumRoots.hs<br />{-# LANGUAGE ForeignFunctionInterface #-}<br />module SumRoots where<br /><br />import Foreign<br />import Foreign.C.Types<br /><br />foreign export ccall sumRootsR :: Ptr CInt -> Ptr CDouble -> Ptr CDouble -> IO ()<br /><br />sumRootsR :: Ptr CInt -> Ptr CDouble -> Ptr CDouble -> IO ()<br />sumRootsR n xs result = do<br /> cN <- peek n<br /> let haskellN = fromIntegral cN :: Int<br /> cXs <- peekArray haskellN xs<br /> let haskellXs = map realToFrac cXs :: [Double]<br /> haskellResult = sumRoots haskellXs<br /> cResult = realToFrac haskellResult :: CDouble<br /> poke result cResult<br /><br />sumRoots :: [Double] -> Double<br />sumRoots xs = sum (map sqrt xs)<br /><br />This adds a lot of complexity - it was lucky for this exposition that CInt and Int are interchangable on Windows. But on my computer this is what it took.<br /><br />Thanks to Shea Levy for walking me through this!Alex Davisnoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-77686505878293860092013-07-10T20:07:53.680+01:002013-07-10T20:07:53.680+01:00No idea I'm afraid, but the GHC manual does sa...No idea I'm afraid, but the GHC manual does say how to call Haskell from Excel, and the same pattern might work, if R can bind to C dlls.Neil Mitchellhttps://www.blogger.com/profile/13084722756124486154noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-91258646575763837512013-07-10T15:09:27.221+01:002013-07-10T15:09:27.221+01:00Do you what would be involved in going the other w...Do you what would be involved in going the other way around (ie calling R code from Haskell code)?Jamesnoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-44017888332595322752011-11-05T06:32:36.201+00:002011-11-05T06:32:36.201+00:00Dominik: Useful information about the FreeBSD comm...Dominik: Useful information about the FreeBSD command line. I think if you were on GHC 7.2 you wouldn't need to include "SumRoots_stub.o"Neil Mitchellhttps://www.blogger.com/profile/13084722756124486154noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-15021003097505093222011-11-05T00:57:14.983+00:002011-11-05T00:57:14.983+00:00BTW to compile on FreeBSD I had to use (and proba...BTW to compile on FreeBSD I had to use (and probably something similar on Linux):<br /><br />ghc -c -fPIC StartEnd.c<br />ghc -c -fPIC SumRoots.hs<br />ghc -shared -dynamic -fPIC -o SumRoots.so SumRoots.o SumRoots_stub.o StartEnd.o -lHSrts-ghc7.0.3 -optl-Wl,-rpath,/usr/local/lib/ghc-7.0.3/<br /><br />I found that on http://www.well-<br />typed.com/blog/30 . I wonder though if you still have to explicitly link against the right RTS.<br />For starters making a wrapper for .C would be cool; maybe one can steal some ideas from the inline and Rcpp R packages. Think I'll try that some time.Dominiknoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-37968299549320824322011-11-04T11:54:52.107+00:002011-11-04T11:54:52.107+00:00Dominik: Thanks for the tip on HsStart - good idea...Dominik: Thanks for the tip on HsStart - good idea.<br /><br />Audun: Thanks for the tip, I've updated the post - that's a much simpler solution.<br /><br />scott/Jesse: The wrapping code is not idiomatic Haskell, but that's a few lines. The idea is you write a small wrapper Haskell/R wrapper (sumRootsR), and then can write lots of idiomatic Haskell that it calls.<br /><br />winterkoninkje: I don't intend to do anything else on this work, but if you do anything my wife may be interested!Neil Mitchellhttps://www.blogger.com/profile/13084722756124486154noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-72282627951924871492011-11-04T11:39:11.155+00:002011-11-04T11:39:11.155+00:00Instead of the manual loop over the array:
xs <...Instead of the manual loop over the array:<br />xs <- forM [0..n-1] $ \i -><br /> peekElemOff xs i<br /><br />You could do the simpler:<br />xs <- peekArray n xs<br /><br />Where peekArray is from Foreign.Marshal.Array.Audunhttps://www.blogger.com/profile/08289738984701974229noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-75831208473361797352011-11-03T04:49:44.364+00:002011-11-03T04:49:44.364+00:00This seems interesting, but the Haskell you have t...This seems interesting, but the Haskell you have to write to be compatible with R doesn't look much like idiomatic Haskell.<br /><br />What's the point of using Haskell when you have to use IO to do something that shouldn't require it?scottnoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-62400016290238967952011-11-02T19:28:31.565+00:002011-11-02T19:28:31.565+00:00"...not languages that are particularly easy ..."...not languages that are particularly easy for beginners"<br /><br />Really, Haskell falls into that category as well. The suggestion (for C/Fortran) probably comes from the fact that there is no wrapper code indirection necessary in that case. <br /><br />The ability to use Haskell instead is still a pretty nice point, however.Jessenoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-38039240064734704492011-11-02T12:16:16.813+00:002011-11-02T12:16:16.813+00:00Hi,
thx for the post, the last time I tried that I...Hi,<br />thx for the post, the last time I tried that I didn't get it to work but I'll try again with your explanation.<br /><br />R also calls an initialization routine after dyn.load() which you can have call HsStart <br />(see: <br />http://cran.r-project.org/doc/manuals/R-exts.html#dyn_002eload-and-dyn_002eunload). Then you could avoid explicitly calling HsStart from R. Doesnt make much difference though,<br /><br /><br /><br />@winterkoninkje: .C() only exposes basic C datatypes, like double*, int*, char* and so on. To my knowledge you cant allocate objects on the C- or Haskell-side that way, you usually allocate all output objects in R code and pass them along to .C(). There's two other functions, .Call() and .External() that pass you real R objects and where you can also allocate R objects in C/Haskell code, but I think you have to use R API functions to create them and have R's gc handle these objects. I found that usually .C() is good enough and much easier to use.Dominiknoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-80439777582106131842011-10-30T22:02:02.487+00:002011-10-30T22:02:02.487+00:00Sweet. Now we just need to write a nice tool for a...Sweet. Now we just need to write a nice tool for autogenerating all the glue, since FFI glue is usually the worst part for beginners. So what sorts of datatypes does R export other than CInt, CDouble, CString, and arrays a la (CInt,Ptr a) ? How does R's GC work in case we wanted to allocate arrays on the Haskell side?Anonymousnoreply@blogger.com