summaryrefslogtreecommitdiff
path: root/src/Data/Profunctor/Optic/Fold1.hs
blob: 7028bf4b9c02e9bac464f0f623513db052fb5751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE TypeOperators         #-}
{-# LANGUAGE TypeFamilies          #-}
module Data.Profunctor.Optic.Fold1 (
    -- * Fold1 & Ixfold1
    Fold1
  , fold1_
  , folding1
  , fold1Vl
  , toFold1
  , cloneFold1
    -- * Cofold1 & Cxfold
  , Cofold1
  , cofold1Vl 
  , cofolding1
    -- * Optics
  , folded1 
  , cofolded1 
  , folded1_
  , nonunital
  , presemiring
  , summed1
  , multiplied1
    -- * Primitive operators
  , withFold1 
  , withCofold1
    -- * Operators
  , folds1
  , cofolds1
  , folds1p
  , nelists
    -- * Carriers
  , FoldRep
  , AFold1
  , Cofold1Rep
  , ACofold1
  , afold1
  , acofold1
  , Star(..)
  , Costar(..)
    -- * Classes
  , Representable(..)
  , Corepresentable(..)
  , Contravariant(..)
  , Bifunctor(..)
    -- * Auxilliary Types
  , Nedl(..)
) where

import Control.Applicative
import Control.Monad ((<=<), void)
import Control.Monad.Reader as Reader hiding (lift)
import Control.Monad.State as State hiding (lift)
import Data.Foldable (Foldable, foldMap, traverse_)
import Data.List.NonEmpty (NonEmpty(..))
import Data.Maybe
import Data.Monoid hiding (All(..), Any(..))
import Data.Prd (Prd(..), Min(..), Max(..))
import Data.Prd.Lattice (Lattice(..))
import Data.Profunctor.Optic.Import
import Data.Profunctor.Optic.Fold
import Data.Profunctor.Optic.Prism (right, just, async)
import Data.Profunctor.Optic.Traversal1
import Data.Profunctor.Optic.Type
import Data.Profunctor.Optic.View (AView, to, from, withPrimView, view, cloneView)
import Data.Semiring (Semiring(..), Prod(..))
import qualified Control.Exception as Ex
import qualified Data.List.NonEmpty as NEL
import qualified Data.Prd as Prd
import qualified Data.Semiring as Rng

-- $setup
-- >>> :set -XNoOverloadedStrings
-- >>> :set -XTypeApplications
-- >>> :set -XFlexibleContexts
-- >>> import Control.Exception hiding (catches)
-- >>> import Data.Functor.Identity
-- >>> import Data.List.Index
-- >>> import Data.List.NonEmpty (NonEmpty(..))
-- >>> import Data.Map as Map
-- >>> import Data.Maybe
-- >>> import Data.Monoid
-- >>> import Data.Semiring hiding (unital,nonunital,presemiring)
-- >>> import Data.Sequence as Seq
-- >>> import qualified Data.List.NonEmpty as NE
-- >>> :load Data.Profunctor.Optic
-- >>> let ixtraversed :: Ixtraversal Int [a] [b] a b ; ixtraversed = ixtraversalVl itraverse

---------------------------------------------------------------------
-- 'Fold1' & 'Ixfold1'
---------------------------------------------------------------------

type AFold1 r s a = Optic' (FoldRep r) s a

-- | Obtain a 'Fold1' directly.
--
-- @ 
-- 'fold1_' ('nelists' o) ≡ o
-- 'fold1_' f ≡ 'to' f . 'fold1Vl' 'traverse1_'
-- 'fold1_' f ≡ 'coercer' . 'lmap' f . 'lift' 'traverse1_'
-- @
--
-- See 'Data.Profunctor.Optic.Property'.
--
-- This can be useful to repn operations from @Data.List.NonEmpty@ and elsewhere into a 'Fold1'.
--
fold1_ :: Foldable1 f => (s -> f a) -> Fold1 s a
fold1_ f = to f . fold1Vl traverse1_
{-# INLINE fold1_ #-}

-- | Obtain a 'Fold1' from a 'Traversable1' functor.
--
-- @
-- 'folding1' f ≡ 'traversed1' . 'to' f
-- 'folding1' f ≡ 'fold1Vl' 'traverse1' . 'to' f
-- @
--
folding1 :: Traversable1 f => (s -> a) -> Fold1 (f s) a
folding1 f = fold1Vl traverse1 . to f
{-# INLINE folding1 #-}

-- | Obtain a 'Fold1' from a Van Laarhoven 'Fold1'.
--
-- See 'Data.Profunctor.Optic.Property'.
--
fold1Vl :: (forall f. Apply f => (a -> f b) -> s -> f t) -> Fold1 s a
fold1Vl f = coercer . repn f . coercer
{-# INLINE fold1Vl #-}

-- | Obtain a 'Fold1' from a 'View' or 'AFold1'.
--
toFold1 :: AView s a -> Fold1 s a
toFold1 = to . view
{-# INLINE toFold1 #-}

-- | TODO: Document
--
afold1 :: Semigroup r => ((a -> r) -> s -> r) -> AFold1 r s a
afold1 o = Star #. (Const #.) #. o .# (getConst #.) .# runStar
{-# INLINE afold1 #-}

-- | Obtain a 'Fold1' from a 'AFold1'.
--
cloneFold1 :: Semigroup a => AFold1 a s a -> View s a
cloneFold1 = cloneView
{-# INLINE cloneFold1 #-}

---------------------------------------------------------------------
-- 'Cofold1' & 'Cxfold'
---------------------------------------------------------------------

type Cofold1Rep r = Costar (Const r)

type ACofold1 r t b = Optic' (Cofold1Rep r) t b

-- | Obtain an 'Cofold1' from a 'Distributive' functor. 
--
-- @
-- 'cofolding1' f ≡ 'cotraversed1' . 'from' f
-- 'cofolding1' f ≡ 'cofold1Vl' 'cotraverse' . 'from' f
-- @
--
cofolding1 :: Distributive f => (b -> t) -> Cofold1 (f t) b
cofolding1 f = cofold1Vl cotraverse . from f
{-# INLINE cofolding1 #-}

-- | Obtain a 'Cofold1' from a Van Laarhoven 'Cofold1'.
--
cofold1Vl :: (forall f. Apply f => (f a -> b) -> f s -> t) -> Cofold1 t b
cofold1Vl f = coercel . corepn f . coercel
{-# INLINE cofold1Vl #-}

-- | TODO: Document
--
acofold1 :: ((r -> b) -> r -> t) -> ACofold1 r t b
acofold1 o = Costar #. (.# getConst) #. o .#  (.# Const) .# runCostar  
{-# INLINE acofold1 #-}

---------------------------------------------------------------------
-- Optics 
---------------------------------------------------------------------

-- | Obtain a 'Fold1' from a 'Traversable1' functor.
--
folded1 :: Traversable1 f => Fold1 (f a) a
folded1 = folding1 id
{-# INLINE folded1 #-}

-- | Obtain an 'Cofold1' from a 'Distributive' functor. 
--
cofolded1 :: Distributive f => Cofold1 (f b) b
cofolded1 = cofolding1 id
{-# INLINE cofolded1 #-}

-- | The canonical 'Fold1'.
--
-- @
-- 'Data.Semigroup.Foldable.foldMap1' ≡ 'withFold1' 'folded1_''
-- @
--
folded1_ :: Foldable1 f => Fold1 (f a) a
folded1_ = fold1_ id
{-# INLINE folded1_ #-}

-- | Expression in a semiring expression with no multiplicative unit.
--
-- @ 
-- 'nonunital' ≡ 'summed' . 'multiplied1'
-- @
--
-- >>> foldOf nonunital $ (fmap . fmap) Just [1 :| [2], 3 :| [4 :: Int]]
-- Just 14
--
nonunital :: Foldable f => Foldable1 g => Monoid r => Semiring r => AFold r (f (g a)) a
nonunital = summed . multiplied1
{-# INLINE nonunital #-}

-- | Expression in a semiring with no additive or multiplicative unit.
--
-- @ 
-- 'presemiring' ≡ 'summed1' . 'multiplied1'
-- @
--
presemiring :: Foldable1 f => Foldable1 g => Semiring r => AFold1 r (f (g a)) a
presemiring = summed1 . multiplied1
{-# INLINE presemiring #-}

-- | Semigroup sum of a non-empty foldable collection.
--
-- >>> 1 <> 2 <> 3 <> 4 :: Int
-- 10
-- >>> fold1Of summed1 $ 1 :| [2,3,4 :: Int]
-- 10
--
summed1 :: Foldable1 f => Semigroup r => AFold1 r (f a) a
summed1 = afold1 foldMap1
{-# INLINE summed1 #-}

-- | Semiring product of a non-empty foldable collection. 
--
-- >>> fold1Of multiplied1 $ fmap Just (1 :| [2..(5 :: Int)])
-- Just 120 
--
multiplied1 :: Foldable1 f => Semiring r => AFold1 r (f a) a
multiplied1 = afold1 Rng.product1
{-# INLINE multiplied1 #-}

---------------------------------------------------------------------
-- Primitive operators
---------------------------------------------------------------------

-- | Map an optic to a semigroup and combine the results.
--
withFold1 :: Semigroup r => AFold1 r s a -> (a -> r) -> s -> r
withFold1 = withPrimView
{-# INLINE withFold1 #-}

-- | TODO: Document
--
-- >>> withCofold1 (from succ) (*2) 3
-- 7
--
-- Compare 'Data.Profunctor.Optic.View.withPrimReview'.
--
withCofold1 :: ACofold1 r t b -> (r -> b) -> r -> t
withCofold1 o = (.# Const) #. runCostar #. o .# Costar .# (.# getConst)
{-# INLINE withCofold1 #-}

---------------------------------------------------------------------
-- Operators
---------------------------------------------------------------------

-- | TODO: Document
--
folds1 :: Semigroup a => AFold1 a s a -> s -> a
folds1 = flip withFold1 id
{-# INLINE folds1 #-}

-- | TODO: Document
--
cofolds1 :: ACofold1 b t b -> b -> t
cofolds1 = flip withCofold1 id
{-# INLINE cofolds1 #-}

-- | Compute the semiring product of the foci of an optic.
--
-- For semirings without a multiplicative unit this is equivalent to @const mempty@:
--
-- >>> productOf folded Just [1..(5 :: Int)]
-- Just 0
--
-- In this situation you most likely want to use 'folds1p'.
--
folds1p :: Semiring r => AFold (Prod r) s a -> (a -> r) -> s -> r
folds1p o p = getProd . withFold1 o (Prod . p)
{-# INLINE folds1p #-}

{-
>>> nelists bitraversed1 ('h' :| "ello", 'w' :| "orld")
 "hello" :| ["world"]
-}

-- | Extract a 'NonEmpty' of the foci of an optic.
--
--
-- @
-- 'nelists' :: 'View' s a        -> s -> NonEmpty a
-- 'nelists' :: 'Fold1' s a       -> s -> NonEmpty a
-- 'nelists' :: 'Lens'' s a       -> s -> NonEmpty a
-- 'nelists' :: 'Iso'' s a        -> s -> NonEmpty a
-- 'nelists' :: 'Traversal1'' s a -> s -> NonEmpty a
-- 'nelists' :: 'Prism'' s a      -> s -> NonEmpty a
-- @
--
nelists :: AFold1 (Nedl a) s a -> s -> NonEmpty a
nelists l = flip getNedl [] . withFold1 l (Nedl #. (:|))
{-# INLINE nelists #-}

------------------------------------------------------------------------------
-- Indexed operators
------------------------------------------------------------------------------

------------------------------------------------------------------------------
-- Auxilliary Types
------------------------------------------------------------------------------

-- A non-empty difference list.
newtype Nedl a = Nedl { getNedl :: [a] -> NEL.NonEmpty a }

instance Semigroup (Nedl a) where
  Nedl f <> Nedl g = Nedl (f . NEL.toList . g)