{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ConstraintKinds, FlexibleContexts, GADTs, ScopedTypeVariables,
TemplateHaskell, TypeOperators #-}
module Frames.Exploration (pipePreview, select, lenses, recToList,
pr, pr1) where
import Data.Char (isSpace, isUpper)
import Data.Proxy
import qualified Data.Vinyl as V
import qualified Data.Vinyl.Class.Method as V
import Data.Vinyl.Functor (ElField(Field), Const(..))
import Frames.Rec
import GHC.TypeLits (Symbol)
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Pipes hiding (Proxy)
import qualified Pipes.Prelude as P
import Pipes.Safe (SafeT, runSafeT, MonadMask)
pipePreview :: (Show b, MonadIO m, MonadMask m)
=> Producer a (SafeT m) () -> Int -> Pipe a b (SafeT m) () -> m ()
pipePreview src n f = runSafeT . runEffect $ src >-> f >-> P.take n >-> P.print
select :: (fs V.⊆ rs) => proxy fs -> Record rs -> Record fs
select _ = V.rcast
lenses :: (fs V.⊆ rs, Functor f)
=> proxy fs -> (Record fs -> f (Record fs)) -> Record rs -> f (Record rs)
lenses _ = V.rsubset
{-# DEPRECATED select "Use Data.Vinyl.rcast with a type application. " #-}
{-# DEPRECATED lenses "Use Data.Vinyl.rsubset with a type application." #-}
pr :: QuasiQuoter
pr = QuasiQuoter mkProxy undefined undefined undefined
where mkProxy s = let ts = map strip $ splitOn ',' s
cons = mapM (conT . mkName) ts
mkList = foldr (AppT . AppT PromotedConsT) PromotedNilT
in case ts of
[h@(t:_)]
| isUpper t -> [|Proxy::Proxy $(fmap head cons)|]
| otherwise -> [|Proxy::Proxy $(varT $ mkName h)|]
_ -> [|Proxy::Proxy $(fmap mkList cons)|]
pr1 :: QuasiQuoter
pr1 = QuasiQuoter mkProxy undefined undefined undefined
where mkProxy s = let sing x = AppT (AppT PromotedConsT x) PromotedNilT
in case s of
t:_
| isUpper t ->
[|Proxy::Proxy $(fmap sing (conT (mkName s)))|]
| otherwise ->
[|Proxy::Proxy $(fmap sing (varT $ mkName s))|]
_ -> error "Empty string passed to pr1"
recToList :: forall a (rs :: [(Symbol,*)]).
(V.RecMapMethod ((~) a) ElField rs, V.RecordToList rs)
=> Record rs -> [a]
recToList = V.recordToList . V.rmapMethod @((~) a) aux
where aux :: a ~ (V.PayloadType ElField t) => V.ElField t -> Const a t
aux (Field x) = Const x
splitOn :: Eq a => a -> [a] -> [[a]]
splitOn d = go
where go [] = []
go xs = let (h,t) = break (== d) xs
in case t of
[] -> [h]
(_:t') -> h : go t'
strip :: String -> String
strip = takeWhile (not . isSpace) . dropWhile isSpace