Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove type argument from SEXPs #410

Draft
wants to merge 24 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
85e7506
Add liquidhaskell dependency
facundominguez Apr 25, 2023
58f4851
Remove some modules temporarily
facundominguez Apr 25, 2023
6dccc91
Remove type argument of SEXP
facundominguez Apr 25, 2023
23a4447
Add specs to Foreign.R
facundominguez Apr 25, 2023
d50470d
Fix building of Foreign.R.Parse
facundominguez Apr 26, 2023
7264233
Make Parse.hsc a regular module Parse.hs
facundominguez Apr 26, 2023
6dfd1d2
Add a specs for Foreign.R.Parse
facundominguez Apr 26, 2023
3d6f135
Add specs to Language.R.Globals
facundominguez Apr 26, 2023
c7ed816
Remove SSEXPTYPE
facundominguez Apr 26, 2023
e7e6da5
Add specs for Control.Monad.R.Class
facundominguez Apr 26, 2023
4d1c7f9
Add specs for Language.R.GC
facundominguez Apr 27, 2023
98b6791
Add specs for Control.Monad.R.Internal
facundominguez Oct 28, 2023
e6e7b27
Add specs for Data.Vector.SEXP.Mutable
facundominguez Nov 15, 2023
561142b
Add specs to Data.Vector.SEXP
facundominguez Dec 7, 2023
9a3cc78
Enable building Language.R.Event
facundominguez Dec 18, 2023
657fdec
Enable building Language.R.Internal.FunWrappers
facundominguez Dec 18, 2023
6f0820e
Enable building Language.R.Instance
facundominguez Dec 18, 2023
b48cd82
Add specs to Language.R.HExp
facundominguez Dec 19, 2023
4a5f934
Remove unpack pragmas and strict fields from Vector
facundominguez Jan 8, 2024
6e4fcc8
Add specs for Language.R*
facundominguez Jan 10, 2024
73b8321
Update inline-r/src/H/Prelude/Interactive.hs
facundominguez Jan 24, 2024
820da66
Enable Liquid Haskell in tests of inline-r
facundominguez Mar 20, 2024
96e8497
Qualify spec of Foreign.R.Internal.globalEnv
facundominguez Mar 20, 2024
655015b
Fix rsafe quasiquoter
facundominguez Mar 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions inline-r/inline-r.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ extra-source-files:
./R/collectAntis.R
extra-tmp-files: inline-r.buildinfo

flag liquidhaskell
description: enable validation with Liquid Haskell
manual: True
default: False

source-repository head
type: git
location: git://github.com/tweag/HaskellR.git
Expand Down Expand Up @@ -126,6 +131,10 @@ library
--- We don't use ticks for promoted constructors, because we use
--- promoted constructors heavily and because they confuse hsc2hs.
ghc-options: -Wall -fno-warn-unticked-promoted-constructors
if flag(liquidhaskell) && impl(ghc == 9.2.5)
build-depends:
liquidhaskell
ghc-options: -fplugin=LiquidHaskell -fplugin-opt=LiquidHaskell:--skip-module

test-suite tests
main-is: tests.hs
Expand Down Expand Up @@ -153,17 +162,22 @@ test-suite tests
vector >=0.12.3.1 && <0.14,
if !os(windows)
build-depends: unix >=2.5 && <2.9,
other-modules:
Test.GC
Test.FunPtr
Test.Constraints
Test.Event
Test.Regions
Test.Vector
Test.Matcher
other-modules:
-- Test.GC
Test.FunPtr
-- Test.Constraints
-- Test.Event
-- Test.Regions
Test.Vector
-- Test.Matcher
ghc-options: -Wall -threaded
hs-source-dirs: tests
default-language: Haskell2010
if flag(liquidhaskell) && impl(ghc == 9.2.5)
build-depends:
liquidhaskell
ghc-options: -fplugin=LiquidHaskell


