{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE UndecidableSuperClasses #-}
module Data.Generics.Product.Fields
(
HasField (..)
, HasField' (..)
, getField
, setField
) where
import Data.Generics.Internal.Families
import Data.Generics.Internal.VL.Lens as VL
import Data.Generics.Internal.Void
import Data.Generics.Product.Internal.GLens
import Data.Kind (Constraint, Type)
import GHC.Generics
import GHC.TypeLits (Symbol, ErrorMessage(..), TypeError)
import Data.Generics.Internal.Profunctor.Lens as P
class HasField (field :: Symbol) s t a b | s field -> a, t field -> b, s field b -> t, t field a -> s where
field :: VL.Lens s t a b
class HasField' (field :: Symbol) s a | s field -> a where
field' :: VL.Lens s s a a
getField :: forall f a s. HasField' f s a => s -> a
getField = VL.view (field' @f)
setField :: forall f s a. HasField' f s a => a -> s -> s
setField = VL.set (field' @f)
instance
( Generic s
, ErrorUnless field s (CollectField field (Rep s))
, GLens' (HasTotalFieldPSym field) (Rep s) a
) => HasField' field s a where
field' f s = VL.ravel (repLens . glens @(HasTotalFieldPSym field)) f s
instance
( Generic s
, ErrorUnless field s (CollectField field (Rep s))
, Generic t
#if __GLASGOW_HASKELL__ < 802
, '(s', t') ~ '(Proxied s, Proxied t)
#else
, s' ~ Proxied s
, t' ~ Proxied t
#endif
, Generic s'
, Generic t'
, GLens' (HasTotalFieldPSym field) (Rep s') a'
, GLens' (HasTotalFieldPSym field) (Rep t') b'
, GLens (HasTotalFieldPSym field) (Rep s) (Rep t) a b
, t ~ Infer s a' b
, s ~ Infer t b' a
) => HasField field s t a b where
field f s = VL.ravel (repLens . glens @(HasTotalFieldPSym field)) f s
instance {-# OVERLAPPING #-} HasField f (Void1 a) (Void1 b) a b where
field = undefined
type family ErrorUnless (field :: Symbol) (s :: Type) (stat :: TypeStat) :: Constraint where
ErrorUnless field s ('TypeStat _ _ '[])
= TypeError
( 'Text "The type "
':<>: 'ShowType s
':<>: 'Text " does not contain a field named '"
':<>: 'Text field ':<>: 'Text "'."
)
ErrorUnless field s ('TypeStat (n ': ns) _ _)
= TypeError
( 'Text "Not all constructors of the type "
':<>: 'ShowType s
':$$: 'Text " contain a field named '"
':<>: 'Text field ':<>: 'Text "'."
':$$: 'Text "The offending constructors are:"
':$$: ShowSymbols (n ': ns)
)
ErrorUnless _ _ ('TypeStat '[] '[] _)
= ()
data HasTotalFieldPSym :: Symbol -> (TyFun (Type -> Type) Bool)
type instance Eval (HasTotalFieldPSym sym) tt = HasTotalFieldP sym tt