{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Frames.ColumnUniverse (
    CoRec,
    Columns,
    ColumnUniverse,
    ColInfo,
    CommonColumns,
    CommonColumnsCat,
    parsedTypeRep,
) where

import Data.Maybe (fromMaybe)
#if __GLASGOW_HASKELL__ < 808
import Data.Semigroup (Semigroup((<>)))
#endif
import Data.Either (fromRight)
import qualified Data.Text as T
import Data.Vinyl
import Data.Vinyl.CoRec
import Data.Vinyl.Functor
import Data.Vinyl.TypeLevel (NatToInt, RIndex)
import Frames.Categorical
import Frames.ColumnTypeable
import Language.Haskell.TH

-- | Extract a function to test whether some value of a given type
-- could be read from some 'T.Text'.
inferParseable :: (Parseable a) => T.Text -> (Maybe :. Parsed) a
inferParseable :: forall a. Parseable a => Text -> (:.) Maybe Parsed a
inferParseable = Maybe (Parsed a) -> Compose Maybe Parsed a
forall l k (f :: l -> *) (g :: k -> l) (x :: k).
f (g x) -> Compose f g x
Compose (Maybe (Parsed a) -> Compose Maybe Parsed a)
-> (Text -> Maybe (Parsed a)) -> Text -> Compose Maybe Parsed a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe (Parsed a)
forall a (m :: * -> *).
(Parseable a, MonadPlus m) =>
Text -> m (Parsed a)
forall (m :: * -> *). MonadPlus m => Text -> m (Parsed a)
parse

-- | Helper to call 'inferParseable' on variants of a 'CoRec'.
inferParseable' :: (Parseable a) => ((->) T.Text :. (Maybe :. Parsed)) a
inferParseable' :: forall a. Parseable a => (:.) ((->) Text) (Maybe :. Parsed) a
inferParseable' = (Text -> (:.) Maybe Parsed a)
-> Compose ((->) Text) (Maybe :. Parsed) a
forall l k (f :: l -> *) (g :: k -> l) (x :: k).
f (g x) -> Compose f g x
Compose Text -> (:.) Maybe Parsed a
forall a. Parseable a => Text -> (:.) Maybe Parsed a
inferParseable

-- * Record Helpers

tryParseAll ::
    forall ts.
    (RecApplicative ts, RPureConstrained Parseable ts) =>
    T.Text
    -> Rec (Maybe :. Parsed) ts
tryParseAll :: forall (ts :: [*]).
(RecApplicative ts, RPureConstrained Parseable ts) =>
Text -> Rec (Maybe :. Parsed) ts
tryParseAll = (forall x.
 (:.) ((->) Text) (Maybe :. Parsed) x
 -> Text -> (:.) Maybe Parsed x)
-> Rec ((->) Text :. (Maybe :. Parsed)) ts
-> Text
-> Rec (Maybe :. Parsed) ts
forall {u} (h :: * -> *) (f :: u -> *) (g :: u -> *) (rs :: [u]).
Applicative h =>
(forall (x :: u). f x -> h (g x)) -> Rec f rs -> h (Rec g rs)
rtraverse Compose ((->) Text) (Maybe :. Parsed) x
-> Text -> (:.) Maybe Parsed x
forall x.
(:.) ((->) Text) (Maybe :. Parsed) x -> Text -> (:.) Maybe Parsed x
forall l k (f :: l -> *) (g :: k -> l) (x :: k).
Compose f g x -> f (g x)
getCompose Rec ((->) Text :. (Maybe :. Parsed)) ts
funs
  where
    funs :: Rec (((->) T.Text) :. (Maybe :. Parsed)) ts
    funs :: Rec ((->) Text :. (Maybe :. Parsed)) ts
funs = forall {k} (c :: k -> Constraint) (ts :: [k]) (f :: k -> *).
RPureConstrained c ts =>
(forall (a :: k). c a => f a) -> Rec f ts
forall (c :: * -> Constraint) (ts :: [*]) (f :: * -> *).
RPureConstrained c ts =>
(forall a. c a => f a) -> Rec f ts
rpureConstrained @Parseable (:.) ((->) Text) (Maybe :. Parsed) a
forall a. Parseable a => (:.) ((->) Text) (Maybe :. Parsed) a
inferParseable'

-- * Column Type Inference

-- | Information necessary for synthesizing row types and comparing
-- types.
newtype ColInfo a = ColInfo (Either (String -> Q [Dec]) Type, Parsed a)

instance (Show a) => Show (ColInfo a) where
    show :: ColInfo a -> String
show (ColInfo (Either (String -> Q [Dec]) Type
t, Parsed a
p)) =
        String
"(ColInfo {"
            String -> ShowS
forall a. [a] -> [a] -> [a]
++ ((String -> Q [Dec]) -> String)
-> (Type -> String) -> Either (String -> Q [Dec]) Type -> String
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> (String -> Q [Dec]) -> String
forall a b. a -> b -> a
const String
"cat") Type -> String
forall a. Show a => a -> String
show Either (String -> Q [Dec]) Type
t
            String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", "
            String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show (Parsed a -> a
forall a. Parsed a -> a
discardConfidence Parsed a
p)
            String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"})"

