j-0.2.1.0: J in Haskell
Safe HaskellNone
LanguageHaskell2010

Language.J

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 <- jinit libLinux

   let 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) hsArr1

   setJData jenv "xs" jArr0
   setJData jenv "ys" jArr1

   bsDispatch jenv "reg_result =: ys %. xs ^/ i.2"

   JDoubleArr res <- getJData jenv "reg_result"
   R.toList res

There are three steps to do the calculation, plus one to get a J environment.

  1. Use jinit with the appropriate file path for your platform
  2. Marshal Haskell values and send them to the J environment. To do so, we use setJData, which takes a JData containing a repa array or a string.
  3. Perform calculations within the J environment. Here, we use bsDispatch to compute some results and assign them within J
  4. 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

Environment

data JEnv Source #

Constructors

JEnv 

jinit Source #

Arguments

:: RawFilePath

Path to J library

-> IO JEnv 

Get a J environment

Passing the resultant JEnv between threads can cause unexpected bugs.

jLoad :: JEnv -> Profile -> IO () Source #

Load user profile.

since 0.1.2.0

data Profile Source #

Since: 0.1.2.0

Constructors

Profile 

linuxProfile Source #

Arguments

:: ByteString

J version, e.g. "9.01"

-> Profile 

Since: 0.1.2.0

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.

bsDispatch :: JEnv -> ByteString -> IO () Source #

Send some J code to the environment.

bsOut :: JEnv -> IO ByteString Source #

Read last output

For debugging

type JVersion = [Int] Source #

Repa

data JData sh Source #

J data backed by repa array

getJData Source #

Arguments

:: Shape sh 
=> JEnv 
-> ByteString

Name of the value in question

-> IO (JData sh) 

\( O(n) \) in the array size

setJData Source #

Arguments

:: Shape sh 
=> JEnv 
-> ByteString

Name

-> JData sh 
-> IO CInt 

\( O(n) \) in the array size

FFI

data J Source #

Abstract context

type JSetAType = Ptr J -> CLLong -> CString -> CLLong -> Ptr () -> IO CInt Source #