{-# LANGUAGE PackageImports #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Data.Generics.Labels
(
Field(..)
, Field'
, Constructor(..)
, Constructor'
) where
import "this" Data.Generics.Product
import "this" Data.Generics.Sum
import "this" Data.Generics.Internal.VL.Lens (Lens)
import "this" Data.Generics.Internal.VL.Prism (Prism)
import Data.Profunctor (Choice)
import Data.Type.Bool (type (&&), If)
import Data.Type.Equality (type (==))
import GHC.OverloadedLabels
import GHC.TypeLits
class Field name s t a b | s name -> a, t name -> b, s name b -> t, t name a -> s where
fieldLens :: Lens s t a b
type Field' name s a = Field name s s a a
instance {-# INCOHERENT #-} HasField name s t a b => Field name s t a b where
fieldLens :: Lens s t a b
fieldLens = forall (name :: Symbol) s t a b.
HasField name s t a b =>
Lens s t a b
field @name
instance {-# INCOHERENT #-} HasField' name s a => Field name s s a a where
fieldLens :: Lens s s a a
fieldLens = forall (name :: Symbol) s a. HasField' name s a => Lens s s a a
field' @name
class Constructor name s t a b | name s -> a, name t -> b where
constructorPrism :: Prism s t a b
type Constructor' name s a = Constructor name s s a a
instance {-# INCOHERENT #-} AsConstructor name s t a b => Constructor name s t a b where
constructorPrism :: Prism s t a b
constructorPrism = forall (name :: Symbol) s t a b.
AsConstructor name s t a b =>
Prism s t a b
_Ctor @name
instance {-# INCOHERENT #-} AsConstructor' name s a => Constructor name s s a a where
constructorPrism :: Prism s s a a
constructorPrism = forall (name :: Symbol) s a.
AsConstructor' name s a =>
Prism s s a a
_Ctor' @name
data LabelType = FieldType | LegacyConstrType | ConstrType
type family ClassifyLabel (name :: Symbol) :: LabelType where
ClassifyLabel name =
If (CmpSymbol "_@" name == 'LT && CmpSymbol "_[" name == 'GT)
'LegacyConstrType
( If (CmpSymbol "@" name == 'LT && CmpSymbol "[" name == 'GT)
'ConstrType
'FieldType
)
instance ( labelType ~ ClassifyLabel name
, IsLabelHelper labelType name p f s t a b
, pafb ~ p a (f b), psft ~ p s (f t)) => IsLabel name (pafb -> psft) where
fromLabel :: pafb -> psft
fromLabel = forall {k} {k} {k} {k} {k} (labelType :: k) (name :: k)
(p :: k -> k -> *) (f :: k -> k) (s :: k) (t :: k) (a :: k)
(b :: k).
IsLabelHelper labelType name p f s t a b =>
p a (f b) -> p s (f t)
labelOutput @labelType @name @p @f
class IsLabelHelper labelType name p f s t a b where
labelOutput :: p a (f b) -> p s (f t)
instance (Functor f, Field name s t a b) => IsLabelHelper 'FieldType name (->) f s t a b where
labelOutput :: (a -> f b) -> s -> f t
labelOutput = forall {k} (name :: k) s t a b. Field name s t a b => Lens s t a b
fieldLens @name
instance ( Applicative f, Choice p, Constructor name s t a b
, name' ~ AppendSymbol "_" name) => IsLabelHelper 'LegacyConstrType name' p f s t a b where
labelOutput :: p a (f b) -> p s (f t)
labelOutput = forall {k} (name :: k) s t a b.
Constructor name s t a b =>
Prism s t a b
constructorPrism @name
instance ( Applicative f, Choice p, Constructor name s t a b
) => IsLabelHelper 'ConstrType name p f s t a b where
labelOutput :: p a (f b) -> p s (f t)
labelOutput = forall {k} (name :: k) s t a b.
Constructor name s t a b =>
Prism s t a b
constructorPrism @name