parsedToColInfo :: (Parseable a) => Parsed a -> ColInfo a
parsedToColInfo :: forall a. Parseable a => Parsed a -> ColInfo a
parsedToColInfo Parsed a
x = case Const (Either (String -> Q [Dec]) Type) a
-> Either (String -> Q [Dec]) Type
forall k a (b :: k). Const a b -> a
getConst Const (Either (String -> Q [Dec]) Type) a
rep of
    Left String -> Q [Dec]
dec -> (Either (String -> Q [Dec]) Type, Parsed a) -> ColInfo a
forall a. (Either (String -> Q [Dec]) Type, Parsed a) -> ColInfo a
ColInfo ((String -> Q [Dec]) -> Either (String -> Q [Dec]) Type
forall a b. a -> Either a b
Left String -> Q [Dec]
dec, Parsed a
x)
    Right Type
ty ->
        (Either (String -> Q [Dec]) Type, Parsed a) -> ColInfo a
forall a. (Either (String -> Q [Dec]) Type, Parsed a) -> ColInfo a
ColInfo (Type -> Either (String -> Q [Dec]) Type
forall a b. b -> Either a b
Right Type
ty, Parsed a
x)
  where
    rep :: Const (Either (String -> Q [Dec]) Type) a
rep = Parsed a -> Const (Either (String -> Q [Dec]) Type) a
forall a.
Parseable a =>
Parsed a -> Const (Either (String -> Q [Dec]) Type) a
representableAsType Parsed a
x

parsedTypeRep :: ColInfo a -> Parsed Type
parsedTypeRep :: forall a. ColInfo a -> Parsed Type
parsedTypeRep (ColInfo (Either (String -> Q [Dec]) Type
t, Parsed a
p)) =
    Type -> Either (String -> Q [Dec]) Type -> Type
forall b a. b -> Either a b -> b
fromRight (Name -> Type
ConT (String -> Name
mkName String
"Categorical")) Either (String -> Q [Dec]) Type
t Type -> Parsed a -> Parsed Type
forall a b. a -> Parsed b -> Parsed a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parsed a
p

-- | Map 'Type's we know about (with a special treatment of
-- synthesized types for categorical variables) to 'Int's for ordering
-- purposes.
orderParsePriorities :: Parsed (Maybe Type) -> Maybe Int
orderParsePriorities :: Parsed (Maybe Type) -> Maybe Int
orderParsePriorities Parsed (Maybe Type)
x =
    case Parsed (Maybe Type) -> Maybe Type
forall a. Parsed a -> a
discardConfidence Parsed (Maybe Type)
x of
        Maybe Type
Nothing -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
6) -- categorical variable
        Just Type
t
            | Type
t Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
tyText -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
uncertainty)
            | Type
t Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
tyDbl -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
uncertainty)
            | Type
t Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
tyInt -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
3 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
uncertainty)
            | Type
t Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
tyBool -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
uncertainty)
            | Bool
otherwise -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
5 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
uncertainty) -- Unknown type
  where
    tyText :: Type
tyText = Name -> Type
ConT (String -> Name
mkName String
"Text")
    tyDbl :: Type
tyDbl = Name -> Type
ConT (String -> Name
mkName String
"Double")
    tyInt :: Type
tyInt = Name -> Type
ConT (String -> Name
mkName String
"Int")
    tyBool :: Type
tyBool = Name -> Type
ConT (String -> Name
mkName String
"Bool")
    uncertainty :: Int
