on master help sign in
subtype -> super
commit5f262ef6a6e9d2ab6af7be9df9c062e63019a7c2 authorCsongor Kiss <kiss.csongor.kiss@gmail.com> dateSun 11 Jun 2017, 11:56 (9 years ago) parent6279123
5 files changed+15−12
README.md+6−6 generic-lens.cabal+2 src/Data/Generics/Internal/Lens.hs+1 src/Data/Generics/Record.hs+1−1 src/Data/Generics/Record/Subtype.hs+5−5
README.md+6−6
@@ -75,7 +75,7 @@ True
7575
7676## Structural subtyping
7777
78−A record is a (structural) `subtype' of another, if its fields are a subset of
78+A record is a (structural) `subtype' of another, if its fields are a superset of
7979those of the other.
8080
8181```haskell
@@ -103,18 +103,18 @@ Animal {name = "Tunyasz", age = 50}
103103>>> smash (Animal "dog" 10) human
104104Human {name = "dog", age = 10, address = "London"}
105105
106−-- 'subtype' is a lens that focuses on a subrecord of a larger record:
107−>>> human ^. subtype @Animal
106+-- 'super' is a lens that focuses on a subrecord of a larger record:
107+>>> human ^. super @Animal
108108Animal {name = "Tunyasz", age = 50}
109109```
110110
111−We can apply a function that operates on a subtype to the larger structure,
112−by focusing on the subtype first:
111+We can apply a function that operates on a supertype to the larger (subtype)
112+structure, by focusing on the supertype first:
113113
114114```haskell
115115growUp :: Animal -> Animal
116116growUp (Animal name age) = Animal name (age + 50)
117117
118−>>> human & subtype @Animal %~ growUp
118+>>> human & super @Animal %~ growUp
119119Human {name = "Tunyasz", age = 60, address = "London"}
120120```
generic-lens.cabal+2
@@ -53,6 +53,8 @@ library
5353
5454 -- Other library packages from which modules are imported.
5555 build-depends: base >= 4.9 && <= 5.0
56+ , lens
57+ , kan-extensions
5658
5759 -- Directories containing source files.
5860 hs-source-dirs: src
src/Data/Generics/Internal/Lens.hs+1
@@ -34,6 +34,7 @@ type Lens' s a
3434-- | Getting
3535(^.) :: s -> ((a -> Const a a) -> s -> Const a s) -> a
3636s ^. l = getConst (l Const s)
37+infixl 8 ^.
3738
3839-- | Setting
3940set :: ((a -> Identity b) -> s -> Identity t) -> b -> s -> t
src/Data/Generics/Record.hs+1−1
@@ -18,7 +18,7 @@ module Data.Generics.Record
1818 (
1919 -- * Subtype relationship
2020 Subtype (..)
21− , subtype
21+ , super
2222
2323 -- * Magic lens
2424 , HasField (..)
src/Data/Generics/Record/Subtype.hs+5−5
@@ -49,7 +49,7 @@
4949-----------------------------------------------------------------------------
5050module Data.Generics.Record.Subtype
5151 ( Subtype (..)
52− , subtype
52+ , super
5353 ) where
5454
5555import Data.Generics.Record.HasField
@@ -86,13 +86,13 @@ instance (GSmash (Rep a) (Rep b), GUpcast (Rep a) (Rep b), Generic a, Generic b)
8686-- | Structural subtype lens. Given a subtype relationship @sub :< sup@,
8787-- we can focus on the @sub@ structure of @sup@.
8888--
89−-- >>> human ^. subtype @Animal
89+-- >>> human ^. super @Animal
9090-- Animal {name = "Tunyasz", age = 50}
9191--
92−-- >>> set (subtype @Animal) (Animal "dog" 10) human
92+-- >>> set (super @Animal) (Animal "dog" 10) human
9393-- Human {name = "dog", age = 10, address = "London"}
94−subtype :: forall sup sub. Subtype sub sup => Lens' sub sup
95−subtype f sub = fmap (flip smash sub) (f (upcast sub))
94+super :: forall sup sub. Subtype sub sup => Lens' sub sup
95+super f sub = fmap (flip smash sub) (f (upcast sub))
9696
9797--------------------------------------------------------------------------------
9898-- * Generic upcasting