{-# LANGUAGE CPP             #-}
{-# LANGUAGE TemplateHaskell #-}
--------------------------------------------------------------------------------
-- |
-- Module      :  Data.Comp.Derive.Utils
-- Copyright   :  (c) 2010-2011 Patrick Bahr
-- License     :  BSD3
-- Maintainer  :  Patrick Bahr <paba@diku.dk>
-- Stability   :  experimental
-- Portability :  non-portable (GHC Extensions)
--
-- This module defines some utility functions for deriving instances
-- for functor based type classes.
--
--------------------------------------------------------------------------------
module Data.Comp.Derive.Utils where


import Control.Monad
import Data.Proxy
import Language.Haskell.TH
import Language.Haskell.TH.ExpandSyns
import Language.Haskell.TH.Syntax

-- reportError is introduced only from version 7.6 of GHC
#if __GLASGOW_HASKELL__ < 706
reportError :: String -> Q ()
reportError = report True
#endif

#if __GLASGOW_HASKELL__ < 800
data DataInfo = DataInfo Cxt Name [TyVarBndr] [Con] [Name]
#elif __GLASGOW_HASKELL__ >= 802
data DataInfo = DataInfo Cxt Name [TyVarBndr] [Con] [DerivClause]
#else
data DataInfo = DataInfo Cxt Name [TyVarBndr] [Con] Cxt
#endif

{-|
  This is the @Q@-lifted version of 'abstractNewtype.
-}
abstractNewtypeQ :: Q Info -> Q (Maybe DataInfo)
abstractNewtypeQ :: Q Info -> Q (Maybe DataInfo)
abstractNewtypeQ = (Info -> Maybe DataInfo) -> Q Info -> Q (Maybe DataInfo)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Info -> Maybe DataInfo
abstractNewtype

{-|
  This function abstracts away @newtype@ declaration, it turns them into
  @data@ declarations.
-}
abstractNewtype :: Info -> Maybe DataInfo
#if __GLASGOW_HASKELL__ < 800
abstractNewtype (TyConI (NewtypeD cxt name args constr derive))
    = Just (DataInfo cxt name args [constr] derive)
abstractNewtype (TyConI (DataD cxt name args constrs derive))
    = Just (DataInfo cxt name args constrs derive)
#else
abstractNewtype :: Info -> Maybe DataInfo
abstractNewtype (TyConI (NewtypeD cxt :: Cxt
cxt name :: Name
name args :: [TyVarBndr]
args _ constr :: Con
constr derive :: [DerivClause]
derive))
    = DataInfo -> Maybe DataInfo
forall a. a -> Maybe a
Just (Cxt -> Name -> [TyVarBndr] -> [Con] -> [DerivClause] -> DataInfo
DataInfo Cxt
cxt Name
name [TyVarBndr]
args [Con
constr] [DerivClause]
derive)
abstractNewtype (TyConI (DataD cxt :: Cxt
cxt name :: Name
name args :: [TyVarBndr]
args _ constrs :: [Con]
constrs derive :: [DerivClause]
derive))
    = DataInfo -> Maybe DataInfo
forall a. a -> Maybe a
Just (Cxt -> Name -> [TyVarBndr] -> [Con] -> [DerivClause] -> DataInfo
DataInfo Cxt
cxt Name
name [TyVarBndr]
args [Con]
constrs [DerivClause]
derive)
#endif
abstractNewtype _ = Maybe DataInfo
forall a. Maybe a
Nothing

{-| This function provides the name and the arity of the given data
constructor, and if it is a GADT also its type.
-}
normalCon :: Con -> (Name,[StrictType], Maybe Type)
normalCon :: Con -> (Name, [StrictType], Maybe Type)
normalCon (NormalC constr :: Name
constr args :: [StrictType]
args) = (Name
constr, [StrictType]
args, Maybe Type
forall a. Maybe a
Nothing)
normalCon (RecC constr :: Name
constr args :: [VarBangType]
args) = (Name
constr, (VarBangType -> StrictType) -> [VarBangType] -> [StrictType]
forall a b. (a -> b) -> [a] -> [b]
map (\(_,s :: Bang
s,t :: Type
t) -> (Bang
s,Type
t)) [VarBangType]
args, Maybe Type
forall a. Maybe a
Nothing)
normalCon (InfixC a :: StrictType
a constr :: Name
constr b :: StrictType
b) = (Name
constr, [StrictType
a,StrictType
b], Maybe Type
forall a. Maybe a
Nothing)
normalCon (ForallC _ _ constr :: Con
constr) = Con -> (Name, [StrictType], Maybe Type)
normalCon Con
constr
#if __GLASGOW_HASKELL__ >= 800
normalCon (GadtC (constr :: Name
constr:_constrs :: [Name]
_constrs) args :: [StrictType]
args typ :: Type
typ) = (Name
constr,[StrictType]
args,Type -> Maybe Type
forall a. a -> Maybe a
Just Type
typ)
#endif

normalCon' :: Con -> (Name,[Type], Maybe Type)
normalCon' :: Con -> (Name, Cxt, Maybe Type)
normalCon' con :: Con
con = (Name
n, (StrictType -> Type) -> [StrictType] -> Cxt
forall a b. (a -> b) -> [a] -> [b]
map StrictType -> Type
forall a b. (a, b) -> b
snd [StrictType]
ts, Maybe Type
t)
  where (n :: Name
n, ts :: [StrictType]
ts, t :: Maybe Type
t) = Con -> (Name, [StrictType], Maybe Type)
normalCon Con
con
      

-- -- | Same as normalCon' but expands type synonyms.
-- normalConExp :: Con -> Q (Name,[Type])
-- normalConExp c = do
--   let (n,ts,t) = normalCon' c
--   ts' <- mapM expandSyns ts
--   return (n, ts')

-- | Same as normalCon' but expands type synonyms.
normalConExp :: Con -> Q (Name,[Type], Maybe Type)
normalConExp :: Con -> Q (Name, Cxt, Maybe Type)
normalConExp c :: Con
c = do
  let (n :: Name
n,ts :: Cxt
ts,t :: Maybe Type
t) = Con -> (Name, Cxt, Maybe Type)
normalCon' Con
c
  Cxt
ts' <- (Type -> Q Type) -> Cxt -> Q Cxt
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> Q Type
expandSyns Cxt
ts
  (Name, Cxt, Maybe Type) -> Q (Name, Cxt, Maybe Type)
forall (m :: * -> *) a. Monad m => a -> m a
return (Name
n, Cxt
ts',Maybe Type
t)


-- | Same as normalConExp' but retains strictness annotations.
normalConStrExp :: Con -> Q (Name,[StrictType], Maybe Type)
normalConStrExp :: Con -> Q (Name, [StrictType], Maybe Type)
normalConStrExp c :: Con
c = do
  let (n :: Name
n,ts :: [StrictType]
ts,t :: Maybe Type
t) = Con -> (Name, [StrictType], Maybe Type)
normalCon Con
c
  [StrictType]
ts' <- (StrictType -> Q StrictType) -> [StrictType] -> Q [StrictType]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\ (st :: Bang
st,ty :: Type
ty) -> do Type
ty' <- Type -> Q Type
expandSyns Type
ty; StrictType -> Q StrictType
forall (m :: * -> *) a. Monad m => a -> m a
return (Bang
st,Type
ty')) [StrictType]
ts
  (Name, [StrictType], Maybe Type)
-> Q (Name, [StrictType], Maybe Type)
forall (m :: * -> *) a. Monad m => a -> m a
return (Name
n, [StrictType]
ts',Maybe Type
t)

-- | Auxiliary function to extract the first argument of a binary type
-- application (the second argument of this function). If the second
-- argument is @Nothing@ or not of the right shape, the first argument
-- is returned as a default.

getBinaryFArg :: Type -> Maybe Type -> Type
getBinaryFArg :: Type -> Maybe Type -> Type
getBinaryFArg _ (Just (AppT (AppT _ t :: Type
t)  _)) = Type
t
getBinaryFArg def :: Type
def _ = Type
def

-- | Auxiliary function to extract the first argument of a type
-- application (the second argument of this function). If the second
-- argument is @Nothing@ or not of the right shape, the first argument
-- is returned as a default.
getUnaryFArg :: Type -> Maybe Type -> Type
getUnaryFArg :: Type -> Maybe Type -> Type
getUnaryFArg _ (Just (AppT _ t :: Type
t)) = Type
t
getUnaryFArg def :: Type
def _ = Type
def



{-|
  This function provides the name and the arity of the given data constructor.
-}
abstractConType :: Con -> (Name,Int)
abstractConType :: Con -> (Name, Int)
abstractConType (NormalC constr :: Name
constr args :: [StrictType]
args) = (Name
constr, [StrictType] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [StrictType]
args)
abstractConType (RecC constr :: Name
constr args :: [VarBangType]
args) = (Name
constr, [VarBangType] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [VarBangType]
args)
abstractConType (InfixC _ constr :: Name
constr _) = (Name
constr, 2)
abstractConType (ForallC _ _ constr :: Con
constr) = Con -> (Name, Int)
abstractConType Con
constr
#if __GLASGOW_HASKELL__ >= 800
abstractConType (GadtC (constr :: Name
constr:_) args :: [StrictType]
args _typ :: Type
_typ) = (Name
constr,[StrictType] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [StrictType]
args) -- Only first Name
#endif

{-|
  This function returns the name of a bound type variable
-}
tyVarBndrName :: TyVarBndr -> Name
tyVarBndrName (PlainTV n :: Name
n) = Name
n
tyVarBndrName (KindedTV n :: Name
n _) = Name
n

containsType :: Type -> Type -> Bool
containsType :: Type -> Type -> Bool
containsType s :: Type
s t :: Type
t
             | Type
s Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
t = Bool
True
             | Bool
otherwise = case Type
s of
                             ForallT _ _ s' :: Type
s' -> Type -> Type -> Bool
containsType Type
s' Type
t
                             AppT s1 :: Type
s1 s2 :: Type
s2 -> Type -> Type -> Bool
containsType Type
s1 Type
t Bool -> Bool -> Bool
|| Type -> Type -> Bool
containsType Type
s2 Type
t
                             SigT s' :: Type
s' _ -> Type -> Type -> Bool
containsType Type
s' Type
t
                             _ -> Bool
False

containsType' :: Type -> Type -> [Int]
containsType' :: Type -> Type -> [Int]
containsType' = Int -> Type -> Type -> [Int]
forall a. Num a => a -> Type -> Type -> [a]
run 0
    where run :: a -> Type -> Type -> [a]
run n :: a
n s :: Type
s t :: Type
t
             | Type
s Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
t = [a
n]
             | Bool
otherwise = case Type
s of
                             ForallT _ _ s' :: Type
s' -> a -> Type -> Type -> [a]
run a
n Type
s' Type
t
                             -- only going through the right-hand side counts!
                             AppT s1 :: Type
s1 s2 :: Type
s2 -> a -> Type -> Type -> [a]
run a
n Type
s1 Type
t [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ a -> Type -> Type -> [a]
run (a
na -> a -> a
forall a. Num a => a -> a -> a
+1) Type
s2 Type
t
                             SigT s' :: Type
s' _ -> a -> Type -> Type -> [a]
run a
n Type
s' Type
t
                             _ -> []


{-|
  This function provides a list (of the given length) of new names based
  on the given string.
-}
newNames :: Int -> String -> Q [Name]
newNames :: Int -> String -> Q [Name]
newNames n :: Int
n name :: String
name = Int -> Q Name -> Q [Name]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n (String -> Q Name
newName String
name)

tupleTypes :: Int -> Int -> [Name]
tupleTypes n :: Int
n m :: Int
m = (Int -> Name) -> [Int] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Name
tupleTypeName [Int
n..Int
m]

{-| Helper function for generating a list of instances for a list of named
 signatures. For example, in order to derive instances 'Functor' and
 'ShowF' for a signature @Exp@, use derive as follows (requires Template
 Haskell):

 > $(derive [makeFunctor, makeShowF] [''Exp])
 -}
derive :: [Name -> Q [Dec]] -> [Name] -> Q [Dec]
derive :: [Name -> Q [Dec]] -> [Name] -> Q [Dec]
derive ders :: [Name -> Q [Dec]]
ders names :: [Name]
names = ([[Dec]] -> [Dec]) -> Q [[Dec]] -> Q [Dec]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [[Dec]] -> [Dec]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Q [[Dec]] -> Q [Dec]) -> Q [[Dec]] -> Q [Dec]
forall a b. (a -> b) -> a -> b
$ [Q [Dec]] -> Q [[Dec]]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [Name -> Q [Dec]
der Name
name | Name -> Q [Dec]
der <- [Name -> Q [Dec]]
ders, Name
name <- [Name]
names]

{-| Apply a class name to type arguments to construct a type class
    constraint.
-}

#if __GLASGOW_HASKELL__ < 710
mkClassP :: Name -> [Type] -> Pred
mkClassP = ClassP
#else
mkClassP :: Name -> [Type] -> Type
mkClassP :: Name -> Cxt -> Type
mkClassP name :: Name
name = (Type -> Type -> Type) -> Type -> Cxt -> Type
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Type -> Type -> Type
AppT (Name -> Type
ConT Name
name)
#endif

{-| This function checks whether the given type constraint is an
equality constraint. If so, the types of the equality constraint are
returned. -}

#if __GLASGOW_HASKELL__ < 710
isEqualP :: Pred -> Maybe (Type, Type)
isEqualP (EqualP x y) = Just (x, y)
isEqualP _ = Nothing
#else
isEqualP :: Type -> Maybe (Type, Type)
isEqualP :: Type -> Maybe (Type, Type)
isEqualP (AppT (AppT EqualityT x :: Type
x) y :: Type
y) = (Type, Type) -> Maybe (Type, Type)
forall a. a -> Maybe a
Just (Type
x, Type
y)
isEqualP _ = Maybe (Type, Type)
forall a. Maybe a
Nothing
#endif

mkInstanceD :: Cxt -> Type -> [Dec] -> Dec
#if __GLASGOW_HASKELL__ < 800
mkInstanceD cxt ty decs = InstanceD cxt ty decs
#else
mkInstanceD :: Cxt -> Type -> [Dec] -> Dec
mkInstanceD cxt :: Cxt
cxt ty :: Type
ty decs :: [Dec]
decs = Maybe Overlap -> Cxt -> Type -> [Dec] -> Dec
InstanceD Maybe Overlap
forall a. Maybe a
Nothing Cxt
cxt Type
ty [Dec]
decs
#endif


-- | This function lifts type class instances over sums
-- of signatures. To this end it assumes that it contains only methods
-- with types of the form @f t1 .. tn -> t@ where @f@ is the signature
-- that is used to construct sums. It also assumes that the sum of signatures
-- occur in the last position in class head. Eg: HasVars v f
-- Since this function is generic it
-- assumes as its first argument the name of the function that is
-- used to lift methods over sums i.e. a function of type
--
-- @
-- (forall f. f t1 .. tn -> t) -> (Sum fs t1 .. tn -> t)
-- @
--
-- where @Sum@ is the sum type constructor. The second argument to
-- this function is expected to be the name of that constructor. The
-- last argument is the name of the class whose instances should be
-- lifted over sums. The class should be such that the last parameter
-- is that of the signature.

liftSumGen :: Name -> Name -> Name -> Name -> Q [Dec]
liftSumGen :: Name -> Name -> Name -> Name -> Q [Dec]
liftSumGen caseName :: Name
caseName sumName :: Name
sumName allName :: Name
allName fname :: Name
fname = do
  ClassI (ClassD _ name :: Name
name targs_ :: [TyVarBndr]
targs_ _ decs :: [Dec]
decs) _ <- Name -> Q Info
reify Name
fname
  let targs :: [Name]
targs = (TyVarBndr -> Name) -> [TyVarBndr] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map TyVarBndr -> Name
tyVarBndrName [TyVarBndr]
targs_
      ts :: Cxt
ts = (Name -> Type) -> [Name] -> Cxt
forall a b. (a -> b) -> [a] -> [b]
map Name -> Type
VarT ([Name] -> [Name]
forall a. [a] -> [a]
init [Name]
targs)
  Name
fs <- String -> Q Name
newName "fs"
  case [Name] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Name]
targs of
    True -> do
      String -> Q ()
reportError (String -> Q ()) -> String -> Q ()
forall a b. (a -> b) -> a -> b
$ "Class " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ " cannot be lifted to sums!"
      [Dec] -> Q [Dec]
forall (m :: * -> *) a. Monad m => a -> m a
return []
    False -> do
      Type
allCxt <- Name -> Q Type
conT Name
allName Q Type -> Q Type -> Q Type
`appT` Q Type
clsType Q Type -> Q Type -> Q Type
`appT` Name -> Q Type
varT Name
fs
      let cxt :: Cxt
cxt = [ Type
allCxt ]
      let tp :: Type
tp = Name -> Type
ConT Name
sumName Type -> Type -> Type
`AppT` Name -> Type
VarT Name
fs
      let complType :: Type
complType = (Type -> Type -> Type) -> Type -> Cxt -> Type
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Type -> Type -> Type
AppT (Name -> Type
ConT Name
name) Cxt
ts Type -> Type -> Type
`AppT` Type
tp
      [Dec]
decs' <- [Q Dec] -> Q [Dec]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ([Q Dec] -> Q [Dec]) -> [Q Dec] -> Q [Dec]
forall a b. (a -> b) -> a -> b
$ (Dec -> [Q Dec]) -> [Dec] -> [Q Dec]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Dec -> [Q Dec]
decl [Dec]
decs
      [Dec] -> Q [Dec]
forall (m :: * -> *) a. Monad m => a -> m a
return [Cxt -> Type -> [Dec] -> Dec
mkInstanceD Cxt
cxt Type
complType [Dec]
decs']
        where decl :: Dec -> [DecQ]
              decl :: Dec -> [Q Dec]
decl (SigD f :: Name
f _) = [Name -> [ClauseQ] -> Q Dec
funD Name
f [Name -> ClauseQ
clause Name
f]]
              decl _ = []
              clause :: Name -> ClauseQ
              clause :: Name -> ClauseQ
clause f :: Name
f = do Name
x <- String -> Q Name
newName "x"
                            Exp
pclsName <- ExpQ
pclsNameM
                            let b :: Body
b = Exp -> Body
NormalB (Name -> Exp
VarE Name
caseName Exp -> Exp -> Exp
`AppE` Exp
pclsName Exp -> Exp -> Exp
`AppE` Name -> Exp
VarE Name
f Exp -> Exp -> Exp
`AppE` Name -> Exp
VarE Name
x)
                            Clause -> ClauseQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Clause -> ClauseQ) -> Clause -> ClauseQ
forall a b. (a -> b) -> a -> b
$ [Pat] -> Body -> [Dec] -> Clause
Clause [Name -> Pat
VarP Name
x] Body
b []
              pclsNameM :: ExpQ
pclsNameM = [e| Proxy :: Proxy $clsType |]
              clsType :: Q Type
clsType = (Q Type -> Type -> Q Type) -> Q Type -> Cxt -> Q Type
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\acc :: Q Type
acc a :: Type
a -> Q Type
acc Q Type -> Q Type -> Q Type
`appT` Type -> Q Type
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
a) (Name -> Q Type
conT Name
name) Cxt
ts


findSig :: [Name] -> [Dec] -> Q (Maybe ([Name],[Name]))
findSig :: [Name] -> [Dec] -> Q (Maybe ([Name], [Name]))
findSig targs :: [Name]
targs decs :: [Dec]
decs = case (Dec -> Q (Maybe Name)) -> [Dec] -> [Q (Maybe Name)]
forall a b. (a -> b) -> [a] -> [b]
map Dec -> Q (Maybe Name)
run [Dec]
decs of
                       []  -> Maybe ([Name], [Name]) -> Q (Maybe ([Name], [Name]))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ([Name], [Name])
forall a. Maybe a
Nothing
                       mx :: Q (Maybe Name)
mx:_ -> do Maybe Name
x <- Q (Maybe Name)
mx
                                  case Maybe Name
x of
                                    Nothing -> Maybe ([Name], [Name]) -> Q (Maybe ([Name], [Name]))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ([Name], [Name])
forall a. Maybe a
Nothing
                                    Just n :: Name
n -> Maybe ([Name], [Name]) -> Q (Maybe ([Name], [Name]))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe ([Name], [Name]) -> Q (Maybe ([Name], [Name])))
-> Maybe ([Name], [Name]) -> Q (Maybe ([Name], [Name]))
forall a b. (a -> b) -> a -> b
$ Name -> [Name] -> Maybe ([Name], [Name])
forall a. Eq a => a -> [a] -> Maybe ([a], [a])
splitNames Name
n [Name]
targs
  where run :: Dec -> Q (Maybe Name)
        run :: Dec -> Q (Maybe Name)
