module Data.HList.Labelable
(Labelable(..),
LabeledOptic,
(.==.),
Projected(..), projected',
LabeledCxt1,
LabeledTo(LabeledTo),
LabeledR(LabeledR),
ToSym, EnsureLabel(toLabel), toLabelSym,
Identity,
LabelableTIPCxt,
LabeledOpticType(..),
LabeledOpticF,
LabeledOpticP,
LabeledOpticTo,
) where
import Data.HList.HListPrelude
import Data.HList.FakePrelude
import Data.HList.Record
import Data.HList.Variant
import Data.HList.TIP
import Data.HList.TIC
import Data.HList.Label3
import Control.Monad.Identity
import GHC.TypeLits
import LensDefs
import GHC.Exts (Constraint)
type LabeledOptic (x :: k) (r :: [*] -> *) (s :: [*]) (t :: [*]) (a :: *) (b :: *)
= forall ty to p f.
(ty ~ LabelableTy r,
LabeledOpticF ty f,
LabeledOpticP ty p,
LabeledOpticTo ty x to) => (a `p` f b) `to` (r s `p` f (r t))
data LabeledOpticType = LabelableLens | LabelablePrism | LabelableLabel
type family LabeledOpticF (ty :: LabeledOpticType) :: (* -> *) -> Constraint
type instance LabeledOpticF LabelableLens = Functor
type instance LabeledOpticF LabelablePrism = Applicative
type instance LabeledOpticF LabelableLabel = (~) Identity
type family LabeledOpticP (ty :: LabeledOpticType) :: (* -> * -> *) -> Constraint
type instance LabeledOpticP LabelableLens = (~) (->)
type instance LabeledOpticP LabelablePrism = Choice
type instance LabeledOpticP LabelableLabel = (~) (->)
type family LabeledOpticTo (ty :: LabeledOpticType) (x :: k) :: (* -> * -> *) -> Constraint
type instance LabeledOpticTo LabelableLens x = (~) (->)
type instance LabeledOpticTo LabelablePrism x = (~) (->)
type instance LabeledOpticTo LabelableLabel x = (~) (LabeledTo x)
class SameLength s t => Labelable (x :: k) (r :: [*] -> *) s t a b
| x s -> a, x t -> b,
x s b -> t, x t a -> s
where
type LabelableTy r :: LabeledOpticType
hLens' :: Label x -> LabeledOptic x r s t a b
data LabeledTo (x :: k) (a :: *) (b :: *) = LabeledTo deriving (Int -> LabeledTo x a b -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (x :: k) a b. Int -> LabeledTo x a b -> ShowS
forall k (x :: k) a b. [LabeledTo x a b] -> ShowS
forall k (x :: k) a b. LabeledTo x a b -> String
showList :: [LabeledTo x a b] -> ShowS
$cshowList :: forall k (x :: k) a b. [LabeledTo x a b] -> ShowS
show :: LabeledTo x a b -> String
$cshow :: forall k (x :: k) a b. LabeledTo x a b -> String
showsPrec :: Int -> LabeledTo x a b -> ShowS
$cshowsPrec :: forall k (x :: k) a b. Int -> LabeledTo x a b -> ShowS
Show)
data LabeledR (x :: [*]) = LabeledR
instance HLens x Record s t a b
=> Labelable x Record s t a b where
type LabelableTy Record = LabelableLens
hLens' :: Label x -> LabeledOptic x Record s t a b
hLens' Label x
x = forall {k} (x :: k) (r :: [*] -> *) (s :: [*]) (t :: [*]) a b.
HLens x r s t a b =>
Label x
-> forall (f :: * -> *). Functor f => (a -> f b) -> r s -> f (r t)
hLens Label x
x
instance LabeledCxt1 s t a b => Labelable x LabeledR s t a b where
type LabelableTy LabeledR = LabelableLabel
hLens' :: Label x -> LabeledOptic x LabeledR s t a b
hLens' Label x
_ = forall k (x :: k) a b. LabeledTo x a b
LabeledTo
type LabeledCxt1 s t a b = (s ~ '[], t ~ '[], a ~ (), b ~ ())
instance (HPrism x s t a b,
to ~ (->)) => Labelable x Variant s t a b where
type LabelableTy Variant = LabelablePrism
hLens' :: Label x -> LabeledOptic x Variant s t a b
hLens' Label x
x p a (f b)
s = forall {k} (x :: k) (s :: [*]) (t :: [*]) a b (p :: * -> * -> *)
(f :: * -> *).
(HPrism x s t a b, Choice p, Applicative f) =>
Label x -> p a (f b) -> p (Variant s) (f (Variant t))
hPrism Label x
x p a (f b)
s
instance (TICPrism s t a b, Label x ~ Label a,a ~ b, s ~ t,
SameLength s t) =>
Labelable (x :: k) TIC s t a b where
type LabelableTy TIC = LabelablePrism
hLens' :: Label x -> LabeledOptic x TIC s t a b
hLens' Label x
_ = forall (s :: [*]) (t :: [*]) a b (p :: * -> * -> *) (f :: * -> *).
(TICPrism s t a b, SameLength s t, Choice p, Applicative f) =>
p a (f b) -> p (TIC s) (f (TIC t))
ticPrism
instance LabelableTIPCxt x s t a b =>
Labelable (x :: k) TIP s t a b where
type LabelableTy TIP = LabelableLens
hLens' :: Label x -> LabeledOptic x TIP s t a b
hLens' Label x
x = forall {k} (x :: k) (r :: [*] -> *) (s :: [*]) (t :: [*]) a b.
HLens x r s t a b =>
Label x
-> forall (f :: * -> *). Functor f => (a -> f b) -> r s -> f (r t)
hLens Label x
x
type LabelableTIPCxt x s t a b =
(s ~ t, a ~ b, Label x ~ Label a,
HLens x TIP s t a b)
x
l .==. :: x -> v -> Tagged l v
.==. v
v = forall x y. EnsureLabel x y => x -> y
toLabel x
l forall {k} (l :: k) v. Label l -> v -> Tagged l v
.=. v
v
infixr 4 .==.
class ToSym label (s :: Symbol) | label -> s
instance LabeledTo x (a `p` f b) (LabeledR s `p` f (LabeledR t)) ~ v1 v2 v3
=> ToSym (v1 v2 v3) x
instance ToSym (label x) x
class EnsureLabel x y | x -> y where
toLabel :: x -> y
instance EnsureLabel (Label x) (Label (x :: k)) where
toLabel :: Label x -> Label x
toLabel Label x
_ = forall {k} (l :: k). Label l
Label
instance EnsureLabel (Proxy x) (Label (x :: k)) where
toLabel :: Proxy x -> Label x
toLabel Proxy x
_ = forall {k} (l :: k). Label l
Label
instance ToSym (a b c) (x :: Symbol) => EnsureLabel (a b c) (Label x) where
toLabel :: a b c -> Label x
toLabel a b c
_ = forall {k} (l :: k). Label l
Label
toLabelSym :: x -> Label x
toLabelSym x
label = forall x y. EnsureLabel x y => x -> y
toLabel x
label forall a. a -> a -> a
`asTypeOf` (forall {k} (l :: k). Label l
Label :: Label (x :: Symbol))
class Projected r s t a b where
projected :: (ty ~ LabelableTy r,
LabeledOpticP ty p,
LabeledOpticF ty f) => r a `p` f (r b) -> r s `p` f (r t)
instance (
H2ProjectByLabels (LabelsOf a) s a_ _s_minus_a,
HRLabelSet a_, HRLabelSet a,
HRearrange (LabelsOf a) a_ a,
HLeftUnion b s bs, HRLabelSet bs,
HRearrange (LabelsOf t) bs t, HRLabelSet t
) => Projected Record s t a b where
projected :: forall (ty :: LabeledOpticType) (p :: * -> * -> *) (f :: * -> *).
(ty ~ LabelableTy Record, LabeledOpticP ty p,
LabeledOpticF ty f) =>
p (Record a) (f (Record b)) -> p (Record s) (f (Record t))
projected p (Record a) (f (Record b))
f Record s
s = (\Record b
b -> forall {l :: [*]} {r :: [*]}.
(HLabelSet (LabelsOf l), HRearrange3 (LabelsOf l) r l,
SameLength' r l, SameLength' r (LabelsOf l), SameLength' l r,
SameLength' (LabelsOf l) r) =>
Record r -> Record l
hRearrange' (Record b
b forall (r :: [*]) (r' :: [*]) (r'' :: [*]).
HLeftUnion r r' r'' =>
Record r -> Record r' -> Record r''
.<++. Record s
s)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> p (Record a) (f (Record b))
f (forall {l :: [*]} {r :: [*]} {t :: [*]} {b :: [*]}.
(HRearrange3 (LabelsOf l) r l, SameLength' r l,
SameLength' r (LabelsOf l), SameLength' l r,
SameLength' (LabelsOf l) r, HLabelSet (LabelsOf l),
HLabelSet (LabelsOf r), HAllTaggedLV r,
H2ProjectByLabels (LabelsOf l) t r b) =>
Record t -> Record l
hProjectByLabels' Record s
s :: Record a)
instance (ExtendsVariant b t,
ProjectVariant s a,
ProjectExtendVariant s t,
HLeftUnion b s bs, HRLabelSet bs,
HRearrange (LabelsOf t) bs t)
=> Projected Variant s t a b where
projected :: forall (ty :: LabeledOpticType) (p :: * -> * -> *) (f :: * -> *).
(ty ~ LabelableTy Variant, LabeledOpticP ty p,
LabeledOpticF ty f) =>
p (Variant a) (f (Variant b)) -> p (Variant s) (f (Variant t))
projected = forall b t s a.
(b -> t)
-> (s -> Either t a)
-> forall (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f b) -> p s (f t)
prism forall (x :: [*]) (y :: [*]).
ExtendsVariant x y =>
Variant x -> Variant y
extendsVariant
(\Variant s
s -> case forall (x :: [*]) (y :: [*]).
ProjectVariant x y =>
Variant x -> Maybe (Variant y)
projectVariant Variant s
s of
Just Variant a
a -> forall a b. b -> Either a b
Right Variant a
a
Maybe (Variant a)
Nothing | Just Variant t
t <- forall (x :: [*]) (y :: [*]).
ProjectExtendVariant x y =>
Variant x -> Maybe (Variant y)
projectExtendVariant Variant s
s -> forall a b. a -> Either a b
Left Variant t
t
Maybe (Variant a)
_ -> forall a. HasCallStack => String -> a
error String
"Data.HList.Labelable.projected impossible"
)
projected' :: p (r b) (f (r b)) -> p (r t) (f (r t))
projected' p (r b) (f (r b))
s = forall {k1} {k2} optic (p :: k1 -> k2 -> *) (a :: k1)
(f :: k1 -> k2) (s :: k1).
(optic ~ (p a (f a) -> p s (f s))) =>
optic -> optic
isSimple forall (r :: [*] -> *) (s :: [*]) (t :: [*]) (a :: [*]) (b :: [*])
(ty :: LabeledOpticType) (p :: * -> * -> *) (f :: * -> *).
(Projected r s t a b, ty ~ LabelableTy r, LabeledOpticP ty p,
LabeledOpticF ty f) =>
p (r a) (f (r b)) -> p (r s) (f (r t))
projected p (r b) (f (r b))
s
instance (to ~ LabeledTo x, ToSym (to p q) x)
=> HExtend (to p q) (Proxy ('[] :: [*])) where
type HExtendR (to p q) (Proxy ('[] :: [*])) = Proxy '[GetXFromLabeledTo to]
.*. :: to p q -> Proxy '[] -> HExtendR (to p q) (Proxy '[])
(.*.) to p q
_ Proxy '[]
_ = forall {k} (t :: k). Proxy t
Proxy
instance (to ~ LabeledTo x, ToSym (to p q) x)
=> HExtend (to p q) (Proxy (x ': xs)) where
type HExtendR (to p q) (Proxy (x ': xs)) = Proxy (GetXFromLabeledTo to ': x ': xs)
.*. :: to p q -> Proxy (x : xs) -> HExtendR (to p q) (Proxy (x : xs))
(.*.) to p q
_ Proxy (x : xs)
_ = forall {k} (t :: k). Proxy t
Proxy
instance (to ~ LabeledTo x, ToSym (to p q) x)
=> HExtend (to p q) (Proxy (Lbl n ns desc ': xs)) where
type HExtendR (to p q) (Proxy (Lbl n ns desc ': xs))
= Proxy (Label (GetXFromLabeledTo to) ': MapLabel (Lbl n ns desc ': xs))
.*. :: to p q
-> Proxy (Lbl n ns desc : xs)
-> HExtendR (to p q) (Proxy (Lbl n ns desc : xs))
(.*.) to p q
_ Proxy (Lbl n ns desc : xs)
_ = forall {k} (t :: k). Proxy t
Proxy
type family GetXFromLabeledTo (to :: * -> * -> *) :: Symbol
type instance GetXFromLabeledTo (LabeledTo x) = x