{-# LANGUAGE DataKinds #-}
{-# Language ViewPatterns #-}

module Language.R.Internal
  ( r1
  , r2
  , installIO
  ) where

import           Control.Memory.Region
import qualified Foreign.R as R
import           Language.R

import Data.ByteString as B
import Foreign.C.String ( withCString )

inVoid :: R V z -> R V z
inVoid :: R V z -> R V z
inVoid = R V z -> R V z
forall a. a -> a
id
{-# INLINE inVoid #-}

-- | Call a pure unary R function of the given name in the global environment.
r1 :: ByteString -> SEXP s a -> IO (SomeSEXP V)
r1 :: ByteString -> SEXP s a -> IO (SomeSEXP V)
r1 fn :: ByteString
fn a :: SEXP s a
a =
    ByteString -> (CString -> IO (SomeSEXP V)) -> IO (SomeSEXP V)
forall a. ByteString -> (CString -> IO a) -> IO a
useAsCString ByteString
fn ((CString -> IO (SomeSEXP V)) -> IO (SomeSEXP V))
-> (CString -> IO (SomeSEXP V)) -> IO (SomeSEXP V)
forall a b. (a -> b) -> a -> b
$ \cfn :: CString
cfn -> CString -> IO (SEXP V 'Symbol)
R.install CString
cfn IO (SEXP V 'Symbol)
-> (SEXP V 'Symbol -> IO (SomeSEXP V)) -> IO (SomeSEXP V)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \f :: SEXP V 'Symbol
f ->
      IO (SEXP V 'Lang)
-> (SEXP Any 'Lang -> IO (SomeSEXP V)) -> IO (SomeSEXP V)
forall (a :: SEXPTYPE) s b.
IO (SEXP V a) -> (SEXP s a -> IO b) -> IO b
R.withProtected (SEXP V 'Symbol -> SEXP V a -> IO (SEXP V 'Lang)
forall s (a :: SEXPTYPE) (b :: SEXPTYPE).
SEXP s a -> SEXP s b -> IO (SEXP V 'Lang)
R.lang2 SEXP V 'Symbol
f (SEXP s a -> SEXP V a
forall t s (a :: SEXPTYPE). (t <= s) => SEXP s a -> SEXP t a
R.release SEXP s a
a)) (R V (SomeSEXP V) -> IO (SomeSEXP V)
forall a s. NFData a => R s a -> IO a
unsafeRunRegion (R V (SomeSEXP V) -> IO (SomeSEXP V))
-> (SEXP Any 'Lang -> R V (SomeSEXP V))
-> SEXP Any 'Lang
-> IO (SomeSEXP V)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. R V (SomeSEXP V) -> R V (SomeSEXP V)
forall z. R V z -> R V z
inVoid (R V (SomeSEXP V) -> R V (SomeSEXP V))
-> (SEXP Any 'Lang -> R V (SomeSEXP V))
-> SEXP Any 'Lang
-> R V (SomeSEXP V)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SEXP Any 'Lang -> R V (SomeSEXP V)
forall (m :: * -> *) s (a :: SEXPTYPE).
MonadR m =>
SEXP s a -> m (SomeSEXP (Region m))
eval)

-- | Call a pure binary R function. See 'r1' for additional comments.
r2 :: ByteString -> SEXP s a -> SEXP s b -> IO (SomeSEXP V)
r2 :: ByteString -> SEXP s a -> SEXP s b -> IO (SomeSEXP V)
r2 fn :: ByteString
fn a :: SEXP s a
a b :: SEXP s b
b =
    ByteString -> (CString -> IO (SomeSEXP V)) -> IO (SomeSEXP V)
forall a. ByteString -> (CString -> IO a) -> IO a
useAsCString ByteString
fn ((CString -> IO (SomeSEXP V)) -> IO (SomeSEXP V))
-> (CString -> IO (SomeSEXP V)) -> IO (SomeSEXP V)
forall a b. (a -> b) -> a -> b
$ \cfn :: CString
cfn -> CString -> IO (SEXP V 'Symbol)
R.install CString
cfn IO (SEXP V 'Symbol)
-> (SEXP V 'Symbol -> IO (SomeSEXP V)) -> IO (SomeSEXP V)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \f :: SEXP V 'Symbol
f ->
      IO (SEXP V 'Lang)
-> (SEXP Any 'Lang -> IO (SomeSEXP V)) -> IO (SomeSEXP V)
forall (a :: SEXPTYPE) s b.
IO (SEXP V a) -> (SEXP s a -> IO b) -> IO b
R.withProtected (SEXP V 'Symbol -> SEXP V a -> SEXP V b -> IO (SEXP V 'Lang)
forall s (a :: SEXPTYPE) (b :: SEXPTYPE) (c :: SEXPTYPE).
SEXP s a -> SEXP s b -> SEXP s c -> IO (SEXP V 'Lang)
R.lang3 SEXP V 'Symbol
f (SEXP s a -> SEXP V a
forall t s (a :: SEXPTYPE). (t <= s) => SEXP s a -> SEXP t a
R.release SEXP s a
a) (SEXP s b -> SEXP V b
forall t s (a :: SEXPTYPE). (t <= s) => SEXP s a -> SEXP t a
R.release SEXP s b
b)) (R V (SomeSEXP V) -> IO (SomeSEXP V)
forall a s. NFData a => R s a -> IO a
unsafeRunRegion (R V (SomeSEXP V) -> IO (SomeSEXP V))
-> (SEXP Any 'Lang -> R V (SomeSEXP V))
-> SEXP Any 'Lang
-> IO (SomeSEXP V)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. R V (SomeSEXP V) -> R V (SomeSEXP V)
forall z. R V z -> R V z
inVoid (R V (SomeSEXP V) -> R V (SomeSEXP V))
-> (SEXP Any 'Lang -> R V (SomeSEXP V))
-> SEXP Any 'Lang
-> R V (SomeSEXP V)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SEXP Any 'Lang -> R V (SomeSEXP V)
forall (m :: * -> *) s (a :: SEXPTYPE).
MonadR m =>
SEXP s a -> m (SomeSEXP (Region m))
eval)

-- | Internalize a symbol name.
installIO :: String -> IO (SEXP V 'R.Symbol)
installIO :: String -> IO (SEXP V 'Symbol)
installIO str :: String
str = String -> (CString -> IO (SEXP V 'Symbol)) -> IO (SEXP V 'Symbol)
forall a. String -> (CString -> IO a) -> IO a
withCString String
str CString -> IO (SEXP V 'Symbol)
R.install