    -- |
    -- Module: Data.Typeable.Optics
    -- Description: Optics for working with 'Typeable'.
    --
    module Data.Typeable.Optics
      ( _cast
      , _gcast
      ) where

    import Data.Typeable
    import Data.Maybe

    import Optics.AffineTraversal

    -- | An 'AffineTraversal'' for working with a 'cast' of a 'Typeable' value.
    _cast :: (Typeable s, Typeable a) => AffineTraversal' s a
    _cast :: forall s a. (Typeable s, Typeable a) => AffineTraversal' s a
    _cast = AffineTraversalVL s s a a -> AffineTraversal s s a a
    forall s t a b.
    AffineTraversalVL s t a b -> AffineTraversal s t a b
    atraversalVL (AffineTraversalVL s s a a -> AffineTraversal s s a a)
    -> AffineTraversalVL s s a a -> AffineTraversal s s a a
    forall a b. (a -> b) -> a -> b
    $ \forall r. r -> f r
    point a -> f a
    f s
    s -> case s -> Maybe a
    forall a b. (Typeable a, Typeable b) => a -> Maybe b
    cast s
    s of
      Just a
    a  -> s -> Maybe s -> s
    forall a. a -> Maybe a -> a
    fromMaybe ([Char] -> s
    forall a. HasCallStack => [Char] -> a
    error [Char]
    "_cast: recast failed") (Maybe s -> s) -> (a -> Maybe s) -> a -> s
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> Maybe s
    forall a b. (Typeable a, Typeable b) => a -> Maybe b
    cast (a -> s) -> f a -> f s
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> a -> f a
    f a
    a
      Maybe a
    Nothing -> s -> f s
    forall r. r -> f r
    point s
    s
    {-# INLINE _cast #-}

    -- | An 'AffineTraversal'' for working with a 'gcast' of a 'Typeable' value.
    _gcast :: (Typeable s, Typeable a) => AffineTraversal' (c s) (c a)
    _gcast :: forall s a (c :: * -> *).
    (Typeable s, Typeable a) =>
    AffineTraversal' (c s) (c a)
    _gcast = AffineTraversalVL (c s) (c s) (c a) (c a)
    -> AffineTraversal (c s) (c s) (c a) (c a)
    forall s t a b.
    AffineTraversalVL s t a b -> AffineTraversal s t a b
    atraversalVL (AffineTraversalVL (c s) (c s) (c a) (c a)
     -> AffineTraversal (c s) (c s) (c a) (c a))
    -> AffineTraversalVL (c s) (c s) (c a) (c a)
    -> AffineTraversal (c s) (c s) (c a) (c a)
    forall a b. (a -> b) -> a -> b
    $ \forall r. r -> f r
    point c a -> f (c a)
    f c s
    s -> case c s -> Maybe (c a)
    forall {k} (a :: k) (b :: k) (c :: k -> *).
    (Typeable a, Typeable b) =>
    c a -> Maybe (c b)
    gcast c s
    s of
      Just c a
    a  -> c s -> Maybe (c s) -> c s
    forall a. a -> Maybe a -> a
    fromMaybe ([Char] -> c s
    forall a. HasCallStack => [Char] -> a
    error [Char]
    "_gcast: recast failed") (Maybe (c s) -> c s) -> (c a -> Maybe (c s)) -> c a -> c s
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . c a -> Maybe (c s)
    forall {k} (a :: k) (b :: k) (c :: k -> *).
    (Typeable a, Typeable b) =>
    c a -> Maybe (c b)
    gcast (c a -> c s) -> f (c a) -> f (c s)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> c a -> f (c a)
    f c a
    a
      Maybe (c a)
    Nothing -> c s -> f (c s)
    forall r. r -> f r
    point c s
    s
    {-# INLINE _gcast #-}