uncertainty = case Parsed (Maybe Type)
x of Definitely Maybe Type
_ -> Int
0; Possibly Maybe Type
_ -> Int
6

-- | We use a join semi-lattice on types for representations. The
--  bottom of the lattice is effectively an error (we have nothing to
--  represent), @Bool < Int@, @Int < Double@, and @forall n. n <= Text@.
--
--  The high-level goal here is that we will pick the "greater" of two
--  choices in 'bestRep'. A 'Definitely' parse result is preferred over
--  a 'Possibly' parse result. If we have two distinct 'Possibly' parse
--  results, we give up. If we have two distinct 'Definitely' parse
--  results, we are in dangerous waters: all data is parseable at
--  /both/ types, so which do we default to? The defaulting choices
--  made here are described in the previous paragraph. If there is no
--  defaulting rule, we give up (i.e. use 'T.Text' as a
--  representation).
lubTypes :: Parsed (Maybe Type) -> Parsed (Maybe Type) -> Maybe Ordering
lubTypes :: Parsed (Maybe Type) -> Parsed (Maybe Type) -> Maybe Ordering
lubTypes Parsed (Maybe Type)
x Parsed (Maybe Type)
y = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Int -> Int -> Ordering) -> Maybe Int -> Maybe (Int -> Ordering)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parsed (Maybe Type) -> Maybe Int
orderParsePriorities Parsed (Maybe Type)
y Maybe (Int -> Ordering) -> Maybe Int -> Maybe Ordering
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parsed (Maybe Type) -> Maybe Int
orderParsePriorities Parsed (Maybe Type)
x

-- instance (T.Text ∈ ts, RPureConstrained Parseable ts) => Monoid (CoRec ColInfo ts) where
--     mempty = CoRec (ColInfo (Right (ConT (mkName "Text")), Possibly T.empty))

-- | A helper For the 'Semigroup' instance below.
mergeEqTypeParses ::
    forall ts.
    (RPureConstrained Parseable ts, T.Text  ts) =>
    CoRec ColInfo ts
    -> CoRec ColInfo ts
    -> CoRec ColInfo ts
mergeEqTypeParses :: forall (ts :: [*]).
(RPureConstrained Parseable ts, Text ∈ ts) =>
CoRec ColInfo ts -> CoRec ColInfo ts -> CoRec ColInfo ts
mergeEqTypeParses x :: CoRec ColInfo ts
x@(CoRec ColInfo a1
_) CoRec ColInfo ts
y =
    CoRec ColInfo ts -> Maybe (CoRec ColInfo ts) -> CoRec ColInfo ts
forall a. a -> Maybe a -> a
fromMaybe CoRec ColInfo ts
definitelyText (Maybe (CoRec ColInfo ts) -> CoRec ColInfo ts)
-> Maybe (CoRec ColInfo ts) -> CoRec ColInfo ts
forall a b. (a -> b) -> a -> b
$
        (forall x. Compose Maybe ColInfo x -> Maybe (ColInfo x))
-> CoRec (Compose Maybe ColInfo) ts -> Maybe (CoRec ColInfo ts)
forall {k} (h :: * -> *) (f :: k -> *) (g :: k -> *) (ts :: [k]).
Functor h =>
(forall (x :: k). f x -> h (g x)) -> CoRec f ts -> h (CoRec g ts)
coRecTraverse
            Compose Maybe ColInfo x -> Maybe (ColInfo x)
forall x. Compose Maybe ColInfo x -> Maybe (ColInfo x)
forall l k (f :: l -> *) (g :: k -> l) (x :: k).
Compose f g x -> f (g x)
getCompose
            (forall {k} (c :: k -> Constraint) (ts :: [k]) (f :: k -> *)
       (g :: k -> *).
RPureConstrained c ts =>
(forall (x :: k). (x ∈ ts, c x) => f x -> g x)
-> CoRec f ts -> CoRec g ts
forall (c :: * -> Constraint) (ts :: [*]) (f :: * -> *)
       (g :: * -> *).
RPureConstrained c ts =>
(forall x. (x ∈ ts, c x) => f x -> g x) -> CoRec f ts -> CoRec g ts
coRecMapC @Parseable ColInfo x -> (:.) Maybe ColInfo x
forall x.
(x ∈ ts, Parseable x) =>
ColInfo x -> Compose Maybe ColInfo x
forall a.
(Parseable a, NatToInt (RIndex a ts)) =>
ColInfo a -> (:.) Maybe ColInfo a
aux CoRec ColInfo ts
x)
  where
    definitelyText :: CoRec ColInfo ts
