{-# LANGUAGE PackageImports #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
#if MIN_VERSION_base(4,12,0)
{-# LANGUAGE NoStarIsType #-}
#endif
{-# 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 | PositionType
type family ClassifyLabel (name :: Symbol) :: LabelType where
ClassifyLabel name =
If (StartsWithDigit name)
'PositionType
( If (StartsWithUnderscoreAndUpperCase name)
'LegacyConstrType
( If (StartsWithUpperCase name)
'ConstrType
'FieldType
)
)
type StartsWithDigit name =
CmpSymbol "/" name == 'LT && CmpSymbol ":" name == 'GT
type StartsWithUnderscoreAndUpperCase name =
CmpSymbol "_@" name == 'LT && CmpSymbol "_[" name == 'GT
type StartsWithUpperCase name =
CmpSymbol "@" name == 'LT && CmpSymbol "[" name == 'GT
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 -> Type) (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)
forall (labelType :: LabelType) (name :: Symbol)
(p :: k -> k -> Type) (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 (name :: k) s t a b. Field name s t a b => Lens s t a b
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
forall (name :: Symbol) 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 (name :: k) s t a b.
Constructor name s t a b =>
Prism s t a b
forall {k} (name :: k) s t a b.
Constructor name s t a b =>
Prism s t a b
constructorPrism @name
class Position (i :: Nat) s t a b | s i -> a, t i -> b, s i b -> t, t i a -> s where
positionLens :: Lens s t a b
instance {-# INCOHERENT #-} HasPosition i s t a b => Position i s t a b where
positionLens :: Lens s t a b
positionLens = forall (i :: Nat) s t a b. HasPosition i s t a b => Lens s t a b
position @i
instance {-# INCOHERENT #-} HasPosition' i s a => Position i s s a a where
positionLens :: Lens s s a a
positionLens = forall (i :: Nat) s a. HasPosition' i s a => Lens s s a a
position' @i
instance ( Functor f, Position i s t a b, i ~ ParseNat name
) => IsLabelHelper 'PositionType name (->) f s t a b where
labelOutput :: (a -> f b) -> s -> f t
labelOutput = forall (i :: Nat) s t a b. Position i s t a b => Lens s t a b
positionLens @i
#if MIN_VERSION_base(4,17,0)
type ParseNat name = ParseNat' 0 (UnconsSymbol name)
type family ParseNat' acc m where
ParseNat' acc ('Just '(hd, tl)) =
ParseNat' (10 * acc + DigitToNat hd) (UnconsSymbol tl)
ParseNat' acc 'Nothing = acc
type DigitToNat c =
If ('0' <=? c && c <=? '9')
(CharToNat c - CharToNat '0')
(TypeError ('Text "Invalid position number"))
#else
type family ParseNat name where
ParseNat name = TypeError ('Text "Positional lenses not supported")
#endif