{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE ImpredicativeTypes #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE QualifiedDo #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeAbstractions #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}

{- |
This module provides 'Foldable' class, and provides a way to loop through it while reborrowing existing 'Borrow's into sublifetime.
The module also introduces 'Borrows', which is a heterogeneous list of 'Borrow's in the same lifetime.
-}
module Control.Monad.Borrow.Pure.Experimental.Loop (
  Borrows (..),
  forReborrowing,
  forReborrowingOf_,
  forReborrowing_,
  iforReborrowingOf_,
  iforReborrowing_,
  Fold,
  Foldable (..),
  IndexedFold,
  ifoldMapDefaultOf,
  FoldableWithIndex (..),
  traverse_,
  for_,
  toListOf,
  toList,
  foldBorrow,
  foldBorrowOf,
  GenericFoldable,
  genericFoldMap,
  ifoldMapDefault,
) where

import Control.Functor.Linear qualified as Control
import Control.Monad.Borrow.Pure
import Control.Monad.Borrow.Pure.BO.Unsafe
import Control.Monad.Borrow.Pure.Experimental.Borrows
import Control.Monad.Borrow.Pure.Experimental.Reborrowable
import Control.Monad.Borrow.Pure.Utils (coerceLin)
import Data.Bifunctor.Linear qualified as Bi
import Data.Functor.Linear qualified as Data
import Data.HashMap.Mutable.Linear qualified as LHM
import Data.List.NonEmpty.Linear (NonEmpty)
import Data.List.NonEmpty.Linear qualified as LNE
import Data.Monoid.Linear
import Data.Vector.Mutable.Linear qualified as LV
import Generics.Linear
import Prelude.Linear hiding (foldMap)
import Prelude.Linear qualified as PL
import Unsafe.Linear qualified as Unsafe

{- |
@'forReborrowingN' iterates over the elements of 'Data.Traversable' @t@
inside the delimited sublifetime, reborrowing the 'Borrows' in @bors@ for that sublifetime.
-}
forReborrowing ::
  (Data.Traversable t, Reborrowable bor) =>
  bor α xs %1 ->
  t b %1 ->
  ( forall β.
    bor (β /\ α) xs %1 ->
    b %1 ->
    BO (β /\ α) c
  ) ->
  BO α (t c, bor α xs)
{-# INLINE forReborrowing #-}
forReborrowing :: forall {k} (t :: * -> *) (bor :: Lifetime -> k -> *)
       (α :: Lifetime) (xs :: k) b c.
(Traversable t, Reborrowable bor) =>
bor α xs
%1 -> t b
%1 -> (forall (β :: Lifetime).
       bor (β /\ α) xs %1 -> b %1 -> BO (β /\ α) c)
-> BO α (t c, bor α xs)
forReborrowing bor α xs
bors t b
tb forall (β :: Lifetime). bor (β /\ α) xs %1 -> b %1 -> BO (β /\ α) c
k =
  (StateT (bor α xs) (BO α) (t c)
 %1 -> bor α xs %1 -> BO α (t c, bor α xs))
-> bor α xs
%1 -> StateT (bor α xs) (BO α) (t c)
%1 -> BO α (t c, bor α xs)
forall a (p :: Multiplicity) b (q :: Multiplicity) c
       (r :: Multiplicity).
(a %p -> b %q -> c) %r -> b %q -> a %p -> c
flip StateT (bor α xs) (BO α) (t c)
%1 -> bor α xs %1 -> BO α (t c, bor α xs)
forall s (m :: * -> *) a. StateT s m a %1 -> s %1 -> m (a, s)
Control.runStateT bor α xs
bors (StateT (bor α xs) (BO α) (t c) %1 -> BO α (t c, bor α xs))
%1 -> StateT (bor α xs) (BO α) (t c) %1 -> BO α (t c, bor α xs)
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
    t b
%1 -> (b %1 -> StateT (bor α xs) (BO α) c)
-> StateT (bor α xs) (BO α) (t c)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a %1 -> (a %1 -> f b) -> f (t b)
Data.for t b
tb \b
a -> (bor α xs %1 -> BO α (c, bor α xs)) -> StateT (bor α xs) (BO α) c
forall s (m :: * -> *) a. (s %1 -> m (a, s)) -> StateT s m a
Control.StateT \bor α xs
bors ->
      bor α xs
%1 -> (forall (β :: Lifetime). bor (β /\ α) xs %1 -> BO (β /\ α) c)
%1 -> BO α (c, bor α xs)
forall {k} (bor :: Lifetime -> k -> *) (α :: Lifetime) (a :: k)
       (α' :: Lifetime) r.
Reborrowable bor =>
bor α a
%1 -> (forall (β :: Lifetime). bor (β /\ α) a %1 -> BO (β /\ α') r)
%1 -> BO α' (r, bor α a)
locally bor α xs
bors (\bor (β /\ α) xs
bors -> bor (β /\ α) xs %1 -> b %1 -> BO (β /\ α) c
forall (β :: Lifetime). bor (β /\ α) xs %1 -> b %1 -> BO (β /\ α) c
k bor (β /\ α) xs
bors b
a)

type Fold s a = forall w. (Monoid w) => (a %1 -> w) -> s %1 -> w

-- See https://github.com/tweag/linear-base/issues/190 for the discussion.
class Foldable t where
  foldMap :: (Monoid w) => (a %1 -> w) -> t a %1 -> w

type IndexedFold i s a = forall w. (Monoid w) => (i %1 -> a %1 -> w) -> s %1 -> w

class (Foldable t) => FoldableWithIndex i t | t -> i where
  ifoldMap :: (Monoid w) => (i %1 -> a %1 -> w) -> t a %1 -> w
  default ifoldMap ::
    (Foldable t, i ~ Int, Monoid w) =>
    (i %1 -> a %1 -> w) -> t a %1 -> w
  ifoldMap = (i %1 -> a %1 -> w) -> t a %1 -> w
(Int %1 -> a %1 -> w) -> t a %1 -> w
IndexedFold Int (t a) a
forall (t :: * -> *) a. Foldable t => IndexedFold Int (t a) a
ifoldMapDefault
  {-# INLINE ifoldMap #-}

ifoldMapDefaultOf :: forall s a. Fold s a %1 -> IndexedFold Int s a
{-# INLINE ifoldMapDefaultOf #-}
ifoldMapDefaultOf :: forall s a. Fold s a %1 -> IndexedFold Int s a
ifoldMapDefaultOf Fold s a
fld Int %1 -> a %1 -> w
k s
s =
  (State (Ur Int) w %1 -> Ur Int %1 -> w)
-> Ur Int %1 -> State (Ur Int) w %1 -> w
forall a (p :: Multiplicity) b (q :: Multiplicity) c
       (r :: Multiplicity).
(a %p -> b %q -> c) %r -> b %q -> a %p -> c
flip State (Ur Int) w %1 -> Ur Int %1 -> w
forall s a. Consumable s => State s a %1 -> s %1 -> a
Control.evalState (Int -> Ur Int
forall a. a -> Ur a
Ur Int
0) (State (Ur Int) w %1 -> w) -> State (Ur Int) w %1 -> w
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ Ap (StateT (Ur Int) Identity) w %1 -> State (Ur Int) w
forall {k} (m :: k -> *) (a :: k). Ap m a %1 -> m a
unAp (Ap (StateT (Ur Int) Identity) w %1 -> State (Ur Int) w)
-> Ap (StateT (Ur Int) Identity) w %1 -> State (Ur Int) w
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ ((a %1 -> Ap (StateT (Ur Int) Identity) w)
 -> s %1 -> Ap (StateT (Ur Int) Identity) w)
%1 -> s
%1 -> (a %1 -> Ap (StateT (Ur Int) Identity) w)
-> Ap (StateT (Ur Int) Identity) w
forall a (p :: Multiplicity) b (q :: Multiplicity) c
       (r :: Multiplicity).
(a %p -> b %q -> c) %r -> b %q -> a %p -> c
flip (a %1 -> Ap (StateT (Ur Int) Identity) w)
-> s %1 -> Ap (StateT (Ur Int) Identity) w
Fold s a
fld s
s ((a %1 -> Ap (StateT (Ur Int) Identity) w)
 -> Ap (StateT (Ur Int) Identity) w)
%1 -> (a %1 -> Ap (StateT (Ur Int) Identity) w)
-> Ap (StateT (Ur Int) Identity) w
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ \a
a -> State (Ur Int) w -> Ap (StateT (Ur Int) Identity) w
forall {k} (m :: k -> *) (a :: k). m a -> Ap m a
Ap Control.do
    Ur i <- StateT (Ur Int) Identity (Ur Int)
forall (m :: * -> *) s. (Applicative m, Dupable s) => StateT s m s
Control.get
    Control.put $ Ur $! i + 1
    Control.pure $ k i a

ifoldMapDefault :: (Foldable t) => IndexedFold Int (t a) a
{-# INLINE ifoldMapDefault #-}
ifoldMapDefault :: forall (t :: * -> *) a. Foldable t => IndexedFold Int (t a) a
ifoldMapDefault = Fold (t a) a %1 -> IndexedFold Int (t a) a
forall s a. Fold s a %1 -> IndexedFold Int s a
ifoldMapDefaultOf (a %1 -> w) -> t a %1 -> w
Fold (t a) a
forall w a. Monoid w => (a %1 -> w) -> t a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap

foldBorrowOf :: Fold s a %1 -> Fold (Borrow bk α s) (Borrow bk α a)
{-# INLINE foldBorrowOf #-}
foldBorrowOf :: forall s a (bk :: BorrowKind) (α :: Lifetime).
Fold s a %1 -> Fold (Borrow bk α s) (Borrow bk α a)
foldBorrowOf Fold s a
fld Borrow bk α a %1 -> w
k = (a %1 -> w) -> s %1 -> w
Fold s a
fld (Borrow bk α a %1 -> w
k (Borrow bk α a %1 -> w) -> (a %1 -> Borrow bk α a) -> a %1 -> w
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. a -> Borrow bk α a
forall (ak :: AliasKind) (α :: Lifetime) a. a -> Alias ak α a
UnsafeAlias) (s %1 -> w)
%1 -> (Alias ('Borrow bk) α s %1 -> s)
-> Alias ('Borrow bk) α s
%1 -> w
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. Alias ('Borrow bk) α s %1 -> s
forall (ak :: AliasKind) (α :: Lifetime) a. Alias ak α a %1 -> a
unsafeUnalias

foldBorrow :: (Foldable t) => Fold (Borrow bk α (t a)) (Borrow bk α a)
{-# INLINE foldBorrow #-}
foldBorrow :: forall (t :: * -> *) (bk :: BorrowKind) (α :: Lifetime) a.
Foldable t =>
Fold (Borrow bk α (t a)) (Borrow bk α a)
foldBorrow = Fold (t a) a %1 -> Fold (Borrow bk α (t a)) (Borrow bk α a)
forall s a (bk :: BorrowKind) (α :: Lifetime).
Fold s a %1 -> Fold (Borrow bk α s) (Borrow bk α a)
foldBorrowOf (a %1 -> w) -> t a %1 -> w
Fold (t a) a
forall w a. Monoid w => (a %1 -> w) -> t a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap

traverse_ :: (Foldable t, Data.Applicative m) => (a %1 -> m ()) -> t a %1 -> m ()
{-# INLINE traverse_ #-}
traverse_ :: forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Applicative m) =>
(a %1 -> m ()) -> t a %1 -> m ()
traverse_ a %1 -> m ()
f = Ap m () %1 -> m ()
forall {k} (m :: k -> *) (a :: k). Ap m a %1 -> m a
unAp (Ap m () %1 -> m ()) -> (t a %1 -> Ap m ()) -> t a %1 -> m ()
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. (a %1 -> Ap m ()) -> t a %1 -> Ap m ()
forall w a. Monoid w => (a %1 -> w) -> t a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap (m () -> Ap m ()
forall {k} (m :: k -> *) (a :: k). m a -> Ap m a
Ap (m () %1 -> Ap m ()) -> (a %1 -> m ()) -> a %1 -> Ap m ()
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. a %1 -> m ()
f)

for_ :: (Foldable t, Data.Applicative m) => t a %1 -> (a %1 -> m ()) -> m ()
{-# INLINE for_ #-}
for_ :: forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Applicative m) =>
t a %1 -> (a %1 -> m ()) -> m ()
for_ = ((a %1 -> m ()) -> t a %1 -> m ())
-> t a %1 -> (a %1 -> m ()) -> m ()
forall a (p :: Multiplicity) b (q :: Multiplicity) c
       (r :: Multiplicity).
(a %p -> b %q -> c) %r -> b %q -> a %p -> c
flip (a %1 -> m ()) -> t a %1 -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Applicative m) =>
(a %1 -> m ()) -> t a %1 -> m ()
traverse_

newtype Ap m a = Ap (m a)
  deriving newtype ((forall a b. (a %1 -> b) -> Ap m a %1 -> Ap m b) -> Functor (Ap m)
forall a b. (a %1 -> b) -> Ap m a %1 -> Ap m b
forall (m :: * -> *) a b.
Functor m =>
(a %1 -> b) -> Ap m a %1 -> Ap m b
forall (f :: * -> *).
(forall a b. (a %1 -> b) -> f a %1 -> f b) -> Functor f
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a %1 -> b) -> Ap m a %1 -> Ap m b
fmap :: forall a b. (a %1 -> b) -> Ap m a %1 -> Ap m b
Data.Functor, Functor (Ap m)
Functor (Ap m) =>
(forall a b. (a %1 -> b) %1 -> Ap m a %1 -> Ap m b)
-> Functor (Ap m)
forall a b. (a %1 -> b) %1 -> Ap m a %1 -> Ap m b
forall (f :: * -> *).
Functor f =>
(forall a b. (a %1 -> b) %1 -> f a %1 -> f b) -> Functor f
forall (m :: * -> *). Functor m => Functor (Ap m)
forall (m :: * -> *) a b.
Functor m =>
(a %1 -> b) %1 -> Ap m a %1 -> Ap m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a %1 -> b) %1 -> Ap m a %1 -> Ap m b
fmap :: forall a b. (a %1 -> b) %1 -> Ap m a %1 -> Ap m b
Control.Functor, Functor (Ap m)
Functor (Ap m) =>
(forall a. a -> Ap m a)
-> (forall a b. Ap m (a %1 -> b) %1 -> Ap m a %1 -> Ap m b)
-> (forall a b c.
    (a %1 -> b %1 -> c) -> Ap m a %1 -> Ap m b %1 -> Ap m c)
-> Applicative (Ap m)
forall a. a -> Ap m a
forall a b. Ap m (a %1 -> b) %1 -> Ap m a %1 -> Ap m b
forall a b c.
(a %1 -> b %1 -> c) -> Ap m a %1 -> Ap m b %1 -> Ap m c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a %1 -> b) %1 -> f a %1 -> f b)
-> (forall a b c. (a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c)
-> Applicative f
forall (m :: * -> *). Applicative m => Functor (Ap m)
forall (m :: * -> *) a. Applicative m => a -> Ap m a
forall (m :: * -> *) a b.
Applicative m =>
Ap m (a %1 -> b) %1 -> Ap m a %1 -> Ap m b
forall (m :: * -> *) a b c.
Applicative m =>
(a %1 -> b %1 -> c) -> Ap m a %1 -> Ap m b %1 -> Ap m c
$cpure :: forall (m :: * -> *) a. Applicative m => a -> Ap m a
pure :: forall a. a -> Ap m a
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
Ap m (a %1 -> b) %1 -> Ap m a %1 -> Ap m b
<*> :: forall a b. Ap m (a %1 -> b) %1 -> Ap m a %1 -> Ap m b
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a %1 -> b %1 -> c) -> Ap m a %1 -> Ap m b %1 -> Ap m c
liftA2 :: forall a b c.
(a %1 -> b %1 -> c) -> Ap m a %1 -> Ap m b %1 -> Ap m c
Data.Applicative, Applicative (Ap m)
Functor (Ap m)
(Applicative (Ap m), Functor (Ap m)) =>
(forall a. a %1 -> Ap m a)
-> (forall a b. Ap m (a %1 -> b) %1 -> Ap m a %1 -> Ap m b)
-> (forall a b c.
    (a %1 -> b %1 -> c) %1 -> Ap m a %1 -> Ap m b %1 -> Ap m c)
-> Applicative (Ap m)
forall a. a %1 -> Ap m a
forall a b. Ap m (a %1 -> b) %1 -> Ap m a %1 -> Ap m b
forall a b c.
(a %1 -> b %1 -> c) %1 -> Ap m a %1 -> Ap m b %1 -> Ap m c
forall (f :: * -> *).
(Applicative f, Functor f) =>
(forall a. a %1 -> f a)
-> (forall a b. f (a %1 -> b) %1 -> f a %1 -> f b)
-> (forall a b c.
    (a %1 -> b %1 -> c) %1 -> f a %1 -> f b %1 -> f c)
-> Applicative f
forall (m :: * -> *). Applicative m => Applicative (Ap m)
forall (m :: * -> *). Applicative m => Functor (Ap m)
forall (m :: * -> *) a. Applicative m => a %1 -> Ap m a
forall (m :: * -> *) a b.
Applicative m =>
Ap m (a %1 -> b) %1 -> Ap m a %1 -> Ap m b
forall (m :: * -> *) a b c.
Applicative m =>
(a %1 -> b %1 -> c) %1 -> Ap m a %1 -> Ap m b %1 -> Ap m c
$cpure :: forall (m :: * -> *) a. Applicative m => a %1 -> Ap m a
pure :: forall a. a %1 -> Ap m a
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
Ap m (a %1 -> b) %1 -> Ap m a %1 -> Ap m b
<*> :: forall a b. Ap m (a %1 -> b) %1 -> Ap m a %1 -> Ap m b
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a %1 -> b %1 -> c) %1 -> Ap m a %1 -> Ap m b %1 -> Ap m c
liftA2 :: forall a b c.
(a %1 -> b %1 -> c) %1 -> Ap m a %1 -> Ap m b %1 -> Ap m c
Control.Applicative)

instance (Data.Applicative f, Semigroup w) => Semigroup (Ap f w) where
  <> :: Ap f w %1 -> Ap f w %1 -> Ap f w
(<>) = (w %1 -> w %1 -> w) -> Ap f w %1 -> Ap f w %1 -> Ap f w
forall a b c.
(a %1 -> b %1 -> c) -> Ap f a %1 -> Ap f b %1 -> Ap f c
forall (f :: * -> *) a b c.
Applicative f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
Data.liftA2 w %1 -> w %1 -> w
forall a. Semigroup a => a %1 -> a %1 -> a
(<>)
  {-# INLINE (<>) #-}

instance (Data.Applicative f, Monoid w) => Monoid (Ap f w) where
  mempty :: Ap f w
mempty = w -> Ap f w
forall a. a -> Ap f a
forall (f :: * -> *) a. Applicative f => a -> f a
Data.pure w
forall a. Monoid a => a
mempty
  {-# INLINE mempty #-}

unAp :: Ap m a %1 -> m a
unAp :: forall {k} (m :: k -> *) (a :: k). Ap m a %1 -> m a
unAp (Ap m a
m) = m a
m
{-# INLINE unAp #-}

forReborrowingOf_ ::
  (Reborrowable bor) =>
  Fold s a %1 ->
  bor α xs %1 ->
  s %1 ->
  ( forall β.
    bor (β /\ α) xs %1 ->
    a %1 ->
    BO (β /\ α) ()
  ) ->
  BO α (bor α xs)
{-# INLINE forReborrowingOf_ #-}
forReborrowingOf_ :: forall {k} (bor :: Lifetime -> k -> *) s a (α :: Lifetime)
       (xs :: k).
Reborrowable bor =>
Fold s a
%1 -> bor α xs
%1 -> s
%1 -> (forall (β :: Lifetime).
       bor (β /\ α) xs %1 -> a %1 -> BO (β /\ α) ())
-> BO α (bor α xs)
forReborrowingOf_ Fold s a
fld bor α xs
bors s
s forall (β :: Lifetime).
bor (β /\ α) xs %1 -> a %1 -> BO (β /\ α) ()
k =
  (StateT (bor α xs) (BO α) () %1 -> bor α xs %1 -> BO α (bor α xs))
-> bor α xs %1 -> StateT (bor α xs) (BO α) () %1 -> BO α (bor α xs)
forall a (p :: Multiplicity) b (q :: Multiplicity) c
       (r :: Multiplicity).
(a %p -> b %q -> c) %r -> b %q -> a %p -> c
flip StateT (bor α xs) (BO α) () %1 -> bor α xs %1 -> BO α (bor α xs)
forall (m :: * -> *) s.
Functor m =>
StateT s m () %1 -> s %1 -> m s
Control.execStateT bor α xs
bors (StateT (bor α xs) (BO α) () %1 -> BO α (bor α xs))
%1 -> StateT (bor α xs) (BO α) () %1 -> BO α (bor α xs)
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
    Ap (StateT (bor α xs) (BO α)) () %1 -> StateT (bor α xs) (BO α) ()
forall {k} (m :: k -> *) (a :: k). Ap m a %1 -> m a
unAp (Ap (StateT (bor α xs) (BO α)) ()
 %1 -> StateT (bor α xs) (BO α) ())
-> Ap (StateT (bor α xs) (BO α)) ()
%1 -> StateT (bor α xs) (BO α) ()
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
      ((a %1 -> Ap (StateT (bor α xs) (BO α)) ())
 -> s %1 -> Ap (StateT (bor α xs) (BO α)) ())
%1 -> s
%1 -> (a %1 -> Ap (StateT (bor α xs) (BO α)) ())
-> Ap (StateT (bor α xs) (BO α)) ()
forall a (p :: Multiplicity) b (q :: Multiplicity) c
       (r :: Multiplicity).
(a %p -> b %q -> c) %r -> b %q -> a %p -> c
flip (a %1 -> Ap (StateT (bor α xs) (BO α)) ())
-> s %1 -> Ap (StateT (bor α xs) (BO α)) ()
Fold s a
fld s
s ((a %1 -> Ap (StateT (bor α xs) (BO α)) ())
 -> Ap (StateT (bor α xs) (BO α)) ())
%1 -> (a %1 -> Ap (StateT (bor α xs) (BO α)) ())
-> Ap (StateT (bor α xs) (BO α)) ()
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
        StateT (bor α xs) (BO α) () -> Ap (StateT (bor α xs) (BO α)) ()
forall {k} (m :: k -> *) (a :: k). m a -> Ap m a
Ap (StateT (bor α xs) (BO α) ()
 %1 -> Ap (StateT (bor α xs) (BO α)) ())
-> (a %1 -> StateT (bor α xs) (BO α) ())
-> a
%1 -> Ap (StateT (bor α xs) (BO α)) ()
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. \a
a -> (bor α xs %1 -> BO α ((), bor α xs)) -> StateT (bor α xs) (BO α) ()
forall s (m :: * -> *) a. (s %1 -> m (a, s)) -> StateT s m a
Control.StateT \bor α xs
bors -> bor α xs
%1 -> (forall (β :: Lifetime).
       bor (β /\ α) xs %1 -> BO (β /\ α) ())
%1 -> BO α ((), bor α xs)
forall {k} (bor :: Lifetime -> k -> *) (α :: Lifetime) (a :: k)
       (α' :: Lifetime) r.
Reborrowable bor =>
bor α a
%1 -> (forall (β :: Lifetime). bor (β /\ α) a %1 -> BO (β /\ α') r)
%1 -> BO α' (r, bor α a)
locally bor α xs
bors (\bor (β /\ α) xs
bors -> bor (β /\ α) xs %1 -> a %1 -> BO (β /\ α) ()
forall (β :: Lifetime).
bor (β /\ α) xs %1 -> a %1 -> BO (β /\ α) ()
k bor (β /\ α) xs
bors a
a)

forReborrowing_ ::
  (Foldable t, Reborrowable bor) =>
  bor α xs %1 ->
  t a %1 ->
  ( forall β.
    bor (β /\ α) xs %1 ->
    a %1 ->
    BO (β /\ α) ()
  ) ->
  BO α (bor α xs)
{-# INLINE forReborrowing_ #-}
forReborrowing_ :: forall {k} (t :: * -> *) (bor :: Lifetime -> k -> *)
       (α :: Lifetime) (xs :: k) a.
(Foldable t, Reborrowable bor) =>
bor α xs
%1 -> t a
%1 -> (forall (β :: Lifetime).
       bor (β /\ α) xs %1 -> a %1 -> BO (β /\ α) ())
-> BO α (bor α xs)
forReborrowing_ = Fold (t a) a
%1 -> bor α xs
%1 -> t a
%1 -> (forall (β :: Lifetime).
       bor (β /\ α) xs %1 -> a %1 -> BO (β /\ α) ())
-> BO α (bor α xs)
forall {k} (bor :: Lifetime -> k -> *) s a (α :: Lifetime)
       (xs :: k).
Reborrowable bor =>
Fold s a
%1 -> bor α xs
%1 -> s
%1 -> (forall (β :: Lifetime).
       bor (β /\ α) xs %1 -> a %1 -> BO (β /\ α) ())
-> BO α (bor α xs)
forReborrowingOf_ (a %1 -> w) -> t a %1 -> w
Fold (t a) a
forall w a. Monoid w => (a %1 -> w) -> t a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap

iforReborrowingOf_ ::
  (Reborrowable bor) =>
  IndexedFold i s a %1 ->
  bor α xs %1 ->
  s %1 ->
  ( forall β.
    bor (β /\ α) xs %1 ->
    i %1 ->
    a %1 ->
    BO (β /\ α) ()
  ) ->
  BO α (bor α xs)
{-# INLINE iforReborrowingOf_ #-}
iforReborrowingOf_ :: forall {k} (bor :: Lifetime -> k -> *) i s a (α :: Lifetime)
       (xs :: k).
Reborrowable bor =>
IndexedFold i s a
%1 -> bor α xs
%1 -> s
%1 -> (forall (β :: Lifetime).
       bor (β /\ α) xs %1 -> i %1 -> a %1 -> BO (β /\ α) ())
-> BO α (bor α xs)
iforReborrowingOf_ IndexedFold i s a
fld bor α xs
bors s
s forall (β :: Lifetime).
bor (β /\ α) xs %1 -> i %1 -> a %1 -> BO (β /\ α) ()
k =
  (StateT (bor α xs) (BO α) () %1 -> bor α xs %1 -> BO α (bor α xs))
-> bor α xs %1 -> StateT (bor α xs) (BO α) () %1 -> BO α (bor α xs)
forall a (p :: Multiplicity) b (q :: Multiplicity) c
       (r :: Multiplicity).
(a %p -> b %q -> c) %r -> b %q -> a %p -> c
flip StateT (bor α xs) (BO α) () %1 -> bor α xs %1 -> BO α (bor α xs)
forall (m :: * -> *) s.
Functor m =>
StateT s m () %1 -> s %1 -> m s
Control.execStateT bor α xs
bors (StateT (bor α xs) (BO α) () %1 -> BO α (bor α xs))
%1 -> StateT (bor α xs) (BO α) () %1 -> BO α (bor α xs)
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
    Ap (StateT (bor α xs) (BO α)) () %1 -> StateT (bor α xs) (BO α) ()
forall {k} (m :: k -> *) (a :: k). Ap m a %1 -> m a
unAp (Ap (StateT (bor α xs) (BO α)) ()
 %1 -> StateT (bor α xs) (BO α) ())
-> Ap (StateT (bor α xs) (BO α)) ()
%1 -> StateT (bor α xs) (BO α) ()
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
      ((i %1 -> a %1 -> Ap (StateT (bor α xs) (BO α)) ())
 -> s %1 -> Ap (StateT (bor α xs) (BO α)) ())
%1 -> s
%1 -> (i %1 -> a %1 -> Ap (StateT (bor α xs) (BO α)) ())
-> Ap (StateT (bor α xs) (BO α)) ()
forall a (p :: Multiplicity) b (q :: Multiplicity) c
       (r :: Multiplicity).
(a %p -> b %q -> c) %r -> b %q -> a %p -> c
flip (i %1 -> a %1 -> Ap (StateT (bor α xs) (BO α)) ())
-> s %1 -> Ap (StateT (bor α xs) (BO α)) ()
IndexedFold i s a
fld s
s \i
i a
a ->
        StateT (bor α xs) (BO α) () -> Ap (StateT (bor α xs) (BO α)) ()
forall {k} (m :: k -> *) (a :: k). m a -> Ap m a
Ap (StateT (bor α xs) (BO α) ()
 %1 -> Ap (StateT (bor α xs) (BO α)) ())
-> StateT (bor α xs) (BO α) ()
%1 -> Ap (StateT (bor α xs) (BO α)) ()
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ (bor α xs %1 -> BO α ((), bor α xs)) -> StateT (bor α xs) (BO α) ()
forall s (m :: * -> *) a. (s %1 -> m (a, s)) -> StateT s m a
Control.StateT \bor α xs
bors -> bor α xs
%1 -> (forall (β :: Lifetime).
       bor (β /\ α) xs %1 -> BO (β /\ α) ())
%1 -> BO α ((), bor α xs)
forall {k} (bor :: Lifetime -> k -> *) (α :: Lifetime) (a :: k)
       (α' :: Lifetime) r.
Reborrowable bor =>
bor α a
%1 -> (forall (β :: Lifetime). bor (β /\ α) a %1 -> BO (β /\ α') r)
%1 -> BO α' (r, bor α a)
locally bor α xs
bors (\bor (β /\ α) xs
bors -> bor (β /\ α) xs %1 -> i %1 -> a %1 -> BO (β /\ α) ()
forall (β :: Lifetime).
bor (β /\ α) xs %1 -> i %1 -> a %1 -> BO (β /\ α) ()
k bor (β /\ α) xs
bors i
i a
a)

iforReborrowing_ ::
  (FoldableWithIndex i t, Reborrowable bor) =>
  bor α xs %1 ->
  t a %1 ->
  ( forall β.
    bor (β /\ α) xs %1 ->
    i %1 ->
    a %1 ->
    BO (β /\ α) ()
  ) ->
  BO α (bor α xs)
{-# INLINE iforReborrowing_ #-}
iforReborrowing_ :: forall {k} i (t :: * -> *) (bor :: Lifetime -> k -> *)
       (α :: Lifetime) (xs :: k) a.
(FoldableWithIndex i t, Reborrowable bor) =>
bor α xs
%1 -> t a
%1 -> (forall (β :: Lifetime).
       bor (β /\ α) xs %1 -> i %1 -> a %1 -> BO (β /\ α) ())
-> BO α (bor α xs)
iforReborrowing_ = IndexedFold i (t a) a
%1 -> bor α xs
%1 -> t a
%1 -> (forall (β :: Lifetime).
       bor (β ':/\ α) xs %1 -> i %1 -> a %1 -> BO (β ':/\ α) ())
-> BO α (bor α xs)
forall {k} (bor :: Lifetime -> k -> *) i s a (α :: Lifetime)
       (xs :: k).
Reborrowable bor =>
IndexedFold i s a
%1 -> bor α xs
%1 -> s
%1 -> (forall (β :: Lifetime).
       bor (β /\ α) xs %1 -> i %1 -> a %1 -> BO (β /\ α) ())
-> BO α (bor α xs)
iforReborrowingOf_ (i %1 -> a %1 -> w) -> t a %1 -> w
IndexedFold i (t a) a
forall w a. Monoid w => (i %1 -> a %1 -> w) -> t a %1 -> w
forall i (t :: * -> *) w a.
(FoldableWithIndex i t, Monoid w) =>
(i %1 -> a %1 -> w) -> t a %1 -> w
ifoldMap

toListOf :: Fold s a %1 -> s %1 -> [a]
{-# INLINE toListOf #-}
toListOf :: forall s a. Fold s a %1 -> s %1 -> [a]
toListOf Fold s a
fld = DList a %1 -> [a]
forall a. DList a %1 -> [a]
fromDList (DList a %1 -> [a]) -> (s %1 -> DList a) %1 -> s %1 -> [a]
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. (a %1 -> DList a) -> s %1 -> DList a
Fold s a
fld a %1 -> DList a
forall a. a %1 -> DList a
singletonDL

toList :: (Foldable t) => t a %1 -> [a]
{-# INLINE toList #-}
toList :: forall (t :: * -> *) a. Foldable t => t a %1 -> [a]
toList = Fold (t a) a %1 -> t a %1 -> [a]
forall s a. Fold s a %1 -> s %1 -> [a]
toListOf (a %1 -> w) -> t a %1 -> w
Fold (t a) a
forall w a. Monoid w => (a %1 -> w) -> t a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap

newtype DList a = DList ([a] %1 -> [a])

fromDList :: DList a %1 -> [a]
{-# INLINE fromDList #-}
fromDList :: forall a. DList a %1 -> [a]
fromDList (DList [a] %1 -> [a]
f) = [a] %1 -> [a]
f []

singletonDL :: a %1 -> DList a
{-# INLINE singletonDL #-}
singletonDL :: forall a. a %1 -> DList a
singletonDL a
a = ([a] %1 -> [a]) -> DList a
forall a. ([a] %1 -> [a]) -> DList a
DList (a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
:)

instance Semigroup (DList a) where
  DList [a] %1 -> [a]
f <> :: DList a %1 -> DList a %1 -> DList a
<> DList [a] %1 -> [a]
g = ([a] %1 -> [a]) -> DList a
forall a. ([a] %1 -> [a]) -> DList a
DList ([a] %1 -> [a]
f ([a] %1 -> [a]) %1 -> ([a] %1 -> [a]) %1 -> [a] %1 -> [a]
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. [a] %1 -> [a]
g)
  {-# INLINE (<>) #-}

instance Monoid (DList a) where
  mempty :: DList a
mempty = ([a] %1 -> [a]) -> DList a
forall a. ([a] %1 -> [a]) -> DList a
DList [a] %1 -> [a]
forall a (q :: Multiplicity). a %q -> a
id
  {-# INLINE mempty #-}

instance Foldable [] where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> [a] %1 -> w
foldMap = (a %1 -> w) -> [a] %1 -> w
forall w a. Monoid w => (a %1 -> w) -> [a] %1 -> w
PL.foldMap
  {-# INLINE foldMap #-}

deriving anyclass instance FoldableWithIndex Int []

instance Foldable Maybe where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> Maybe a %1 -> w
foldMap a %1 -> w
f = w -> (a %1 -> w) -> Maybe a %1 -> w
forall b a. b -> (a %1 -> b) -> Maybe a %1 -> b
maybe w
forall a. Monoid a => a
mempty a %1 -> w
f
  {-# INLINE foldMap #-}

instance FoldableWithIndex () Maybe where
  ifoldMap :: forall w a. Monoid w => (() %1 -> a %1 -> w) -> Maybe a %1 -> w
ifoldMap () %1 -> a %1 -> w
f = (a %1 -> w) -> Maybe a %1 -> w
forall w a. Monoid w => (a %1 -> w) -> Maybe a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap (() %1 -> a %1 -> w
f ())
  {-# INLINE ifoldMap #-}

instance (Consumable e) => Foldable ((,) e) where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> (e, a) %1 -> w
foldMap a %1 -> w
f = (() %1 -> w %1 -> w) -> ((), w) %1 -> w
forall a (p :: Multiplicity) b c (q :: Multiplicity).
(a %p -> b %p -> c) %q -> (a, b) %p -> c
uncurry () %1 -> w %1 -> w
forall a b. Consumable a => a %1 -> b %1 -> b
lseq (((), w) %1 -> w) -> ((e, a) %1 -> ((), w)) -> (e, a) %1 -> w
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. (e %1 -> ()) -> (a %1 -> w) -> (e, a) %1 -> ((), w)
forall a b c d. (a %1 -> b) -> (c %1 -> d) -> (a, c) %1 -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a %1 -> b) -> (c %1 -> d) -> p a c %1 -> p b d
Bi.bimap e %1 -> ()
forall a. Consumable a => a %1 -> ()
consume a %1 -> w
f
  {-# INLINE foldMap #-}

instance (Consumable e) => Foldable (Either e) where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> Either e a %1 -> w
foldMap a %1 -> w
f = (e %1 -> w) -> (a %1 -> w) -> Either e a %1 -> w
forall a c b. (a %1 -> c) -> (b %1 -> c) -> Either a b %1 -> c
either ((() %1 -> w %1 -> w
forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` w
forall a. Monoid a => a
mempty) (() %1 -> w) -> (e %1 -> ()) -> e %1 -> w
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. e %1 -> ()
forall a. Consumable a => a %1 -> ()
consume) a %1 -> w
f
  {-# INLINE foldMap #-}

instance Foldable NonEmpty where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> NonEmpty a %1 -> w
foldMap a %1 -> w
f = (a %1 -> w) -> [a] %1 -> w
forall w a. Monoid w => (a %1 -> w) -> [a] %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap a %1 -> w
f ([a] %1 -> w) -> (NonEmpty a %1 -> [a]) -> NonEmpty a %1 -> w
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. NonEmpty a %1 -> [a]
forall a. NonEmpty a %1 -> [a]
LNE.toList
  {-# INLINE foldMap #-}

instance Foldable U1 where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> U1 a %1 -> w
foldMap a %1 -> w
_f = \U1 a
U1 -> w
forall a. Monoid a => a
mempty
  {-# INLINE foldMap #-}

instance Foldable V1 where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> V1 a %1 -> w
foldMap a %1 -> w
_ = \case {}
  {-# INLINE foldMap #-}

instance (Foldable f) => Foldable (M1 i c f) where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> M1 i c f a %1 -> w
foldMap a %1 -> w
f = (f a %1 -> w) %1 -> M1 i c f a %1 -> w
forall a b. Coercible a b => a %1 -> b
coerceLin ((f a %1 -> w) %1 -> M1 i c f a %1 -> w)
-> (f a %1 -> w) %1 -> M1 i c f a %1 -> w
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap @f a %1 -> w
f
  {-# INLINE foldMap #-}

instance (Foldable f) => Foldable (MP1 m f) where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> MP1 m f a %1 -> w
foldMap a %1 -> w
f (MP1 f a
x) = (a %1 -> w) -> f a %1 -> w
forall w a. Monoid w => (a %1 -> w) -> f a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap a %1 -> w
f f a
x
  {-# INLINE foldMap #-}

instance (Foldable f, Foldable g) => Foldable (f :*: g) where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> (:*:) f g a %1 -> w
foldMap a %1 -> w
f (f a
x :*: g a
y) = (a %1 -> w) -> f a %1 -> w
forall w a. Monoid w => (a %1 -> w) -> f a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap a %1 -> w
f f a
x w %1 -> w %1 -> w
forall a. Semigroup a => a %1 -> a %1 -> a
<> (a %1 -> w) -> g a %1 -> w
forall w a. Monoid w => (a %1 -> w) -> g a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap a %1 -> w
f g a
y

instance (Foldable f, Foldable g) => Foldable (f :+: g) where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> (:+:) f g a %1 -> w
foldMap a %1 -> w
f = \case
    L1 f a
x -> (a %1 -> w) -> f a %1 -> w
forall w a. Monoid w => (a %1 -> w) -> f a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap a %1 -> w
f f a
x
    R1 g a
y -> (a %1 -> w) -> g a %1 -> w
forall w a. Monoid w => (a %1 -> w) -> g a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap a %1 -> w
f g a
y
  {-# INLINE foldMap #-}

type GenericFoldable t = (Generic1 t, Foldable (Rep1 t))

genericFoldMap :: (GenericFoldable t, Monoid w) => (a %1 -> w) -> t a %1 -> w
{-# INLINE genericFoldMap #-}
genericFoldMap :: forall (t :: * -> *) w a.
(GenericFoldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
genericFoldMap a %1 -> w
f = (a %1 -> w) -> Rep1 t a %1 -> w
forall w a. Monoid w => (a %1 -> w) -> Rep1 t a %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap a %1 -> w
f (Rep1 t a %1 -> w) -> (t a %1 -> Rep1 t a) -> t a %1 -> w
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. t a %1 -> Rep1 t a
forall p (m :: Multiplicity). t p %m -> Rep1 t p
forall {k} (f :: k -> *) (p :: k) (m :: Multiplicity).
Generic1 f =>
f p %m -> Rep1 f p
from1

instance (GenericFoldable t) => Foldable (Generically1 t) where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> Generically1 t a %1 -> w
foldMap a %1 -> w
f = (a %1 -> w) -> t a %1 -> w
forall (t :: * -> *) w a.
(GenericFoldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
genericFoldMap a %1 -> w
f (t a %1 -> w)
-> (Generically1 t a %1 -> t a) -> Generically1 t a %1 -> w
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. (\(Generically1 t a
x) -> t a
x)
  {-# INLINE foldMap #-}

instance Foldable LV.Vector where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> Vector a %1 -> w
foldMap a %1 -> w
f Vector a
vec =
    Vector a %1 -> (Ur Int, Vector a)
forall a. Vector a %1 -> (Ur Int, Vector a)
LV.size Vector a
vec (Ur Int, Vector a) %1 -> ((Ur Int, Vector a) %1 -> w) -> w
forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
      (Ur Int
n, Vector a
vec) -> DataFlow.do
        let {-# INLINE loop #-}
            loop :: Vector a %1 -> Int -> w -> w
loop !Vector a
vec !Int
i !w
w
              | Int
i Int %1 -> Int %1 -> Bool
forall a. Ord a => a %1 -> a %1 -> Bool
< Int
n =
                  Int -> Vector a %1 -> (Ur a, Vector a)
forall a. HasCallStack => Int -> Vector a %1 -> (Ur a, Vector a)
LV.unsafeGet Int
i Vector a
vec (Ur a, Vector a) %1 -> ((Ur a, Vector a) %1 -> w) -> w
forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \(Ur a
a, Vector a
vec) -> DataFlow.do
                    let !w' :: w
w' = w
w w %1 -> w %1 -> w
forall a. Semigroup a => a %1 -> a %1 -> a
<> a %1 -> w
f a
a
                    Vector a %1 -> Int -> w -> w
loop Vector a
vec (Int
i Int %1 -> Int %1 -> Int
forall a. Additive a => a %1 -> a %1 -> a
+ Int
1) w
w'
              | Bool
otherwise = Vector a
vec Vector a %1 -> w %1 -> w
forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` w
w
        Vector a %1 -> Int -> w -> w
loop Vector a
vec Int
0 w
forall a. Monoid a => a
mempty
  {-# INLINE foldMap #-}

deriving anyclass instance FoldableWithIndex Int LV.Vector

instance Foldable (LHM.HashMap k) where
  foldMap :: forall w a. Monoid w => (a %1 -> w) -> HashMap k a %1 -> w
foldMap a %1 -> w
f HashMap k a
hm = ((k, a) %1 -> w) -> [(k, a)] %1 -> w
forall w a. Monoid w => (a %1 -> w) -> [a] %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap (((k, a) -> w) %1 -> (k, a) %1 -> w
forall a b (p :: Multiplicity) (x :: Multiplicity).
(a %p -> b) %1 -> a %x -> b
Unsafe.toLinear \(k
_, a
v) -> a %1 -> w
f a
v) ([(k, a)] %1 -> w) -> [(k, a)] %1 -> w
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ Ur [(k, a)] %1 -> [(k, a)]
forall a. Ur a %1 -> a
unur (Ur [(k, a)] %1 -> [(k, a)]) -> Ur [(k, a)] %1 -> [(k, a)]
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ HashMap k a %1 -> Ur [(k, a)]
forall k v. HashMap k v %1 -> Ur [(k, v)]
LHM.toList HashMap k a
hm

instance FoldableWithIndex k (LHM.HashMap k) where
  ifoldMap :: forall w a. Monoid w => (k %1 -> a %1 -> w) -> HashMap k a %1 -> w
ifoldMap k %1 -> a %1 -> w
f = ((k, a) %1 -> w) -> [(k, a)] %1 -> w
forall w a. Monoid w => (a %1 -> w) -> [a] %1 -> w
forall (t :: * -> *) w a.
(Foldable t, Monoid w) =>
(a %1 -> w) -> t a %1 -> w
foldMap ((k %1 -> a %1 -> w) -> (k, a) %1 -> w
forall a (p :: Multiplicity) b c (q :: Multiplicity).
(a %p -> b %p -> c) %q -> (a, b) %p -> c
uncurry k %1 -> a %1 -> w
f) ([(k, a)] %1 -> w)
-> (HashMap k a %1 -> [(k, a)]) -> HashMap k a %1 -> w
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. Ur [(k, a)] %1 -> [(k, a)]
forall a. Ur a %1 -> a
unur (Ur [(k, a)] %1 -> [(k, a)])
-> (HashMap k a %1 -> Ur [(k, a)]) -> HashMap k a %1 -> [(k, a)]
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. HashMap k a %1 -> Ur [(k, a)]
forall k v. HashMap k v %1 -> Ur [(k, v)]
LHM.toList