{-# LANGUAGE UndecidableInstances #-}
module Data.Generic.Labels
(
Adapt(..), Inject(..), Project(..)
, (:=)(..)
, UncheckedAdapt(..), UncheckedInject(..), UncheckedProject(..)
)
where
import GHC.Generics
import Data.Label
( (:=)(..) )
import Data.Generic.Labels.Internal
( GAdapt
( gAdapt )
)
import Data.Generic.Labels.Internal.Errors
( CheckAdapt, CheckInject, CheckProject )
class UncheckedAdapt args opt all where
uncheckedAdapt :: args -> opt -> all
instance {-# OVERLAPPING #-} UncheckedAdapt a a a where
uncheckedAdapt :: a -> a -> a
uncheckedAdapt = a -> a -> a
forall a b. a -> b -> a
const
instance {-# OVERLAPPING #-} UncheckedAdapt a opt a where
uncheckedAdapt :: a -> opt -> a
uncheckedAdapt = a -> opt -> a
forall a b. a -> b -> a
const
instance {-# OVERLAPPING #-} ( a ~ b )
=> UncheckedAdapt ( lbl := a ) opt ( lbl := b ) where
uncheckedAdapt :: (lbl := a) -> opt -> lbl := b
uncheckedAdapt = (lbl := a) -> opt -> lbl := b
forall a b. a -> b -> a
const
instance {-# OVERLAPPING #-} ( a ~ b, o ~ b )
=> UncheckedAdapt ( lbl := a ) ( lbl := o ) ( lbl := b ) where
uncheckedAdapt :: (lbl := a) -> (lbl := o) -> lbl := b
uncheckedAdapt = (lbl := a) -> (lbl := o) -> lbl := b
forall a b. a -> b -> a
const
instance {-# OVERLAPPING #-}
( Generic all
, argFld ~ S1 ( MetaSel ( Just lbl1 ) NoSourceUnpackedness NoSourceStrictness DecidedLazy ) ( Rec0 a )
, optFld ~ S1 ( MetaSel ( Just lbl2 ) NoSourceUnpackedness NoSourceStrictness DecidedLazy ) ( Rec0 o )
, GAdapt argFld optFld ( Rep all )
)
=> UncheckedAdapt ( lbl1 := a ) ( lbl2 := o ) all where
uncheckedAdapt :: (lbl1 := a) -> (lbl2 := o) -> all
uncheckedAdapt ( Label lbl1
_ := a
arg ) ( Label lbl2
_ := o
opt ) =
Rep all Any -> all
forall a x. Generic a => Rep a x -> a
to (Rep all Any -> all) -> Rep all Any -> all
forall a b. (a -> b) -> a -> b
$ argFld Any -> optFld Any -> Rep all Any
forall (args :: Type -> Type) (opt :: Type -> Type)
(all :: Type -> Type) p.
GAdapt args opt all =>
args p -> opt p -> all p
gAdapt ( K1 R a x
-> M1
S
('MetaSel
('Just lbl1)
'NoSourceUnpackedness
'NoSourceStrictness
'DecidedLazy)
(Rec0 a)
x
forall k i (c :: Meta) (f :: k -> Type) (p :: k). f p -> M1 i c f p
M1 ( a -> K1 R a x
forall k i c (p :: k). c -> K1 i c p
K1 a
arg ) :: argFld x ) ( K1 R o x
-> M1
S
('MetaSel
('Just lbl2)
'NoSourceUnpackedness
'NoSourceStrictness
'DecidedLazy)
(Rec0 o)
x
forall k i (c :: Meta) (f :: k -> Type) (p :: k). f p -> M1 i c f p
M1 ( o -> K1 R o x
forall k i c (p :: k). c -> K1 i c p
K1 o
opt ) :: optFld x )
instance
( Generic opt, Generic all
, argFld ~ S1 ( MetaSel ( Just lbl ) NoSourceUnpackedness NoSourceStrictness DecidedLazy ) ( Rec0 a )
, GAdapt argFld ( Rep opt ) ( Rep all )
)
=> UncheckedAdapt ( lbl := a ) opt all where
uncheckedAdapt :: (lbl := a) -> opt -> all
uncheckedAdapt ( Label lbl
_ := a
arg ) opt
opt =
Rep all Any -> all
forall a x. Generic a => Rep a x -> a
to (Rep all Any -> all) -> Rep all Any -> all
forall a b. (a -> b) -> a -> b
$ argFld Any -> Rep opt Any -> Rep all Any
forall (args :: Type -> Type) (opt :: Type -> Type)
(all :: Type -> Type) p.
GAdapt args opt all =>
args p -> opt p -> all p
gAdapt ( K1 R a x
-> M1
S
('MetaSel
('Just lbl) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy)
(Rec0 a)
x
forall k i (c :: Meta) (f :: k -> Type) (p :: k). f p -> M1 i c f p
M1 ( a -> K1 R a x
forall k i c (p :: k). c -> K1 i c p
K1 a
arg ) :: argFld x ) ( opt -> Rep opt Any
forall a x. Generic a => a -> Rep a x
from opt
opt )
instance {-# OVERLAPPING #-}
( Generic args, Generic all
, optFld ~ S1 ( MetaSel ( Just lbl ) NoSourceUnpackedness NoSourceStrictness DecidedLazy ) ( Rec0 o )
, GAdapt ( Rep args ) optFld ( Rep all )
)
=> UncheckedAdapt args ( lbl := o ) all where
uncheckedAdapt :: args -> (lbl := o) -> all
uncheckedAdapt args
args ( Label lbl
_ := o
opt ) =
Rep all Any -> all
forall a x. Generic a => Rep a x -> a
to (Rep all Any -> all) -> Rep all Any -> all
forall a b. (a -> b) -> a -> b
$ Rep args Any -> optFld Any -> Rep all Any
forall (args :: Type -> Type) (opt :: Type -> Type)
(all :: Type -> Type) p.
GAdapt args opt all =>
args p -> opt p -> all p
gAdapt ( args -> Rep args Any
forall a x. Generic a => a -> Rep a x
from args
args ) ( K1 R o x
-> M1
S
('MetaSel
('Just lbl) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy)
(Rec0 o)
x
forall k i (c :: Meta) (f :: k -> Type) (p :: k). f p -> M1 i c f p
M1 ( o -> K1 R o x
forall k i c (p :: k). c -> K1 i c p
K1 o
opt ) :: optFld x )
instance {-# OVERLAPPABLE #-}
( Generic args, Generic opt, Generic all
, GAdapt ( Rep args ) ( Rep opt ) ( Rep all )
)
=> UncheckedAdapt args opt all where
uncheckedAdapt :: args -> opt -> all
uncheckedAdapt args
args opt
opt =
Rep all Any -> all
forall a x. Generic a => Rep a x -> a
to (Rep all Any -> all) -> Rep all Any -> all
forall a b. (a -> b) -> a -> b
$ Rep args Any -> Rep opt Any -> Rep all Any
forall (args :: Type -> Type) (opt :: Type -> Type)
(all :: Type -> Type) p.
GAdapt args opt all =>
args p -> opt p -> all p
gAdapt ( args -> Rep args Any
forall a x. Generic a => a -> Rep a x
from args
args ) ( opt -> Rep opt Any
forall a x. Generic a => a -> Rep a x
from opt
opt )
class ( UncheckedAdapt args opt all ) => Adapt args opt all where
adapt
:: args
-> opt
-> all
instance ( UncheckedAdapt args opt all, CheckAdapt args opt all )
=> Adapt args opt all where
adapt :: args -> opt -> all
adapt = args -> opt -> all
forall args opt all.
UncheckedAdapt args opt all =>
args -> opt -> all
uncheckedAdapt
class UncheckedAdapt small big big => UncheckedInject small big where
uncheckedInject :: small -> big -> big
instance UncheckedAdapt small big big => UncheckedInject small big where
uncheckedInject :: small -> big -> big
uncheckedInject = small -> big -> big
forall args opt all.
UncheckedAdapt args opt all =>
args -> opt -> all
uncheckedAdapt
class ( UncheckedInject small big ) => Inject small big where
inject :: small -> big -> big
instance ( UncheckedInject small big, CheckInject small big )
=> Inject small big where
inject :: small -> big -> big
inject = small -> big -> big
forall small big. UncheckedInject small big => small -> big -> big
uncheckedInject
class UncheckedAdapt big big small => UncheckedProject big small where
uncheckedProject :: big -> small
instance UncheckedAdapt big big small => UncheckedProject big small where
uncheckedProject :: big -> small
uncheckedProject big
big = big -> big -> small
forall args opt all.
UncheckedAdapt args opt all =>
args -> opt -> all
uncheckedAdapt big
big big
big
class ( UncheckedProject big small ) => Project big small where
project :: big -> small
instance ( UncheckedProject big small, CheckProject big small )
=> Project big small where
project :: big -> small
project = big -> small
forall big small. UncheckedProject big small => big -> small
uncheckedProject