{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Variable.Variable where
import Data.Aeson (ToJSON (..), Value)
import Data.Singletons
import Data.Text
import qualified Data.Vector as V
import GHC.Generics (Generic)
import Variable.Attributes (VarAttrs (..))
import Variable.R.Factor
import Variable.R.SEXP
import Variable.R.Stype
data Variable where
RVector :: (RTypeRepConstraints r) => RTypeRep r -> VarAttrs -> Variable
RFactor :: Factor -> VarAttrs -> Variable
StypeVector :: (RTypeRepConstraints r) => Stype r -> VarAttrs -> Variable
RAtomicVectorElem :: (RTypeRepConstraints r) => SEXPElem r -> VarAttrs -> Variable
type VariableRow = [Variable]
rVector :: (RTypeRepConstraints r) => Text -> RTypeRep r -> Variable
rVector :: forall (r :: SEXPTYPE).
RTypeRepConstraints r =>
Text -> RTypeRep r -> Variable
rVector Text
nm RTypeRep r
rv = forall (r :: SEXPTYPE).
RTypeRepConstraints r =>
RTypeRep r -> VarAttrs -> Variable
RVector RTypeRep r
rv VarAttrs
a
where
a :: VarAttrs
a = Text -> Text -> VarAttrs
MkVarAttrs (forall (r :: SEXPTYPE). SingI r => RTypeRep r -> Text
textSEXPTYPEOf RTypeRep r
rv) Text
nm
rFactor :: (AsCharacter a) => Text -> a -> V.Vector Text -> Variable
rFactor :: forall a. AsCharacter a => Text -> a -> Vector Text -> Variable
rFactor Text
nm a
rv Vector Text
lvls = Factor -> VarAttrs -> Variable
RFactor (forall a. AsCharacter a => a -> Vector Text -> Factor
factor a
rv Vector Text
lvls) VarAttrs
a
where
a :: VarAttrs
a = Text -> Text -> VarAttrs
MkVarAttrs Text
"STRSXP" Text
nm
stypeVector :: (RTypeRepConstraints r) => Text -> Stype r -> Variable
stypeVector :: forall (r :: SEXPTYPE).
RTypeRepConstraints r =>
Text -> Stype r -> Variable
stypeVector Text
nm Stype r
sv = forall (r :: SEXPTYPE).
RTypeRepConstraints r =>
Stype r -> VarAttrs -> Variable
StypeVector Stype r
sv VarAttrs
a
where
a :: VarAttrs
a = Text -> Text -> VarAttrs
MkVarAttrs Text
ty Text
nm
(Text
ty, StypeAttrs
_) = forall (r :: SEXPTYPE). Stype r -> (Text, StypeAttrs)
stypeInfoOf Stype r
sv
rAtomicVectorElem :: (RTypeRepConstraints r) => Text -> SEXPElem r -> Variable
rAtomicVectorElem :: forall (r :: SEXPTYPE).
RTypeRepConstraints r =>
Text -> SEXPElem r -> Variable
rAtomicVectorElem Text
nm SEXPElem r
e = forall (r :: SEXPTYPE).
RTypeRepConstraints r =>
SEXPElem r -> VarAttrs -> Variable
RAtomicVectorElem SEXPElem r
e VarAttrs
a
where
a :: VarAttrs
a = Text -> Text -> VarAttrs
MkVarAttrs Text
ty Text
nm
ty :: Text
ty = forall (r :: SEXPTYPE). SingI r => SEXPElem r -> Text
textSEXPTYPEOfElem SEXPElem r
e
data VariableWrapped = MkVariableWrapped
{
VariableWrapped -> Text
varTarget :: Text,
VariableWrapped -> Value
vals :: Value,
VariableWrapped -> VarAttrs
attrs :: VarAttrs,
VariableWrapped -> Value
subAttrs :: Value
}
deriving (VariableWrapped -> VariableWrapped -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VariableWrapped -> VariableWrapped -> Bool
$c/= :: VariableWrapped -> VariableWrapped -> Bool
== :: VariableWrapped -> VariableWrapped -> Bool
$c== :: VariableWrapped -> VariableWrapped -> Bool
Eq, forall x. Rep VariableWrapped x -> VariableWrapped
forall x. VariableWrapped -> Rep VariableWrapped x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep VariableWrapped x -> VariableWrapped
$cfrom :: forall x. VariableWrapped -> Rep VariableWrapped x
Generic, Eq VariableWrapped
VariableWrapped -> VariableWrapped -> Bool
VariableWrapped -> VariableWrapped -> Ordering
VariableWrapped -> VariableWrapped -> VariableWrapped
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: VariableWrapped -> VariableWrapped -> VariableWrapped
$cmin :: VariableWrapped -> VariableWrapped -> VariableWrapped
max :: VariableWrapped -> VariableWrapped -> VariableWrapped
$cmax :: VariableWrapped -> VariableWrapped -> VariableWrapped
>= :: VariableWrapped -> VariableWrapped -> Bool
$c>= :: VariableWrapped -> VariableWrapped -> Bool
> :: VariableWrapped -> VariableWrapped -> Bool
$c> :: VariableWrapped -> VariableWrapped -> Bool
<= :: VariableWrapped -> VariableWrapped -> Bool
$c<= :: VariableWrapped -> VariableWrapped -> Bool
< :: VariableWrapped -> VariableWrapped -> Bool
$c< :: VariableWrapped -> VariableWrapped -> Bool
compare :: VariableWrapped -> VariableWrapped -> Ordering
$ccompare :: VariableWrapped -> VariableWrapped -> Ordering
Ord, Int -> VariableWrapped -> ShowS
[VariableWrapped] -> ShowS
VariableWrapped -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VariableWrapped] -> ShowS
$cshowList :: [VariableWrapped] -> ShowS
show :: VariableWrapped -> String
$cshow :: VariableWrapped -> String
showsPrec :: Int -> VariableWrapped -> ShowS
$cshowsPrec :: Int -> VariableWrapped -> ShowS
Show)
instance ToJSON VariableWrapped
instance Show Variable where
show :: Variable -> String
show = forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. Variable -> VariableWrapped
asVariableWrapped
instance ToJSON Variable where
toJSON :: Variable -> Value
toJSON = forall a. ToJSON a => a -> Value
toJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. Variable -> VariableWrapped
asVariableWrapped
atomicRVectorAsWrapped :: (RTypeRepConstraints s) => RTypeRep s -> VarAttrs -> VariableWrapped
atomicRVectorAsWrapped :: forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
RTypeRep s -> VarAttrs -> VariableWrapped
atomicRVectorAsWrapped Vector (Maybe (SEXPElem s))
rv VarAttrs
a = Text -> Value -> VarAttrs -> Value -> VariableWrapped
MkVariableWrapped Text
"RVector" (forall a. ToJSON a => a -> Value
toJSON Vector (Maybe (SEXPElem s))
rv) VarAttrs
a (forall a. ToJSON a => a -> Value
toJSON ())
rVectorAsWrappedA :: (RTypeRepConstraints s) => Sing s -> RTypeRep s -> VarAttrs -> VariableWrapped
rVectorAsWrappedA :: forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
Sing s -> RTypeRep s -> VarAttrs -> VariableWrapped
rVectorAsWrappedA Sing s
sxp RTypeRep s
rv VarAttrs
a = case Sing s
sxp of
Sing s
SSEXPTYPE s
SLGLSXP -> forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
RTypeRep s -> VarAttrs -> VariableWrapped
atomicRVectorAsWrapped RTypeRep s
rv VarAttrs
a
Sing s
SSEXPTYPE s
SINTSXP -> forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
RTypeRep s -> VarAttrs -> VariableWrapped
atomicRVectorAsWrapped RTypeRep s
rv VarAttrs
a
Sing s
SSEXPTYPE s
SREALSXP -> forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
RTypeRep s -> VarAttrs -> VariableWrapped
atomicRVectorAsWrapped RTypeRep s
rv VarAttrs
a
Sing s
SSEXPTYPE s
SCPLSXP -> forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
RTypeRep s -> VarAttrs -> VariableWrapped
atomicRVectorAsWrapped RTypeRep s
rv VarAttrs
a
Sing s
SSEXPTYPE s
SSTRSXP -> forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
RTypeRep s -> VarAttrs -> VariableWrapped
atomicRVectorAsWrapped RTypeRep s
rv VarAttrs
a
Sing s
SSEXPTYPE s
SVECSXP ->
Text -> Value -> VarAttrs -> Value -> VariableWrapped
MkVariableWrapped
Text
"RVector"
( forall a. ToJSON a => a -> Value
toJSON forall a b. (a -> b) -> a -> b
$
forall a b. (a -> b) -> Vector a -> Vector b
V.map
( \case
Maybe (Text, SomeRTypeRep)
Nothing -> forall a. Maybe a
Nothing
Just (Text
nm, SomeRTypeRep
v) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
SomeRTypeRep
-> (forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
RTypeRep s -> t)
-> t
withSomeRTypeRep SomeRTypeRep
v (\RTypeRep s
rv' -> forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
Sing s -> RTypeRep s -> VarAttrs -> VariableWrapped
rVectorAsWrappedA (forall (r :: SEXPTYPE). SingI r => RTypeRep r -> Sing r
singOfRTypeRep RTypeRep s
rv') RTypeRep s
rv' (Text -> Text -> VarAttrs
MkVarAttrs (SomeRTypeRep -> Text
textSEXPTYPEOfErased SomeRTypeRep
v) Text
nm))
)
RTypeRep s
rv
)
VarAttrs
a
(forall a. ToJSON a => a -> Value
toJSON ())
rVectorAsWrapped :: forall s. (RTypeRepConstraints s) => RTypeRep s -> VarAttrs -> VariableWrapped
rVectorAsWrapped :: forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
RTypeRep s -> VarAttrs -> VariableWrapped
rVectorAsWrapped = forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
Sing s -> RTypeRep s -> VarAttrs -> VariableWrapped
rVectorAsWrappedA (forall {k} (a :: k). SingI a => Sing a
sing @s)
asVariableWrapped :: Variable -> VariableWrapped
asVariableWrapped :: Variable -> VariableWrapped
asVariableWrapped (RVector RTypeRep r
rv VarAttrs
a) = forall (s :: SEXPTYPE).
RTypeRepConstraints s =>
RTypeRep s -> VarAttrs -> VariableWrapped
rVectorAsWrapped RTypeRep r
rv VarAttrs
a
asVariableWrapped (RFactor Factor
rv VarAttrs
a) = Text -> Value -> VarAttrs -> Value -> VariableWrapped
MkVariableWrapped Text
"RFactor" (forall a. ToJSON a => a -> Value
toJSON forall a b. (a -> b) -> a -> b
$ Factor -> RTypeRep 'STRSXP
values Factor
rv) VarAttrs
a (forall a. ToJSON a => a -> Value
toJSON forall a b. (a -> b) -> a -> b
$ Factor -> Vector Text
levels Factor
rv)
asVariableWrapped (StypeVector Stype r
sv VarAttrs
a) = Text -> Value -> VarAttrs -> Value -> VariableWrapped
MkVariableWrapped Text
"StypeVector" (forall a. ToJSON a => a -> Value
toJSON forall a b. (a -> b) -> a -> b
$ forall (r :: SEXPTYPE). Stype r -> RTypeRep r
stypeRTypeRep Stype r
sv) VarAttrs
a (forall a. ToJSON a => a -> Value
toJSON forall a b. (a -> b) -> a -> b
$ forall (r :: SEXPTYPE). Stype r -> WrappedStypeAttrs
wrappedStypeAttrs Stype r
sv)
asVariableWrapped (RAtomicVectorElem SEXPElem r
e VarAttrs
a) = Text -> Value -> VarAttrs -> Value -> VariableWrapped
MkVariableWrapped Text
"RAtomicVectorElem" (forall a. ToJSON a => a -> Value
toJSON SEXPElem r
e) VarAttrs
a (forall a. ToJSON a => a -> Value
toJSON ())
varAttrs :: Variable -> VarAttrs
varAttrs :: Variable -> VarAttrs
varAttrs (RVector RTypeRep r
_ VarAttrs
a) = VarAttrs
a
varAttrs (RFactor Factor
_ VarAttrs
a) = VarAttrs
a
varAttrs (StypeVector Stype r
_ VarAttrs
a) = VarAttrs
a
varAttrs (RAtomicVectorElem SEXPElem r
_ VarAttrs
a) = VarAttrs
a