@@ -36,12 +36,7 @@ import qualified Data.Vector.SEXP as Vector
36
36
import Control.Monad.R.Class
37
37
import Foreign.R
38
38
( SEXP
39
- , SomeSEXP (.. )
40
39
, typeOf
41
- , asTypeOf
42
- , cast
43
- , unSomeSEXP
44
- , unsafeCoerce
45
40
)
46
41
import qualified Foreign.R as R
47
42
import qualified Foreign.R.Parse as R
@@ -73,88 +68,96 @@ import Prelude
73
68
-- the dependency hierarchy.
74
69
75
70
-- | Parse and then evaluate expression.
76
- parseEval :: ByteString -> IO (SomeSEXP V )
71
+ parseEval :: ByteString -> IO (SEXP V )
77
72
parseEval txt = useAsCString txt $ \ ctxt ->
78
73
R. withProtected (R. mkString ctxt) $ \ rtxt ->
79
74
alloca $ \ status -> do
80
75
R. withProtected (R. parseVector rtxt 1 status (R. release nilValue)) $ \ exprs -> do
81
76
rc <- fromIntegral <$> peek status
82
77
unless (R. PARSE_OK == toEnum rc) $
83
78
runRegion $ throwRMessage $ " Parse error in: " ++ C8. unpack txt
84
- SomeSEXP expr <- peek $ castPtr $ R. unsafeSEXPToVectorPtr exprs
79
+ expr <- peek $ castPtr $ R. unsafeSEXPToVectorPtr exprs
85
80
runRegion $ do
86
- SomeSEXP val <- eval expr
87
- return $ SomeSEXP (R. release val)
81
+ val <- eval expr
82
+ return (R. release val)
88
83
89
84
-- | Parse file and perform some actions on parsed file.
90
85
--
91
86
-- This function uses continuation because this is an easy way to make
92
87
-- operations GC-safe.
93
- parseFile :: FilePath -> (SEXP s 'R.Expr -> IO a ) -> IO a
88
+ {-@ parseFile :: FilePath -> (SEXP s Foreign.R.Type. Expr -> IO a ) -> IO a @-}
89
+ parseFile :: FilePath -> (SEXP s -> IO a ) -> IO a
94
90
{-# DEPRECATED parseFile "Use [r| parse(file=\"path/to/file\") |] instead." #-}
95
91
parseFile fl f = do
96
92
withCString fl $ \ cfl ->
97
93
R. withProtected (R. mkString cfl) $ \ rfl ->
98
- r1 (C8. pack " parse" ) rfl >>= \ ( R. SomeSEXP s) ->
99
- return ( R. unsafeCoerce s) `R.withProtected` f
94
+ r1 (C8. pack " parse" ) rfl >>= \ s ->
95
+ return s `R.withProtected` f
100
96
97
+ {-@ parseText :: String -> Bool -> IO (R. SEXP V Foreign.R.Type. Expr ) @-}
101
98
parseText
102
99
:: String -- ^ Text to parse
103
100
-> Bool -- ^ Whether to annotate the AST with source locations.
104
- -> IO (R. SEXP V 'R. Expr )
101
+ -> IO (R. SEXP V )
105
102
{-# DEPRECATED parseText "Use [r| parse(text=...) |] instead." #-}
106
103
parseText txt b = do
107
104
s <- parseEval $ C8. pack $
108
105
" parse(text=" ++ show txt ++ " , keep.source=" ++ keep ++ " )"
109
- return $ (sing :: R. SSEXPTYPE 'R. Expr ) `R.cast ` s
106
+ return $ R. Expr `R.checkSEXPTYPE ` s
110
107
where
111
108
keep | b = " TRUE"
112
109
| otherwise = " FALSE"
113
110
114
111
-- | Internalize a symbol name.
115
- install :: MonadR m => String -> m (SEXP V 'R.Symbol )
112
+ {-@ install :: String -> m (SEXP V Foreign.R.Type. Symbol ) @-}
113
+ install :: MonadR m => String -> m (SEXP V )
116
114
install = io . installIO
117
115
118
116
{-# DEPRECATED string, strings "Use mkSEXP instead" #-}
119
117
120
118
-- | Create an R character string from a Haskell string.
121
- string :: String -> IO (SEXP V 'R.Char )
119
+ {-@ string :: String -> IO (SEXP V Foreign.R.Type. Char ) @-}
120
+ string :: String -> IO (SEXP V )
122
121
string str = withCString str R. mkChar
123
122
124
123
-- | Create an R string vector from a Haskell string.
125
- strings :: String -> IO (SEXP V 'R.String )
124
+ {-@ strings :: String -> IO (SEXP V Foreign.R.Type. String ) @-}
125
+ strings :: String -> IO (SEXP V )
126
126
strings str = withCString str R. mkString
127
127
128
128
-- | Evaluate a (sequence of) expression(s) in the given environment, returning the
129
129
-- value of the last.
130
- evalEnv :: MonadR m => SEXP s a -> SEXP s 'R.Env -> m (SomeSEXP (Region m ))
131
- evalEnv (hexp -> Language.R.HExp. Expr _ v) rho = acquireSome =<< do
130
+ {-@ assume evalEnv :: SEXP s a -> TSEXP s Foreign.R.Type. Env -> m (SEXP (Region m )) @-}
131
+ {-@ ignore evalEnv @- }
132
+ evalEnv :: MonadR m => SEXP s -> SEXP s -> m (SEXP (Region m ))
133
+ evalEnv (hexp -> Language.R.HExp. Expr _ v) rho = acquire =<< do
132
134
io $ alloca $ \ p -> do
133
- mapM_ (\ ( SomeSEXP s) -> void $ R. protect s) (Vector. toList v)
134
- x <- Prelude. last <$> forM (Vector. toList v) (\ ( SomeSEXP s) -> do
135
+ mapM_ (\ s -> void $ R. protect s) (Vector. toList v)
136
+ x <- Prelude. last <$> forM (Vector. toList v) (\ s -> do
135
137
z <- R. tryEvalSilent s (R. release rho) p
136
138
e <- peek p
137
139
when (e /= 0 ) $ runRegion $ throwR rho
138
140
return z)
139
141
R. unprotect (Vector. length v)
140
142
return x
141
- evalEnv x rho = acquireSome =<< do
143
+ evalEnv x rho = acquire =<< do
142
144
io $ alloca $ \ p -> R. withProtected (return (R. release x)) $ \ _ -> do
143
145
v <- R. tryEvalSilent x rho p
144
146
e <- peek p
145
147
when (e /= 0 ) $ runRegion $ throwR rho
146
148
return v
147
149
148
150
-- | Evaluate a (sequence of) expression(s) in the global environment.
149
- eval :: MonadR m => SEXP s a -> m (SomeSEXP (Region m ))
151
+ eval :: MonadR m => SEXP s -> m (SEXP (Region m ))
150
152
eval x = evalEnv x (R. release globalEnv)
151
153
152
154
-- | Silent version of 'eval' function that discards it's result.
153
- eval_ :: MonadR m => SEXP s a -> m ()
155
+ eval_ :: MonadR m => SEXP s -> m ()
154
156
eval_ = void . eval
155
157
156
158
-- | Throw an R error as an exception.
157
- throwR :: MonadR m => R. SEXP s 'R.Env -- ^ Environment in which to find error.
159
+ {- @ throwR :: TSEXP s Foreign.R.Type.Env -> m a @-}
160
+ throwR :: MonadR m => R. SEXP s -- ^ Environment in which to find error.
158
161
-> m a
159
162
throwR env = getErrorMessage env >>= io . throwIO . R. RError
160
163
@@ -173,12 +176,13 @@ throwRMessage :: MonadR m => String -> m a
173
176
throwRMessage = io . throwIO . R. RError
174
177
175
178
-- | Read last error message.
176
- getErrorMessage :: MonadR m => R. SEXP s 'R.Env -> m String
179
+ {- @ getErrorMessage :: TSEXP s Foreign.R.Type.Env -> m String @-}
180
+ getErrorMessage :: MonadR m => R. SEXP s -> m String
177
181
getErrorMessage e = io $ do
178
182
R. withProtected (withCString " geterrmessage" ((R. install >=> R. lang1))) $ \ f -> do
179
183
R. withProtected (return (R. release e)) $ \ env -> do
180
184
peekCString
181
185
=<< R.char
182
186
=<< peek
183
- =<< R. string . R. cast (sing :: R. SSEXPTYPE 'R. String )
187
+ =<< R.string . checkSEXPTYPE R. SString
184
188
=<< R.eval f env
0 commit comments