| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Language.J
Contents
Description
Marshal a limited subset of J arrays into Repa arrays.
Tutorial
Suppose we wish to perform linear regression. In J we could do:
xs := 1 2 3 ys := 2 4 6 reg_result =: ys %. xs ^/ i.2
To do this with Haskell data:
do jenv <-jinitlibLinuxlet hsArr0 = R.fromListUnboxed (R.ix1 3) [1.0,2.0,3.0] hsArr1 = R.fromListUnboxed (R.ix1 3) [2.0,4.0,6.0] jArr0 =JDoubleArr$ R.copyS $ R.map (realToFrac :: Double -> CDouble) hsArr0 jArr1 =JDoubleArr$ R.copyS $ R.map (realToFrac :: Double -> CDouble) hsArr1setJDatajenv "xs" jArr0setJDatajenv "ys" jArr1bsDispatchjenv "reg_result =: ys %. xs ^/ i.2"JDoubleArrres <-getJDatajenv "reg_result" R.toList res
There are three steps to do the calculation, plus one to get a J environment.
- Use
jinitwith the appropriate file path for your platform - Marshal Haskell values and send them to the J environment. To do so, we
use
setJData, which takes aJDatacontaining a repa array or a string. - Perform calculations within the J environment. Here, we use
bsDispatchto compute some results and assign them within J - Marshal J values back to Haskell. We use
getJData.
Since marshaling data between J and Haskell is expensive, it's best to do as much computation as possible in J.
Loading Profile
If you would like to use user libraries, you need to use jLoad on the
JEnv. As an example:
do
jenv <- jinit libLinux
jLoad jenv (linuxProfile "9.01")
bsDispatch jenv "load'tables/csv'"
This will load the CSV addon, assuming it is installed.
FFI
If you want to marshal data yourself, say to use a Vector, look at JEnv.
Synopsis
- data JEnv = JEnv {}
- jinit :: RawFilePath -> IO JEnv
- jLoad :: JEnv -> Profile -> IO ()
- data Profile = Profile {}
- linuxProfile :: ByteString -> Profile
- macProfile :: JVersion -> Profile
- windowsProfile :: JVersion -> Profile
- libLinux :: RawFilePath
- libMac :: JVersion -> RawFilePath
- profLinux :: ByteString -> ByteString
- bsDispatch :: JEnv -> ByteString -> IO ()
- bsOut :: JEnv -> IO ByteString
- type JVersion = [Int]
- data JData sh
- getJData :: Shape sh => JEnv -> ByteString -> IO (JData sh)
- setJData :: Shape sh => JEnv -> ByteString -> JData sh -> IO CInt
- data J
- type JDoType = Ptr J -> CString -> IO CInt
- type JGetMType = Ptr J -> CString -> Ptr CLLong -> Ptr CLLong -> Ptr (Ptr CLLong) -> Ptr (Ptr CChar) -> IO CInt
- type JGetRType = Ptr J -> IO CString
- type JSetAType = Ptr J -> CLLong -> CString -> CLLong -> Ptr () -> IO CInt
Environment
Arguments
| :: RawFilePath | Path to J library |
| -> IO JEnv |
Get a J environment
Passing the resultant JEnv between threads can cause unexpected bugs.
Since: 0.1.2.0
Constructors
| Profile | |
Fields
| |
macProfile :: JVersion -> Profile Source #
Since: 0.1.2.0
windowsProfile :: JVersion -> Profile Source #
Since: 0.1.2.0
libLinux :: RawFilePath Source #
Expected RawFilePath to the library on a Linux machine.
libMac :: JVersion -> RawFilePath Source #
Expected RawFilePath to the library on Mac.
profLinux :: ByteString -> ByteString Source #
bsDispatch :: JEnv -> ByteString -> IO () Source #
Send some J code to the environment.
Repa
J data backed by repa array
Arguments
| :: Shape sh | |
| => JEnv | |
| -> ByteString | Name of the value in question |
| -> IO (JData sh) |
\( O(n) \) in the array size
\( O(n) \) in the array size