module Optics.Cons.Core
(
Cons(..)
, (<|)
, cons
, uncons
, _head, _tail
, pattern (:<)
, Snoc(..)
, (|>)
, snoc
, unsnoc
, _init, _last
, pattern (:>)
) where
import Control.Applicative (ZipList(..))
import Data.Coerce
import Data.Sequence hiding ((<|), (|>), (:<), (:>))
import qualified Data.Sequence as Seq
import Data.Tuple.Optics
import Optics.AffineFold
import Optics.AffineTraversal
import Optics.Coerce
import Optics.Internal.Utils
import Optics.Optic
import Optics.Prism
import Optics.Review
infixr 5 <|, `cons`
infixl 5 |>, `snoc`
pattern (:<) :: forall s a. Cons s s a a => a -> s -> s
pattern $b:< :: forall s a. Cons s s a a => a -> s -> s
$m:< :: forall {r} {s} {a}.
Cons s s a a =>
s -> (a -> s -> r) -> ((# #) -> r) -> r
(:<) a s <- (preview _Cons -> Just (a, s)) where
(:<) a
a s
s = forall k (is :: IxList) t b.
Is k A_Review =>
Optic' k is t b -> b -> t
review forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
_Cons (a
a, s
s)
infixr 5 :<
infixl 5 :>
pattern (:>) :: forall s a. Snoc s s a a => s -> a -> s
pattern $b:> :: forall s a. Snoc s s a a => s -> a -> s
$m:> :: forall {r} {s} {a}.
Snoc s s a a =>
s -> (s -> a -> r) -> ((# #) -> r) -> r
(:>) s a <- (preview _Snoc -> Just (s, a)) where
(:>) s
a a
s = forall k (is :: IxList) t b.
Is k A_Review =>
Optic' k is t b -> b -> t
review forall s t a b. Snoc s t a b => Prism s t (s, a) (t, b)
_Snoc (s
a, a
s)
class Cons s t a b | s -> a, t -> b, s b -> t, t a -> s where
_Cons :: Prism s t (a, s) (b, t)
instance Cons [a] [b] a b where
_Cons :: Prism [a] [b] (a, [a]) (b, [b])
_Cons = forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry' (:)) forall a b. (a -> b) -> a -> b
$ \[a]
aas -> case [a]
aas of
(a
a:[a]
as) -> forall a b. b -> Either a b
Right (a
a, [a]
as)
[] -> forall a b. a -> Either a b
Left []
{-# INLINE _Cons #-}
instance Cons (ZipList a) (ZipList b) a b where
_Cons :: Prism (ZipList a) (ZipList b) (a, ZipList a) (b, ZipList b)
_Cons = forall s s' k (is :: IxList) t a b.
Coercible s s' =>
Optic k is s t a b -> Optic k is s' t a b
coerceS forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t t' k (is :: IxList) s a b.
Coercible t t' =>
Optic k is s t a b -> Optic k is s t' a b
coerceT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a a' k (is :: IxList) s t b.
Coercible a a' =>
Optic k is s t a b -> Optic k is s t a' b
coerceA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b b' k (is :: IxList) s t a.
Coercible b b' =>
Optic k is s t a b -> Optic k is s t a b'
coerceB forall a b. (a -> b) -> a -> b
$ Prism [a] [b] (a, [a]) (b, [b])
listCons
where
listCons :: Prism [a] [b] (a, [a]) (b, [b])
listCons :: Prism [a] [b] (a, [a]) (b, [b])
listCons = forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
_Cons
{-# INLINE _Cons #-}
instance Cons (Seq a) (Seq b) a b where
_Cons :: Prism (Seq a) (Seq b) (a, Seq a) (b, Seq b)
_Cons = forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry' forall a. a -> Seq a -> Seq a
(Seq.<|)) forall a b. (a -> b) -> a -> b
$ \Seq a
aas -> case forall a. Seq a -> ViewL a
viewl Seq a
aas of
a
a Seq.:< Seq a
as -> forall a b. b -> Either a b
Right (a
a, Seq a
as)
ViewL a
EmptyL -> forall a b. a -> Either a b
Left forall a. Monoid a => a
mempty
{-# INLINE _Cons #-}
(<|) :: Cons s s a a => a -> s -> s
<| :: forall s a. Cons s s a a => a -> s -> s
(<|) = forall a b c. ((a, b) -> c) -> a -> b -> c
curry (forall k (is :: IxList) t b.
Is k A_Review =>
Optic' k is t b -> b -> t
review forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
_Cons)
{-# INLINE (<|) #-}
cons :: Cons s s a a => a -> s -> s
cons :: forall s a. Cons s s a a => a -> s -> s
cons = forall a b c. ((a, b) -> c) -> a -> b -> c
curry (forall k (is :: IxList) t b.
Is k A_Review =>
Optic' k is t b -> b -> t
review forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
_Cons)
{-# INLINE cons #-}
uncons :: Cons s s a a => s -> Maybe (a, s)
uncons :: forall s a. Cons s s a a => s -> Maybe (a, s)
uncons = forall k (is :: IxList) s a.
Is k An_AffineFold =>
Optic' k is s a -> s -> Maybe a
preview forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
_Cons
{-# INLINE uncons #-}
_head :: Cons s s a a => AffineTraversal' s a
_head :: forall s a. Cons s s a a => AffineTraversal' s a
_head = forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
_Cons forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% forall s t a b. Field1 s t a b => Lens s t a b
_1
{-# INLINE _head #-}
_tail :: Cons s s a a => AffineTraversal' s s
_tail :: forall s a. Cons s s a a => AffineTraversal' s s
_tail = forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
_Cons forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% forall s t a b. Field2 s t a b => Lens s t a b
_2
{-# INLINE _tail #-}
class Snoc s t a b | s -> a, t -> b, s b -> t, t a -> s where
_Snoc :: Prism s t (s, a) (t, b)
instance Snoc [a] [b] a b where
_Snoc :: Prism [a] [b] ([a], a) ([b], b)
_Snoc = forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism (\([b]
as,b
a) -> [b]
as forall a. [a] -> [a] -> [a]
Prelude.++ [b
a]) forall a b. (a -> b) -> a -> b
$ \[a]
aas -> if forall (t :: * -> *) a. Foldable t => t a -> Bool
Prelude.null [a]
aas
then forall a b. a -> Either a b
Left []
else forall a b. b -> Either a b
Right (forall a. [a] -> [a]
Prelude.init [a]
aas, forall a. [a] -> a
Prelude.last [a]
aas)
{-# INLINE _Snoc #-}
instance Snoc (ZipList a) (ZipList b) a b where
_Snoc :: Prism (ZipList a) (ZipList b) (ZipList a, a) (ZipList b, b)
_Snoc = forall k (is :: IxList) s t a b r.
Is k A_Prism =>
Optic k is s t a b -> ((b -> t) -> (s -> Either t a) -> r) -> r
withPrism Prism [a] [b] ([a], a) ([b], b)
listSnoc forall a b. (a -> b) -> a -> b
$ \([b], b) -> [b]
listReview [a] -> Either [b] ([a], a)
listPreview ->
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism (coerce :: forall a b. Coercible a b => a -> b
coerce ([b], b) -> [b]
listReview) (coerce :: forall a b. Coercible a b => a -> b
coerce [a] -> Either [b] ([a], a)
listPreview) where
listSnoc :: Prism [a] [b] ([a], a) ([b], b)
listSnoc :: Prism [a] [b] ([a], a) ([b], b)
listSnoc = forall s t a b. Snoc s t a b => Prism s t (s, a) (t, b)
_Snoc
{-# INLINE _Snoc #-}
instance Snoc (Seq a) (Seq b) a b where
_Snoc :: Prism (Seq a) (Seq b) (Seq a, a) (Seq b, b)
_Snoc = forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry' forall a. Seq a -> a -> Seq a
(Seq.|>)) forall a b. (a -> b) -> a -> b
$ \Seq a
aas -> case forall a. Seq a -> ViewR a
viewr Seq a
aas of
Seq a
as Seq.:> a
a -> forall a b. b -> Either a b
Right (Seq a
as, a
a)
ViewR a
EmptyR -> forall a b. a -> Either a b
Left forall a. Monoid a => a
mempty
{-# INLINE _Snoc #-}
_init :: Snoc s s a a => AffineTraversal' s s
_init :: forall s a. Snoc s s a a => AffineTraversal' s s
_init = forall s t a b. Snoc s t a b => Prism s t (s, a) (t, b)
_Snoc forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% forall s t a b. Field1 s t a b => Lens s t a b
_1
{-# INLINE _init #-}
_last :: Snoc s s a a => AffineTraversal' s a
_last :: forall s a. Snoc s s a a => AffineTraversal' s a
_last = forall s t a b. Snoc s t a b => Prism s t (s, a) (t, b)
_Snoc forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% forall s t a b. Field2 s t a b => Lens s t a b
_2
{-# INLINE _last #-}
(|>) :: Snoc s s a a => s -> a -> s
|> :: forall s a. Snoc s s a a => s -> a -> s
(|>) = forall a b c. ((a, b) -> c) -> a -> b -> c
curry (forall k (is :: IxList) t b.
Is k A_Review =>
Optic' k is t b -> b -> t
review forall s t a b. Snoc s t a b => Prism s t (s, a) (t, b)
_Snoc)
{-# INLINE (|>) #-}
snoc :: Snoc s s a a => s -> a -> s
snoc :: forall s a. Snoc s s a a => s -> a -> s
snoc = forall a b c. ((a, b) -> c) -> a -> b -> c
curry (forall k (is :: IxList) t b.
Is k A_Review =>
Optic' k is t b -> b -> t
review forall s t a b. Snoc s t a b => Prism s t (s, a) (t, b)
_Snoc)
{-# INLINE snoc #-}
unsnoc :: Snoc s s a a => s -> Maybe (s, a)
unsnoc :: forall s a. Snoc s s a a => s -> Maybe (s, a)
unsnoc s
s = forall k (is :: IxList) s a.
Is k An_AffineFold =>
Optic' k is s a -> s -> Maybe a
preview forall s t a b. Snoc s t a b => Prism s t (s, a) (t, b)
_Snoc s
s
{-# INLINE unsnoc #-}