{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Data.Finitary.Optics where
import Data.Finitary (Finitary (..))
import Data.Finite (strengthenN, weakenN)
import GHC.TypeNats (type (<=))
import Optics.Iso (Iso', iso)
import Optics.Prism (Prism', prism')
reindexed :: (Finitary a, Finitary b, Cardinality a ~ Cardinality b) => Iso' a b
reindexed :: Iso' a b
reindexed = (a -> b) -> (b -> a) -> Iso' a b
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (Finite (Cardinality b) -> b
forall a. Finitary a => Finite (Cardinality a) -> a
fromFinite (Finite (Cardinality b) -> b)
-> (a -> Finite (Cardinality b)) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Finite (Cardinality b)
forall a. Finitary a => a -> Finite (Cardinality a)
toFinite) (Finite (Cardinality b) -> a
forall a. Finitary a => Finite (Cardinality a) -> a
fromFinite (Finite (Cardinality b) -> a)
-> (b -> Finite (Cardinality b)) -> b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Finite (Cardinality b)
forall a. Finitary a => a -> Finite (Cardinality a)
toFinite)
tighter :: (Finitary a, Finitary b, Cardinality b <= Cardinality a) => Prism' a b
tighter :: Prism' a b
tighter =
(b -> a) -> (a -> Maybe b) -> Prism' a b
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism'
(Finite (Cardinality a) -> a
forall a. Finitary a => Finite (Cardinality a) -> a
fromFinite (Finite (Cardinality a) -> a)
-> (b -> Finite (Cardinality a)) -> b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Finite (Cardinality b) -> Finite (Cardinality a)
forall (n :: Nat) (m :: Nat). (n <= m) => Finite n -> Finite m
weakenN (Finite (Cardinality b) -> Finite (Cardinality a))
-> (b -> Finite (Cardinality b)) -> b -> Finite (Cardinality a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Finite (Cardinality b)
forall a. Finitary a => a -> Finite (Cardinality a)
toFinite)
((Finite (Cardinality b) -> b)
-> Maybe (Finite (Cardinality b)) -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Finite (Cardinality b) -> b
forall a. Finitary a => Finite (Cardinality a) -> a
fromFinite (Maybe (Finite (Cardinality b)) -> Maybe b)
-> (a -> Maybe (Finite (Cardinality b))) -> a -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Finite (Cardinality a) -> Maybe (Finite (Cardinality b))
forall (n :: Nat) (m :: Nat).
(KnownNat n, n <= m) =>
Finite m -> Maybe (Finite n)
strengthenN (Finite (Cardinality a) -> Maybe (Finite (Cardinality b)))
-> (a -> Finite (Cardinality a))
-> a
-> Maybe (Finite (Cardinality b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Finite (Cardinality a)
forall a. Finitary a => a -> Finite (Cardinality a)
toFinite)