2023-04-12 11:34:33 -04:00
|
|
|
module Control.Lens.Traversal
|
|
|
|
|
|
|
|
import Control.Monad.State
|
2023-04-19 14:07:50 -04:00
|
|
|
import Data.List
|
2023-04-12 11:34:33 -04:00
|
|
|
import Data.Profunctor
|
|
|
|
import Data.Profunctor.Traversing
|
2023-04-12 11:55:22 -04:00
|
|
|
import Control.Applicative.Backwards
|
2023-04-14 13:16:08 -04:00
|
|
|
import Control.Applicative.Indexing
|
2023-04-12 11:34:33 -04:00
|
|
|
import Control.Lens.Optic
|
2023-04-19 14:07:50 -04:00
|
|
|
import Control.Lens.Indexed
|
2023-04-12 11:34:33 -04:00
|
|
|
import Control.Lens.Optional
|
2023-04-15 21:44:56 -04:00
|
|
|
import Control.Lens.Lens
|
2023-04-14 13:16:08 -04:00
|
|
|
import Control.Lens.Prism
|
2023-04-12 11:34:33 -04:00
|
|
|
|
|
|
|
%default total
|
|
|
|
|
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- Type definitions
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
record IsTraversal p where
|
|
|
|
constructor MkIsTraversal
|
|
|
|
runIsTraversal : Traversing p
|
|
|
|
|
|
|
|
export %hint
|
|
|
|
traversalToOptional : IsTraversal p => IsOptional p
|
|
|
|
traversalToOptional @{MkIsTraversal _} = MkIsOptional %search
|
|
|
|
|
2023-04-20 11:15:39 -04:00
|
|
|
export %hint
|
|
|
|
indexedTraversal : IsTraversal p => IsTraversal (Indexed i p)
|
|
|
|
indexedTraversal @{MkIsTraversal _} = MkIsTraversal %search
|
|
|
|
|
2023-04-12 11:34:33 -04:00
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
||| A traversal is a lens that may have more than one focus.
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
0 Traversal : (s,t,a,b : Type) -> Type
|
|
|
|
Traversal = Optic IsTraversal
|
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
||| `Traversal'` is the `Simple` version of `Traversal`.
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
0 Traversal' : (s,a : Type) -> Type
|
2023-04-12 11:59:51 -04:00
|
|
|
Traversal' = Simple Traversal
|
2023-04-12 11:34:33 -04:00
|
|
|
|
2023-04-20 13:24:43 -04:00
|
|
|
||| An indexed traversal allows access to the indices of the values as they are
|
|
|
|
||| being modified.
|
2023-04-19 14:07:50 -04:00
|
|
|
public export
|
|
|
|
0 IndexedTraversal : (i,s,t,a,b : Type) -> Type
|
|
|
|
IndexedTraversal = IndexedOptic IsTraversal
|
|
|
|
|
2023-04-20 13:24:43 -04:00
|
|
|
||| `IndexedTraversal'` is the `Simple` version of `IndexedTraversal`.
|
2023-04-19 14:07:50 -04:00
|
|
|
public export
|
|
|
|
0 IndexedTraversal' : (i,s,a : Type) -> Type
|
|
|
|
IndexedTraversal' = Simple . IndexedTraversal
|
|
|
|
|
2023-04-12 11:34:33 -04:00
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- Utilities for traversals
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
2023-10-19 14:30:56 -04:00
|
|
|
||| Construct a traversal from a `traverse`-like function.
|
|
|
|
public export
|
|
|
|
traversal : (forall f. Applicative f => (a -> f b) -> s -> f t) -> Traversal s t a b
|
|
|
|
traversal f @{MkIsTraversal _} = wander f
|
|
|
|
|
|
|
|
||| Construct an indexed traversal from a `traverse`-like function.
|
|
|
|
public export
|
|
|
|
itraversal : (forall f. Applicative f => (i -> a -> f b) -> s -> f t) -> IndexedTraversal i s t a b
|
|
|
|
itraversal f @{MkIsTraversal _} @{ind} = wander (f . curry) . indexed @{ind}
|
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
|
2023-04-20 13:24:43 -04:00
|
|
|
||| Convert a traversal into an indexed traversal, adding an index for the
|
|
|
|
||| ordinal position of the focus.
|
2023-04-19 14:07:50 -04:00
|
|
|
public export
|
2023-04-20 11:15:39 -04:00
|
|
|
iordinal : Num i => Traversal s t a b -> IndexedTraversal i s t a b
|
2023-10-19 14:30:56 -04:00
|
|
|
iordinal l = itraversal func
|
2023-04-19 14:07:50 -04:00
|
|
|
where
|
2023-10-19 14:30:56 -04:00
|
|
|
func : Applicative f => (i -> a -> f b) -> s -> f t
|
2023-04-20 11:15:39 -04:00
|
|
|
func = indexing $ applyStar . l . MkStar {f = Indexing i f}
|
2023-04-19 14:07:50 -04:00
|
|
|
|
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
||| Derive a traversal from a `Traversable` implementation.
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
traversed : Traversable t => Traversal (t a) (t b) a b
|
|
|
|
traversed @{_} @{MkIsTraversal _} = traverse'
|
|
|
|
|
2023-04-20 13:24:43 -04:00
|
|
|
||| Derive an indexed traversal from a `Traversable` implementation.
|
2023-04-19 14:07:50 -04:00
|
|
|
public export
|
2023-04-20 11:15:39 -04:00
|
|
|
itraversed : Num i => Traversable t => IndexedTraversal i (t a) (t b) a b
|
2023-04-19 14:07:50 -04:00
|
|
|
itraversed = iordinal traversed
|
|
|
|
|
2023-04-14 13:16:08 -04:00
|
|
|
||| Contstruct a traversal over a `Bitraversable` container with matching types.
|
|
|
|
public export
|
|
|
|
both : Bitraversable t => Traversal (t a a) (t b b) a b
|
2023-10-19 14:30:56 -04:00
|
|
|
both = traversal (\f => bitraverse f f)
|
2023-04-14 13:16:08 -04:00
|
|
|
|
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
||| Reverse the order of a traversal's focuses.
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
backwards : Traversal s t a b -> Traversal s t a b
|
2023-10-19 14:30:56 -04:00
|
|
|
backwards l = traversal func
|
2023-04-12 11:34:33 -04:00
|
|
|
where
|
|
|
|
func : Applicative f => (a -> f b) -> s -> f t
|
|
|
|
func fn = forwards . applyStar {f = Backwards f} (l $ MkStar (MkBackwards . fn))
|
|
|
|
|
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
||| Map each focus of a traversal to a computation, evaluate those computations
|
|
|
|
||| and combine the results.
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
traverseOf : Applicative f => Traversal s t a b -> (a -> f b) -> s -> f t
|
|
|
|
traverseOf l = applyStar . l . MkStar {f}
|
|
|
|
|
2023-04-20 13:24:43 -04:00
|
|
|
||| Map each focus of a traversal to a computation with acces to the index,
|
|
|
|
||| evaluate those computations and combine the results.
|
2023-04-19 14:07:50 -04:00
|
|
|
public export
|
|
|
|
itraverseOf : Applicative f => IndexedTraversal i s t a b -> (i -> a -> f b) -> s -> f t
|
|
|
|
itraverseOf l = traverseOf (l @{%search} @{Idxed}) . uncurry
|
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
||| A version of `traverseOf` but with the arguments flipped.
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
forOf : Applicative f => Traversal s t a b -> s -> (a -> f b) -> f t
|
2023-04-19 14:07:50 -04:00
|
|
|
forOf = flip . traverseOf
|
|
|
|
|
2023-04-20 13:24:43 -04:00
|
|
|
||| A version of `itraverseOf` but with the arguments flipped.
|
2023-04-19 14:07:50 -04:00
|
|
|
public export
|
|
|
|
iforOf : Applicative f => IndexedTraversal i s t a b -> s -> (i -> a -> f b) -> f t
|
|
|
|
iforOf = flip . itraverseOf
|
2023-04-12 11:34:33 -04:00
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
||| Evaluate each computation within the traversal and collect the results.
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
sequenceOf : Applicative f => Traversal s t (f a) a -> s -> f t
|
|
|
|
sequenceOf l = traverseOf l id
|
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
||| Fold across the focuses of a traversal from the leftmost focus, providing a
|
2023-04-14 13:16:08 -04:00
|
|
|
||| replacement value for each, and return the final accumulator along with the
|
|
|
|
||| new structure.
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
mapAccumLOf : Traversal s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
|
|
|
|
mapAccumLOf l f z =
|
|
|
|
let g = state . flip f
|
|
|
|
in runState z . traverseOf l g
|
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
||| Fold across the focuses of a traversal from the rightmost focus, providing a
|
2023-04-14 13:16:08 -04:00
|
|
|
||| replacement value for each, and return the final accumulator along with the
|
|
|
|
||| new structure.
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
mapAccumROf : Traversal s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
|
|
|
|
mapAccumROf l f z =
|
|
|
|
let g = MkBackwards {f=State acc} . state . flip f
|
|
|
|
in runState z . forwards . traverseOf l g
|
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
||| Fold across the focuses of a traversal from the left, returning each
|
|
|
|
||| intermediate value of the fold.
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
scanl1Of : Traversal s t a a -> (a -> a -> a) -> s -> t
|
|
|
|
scanl1Of l f =
|
|
|
|
let step : Maybe a -> a -> (Maybe a, a)
|
|
|
|
step Nothing x = (Just x, x)
|
|
|
|
step (Just s) x = let r = f s x in (Just r, r)
|
|
|
|
in snd . mapAccumLOf l step Nothing
|
|
|
|
|
2023-04-12 22:41:23 -04:00
|
|
|
||| Fold across the focuses of a traversal from the right, returning each
|
|
|
|
||| intermediate value of the fold.
|
2023-04-12 11:34:33 -04:00
|
|
|
public export
|
|
|
|
scanr1Of : Traversal s t a a -> (a -> a -> a) -> s -> t
|
|
|
|
scanr1Of l f =
|
|
|
|
let step : Maybe a -> a -> (Maybe a, a)
|
|
|
|
step Nothing x = (Just x, x)
|
|
|
|
step (Just s) x = let r = f s x in (Just r, r)
|
|
|
|
in snd . mapAccumROf l step Nothing
|
2023-04-14 13:16:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
||| Try to map over a traversal, failing if the traversal has no focuses.
|
|
|
|
public export
|
|
|
|
failover : Alternative f => Traversal s t a b -> (a -> b) -> s -> f t
|
|
|
|
failover l f x =
|
|
|
|
let _ = Bool.Monoid.Any
|
|
|
|
(b, y) = traverseOf l ((True,) . f) x
|
|
|
|
in guard b $> y
|
|
|
|
|
2023-04-20 13:24:43 -04:00
|
|
|
||| Try to map over an indexed traversal, failing if the traversal has no focuses.
|
2023-04-19 14:07:50 -04:00
|
|
|
public export
|
|
|
|
ifailover : Alternative f => IndexedTraversal i s t a b -> (i -> a -> b) -> s -> f t
|
|
|
|
ifailover l = failover (l @{%search} @{Idxed}) . uncurry
|
|
|
|
|
|
|
|
|
|
|
|
partsOf_update : a -> State (List a) a
|
|
|
|
partsOf_update x = get >>= \case
|
|
|
|
x' :: xs' => put xs' >> pure x'
|
|
|
|
[] => pure x
|
|
|
|
|
2023-04-15 21:44:56 -04:00
|
|
|
||| Convert a traversal into a lens over a list of values.
|
|
|
|
|||
|
|
|
|
||| Note that this is only a true lens if the invariant of the list's length is
|
|
|
|
||| maintained. You should avoid mapping `over` this lens with a function that
|
|
|
|
||| changes the list's length.
|
|
|
|
public export
|
|
|
|
partsOf : Traversal s t a a -> Lens s t (List a) (List a)
|
|
|
|
partsOf l = lens (runForget $ l $ MkForget pure)
|
2023-04-19 14:07:50 -04:00
|
|
|
(flip evalState . traverseOf l partsOf_update)
|
|
|
|
|
2023-04-20 13:24:43 -04:00
|
|
|
||| Convert an indexed traversal into an indexed lens over a list of values, with
|
|
|
|
||| access to a list of indices.
|
|
|
|
|||
|
|
|
|
||| Note that this is only a true lens if the invariant of the list's length is
|
|
|
|
||| maintained. You should avoid mapping `over` this lens with a function that
|
|
|
|
||| changes the list's length.
|
2023-04-19 14:07:50 -04:00
|
|
|
public export
|
|
|
|
ipartsOf : IndexedTraversal i s t a a -> IndexedLens (List i) s t (List a) (List a)
|
|
|
|
ipartsOf l = ilens (unzip . (runForget $ l @{%search} @{Idxed} $ MkForget pure))
|
|
|
|
(flip evalState . itraverseOf l (const partsOf_update))
|
2023-04-15 21:44:56 -04:00
|
|
|
|
2023-04-14 13:16:08 -04:00
|
|
|
|
|
|
|
||| Construct an optional that focuses on the first value of a traversal.
|
|
|
|
|||
|
|
|
|
||| For the fold version of this, see `pre`.
|
|
|
|
public export
|
|
|
|
singular : Traversal' s a -> Optional' s a
|
|
|
|
singular l = optional' (runForget $ l (MkForget Just)) set
|
|
|
|
where
|
|
|
|
set : s -> a -> s
|
|
|
|
set str x = evalState True $ traverseOf l
|
|
|
|
(\y => if !get then put False $> x else pure y) str
|
|
|
|
|
2023-04-20 13:24:43 -04:00
|
|
|
||| Construct an indexed optional that focuses on the first value of an
|
|
|
|
||| indexed traversal.
|
|
|
|
|||
|
|
|
|
||| For the fold version of this, see `ipre`.
|
|
|
|
public export
|
|
|
|
isingular : IndexedTraversal' i s a -> IndexedOptional' i s a
|
|
|
|
isingular l = ioptional' (runForget $ l @{%search} @{Idxed} (MkForget Just)) set
|
|
|
|
where
|
|
|
|
set : s -> a -> s
|
|
|
|
set str x = evalState True $ itraverseOf l
|
|
|
|
(\_,y => if !get then put False $> x else pure y) str
|
|
|
|
|
2023-04-14 13:16:08 -04:00
|
|
|
||| Filter the focuses of a traversal by a predicate on their ordinal positions.
|
|
|
|
public export
|
|
|
|
elementsOf : Traversal s t a a -> (Nat -> Bool) -> Traversal s t a a
|
2023-10-19 14:30:56 -04:00
|
|
|
elementsOf l p = traversal func
|
2023-04-14 13:16:08 -04:00
|
|
|
where
|
|
|
|
func : Applicative f => (a -> f a) -> s -> f t
|
|
|
|
func fn = indexing {f} (traverseOf l) $
|
|
|
|
\i,x => if p i then fn x else pure {f} x
|
|
|
|
|
|
|
|
||| Traverse over the elements of a `Traversable` container that satisfy a
|
|
|
|
||| predicate.
|
|
|
|
public export
|
|
|
|
elements : Traversable t => (Nat -> Bool) -> Traversal' (t a) a
|
|
|
|
elements = elementsOf traversed
|
|
|
|
|
|
|
|
||| Construct an optional that focuses on the nth element of a traversal.
|
|
|
|
public export
|
|
|
|
elementOf : Traversal' s a -> Nat -> Optional' s a
|
|
|
|
elementOf l n = singular $ elementsOf l (n ==)
|
|
|
|
|
|
|
|
||| Construct an optional that focuses on the nth element of a `Traversable`
|
|
|
|
||| container.
|
|
|
|
public export
|
|
|
|
element : Traversable t => Nat -> Optional' (t a) a
|
|
|
|
element = elementOf traversed
|
|
|
|
|
|
|
|
|
|
|
|
||| Limit a traversal to its first n focuses.
|
|
|
|
public export
|
|
|
|
taking : Nat -> Traversal s t a a -> Traversal s t a a
|
|
|
|
taking n l = elementsOf l (< n)
|
|
|
|
|
|
|
|
||| Remove the first n focuses from a traversal.
|
|
|
|
public export
|
|
|
|
dropping : Nat -> Traversal s t a a -> Traversal s t a a
|
|
|
|
dropping i l = elementsOf l (>= i)
|