Skip to content

Commit

Permalink
Add specs for Control.Monad.R.Class
Browse files Browse the repository at this point in the history
  • Loading branch information
facundominguez committed Oct 25, 2023
1 parent 7265159 commit a3e7508
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion inline-r/inline-r.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ library
Foreign.R.EventLoop
-- Language.R.Event
other-modules:
-- Control.Monad.R.Class
Control.Monad.R.Class
-- Control.Monad.R.Internal
-- Data.Vector.SEXP.Mutable.Internal
Internal.Error
Expand Down
20 changes: 12 additions & 8 deletions inline-r/src/Control/Monad/R/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
-- Copyright: (C) 2013 Amgen, Inc.
--

{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DefaultSignatures #-}
{-# OPTIONS_GHC -fplugin-opt=LiquidHaskell:--skip-module=False #-}

-- Sidesteps a failure when verifying: liftIO . protect
{-@ LIQUID "--prune-unsorted" @-}

module Control.Monad.R.Class
( MonadR(..)
, Region
, acquireSome
) where

import Control.Memory.Region
import qualified Data.Kind
import Foreign.C -- XXX: only needed to help name resolution in LH
import Foreign.R

import Control.Applicative
Expand All @@ -29,15 +36,16 @@ class (Applicative m, MonadIO m, MonadCatch m, MonadMask m, PrimMonad m)
io :: IO a -> m a
io = liftIO

{-@ acquire :: a:SEXP V -> m (TSEXP (Region m) (typeOf a)) @-}
-- | Acquire ownership in the current region of the given object. This means
-- that the liveness of the object is guaranteed so long as the current region
-- remains active (the R garbage collector will not attempt to free it).
acquire :: s ~ V => SEXP s a -> m (SEXP (Region m) a)
default acquire :: (MonadIO m, Region m ~ G) => SEXP s a -> m (SEXP (Region m) a)
acquire :: SEXP V -> m (SEXP (Region m))
default acquire :: (MonadIO m, Region m ~ G) => SEXP V -> m (SEXP (Region m))
acquire = liftIO . protect

-- | A reification of an R execution context, i.e. a "session".
data ExecContext m :: *
data ExecContext m :: Data.Kind.Type

-- | Get the current execution context.
getExecContext :: m (ExecContext m)
Expand All @@ -48,7 +56,3 @@ class (Applicative m, MonadIO m, MonadCatch m, MonadMask m, PrimMonad m)
unsafeRunWithExecContext :: m a -> ExecContext m -> IO a

type Region m = PrimState m

-- | 'acquire' for 'SomeSEXP'.
acquireSome :: (MonadR m) => SomeSEXP V -> m (SomeSEXP (Region m))
acquireSome (SomeSEXP s) = SomeSEXP <$> acquire s

0 comments on commit a3e7508

Please sign in to comment.