@@ -10,8 +10,28 @@
10{-# LANGUAGE TypeApplications #-}
10{-# LANGUAGE TypeApplications #-}
11{-# LANGUAGE TypeOperators #-}
11{-# LANGUAGE TypeOperators #-}
13+-----------------------------------------------------------------------------
15+-- Module : Records.Generic
16+-- Copyright : (C) 2016 Csongor Kiss
18+-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
19+-- Stability : experimental
20+-- Portability : non-portable
22+-- Magic record operations using Generics
24+-- These classes need not be instantiated manually, as GHC can automatically
25+-- prove valid instances via Generics. Only the `Generic` class needs to
26+-- be derived (see examples).
28+-----------------------------------------------------------------------------
13module Records.Generic
29module Records.Generic
14− ( HasField (..)
32+ HasField (..)
34+ -- * Subtype relationship
15 , Subtype (..)
35 , Subtype (..)
@@ -100,13 +120,14 @@ instance {-# OVERLAPS #-} Convert rep f => Convert rep (M1 i c f) where
101-- Exported stuff
121-- Exported stuff
103−-- Structural subtype relationship
123+-- |Structural subtype relationship
104class Subtype a b where
124class Subtype a b where
105 upcast :: a -> b
125 upcast :: a -> b
107instance (Convert (Rep a) (Rep b), Generic a, Generic b) => Subtype a b where
127instance (Convert (Rep a) (Rep b), Generic a, Generic b) => Subtype a b where
108 upcast = to . convert . from
128 upcast = to . convert . from
130+-- |Records that have a field with a given name
110class Generic rec => HasField rec (field :: Symbol) a where
131class Generic rec => HasField rec (field :: Symbol) a where
111 getField :: rec -> Proxy field -> a
132 getField :: rec -> Proxy field -> a