definitelyText = ColInfo Text -> CoRec ColInfo ts
forall {k} (a1 :: k) (b :: [k]) (a :: k -> *).
RElem a1 b (RIndex a1 b) =>
a a1 -> CoRec a b
CoRec ((Either (String -> Q [Dec]) Type, Parsed Text) -> ColInfo Text
forall a. (Either (String -> Q [Dec]) Type, Parsed a) -> ColInfo a
ColInfo (Type -> Either (String -> Q [Dec]) Type
forall a b. b -> Either a b
Right (Name -> Type
ConT (String -> Name
mkName String
"Text")), Text -> Parsed Text
forall a. a -> Parsed a
Definitely Text
T.empty))
    aux ::
        forall a.
        (Parseable a, NatToInt (RIndex a ts)) =>
        ColInfo a
        -> (Maybe :. ColInfo) a
    aux :: forall a.
(Parseable a, NatToInt (RIndex a ts)) =>
ColInfo a -> (:.) Maybe ColInfo a
aux (ColInfo (Either (String -> Q [Dec]) Type
_, Parsed a
pX)) =
        case forall {k} (t :: k) (ts :: [k]) (f :: k -> *).
NatToInt (RIndex t ts) =>
CoRec f ts -> Maybe (f t)
forall t (ts :: [*]) (f :: * -> *).
NatToInt (RIndex t ts) =>
CoRec f ts -> Maybe (f t)
asA' @a CoRec ColInfo ts
y of
            Maybe (ColInfo a)
Nothing -> Maybe (ColInfo a) -> (:.) Maybe ColInfo a
forall l k (f :: l -> *) (g :: k -> l) (x :: k).
f (g x) -> Compose f g x
Compose Maybe (ColInfo a)
forall a. Maybe a
Nothing
            Just (ColInfo (Either (String -> Q [Dec]) Type
_, Parsed a
pY)) ->
                (:.) Maybe ColInfo a
-> (Parsed a -> (:.) Maybe ColInfo a)
-> Maybe (Parsed a)
-> (:.) Maybe ColInfo a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
                    (Maybe (ColInfo a) -> (:.) Maybe ColInfo a
forall l k (f :: l -> *) (g :: k -> l) (x :: k).
f (g x) -> Compose f g x
Compose Maybe (ColInfo a)
forall a. Maybe a
Nothing)
                    (Maybe (ColInfo a) -> (:.) Maybe ColInfo a
forall l k (f :: l -> *) (g :: k -> l) (x :: k).
f (g x) -> Compose f g x
Compose (Maybe (ColInfo a) -> (:.) Maybe ColInfo a)
-> (Parsed a -> Maybe (ColInfo a))
-> Parsed a
-> (:.) Maybe ColInfo a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ColInfo a -> Maybe (ColInfo a)
forall a. a -> Maybe a
Just (ColInfo a -> Maybe (ColInfo a))
-> (Parsed a -> ColInfo a) -> Parsed a -> Maybe (ColInfo a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parsed a -> ColInfo a
forall a. Parseable a => Parsed a -> ColInfo a
parsedToColInfo)
                    (Parsed a -> Parsed a -> Maybe (Parsed a)
forall a (m :: * -> *).
(Parseable a, MonadPlus m) =>
Parsed a -> Parsed a -> m (Parsed a)
forall (m :: * -> *).
MonadPlus m =>
Parsed a -> Parsed a -> m (Parsed a)
parseCombine Parsed a
pX Parsed a
pY)

instance
    (T.Text  ts, RPureConstrained Parseable ts) =>
    Semigroup (CoRec ColInfo ts)
    where
    (<>) :: (T.Text  ts, RPureConstrained Parseable ts) => CoRec ColInfo ts -> CoRec ColInfo ts -> CoRec ColInfo ts
    x :: CoRec ColInfo ts
x@(CoRec (ColInfo (Either (String -> Q [Dec]) Type
tyX, Parsed a1
pX))) <> :: (Text ∈ ts, RPureConstrained Parseable ts) =>
CoRec ColInfo ts -> CoRec ColInfo ts -> CoRec ColInfo ts
<> y :: CoRec ColInfo ts
y@(CoRec (ColInfo (Either (String -> Q [Dec]) Type
tyY, Parsed a1
pY))) =
        case Parsed (Maybe Type) -> Parsed (Maybe Type) -> Maybe Ordering
lubTypes
            (((String -> Q [Dec]) -> Maybe Type)
-> (Type -> Maybe Type)
-> Either (String -> Q [Dec]) Type
-> Maybe Type
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Type -> (String -> Q [Dec]) -> Maybe Type
forall a b. a -> b -> a
const Maybe Type
forall a. Maybe a
Nothing) Type -> Maybe Type
forall a. a -> Maybe a
Just Either (String -> Q [Dec]) Type
tyX Maybe Type -> Parsed a1 -> Parsed (Maybe Type)
forall a b. a -> Parsed b -> Parsed a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parsed a1
pX)
            (((String -> Q [Dec]) -> Maybe Type)
-> (Type -> Maybe Type)
-> Either (String -> Q [Dec]) Type
-> Maybe Type
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Type -> (String -> Q [Dec]) -> Maybe Type
forall a b. a -> b -> a
const Maybe Type
forall a. Maybe a
Nothing) Type -> Maybe Type
forall a. a -> Maybe a
Just Either (String -> Q [Dec]) Type
tyY Maybe Type -> Parsed a1 -> Parsed (Maybe Type)
forall a b. a -> Parsed b -> Parsed a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parsed a1
pY) of
            Just Ordering