run (SigD _ ty :: Type
ty) = do
          Type
ty' <- Type -> Q Type
expandSyns Type
ty
          Maybe Name -> Q (Maybe Name)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Name -> Q (Maybe Name)) -> Maybe Name -> Q (Maybe Name)
forall a b. (a -> b) -> a -> b
$ Bool -> Type -> Maybe Name
getSig Bool
False Type
ty'
        run _ = Maybe Name -> Q (Maybe Name)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Name
forall a. Maybe a
Nothing
        getSig :: Bool -> Type -> Maybe Name
getSig t :: Bool
t (ForallT _ _ ty :: Type
ty) = Bool -> Type -> Maybe Name
getSig Bool
t Type
ty
        getSig False (AppT (AppT ArrowT ty :: Type
ty) _) = Bool -> Type -> Maybe Name
getSig Bool
True Type
ty
        getSig True (AppT ty :: Type
ty _) = Bool -> Type -> Maybe Name
getSig Bool
True Type
ty
        getSig True (VarT n :: Name
n) = Name -> Maybe Name
forall a. a -> Maybe a
Just Name
n
        getSig _ _ = Maybe Name
forall a. Maybe a
Nothing
        splitNames :: a -> [a] -> Maybe ([a], [a])
splitNames y :: a
y (x :: a
x:xs :: [a]
xs)
          | a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x = ([a], [a]) -> Maybe ([a], [a])
forall a. a -> Maybe a
Just ([],[a]
xs)
          | Bool
otherwise = do (xs1 :: [a]
xs1,xs2 :: [a]
xs2) <- a -> [a] -> Maybe ([a], [a])
splitNames a
y [a]
xs
                           ([a], [a]) -> Maybe ([a], [a])
forall (m :: * -> *) a. Monad m => a -> m a
return (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs1,[a]
xs2)
        splitNames _ [] = Maybe ([a], [a])
forall a. Maybe a
Nothing