on master help sign in
subtype -> super
5f262efCsongor Kiss
README.md+6−6
@@ -75,7 +75,7 @@ True
75
75
76## Structural subtyping
76## Structural subtyping
77
77
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
79those of the other.
79those of the other.
80
80
81```haskell
81```haskell
@@ -103,18 +103,18 @@ Animal {name = "Tunyasz", age = 50}
103>>> smash (Animal "dog" 10) human
103>>> smash (Animal "dog" 10) human
104Human {name = "dog", age = 10, address = "London"}
104Human {name = "dog", age = 10, address = "London"}
105
105
106−-- 'subtype' is a lens that focuses on a subrecord of a larger record:
106+-- 'super' is a lens that focuses on a subrecord of a larger record:
107−>>> human ^. subtype @Animal
107+>>> human ^. super @Animal
108Animal {name = "Tunyasz", age = 50}
108Animal {name = "Tunyasz", age = 50}
109```
109```
110
110
111−We can apply a function that operates on a subtype to the larger structure,
111+We can apply a function that operates on a supertype to the larger (subtype)
112−by focusing on the subtype first:
112+structure, by focusing on the supertype first:
113
113
114```haskell
114```haskell
115growUp :: Animal -> Animal
115growUp :: Animal -> Animal
116growUp (Animal name age) = Animal name (age + 50)
116growUp (Animal name age) = Animal name (age + 50)
117
117
118−>>> human & subtype @Animal %~ growUp
118+>>> human & super @Animal %~ growUp
119Human {name = "Tunyasz", age = 60, address = "London"}
119Human {name = "Tunyasz", age = 60, address = "London"}
120```
120```
generic-lens.cabal+2
@@ -53,6 +53,8 @@ library
53
53
54 -- Other library packages from which modules are imported.
54 -- Other library packages from which modules are imported.
55 build-depends: base >= 4.9 && <= 5.0
55 build-depends: base >= 4.9 && <= 5.0
56+ , lens
57+ , kan-extensions
56
58
57 -- Directories containing source files.
59 -- Directories containing source files.
58 hs-source-dirs: src
60 hs-source-dirs: src
src/Data/Generics/Internal/Lens.hs+1
@@ -34,6 +34,7 @@ type Lens' s a
34-- | Getting
34-- | Getting
35(^.) :: s -> ((a -> Const a a) -> s -> Const a s) -> a
35(^.) :: s -> ((a -> Const a a) -> s -> Const a s) -> a
36s ^. l = getConst (l Const s)
36s ^. l = getConst (l Const s)
37+infixl 8 ^.
37
38
38-- | Setting
39-- | Setting
39set :: ((a -> Identity b) -> s -> Identity t) -> b -> s -> t
40set :: ((a -> Identity b) -> s -> Identity t) -> b -> s -> t
src/Data/Generics/Record.hs+1−1
@@ -18,7 +18,7 @@ module Data.Generics.Record
18 (
18 (
19 -- * Subtype relationship
19 -- * Subtype relationship
20 Subtype (..)
20 Subtype (..)
21− , subtype
21+ , super
22
22
23 -- * Magic lens
23 -- * Magic lens
24 , HasField (..)
24 , HasField (..)
src/Data/Generics/Record/Subtype.hs+5−5
@@ -49,7 +49,7 @@
49-----------------------------------------------------------------------------
49-----------------------------------------------------------------------------
50module Data.Generics.Record.Subtype
50module Data.Generics.Record.Subtype
51 ( Subtype (..)
51 ( Subtype (..)
52− , subtype
52+ , super
53 ) where
53 ) where
54
54
55import Data.Generics.Record.HasField
55import Data.Generics.Record.HasField
@@ -86,13 +86,13 @@ instance (GSmash (Rep a) (Rep b), GUpcast (Rep a) (Rep b), Generic a, Generic b)
86-- | Structural subtype lens. Given a subtype relationship @sub :< sup@,
86-- | Structural subtype lens. Given a subtype relationship @sub :< sup@,
87-- we can focus on the @sub@ structure of @sup@.
87-- we can focus on the @sub@ structure of @sup@.
88--
88--
89−-- >>> human ^. subtype @Animal
89+-- >>> human ^. super @Animal
90-- Animal {name = "Tunyasz", age = 50}
90-- Animal {name = "Tunyasz", age = 50}
91--
91--
92−-- >>> set (subtype @Animal) (Animal "dog" 10) human
92+-- >>> set (super @Animal) (Animal "dog" 10) human
93-- Human {name = "dog", age = 10, address = "London"}
93-- Human {name = "dog", age = 10, address = "London"}
94−subtype :: forall sup sub. Subtype sub sup => Lens' sub sup
94+super :: forall sup sub. Subtype sub sup => Lens' sub sup
95−subtype f sub = fmap (flip smash sub) (f (upcast sub))
95+super f sub = fmap (flip smash sub) (f (upcast sub))
96
96
97--------------------------------------------------------------------------------
97--------------------------------------------------------------------------------
98-- * Generic upcasting
98-- * Generic upcasting