GT -> CoRec ColInfo ts
x
            Just Ordering
LT -> CoRec ColInfo ts
y
            Just Ordering
EQ -> CoRec ColInfo ts -> CoRec ColInfo ts -> CoRec ColInfo ts
forall (ts :: [*]).
(RPureConstrained Parseable ts, Text ∈ ts) =>
CoRec ColInfo ts -> CoRec ColInfo ts -> CoRec ColInfo ts
mergeEqTypeParses CoRec ColInfo ts
x CoRec ColInfo ts
y
            Maybe Ordering
Nothing -> CoRec ColInfo ts
forall a. HasCallStack => a
undefined -- mempty

-- | Find the best (i.e. smallest) 'CoRec' variant to represent a
--  parsed value. For inspection in GHCi after loading this module,
--  consider this example:
--
--  >>> :set -XTypeApplications
--  >>> :set -XOverloadedStrings
--  >>> import Data.Vinyl.CoRec (foldCoRec)
--  >>> foldCoRec parsedTypeRep (bestRep @CommonColumns "2.3")
--  Definitely Double
bestRep ::
    forall ts.
    ( RPureConstrained Parseable ts
    , RPureConstrained (ShowF ColInfo) ts
    , FoldRec ts ts
    , RecApplicative ts
    , T.Text  ts
    ) =>
    T.Text
    -> CoRec ColInfo ts
bestRep :: forall (ts :: [*]).
(RPureConstrained Parseable ts,
 RPureConstrained (ShowF ColInfo) ts, FoldRec ts ts,
 RecApplicative ts, Text ∈ ts) =>
Text -> CoRec ColInfo ts
bestRep Text
t
    -- \| trace (show (aux t)) False = undefined
    | Text -> Bool
T.null Text
t Bool -> Bool -> Bool
|| Text
t Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"NA" = ColInfo Text -> CoRec ColInfo ts
forall {k} (a1 :: k) (b :: [k]) (a :: k -> *).
RElem a1 b (RIndex a1 b) =>
a a1 -> CoRec a b
CoRec (Parsed Text -> ColInfo Text
forall a. Parseable a => Parsed a -> ColInfo a
parsedToColInfo (Text -> Parsed Text
forall a. a -> Parsed a
Possibly Text
T.empty))
    | Bool
otherwise =
        forall {k} (c :: k -> Constraint) (ts :: [k]) (f :: k -> *)
       (g :: k -> *).
RPureConstrained c ts =>
(forall (x :: k). (x ∈ ts, c x) => f x -> g x)
-> CoRec f ts -> CoRec g ts
forall (c :: * -> Constraint) (ts :: [*]) (f :: * -> *)
       (g :: * -> *).
