{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
module DataFrame.Typed.Types (
TypedDataFrame (..),
Column,
TExpr (..),
TSortOrder (..),
TypedGrouped (..),
TAgg (..),
taggToNamedExprs,
These (..),
) where
import Data.Kind (Type)
import Data.These (These (..))
import GHC.TypeLits (Symbol)
import qualified Data.Text as T
import DataFrame.Internal.Column (Columnable)
import qualified DataFrame.Internal.DataFrame as D
import DataFrame.Internal.Expression (Expr, NamedExpr, UExpr (..))
newtype TypedDataFrame (cols :: [Type]) = TDF {forall (cols :: [*]). TypedDataFrame cols -> DataFrame
unTDF :: D.DataFrame}
instance Show (TypedDataFrame cols) where
show :: TypedDataFrame cols -> String
show (TDF DataFrame
df) = DataFrame -> String
forall a. Show a => a -> String
show DataFrame
df
instance Eq (TypedDataFrame cols) where
(TDF DataFrame
a) == :: TypedDataFrame cols -> TypedDataFrame cols -> Bool
== (TDF DataFrame
b) = DataFrame
a DataFrame -> DataFrame -> Bool
forall a. Eq a => a -> a -> Bool
== DataFrame
b
data Column (name :: Symbol) (a :: Type)
newtype TExpr (cols :: [Type]) a = TExpr {forall (cols :: [*]) a. TExpr cols a -> Expr a
unTExpr :: Expr a}
data TSortOrder (cols :: [Type]) where
Asc :: (Columnable a) => TExpr cols a -> TSortOrder cols
Desc :: (Columnable a) => TExpr cols a -> TSortOrder cols
newtype TypedGrouped (keys :: [Symbol]) (cols :: [Type])
= TGD {forall (keys :: [Symbol]) (cols :: [*]).
TypedGrouped keys cols -> GroupedDataFrame
unTGD :: D.GroupedDataFrame}
data TAgg (keys :: [Symbol]) (cols :: [Type]) (aggs :: [Type]) where
TAggNil :: TAgg keys cols '[]
TAggCons ::
(Columnable a) =>
T.Text ->
TExpr cols a ->
TAgg keys cols aggs ->
TAgg keys cols (Column name a ': aggs)
taggToNamedExprs :: TAgg keys cols aggs -> [NamedExpr]
taggToNamedExprs :: forall (keys :: [Symbol]) (cols :: [*]) (aggs :: [*]).
TAgg keys cols aggs -> [NamedExpr]
taggToNamedExprs = [NamedExpr] -> [NamedExpr]
forall a. [a] -> [a]
reverse ([NamedExpr] -> [NamedExpr])
-> (TAgg keys cols aggs -> [NamedExpr])
-> TAgg keys cols aggs
-> [NamedExpr]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TAgg keys cols aggs -> [NamedExpr]
forall (keys :: [Symbol]) (cols :: [*]) (aggs :: [*]).
TAgg keys cols aggs -> [NamedExpr]
go
where
go :: TAgg keys cols aggs -> [NamedExpr]
go :: forall (keys :: [Symbol]) (cols :: [*]) (aggs :: [*]).
TAgg keys cols aggs -> [NamedExpr]
go TAgg keys cols aggs
TAggNil = []
go (TAggCons Text
name (TExpr Expr a
expr) TAgg keys cols aggs
rest) = (Text
name, Expr a -> UExpr
forall a. Columnable a => Expr a -> UExpr
UExpr Expr a
expr) NamedExpr -> [NamedExpr] -> [NamedExpr]
forall a. a -> [a] -> [a]
: TAgg keys cols aggs -> [NamedExpr]
forall (keys :: [Symbol]) (cols :: [*]) (aggs :: [*]).
TAgg keys cols aggs -> [NamedExpr]
go TAgg keys cols aggs
rest