{-# LANGUAGE CPP                   #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE TypeFamilies          #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.RAVec.Tree.DF.Optics (
    -- * Indexing
    ix,
    ) where

import Control.Applicative ((<$>))
import Data.Wrd            (Wrd (..))
import Prelude             (Functor)

import qualified Data.Type.Nat as N
import qualified Optics.Core   as L

import Data.RAVec.Tree.DF

-- $setup
-- >>> import Optics.Core (set, (&), (.~))
-- >>> import Prelude
-- >>> import Data.RAVec.Tree.DF
-- >>> import Data.Wrd (Wrd (..))

type LensLikeVL f s t a b = (a -> f b) -> s -> f t
type LensLikeVL' f s a = LensLikeVL f s s a a

-------------------------------------------------------------------------------
-- Indexing
-------------------------------------------------------------------------------

-- | Index lens.
--
-- >>> let tree = Node (Node (Leaf 'a') (Leaf 'b')) (Node (Leaf 'c') (Leaf 'd'))
-- >>> tree & ix (W1 $ W0 WE) .~ 'z'
-- Node (Node (Leaf 'a') (Leaf 'b')) (Node (Leaf 'z') (Leaf 'd'))
--
ix :: Wrd n -> L.Lens' (Tree n a) a
ix :: Wrd n -> Lens' (Tree n a) a
ix Wrd n
i = LensVL (Tree n a) (Tree n a) a a -> Lens' (Tree n a) a
forall s t a b. LensVL s t a b -> Lens s t a b
L.lensVL (Wrd n -> LensLikeVL' f (Tree n a) a
forall (f :: * -> *) (n :: Nat) a.
Functor f =>
Wrd n -> LensLikeVL' f (Tree n a) a
ixVL Wrd n
i)

ixVL :: Functor f => Wrd n -> LensLikeVL' f (Tree n a) a
ixVL :: Wrd n -> LensLikeVL' f (Tree n a) a
ixVL Wrd n
WE      a -> f a
f (Leaf x)   = a -> Tree 'Z a
forall a. a -> Tree 'Z a
Leaf (a -> Tree 'Z a) -> f a -> f (Tree 'Z a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
x
ixVL (W0 Wrd n1
is) a -> f a
f (Node x y) = (Tree n1 a -> Tree n1 a -> Tree ('S n1) a
forall (n :: Nat) a. Tree n a -> Tree n a -> Tree ('S n) a
`Node` Tree n1 a
y) (Tree n1 a -> Tree ('S n1) a)
-> f (Tree n1 a) -> f (Tree ('S n1) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Wrd n1 -> LensLikeVL' f (Tree n1 a) a
forall (f :: * -> *) (n :: Nat) a.
Functor f =>
Wrd n -> LensLikeVL' f (Tree n a) a
ixVL Wrd n1
is a -> f a
f Tree n1 a
x
ixVL (W1 Wrd n1
is) a -> f a
f (Node x y) = (Tree n1 a
x Tree n1 a -> Tree n1 a -> Tree ('S n1) a
forall (n :: Nat) a. Tree n a -> Tree n a -> Tree ('S n) a
`Node`) (Tree n1 a -> Tree ('S n1) a)
-> f (Tree n1 a) -> f (Tree ('S n1) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Wrd n1 -> LensLikeVL' f (Tree n1 a) a
forall (f :: * -> *) (n :: Nat) a.
Functor f =>
Wrd n -> LensLikeVL' f (Tree n a) a
ixVL Wrd n1
is a -> f a
f Tree n1 a
y

-------------------------------------------------------------------------------
-- Instances
-------------------------------------------------------------------------------

#if !MIN_VERSION_optics_core(0,4,0)
instance N.SNatI n => L.FunctorWithIndex (Wrd n) (Tree n) where
    imap = imap

instance N.SNatI n => L.FoldableWithIndex (Wrd n) (Tree n) where
    ifoldMap = ifoldMap
    ifoldr   = ifoldr

instance N.SNatI n => L.TraversableWithIndex (Wrd n) (Tree n) where
    itraverse = itraverse
#endif

instance N.SNatI n => L.Each (Wrd n) (Tree n a) (Tree n b) a b

type instance L.Index (Tree n a)   = Wrd n
type instance L.IxValue (Tree n a) = a

instance L.Ixed (Tree n a) where
    type IxKind (Tree n a) = L.A_Lens
    ix :: Index (Tree n a)
-> Optic' (IxKind (Tree n a)) NoIx (Tree n a) (IxValue (Tree n a))
ix = Index (Tree n a)
-> Optic' (IxKind (Tree n a)) NoIx (Tree n a) (IxValue (Tree n a))
forall (n :: Nat) a. Wrd n -> Lens' (Tree n a) a
ix