RPureConstrained c ts =>
(forall x. (x ∈ ts, c x) => f x -> g x) -> CoRec f ts -> CoRec g ts
coRecMapC @Parseable Parsed x -> ColInfo x
forall x. (x ∈ ts, Parseable x) => Parsed x -> ColInfo x
forall a. Parseable a => Parsed a -> ColInfo a
parsedToColInfo
            (CoRec Parsed ts -> CoRec ColInfo ts)
-> (Text -> CoRec Parsed ts) -> Text -> CoRec ColInfo ts
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoRec Parsed ts -> Maybe (CoRec Parsed ts) -> CoRec Parsed ts
forall a. a -> Maybe a -> a
fromMaybe (Parsed Text -> CoRec Parsed ts
forall {k} (a1 :: k) (b :: [k]) (a :: k -> *).
RElem a1 b (RIndex a1 b) =>
a a1 -> CoRec a b
CoRec (Text -> Parsed Text
forall a. a -> Parsed a
Possibly Text
T.empty :: Parsed T.Text))
            (Maybe (CoRec Parsed ts) -> CoRec Parsed ts)
-> (Text -> Maybe (CoRec Parsed ts)) -> Text -> CoRec Parsed ts
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rec (Maybe :. Parsed) ts -> Maybe (CoRec Parsed ts)
forall {k} (ts :: [k]) (f :: k -> *).
FoldRec ts ts =>
Rec (Maybe :. f) ts -> Maybe (CoRec f ts)
firstField
            (Rec (Maybe :. Parsed) ts -> Maybe (CoRec Parsed ts))
-> (Text -> Rec (Maybe :. Parsed) ts)
-> Text
-> Maybe (CoRec Parsed ts)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Rec (Maybe :. Parsed) ts
forall (ts :: [*]).
(RecApplicative ts, RPureConstrained Parseable ts) =>
Text -> Rec (Maybe :. Parsed) ts
tryParseAll :: T.Text -> Rec (Maybe :. Parsed) ts)
            (Text -> CoRec ColInfo ts) -> Text -> CoRec ColInfo ts
forall a b. (a -> b) -> a -> b
$ Text
t
-- where
--   aux =
--       coRecMapC @Parseable parsedToColInfo
--           . fromMaybe (CoRec (Possibly T.empty :: Parsed T.Text))
--           . firstField
--           . (tryParseAll :: T.Text -> Rec (Maybe :. Parsed) ts)
{-# INLINEABLE bestRep #-}

instance
    ( RPureConstrained Parseable ts
    , FoldRec ts ts
    , RPureConstrained (ShowF ColInfo) ts
    , RecApplicative ts
    , T.Text  ts
    ) =>
    ColumnTypeable (CoRec ColInfo ts)
    where
    colType :: CoRec ColInfo ts -> Either (String -> Q [Dec]) Type
colType (CoRec (ColInfo (Either (String -> Q [Dec]) Type
t, Parsed a1
_))) = Either (String -> Q [Dec]) Type
t
    {-# INLINE colType #-}
    inferType :: Text -> CoRec ColInfo ts
inferType = Text -> CoRec ColInfo ts
forall (ts :: [*]).
(RPureConstrained Parseable ts,
 RPureConstrained (ShowF ColInfo) ts, FoldRec ts ts,
 RecApplicative ts, Text ∈ ts) =>
Text -> CoRec ColInfo ts
bestRep
    {-# INLINEABLE inferType #-}

#if !MIN_VERSION_vinyl(0,11,0)
instance forall ts. (RPureConstrained Show ts, RecApplicative ts)
  => Show (CoRec ColInfo ts) where
  show x = "(Col " ++ onCoRec @Show show x ++")"
#endif  

-- * Common Columns

-- | Common column types: 'Bool', 'Int', 'Double', 'T.Text'
type CommonColumns = [Bool, Int, Double, T.Text]

-- | Common column types including categorical types.
type CommonColumnsCat = [Bool, Int, Double, Categorical 8, T.Text]

-- | Define a set of variants that captures all possible column types.
type ColumnUniverse = CoRec ColInfo

-- | A universe of common column variants. These are the default
--  column types that @Frames@ can infer. See the
--  <http://acowley.github.io/Frames/#sec-4 Tutorial> for an example of
--  extending the default types with your own.
type Columns = ColumnUniverse CommonColumns