@@ -21,36 +21,15 @@
21-- Stability : experimental
21-- Stability : experimental
22-- Portability : non-portable
22-- Portability : non-portable
24−-- Structural subtype relationship between record types.
24+-- Structural subtype relationships between product types.
26−-- The running example in this module is the following two types:
32−-- import GHC.Generics
33−-- import Data.Generics.Record
38−-- , address :: String
39−-- } deriving (Generic, Show)
41−-- data Animal = Animal
44−-- } deriving (Generic, Show)
47−-- human = Human \"Tunyasz\" 50 \"London\"
51-----------------------------------------------------------------------------
26-----------------------------------------------------------------------------
52module Data.Generics.Product.Subtype
28module Data.Generics.Product.Subtype
53− ( Subtype (..)
32+ Subtype (..)
56import Data.Generics.Internal.Families
35import Data.Generics.Internal.Families
@@ -60,14 +39,36 @@ import Data.Generics.Product.Fields
60import Data.Kind (Type)
39import Data.Kind (Type)
61import GHC.Generics
40import GHC.Generics
44+-- module Example where
46+-- import Data.Generics.Product
47+-- import GHC.Generics
52+-- , address :: String
53+-- } deriving (Generic, Show)
55+-- data Animal = Animal
58+-- } deriving (Generic, Show)
61+-- human = Human \"Tunyasz\" 50 \"London\"
63-- |Structural subtype relationship
64-- |Structural subtype relationship
65-- @sub@ is a (structural) `subtype' of @sup@, if its fields are a subset of
66-- @sub@ is a (structural) `subtype' of @sup@, if its fields are a subset of
66-- those of @sup@.
67-- those of @sup@.
68class Subtype sup sub where
69class Subtype sup sub where
69− -- | Structural subtype lens. Given a subtype relationship @sub :< sup@,
70+ -- |Structural subtype lens. Given a subtype relationship @sub :< sup@,
70− -- we can focus on the @sub@ structure of @sup@.
71+ -- we can focus on the @sub@ structure of @sup@.
72 -- >>> human ^. super @Animal
73 -- >>> human ^. super @Animal
73 -- Animal {name = "Tunyasz", age = 50}
74 -- Animal {name = "Tunyasz", age = 50}
@@ -78,7 +79,7 @@ class Subtype sup sub where
78 super f sub
79 super f sub
79 = fmap (`smash` sub) (f (upcast sub))
80 = fmap (`smash` sub) (f (upcast sub))
81− -- | Cast the more specific subtype to the more general supertype
82+ -- |Cast the more specific subtype to the more general supertype
83 -- >>> upcast human :: Animal
84 -- >>> upcast human :: Animal
84 -- Animal {name = "Tunyasz", age = 50}
85 -- Animal {name = "Tunyasz", age = 50}
@@ -86,7 +87,7 @@ class Subtype sup sub where
86 upcast :: sub -> sup
87 upcast :: sub -> sup
87 upcast s = s ^. super @sup
88 upcast s = s ^. super @sup
89− -- | Plug a smaller structure into a larger one
90+ -- |Plug a smaller structure into a larger one
91 -- >>> smash (Animal "dog" 10) human
92 -- >>> smash (Animal "dog" 10) human
92 -- Human {name = "dog", age = 10, address = "London"}
93 -- Human {name = "dog", age = 10, address = "London"}
@@ -95,7 +96,6 @@ class Subtype sup sub where
96 {-# MINIMAL super | smash, upcast #-}
97 {-# MINIMAL super | smash, upcast #-}
98−-- | Instances are created by the compiler
100 ( GSmash (Rep a) (Rep b)
100 ( GSmash (Rep a) (Rep b)
101 , GUpcast (Rep a) (Rep b)
101 , GUpcast (Rep a) (Rep b)
@@ -108,7 +108,6 @@ instance
108--------------------------------------------------------------------------------
108--------------------------------------------------------------------------------
109-- * Generic upcasting
109-- * Generic upcasting
111−-- | Upcast 'sub to 'sup' (generic rep)
112class GUpcast (sub :: Type -> Type) (sup :: Type -> Type) where
111class GUpcast (sub :: Type -> Type) (sup :: Type -> Type) where
113 gupcast :: sub p -> sup p
112 gupcast :: sub p -> sup p