git.iko.soy/semi-iso-optics
on master help sign in
Documentation.
commit23f67c06670a681182a8a024c0c88fe961d6c51c authorPaweł Nowak <pawel834@gmail.com> dateThu 11 Dec 2014, 21:26 (12 years ago) parent98d7090
3 files changed+64−7
Control/Category/Structures.hs+19 Control/SIArrow.hs+43−5 semi-iso.cabal+2−2
Control/Category/Structures.hs+19
@@ -28,12 +28,20 @@ infixl 3 /+/
2828
2929-- | A category with finite products.
3030class Category cat => Products cat where
31+ -- | Send the first component of the input through the argument arrow, and copy the rest unchanged to the output.
32+ --
33+ -- @first a@ is equal to @a *** id@.
3134 first :: cat a b -> cat (a, c) (b, c)
3235 first a = a *** id
3336
37+ -- | A mirror image of 'first'.
38+ --
39+ -- @second a@ is equal to @id *** a@.
3440 second :: cat a b -> cat (c, a) (c, b)
3541 second a = id *** a
3642
43+ -- | A product of two arrows.
44+ -- Split the input between the two argument arrows and combine their output.
3745 (***) :: cat a b -> cat c d -> cat (a, c) (b, d)
3846 a *** b = first a >>> second b
3947
@@ -44,12 +52,20 @@ instance Monad m => Products (Kleisli m) where
4452
4553-- | A category with finite coproducts.
4654class Category cat => Coproducts cat where
55+ -- | Feed marked inputs through the argument arrow, passing the rest through unchanged to the output.
56+ --
57+ -- @left a@ is equal to @a +++ id@.
4758 left :: cat a b -> cat (Either a c) (Either b c)
4859 left a = a +++ id
4960
61+ -- | A mirror image of left.
62+ --
63+ -- @right a@ is equal to @id +++ a@.
5064 right :: cat a b -> cat (Either c a) (Either c b)
5165 right a = id +++ a
5266
67+ -- | A coproduct of two arrows.
68+ -- Split the input between the two argument arrows, retagging and merging their outputs.
5369 (+++) :: cat a b -> cat c d -> cat (Either a c) (Either b d)
5470 a +++ b = left a >>> right b
5571
@@ -60,7 +76,9 @@ instance Monad m => Coproducts (Kleisli m) where
6076
6177-- | A category @cat@ is a CatPlus when @cat a b@ is a monoid for all a, b.
6278class Category cat => CatPlus cat where
79+ -- | The identity of '/+/'.
6380 cempty :: cat a b
81+ -- | An associative operation on arrows.
6482 (/+/) :: cat a b -> cat a b -> cat a b
6583
6684 {-# MINIMAL cempty, (/+/) #-}
@@ -71,6 +89,7 @@ instance MonadPlus m => CatPlus (Kleisli m) where
7189
7290-- | A category transformer.
7391class CategoryTrans t where
92+ -- | Lift an arrow from the base category.
7493 clift :: Category cat => cat a b -> t cat a b
7594
7695 {-# MINIMAL clift #-}
Control/SIArrow.hs+43−5
@@ -2,13 +2,14 @@
22{-# LANGUAGE ConstraintKinds #-}
33{- |
44Module : Control.SIArrow
5−Description : Semi-iso arrows.
5+Description : Categories of reversible computations.
66Copyright : (c) Paweł Nowak
77License : MIT
88
99Maintainer : Paweł Nowak <pawel834@gmail.com>
1010Stability : experimental
1111
12+Categories of reversible computations.
1213-}
1314module Control.SIArrow (
1415 -- * Arrow.
@@ -43,18 +44,33 @@ infixr 1 ^>>, >>^
4344infixr 1 ^<<, <<^
4445infixl 4 /$/, /$~
4546infixl 5 /*/, */, /*
47+infixl 3 /?/
4648
47−-- Categories.
48−
49+-- | A category equipped with an embedding 'siarr' from @SemiIso@ into @cat@ and some
50+-- additional structure.
51+--
52+-- SIArrow abstracts categories of reversible computations
53+-- (with reversible side effects).
54+--
55+-- The category @cat@ should contain @SemiIso@ as a sort of
56+-- \"subcategory of pure computations\".
4957class (Products cat, Coproducts cat, CatPlus cat) => SIArrow cat where
58+ -- | Allows you to lift a SemiIso into @cat@. The resulting arrow should be
59+ -- in some sense minimal or \"pure\", similiar to 'pure', 'return' and
60+ -- 'arr' from "Control.Category".
5061 siarr :: ASemiIso' a b -> cat a b
5162
63+ -- | Reversed version of 'siarr'.
64+ --
65+ -- Use this where you would use 'pure'.
5266 sipure :: ASemiIso' b a -> cat a b
5367 sipure = siarr . rev
5468
69+ -- | @sisome v@ repeats @v@ as long as possible, but no less then once.
5570 sisome :: cat () b -> cat () [b]
5671 sisome v = _Cons /$/ v /*/ simany v
5772
73+ -- | @simany v@ repeats @v@ as long as possible.
5874 simany :: cat () b -> cat () [b]
5975 simany v = sisome v /+/ sipure _Empty
6076
@@ -66,41 +82,63 @@ instance MonadPlus m => SIArrow (Kleisli m) where
6682instance SIArrow ReifiedSemiIso' where
6783 siarr = reifySemiIso
6884
85+-- | Composes a SemiIso with an arrow.
6986(^>>) :: SIArrow cat => ASemiIso' a b -> cat b c -> cat a c
7087f ^>> a = a . siarr f
7188
89+-- | Composes an arrow with a SemiIso.
7290(>>^) :: SIArrow cat => cat a b -> ASemiIso' b c -> cat a c
7391a >>^ f = siarr f . a
7492
93+-- | Composes a SemiIso with an arrow, backwards.
7594(^<<) :: SIArrow cat => ASemiIso' b c -> cat a b -> cat a c
7695f ^<< a = siarr f . a
7796
97+-- | Composes an arrow with a SemiIso, backwards.
7898(<<^) :: SIArrow cat => cat b c -> ASemiIso' a b -> cat a c
7999a <<^ f = a . siarr f
80100
101+-- | Postcomposes an arrow with a reversed SemiIso. The analogue of '<$>'.
81102(/$/) :: SIArrow cat => ASemiIso' b' b -> cat a b -> cat a b'
82103ai /$/ f = sipure ai . f
83104
84−-- | > ai /$~ f = ai . morphed /$/ f
105+-- | Convenient fmap.
106+--
107+-- > ai /$~ f = ai . morphed /$/ f
85108--
86109-- This operator handles all the hairy stuff with uncurried application:
87110-- it reassociates the argument tuple and removes unnecessary (or adds necessary)
88111-- units to match the function type. You don't have to use @/*@ and @*/@ with this
89−-- operator.
112+-- operator.
90113(/$~) :: (SIArrow cat, HFoldable b', HFoldable b,
91114 HUnfoldable b', HUnfoldable b, Rep b' ~ Rep b)
92115 => ASemiIso' a b' -> cat c b -> cat c a
93116ai /$~ h = cloneSemiIso ai . morphed /$/ h
94117
118+-- | The product of two arrows with duplicate units removed. Side effect are
119+-- sequenced from left to right.
120+--
121+-- The uncurried analogue of '<*>'.
95122(/*/) :: SIArrow cat => cat () b -> cat () c -> cat () (b, c)
96123a /*/ b = unit ^>> (a *** b)
97124
125+-- | The product of two arrows, where the second one has no input and no output
126+-- (but can have side effects), with duplicate units removed. Side effect are
127+-- sequenced from left to right.
128+--
129+-- The uncurried analogue of '<*'.
98130(/*) :: SIArrow cat => cat () a -> cat () () -> cat () a
99131f /* g = unit /$/ f /*/ g
100132
133+-- | The product of two arrows, where the first one has no input and no output
134+-- (but can have side effects), with duplicate units removed. Side effect are
135+-- sequenced from left to right.
136+--
137+-- The uncurried analogue of '*>'.
101138(*/) :: SIArrow cat => cat () () -> cat () a -> cat () a
102139f */ g = unit . swapped /$/ f /*/ g
103140
141+-- | An arrow that fails with an error message.
104142sifail :: SIArrow cat => String -> cat a b
105143sifail = siarr . alwaysFailing
106144
semi-iso.cabal+2−2
@@ -25,10 +25,10 @@ description: Semi-isomorphisms are partial isomorphisms with weakened is
2525 dreaded @arr@).
2626
2727 SIArrow abstracts categories of reversible computations (with reversible side effects). In
28− the case of parsing and pretty-printing using the "syntax" library if we have an arrow
28+ the case of parsing and pretty-printing using the "syntax" library if we have an arrow
2929 @SIArrow cat => cat a b@ then we can:
3030 .
31− * Evaluate it from left to right, turning a value of type @a@ into a value of type @b@,
31+ * Evaluate it from left to right, turning a value of type @a@ into a value of type @b@,
3232 with the side effect of consuming a sequence. (Parsing)
3333 .
3434 * Evaluate it from right to left, turning a value of type @b@ into a value of type @a@,