Skip to content

Commit 8f2ead5

Browse files
committed
replicateN
1 parent 0ac78ca commit 8f2ead5

File tree

6 files changed

+36
-17
lines changed

6 files changed

+36
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ stack.yaml.lock
2323
tags
2424
.*.swp
2525
.qodo
26+
SCRATCH*

TODO

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
TODO
22

33
x Monadic interface ala Lysxia
4-
Parsec profunctor with TokenTest & NonTerminal errors ala Leijen
4+
Monadic example grammar
5+
More Tests
6+
Parsec profunctor with either TokenTest or NonTerminal errors ala Leijen
57
Categoric interface with diid
68
Arrowic?
7-
Tests
9+
Read Chomsky
810
Documents
911
Announcement
1012
Delete TODO

src/Data/Profunctor/Do/Bond.hs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ module Data.Profunctor.Do.Bond
1212
( -- *
1313
(>>=)
1414
, (>>)
15+
, (<$>)
1516
, fail
1617
, return
1718
) where
1819

20+
import Control.Lens (Optic)
21+
import Control.Monad (join)
1922
import Data.Profunctor (Profunctor (dimap))
2023
import Data.Profunctor.Do.Polyadic.Bind (fail)
21-
import Data.Profunctor.Monadic (Monadic (bondM))
22-
import Prelude hiding ((>>), (>>=), fail)
24+
import Data.Profunctor.Monadic (Monadic (liftP, bondM))
25+
import Prelude (Applicative (pure), const, fmap, flip, fst, snd, return, (.))
2326

2427
(>>=) :: Monadic m p => p m a a -> (a -> p m b c) -> p m (a,b) (a,c)
2528
infixl 1 >>=
@@ -28,3 +31,10 @@ infixl 1 >>=
2831
(>>) :: Monadic m p => p m () () -> p m b c -> p m b c
2932
infixl 1 >>
3033
x >> y = dimap ((),) snd (x >>= const y)
34+
35+
(<$>)
36+
:: (Monadic m p, Applicative m)
37+
=> Optic (p m) m s t a b
38+
-> p m (a,()) (b,()) -> p m s t
39+
infixl 4 <$>
40+
f <$> x = join (fmap liftP (f (dimap (,()) (pure . fst) x)))

src/Data/Profunctor/Grammar.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ instance Monad m => Polyadic m Printor where
214214
(Printor mg, f) <- mf a
215215
(b, g) <- mg a
216216
return (b, g . f)
217-
bondP f (Printor m) = Printor $ \(a0,b) -> do
218-
(a1,g) <- m a0
219-
(c,h) <- runPrintor (f a1) b
220-
return ((a1,c), h . g)
217+
bondP f (Printor m) = Printor $ \(x,b) -> do
218+
(y,g) <- m x
219+
(c,h) <- runPrintor (f y) b
220+
return ((y,c), h . g)
221221
instance Applicative f => Distributor (Printor s s f) where
222222
zeroP = Printor absurd
223223
Printor p >+< Printor q = Printor $

src/Data/Profunctor/Monadic.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ class
3434
bondM :: (a -> p m b c) -> p m a a -> p m (a,b) (a,c)
3535
instance Monad m => Monadic m Kleisli where
3636
liftP = Kleisli . return
37-
bondM g (Kleisli f) = Kleisli $ \(a0,b) -> do
38-
a1 <- f a0
39-
c <- runKleisli (g a1) b
40-
return (a1,c)
37+
bondM g (Kleisli f) = Kleisli $ \(x,b) -> do
38+
y <- f x
39+
c <- runKleisli (g y) b
40+
return (y,c)
4141
instance Monad m => Monadic m Star where
4242
liftP = Star . return
43-
bondM g (Star f) = Star $ \(a0,b) -> do
44-
a1 <- f a0
45-
c <- runStar (g a1) b
46-
return (a1,c)
43+
bondM g (Star f) = Star $ \(x,b) -> do
44+
y <- f x
45+
c <- runStar (g y) b
46+
return (y,c)
4747

4848
monochrome_
4949
:: (Monadic m p, Applicative m)

src/Data/Profunctor/Monoidal.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Data.Profunctor.Monoidal
55
Monoidal
66
, oneP, (>*<), (>*), (*<)
77
, dimap2, foreverP, replicateP
8-
, (>:<), asEmpty
8+
, (>:<), asEmpty, replicateN
99
, meander, eotFunList
1010
) where
1111

@@ -115,6 +115,12 @@ asEmpty = _Empty >? oneP
115115
x >:< xs = _Cons >? x >*< xs
116116
infixr 5 >:<
117117

118+
replicateN
119+
:: (Monoidal p, Choice p, AsEmpty s, AsEmpty t, Cons s t a b)
120+
=> Int -> p a b -> p s t
121+
replicateN n _ | n <= 0 = lmap (const Empty) asEmpty
122+
replicateN n a = a >:< replicateN (n-1) a
123+
118124
{- | For any `Monoidal`, `Choice` & `Strong` `Profunctor`,
119125
`meander` is invertible and gives a default implementation for the
120126
`Data.Profunctor.Traversing.wander`

0 commit comments

Comments
 (0)