-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add type synonym for affine traversal * Formulate Index in terms of AffineTraversal' Sorry for the ugly duplication with At, maybe we want to put them in the same module? Then index i = at i <<< _Just * Add `AffineTraversal` and `Stall` * Fix build * Re-export Stall * Fix the Index instance of Set * Really fix the Index instance of Set * Fix AffineTraversal intro text. Co-authored-by: Nicholas Scheel <[email protected]>
- Loading branch information
1 parent
7d77d69
commit c869e57
Showing
5 changed files
with
172 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
-- | This module defines functions for working with affine traversals. | ||
-- | An `AffineTraversal` is a `Traversal` that applies to at most one element. | ||
-- | | ||
-- | These arise most frequently as the composition of a `Lens` with a `Prism`. | ||
module Data.Lens.AffineTraversal | ||
( affineTraversal | ||
, affineTraversal' | ||
, withAffineTraversal | ||
, cloneAffineTraversal | ||
, module Data.Lens.Types | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Either (Either(..), either) | ||
import Data.Lens.Internal.Stall (Stall(..)) | ||
import Data.Lens.Types (AffineTraversal, AffineTraversal', AnAffineTraversal, AnAffineTraversal') | ||
import Data.Profunctor (dimap) | ||
import Data.Profunctor.Choice (right) | ||
import Data.Profunctor.Strong (second, (&&&)) | ||
import Data.Tuple (Tuple(..)) | ||
|
||
affineTraversal :: | ||
forall s t a b . | ||
(s -> b -> t) -> | ||
(s -> Either t a) -> | ||
AffineTraversal s t a b | ||
affineTraversal set pre = | ||
affineTraversal' (set &&& pre) | ||
|
||
affineTraversal' :: | ||
forall s t a b . | ||
(s -> Tuple (b -> t) (Either t a)) -> | ||
AffineTraversal s t a b | ||
affineTraversal' to pab = | ||
dimap to (\(Tuple b f) -> either identity b f) (second (right pab)) | ||
|
||
withAffineTraversal :: | ||
forall s t a b r . | ||
AnAffineTraversal s t a b -> | ||
((s -> b -> t) -> (s -> Either t a) -> r) -> | ||
r | ||
withAffineTraversal l f = case l (Stall (const identity) Right) of | ||
Stall g h -> f g h | ||
|
||
cloneAffineTraversal :: | ||
forall s t a b . | ||
AnAffineTraversal s t a b -> | ||
AffineTraversal s t a b | ||
cloneAffineTraversal l = | ||
withAffineTraversal l \x y p -> | ||
affineTraversal x y p |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
-- | This module defines the `Stall` profunctor | ||
module Data.Lens.Internal.Stall where | ||
|
||
import Prelude | ||
|
||
import Data.Bifunctor (lmap) | ||
import Data.Either (Either(..)) | ||
import Data.Profunctor (class Profunctor) | ||
import Data.Profunctor.Strong (class Strong) | ||
import Data.Profunctor.Choice (class Choice) | ||
import Data.Tuple (Tuple(..)) | ||
|
||
-- | The `Stall` profunctor characterizes an `AffineTraversal`. | ||
data Stall a b s t = Stall (s -> b -> t) (s -> Either t a) | ||
|
||
instance functorStall :: Functor (Stall a b s) where | ||
map f (Stall u p) = | ||
Stall (map f <<< u) (lmap f <<< p) | ||
|
||
instance profunctorStall :: Profunctor (Stall a b) where | ||
dimap f g (Stall u p) = | ||
Stall (map g <<< u <<< f) (lmap g <<< p <<< f) | ||
|
||
instance strongStall :: Strong (Stall a b) where | ||
first (Stall u p) = | ||
Stall (\(Tuple s x) b -> Tuple (u s b) x) | ||
(\(Tuple s x) -> lmap (\t -> Tuple t x) (p s)) | ||
|
||
second (Stall u p) = | ||
Stall (\(Tuple x s) b -> Tuple x (u s b)) | ||
(\(Tuple x s) -> lmap (Tuple x) (p s)) | ||
|
||
instance choiceStall :: Choice (Stall a b) where | ||
left (Stall u p) = | ||
Stall | ||
(case _ of | ||
Left s -> \b -> Left (u s b) | ||
Right x -> \_ -> Right x) | ||
(case _ of | ||
Left s -> lmap Left (p s) | ||
Right x -> Left (Right x)) | ||
|
||
right (Stall u p) = | ||
Stall | ||
(case _ of | ||
Left x -> \_ -> Left x | ||
Right s -> \b -> Right (u s b)) | ||
(case _ of | ||
Left x -> Left (Left x) | ||
Right s -> lmap Right (p s)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters