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
[LabeledTo x a b] -> ShowS
LabeledTo x a b -> String
(Int -> LabeledTo x a b -> ShowS)
-> (LabeledTo x a b -> String)
-> ([LabeledTo x a b] -> ShowS)
-> Show (LabeledTo x a b)
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 = Label x
-> forall (f :: * -> *).
Functor f =>
(a -> f b) -> Record s -> f (Record t)
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
_ = to (p a (f b)) (p (LabeledR s) (f (LabeledR t)))
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 = Label x -> p a (f b) -> p (Variant s) (f (Variant t))
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
_ = to (p a (f b)) (p (TIC s) (f (TIC t)))
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 = Label x
-> forall (f :: * -> *).
Functor f =>
(b -> f b) -> TIP t -> f (TIP t)
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 = x -> Label l
forall x y. EnsureLabel x y => x -> y
toLabel x
l Label l -> v -> Tagged l v
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
_ = 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
_ = Label 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
_ = Label x
forall k (l :: k). Label l
Label
toLabelSym :: x -> Label x
toLabelSym x
label = x -> Label x
forall x y. EnsureLabel x y => x -> y
toLabel x
label Label x -> Label x -> Label x
forall a. a -> a -> a
`asTypeOf` (forall k (l :: k). Label l
forall (x :: Symbol). Label x
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 :: 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 -> Record bs -> Record t
forall (l :: [*]) (r :: [*]).
(HLabelSet (LabelsOf l), HRearrange3 (LabelsOf l) r l,
SameLength' (LabelsOf l) r, SameLength' r (LabelsOf l),
SameLength' r l, SameLength' l r) =>
Record r -> Record l
hRearrange' (Record b
b Record b -> Record s -> Record bs
forall (r :: [*]) (r' :: [*]) (r'' :: [*]).
HLeftUnion r r' r'' =>
Record r -> Record r' -> Record r''
.<++. Record s
s)) (Record b -> Record t) -> f (Record b) -> f (Record t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> p (Record a) (f (Record b))
Record a -> f (Record b)
f (Record s -> Record a
forall (l :: [*]) (r :: [*]) (t :: [*]) (b :: [*]).
(HRearrange3 (LabelsOf l) r l, SameLength' (LabelsOf l) r,
SameLength' r (LabelsOf l), SameLength' r l, SameLength' 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 :: p (Variant a) (f (Variant b)) -> p (Variant s) (f (Variant t))
projected = (Variant b -> Variant t)
-> (Variant s -> Either (Variant t) (Variant a))
-> forall (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p (Variant a) (f (Variant b)) -> p (Variant s) (f (Variant t))
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 Variant b -> Variant t
forall (x :: [*]) (y :: [*]).
ExtendsVariant x y =>
Variant x -> Variant y
extendsVariant
(\Variant s
s -> case Variant s -> Maybe (Variant a)
forall (x :: [*]) (y :: [*]).
ProjectVariant x y =>
Variant x -> Maybe (Variant y)
projectVariant Variant s
s of
Just Variant a
a -> Variant a -> Either (Variant t) (Variant a)
forall a b. b -> Either a b
Right Variant a
a
Maybe (Variant a)
Nothing | Just Variant t
t <- Variant s -> Maybe (Variant t)
forall (x :: [*]) (y :: [*]).
ProjectExtendVariant x y =>
Variant x -> Maybe (Variant y)
projectExtendVariant Variant s
s -> Variant t -> Either (Variant t) (Variant a)
forall a b. a -> Either a b
Left Variant t
t
Maybe (Variant a)
_ -> String -> Either (Variant t) (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 = (p (r b) (f (r b)) -> p (r t) (f (r t)))
-> p (r b) (f (r b)) -> p (r t) (f (r t))
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 p (r b) (f (r b)) -> p (r t) (f (r t))
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 '[]
_ = HExtendR (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)
_ = HExtendR (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)
_ = HExtendR (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