test-suite test-qq
main-is: test-qq.hs
Expand All @@ -179,6 +193,10 @@ test-suite test-qq
ghc-options: -Wall -threaded
hs-source-dirs: tests
default-language: Haskell2010
if flag(liquidhaskell) && impl(ghc == 9.2.5)
build-depends:
liquidhaskell
ghc-options: -fplugin=LiquidHaskell

test-suite test-shootout
main-is: test-shootout.hs
Expand All @@ -198,6 +216,7 @@ test-suite test-shootout
default-language: Haskell2010
if os(windows)
buildable: False
buildable: False

test-suite test-env1
main-is: test-env1.hs
Expand All @@ -210,6 +229,7 @@ test-suite test-env1
ghc-options: -Wall -threaded
hs-source-dirs: tests
default-language: Haskell2010
buildable: False

test-suite test-env2
main-is: test-env2.hs
Expand All @@ -222,6 +242,7 @@ test-suite test-env2
ghc-options: -Wall -threaded
hs-source-dirs: tests
default-language: Haskell2010
buildable: False

benchmark bench-qq
main-is: bench-qq.hs
Expand All @@ -236,6 +257,7 @@ benchmark bench-qq
ghc-options: -Wall -threaded
hs-source-dirs: tests
default-language: Haskell2010
buildable: False

benchmark bench-hexp
main-is: bench-hexp.hs
Expand All @@ -250,3 +272,4 @@ benchmark bench-hexp
ghc-options: -Wall -threaded
hs-source-dirs: tests
default-language: Haskell2010
buildable: False
9 changes: 9 additions & 0 deletions inline-r/src/Control/Memory/Region.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
-- of an object. That is, regions have scopes, and objects within a region are
-- guaranteed to remain live within the scope of that region.

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

module Control.Memory.Region where

Expand Down Expand Up @@ -35,3 +38,9 @@ type family a <= b :: Constraint
type instance a <= a = ()
type instance a <= G = ()
type instance V <= b = ()

-- | An alias for (<=).
--
-- XXX: LH complains when using (<=) in type signatures
class SubRegion s' s where
instance s' <= s => SubRegion s' s where
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
18 changes: 15 additions & 3 deletions inline-r/src/Control/Monad/R/Internal.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- |
-- Copyright: (C) 2016 Tweag I/O Limited.

{-# OPTIONS_GHC -fplugin-opt=LiquidHaskell:--skip-module=False #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand All @@ -11,15 +12,26 @@ import Control.Memory.Region
import Control.Monad.R.Class
import Data.Proxy (Proxy(..))
import Data.Reflection (Reifies, reify)
import Foreign.R (SEXP)
import Foreign.R.Context -- XXX: Needed to help LH name resolution
import Foreign.R.Internal -- XXX: Needed to help LH name resolution

newtype AcquireIO s = AcquireIO (forall ty. SEXP V ty -> IO (SEXP s ty))
{-@ type AcquireIO s = forall <p :: SEXP s -> Bool > . (SEXP V)<p> -> IO ((SEXP s)<p>) @-}
type AcquireIO s = SEXP V -> IO (SEXP s)

-- XXX: It is not possible to give a specification in LH to withAcquire.
-- Apparently the constraints of the nested function can't be expressed in
-- specs.
withAcquire
:: forall m r.
(MonadR m)
=> (forall s. Reifies s (AcquireIO (Region m)) => Proxy s -> m r)
-> m r
withAcquire f = do
cxt <- getExecContext
reify (AcquireIO (\sx -> unsafeRunWithExecContext (acquire sx) cxt)) f
reify (\sx -> unsafeRunWithExecContext (acquire sx) cxt) f

getAcquireIO
:: MonadR m => m (AcquireIO (Region m))
getAcquireIO = do
cxt <- getExecContext
return (\sx -> unsafeRunWithExecContext (acquire sx) cxt)
Loading
Loading