on master help sign in
Docs
4147770Csongor Kiss
README.md+2
@@ -1,6 +1,8 @@
1# generic-records
1# generic-records
2Magic record operations using generics (i.e. both type- and morally safe - no TH)
2Magic record operations using generics (i.e. both type- and morally safe - no TH)
3
3
4+Available on [Hackage](https://hackage.haskell.org/package/generic-records)
5+
4This package uses the GHC8 Generic representation of records to derive magic classes (siilar to the OverloadedRecordLabels proposal), and prove structural subtyping relationship between records.
6This package uses the GHC8 Generic representation of records to derive magic classes (siilar to the OverloadedRecordLabels proposal), and prove structural subtyping relationship between records.
5The latter can be used to upcast the "sub-record" to the more general "interface".
7The latter can be used to upcast the "sub-record" to the more general "interface".
6
8
generic-records.cabal+2
@@ -18,6 +18,8 @@ synopsis: Magic record operations using generics
18-- A longer description of the package.
18-- A longer description of the package.
19description: This package uses the GHC8 Generic representation of records to derive magic classes (a la OverloadedRecordLabels), and prove structural subtyping relationship between records. The latter can be used to upcast the "sub-record" to the more general "interface".
19description: This package uses the GHC8 Generic representation of records to derive magic classes (a la OverloadedRecordLabels), and prove structural subtyping relationship between records. The latter can be used to upcast the "sub-record" to the more general "interface".
20
20
21+homepage: https://github.com/kcsongor/generic-records
22+
21-- The license under which the package is released.
23-- The license under which the package is released.
22license: BSD3
24license: BSD3
23
25
src/Records/Generic.hs+23−2
@@ -10,8 +10,28 @@
10{-# LANGUAGE TypeApplications #-}
10{-# LANGUAGE TypeApplications #-}
11{-# LANGUAGE TypeOperators #-}
11{-# LANGUAGE TypeOperators #-}
12
12
13+-----------------------------------------------------------------------------
14+-- |
15+-- Module : Records.Generic
16+-- Copyright : (C) 2016 Csongor Kiss
17+-- License : BSD3
18+-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
19+-- Stability : experimental
20+-- Portability : non-portable
21+--
22+-- Magic record operations using Generics
23+--
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).
27+--
28+-----------------------------------------------------------------------------
13module Records.Generic
29module Records.Generic
30+ (
31+ -- * Magic getter
14− ( HasField (..)
32+ HasField (..)
33+
34+ -- * Subtype relationship
15 , Subtype (..)
35 , Subtype (..)
16 ) where
36 ) where
17
37
@@ -100,13 +120,14 @@ instance {-# OVERLAPS #-} Convert rep f => Convert rep (M1 i c f) where
100
120
101-- Exported stuff
121-- Exported stuff
102
122
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
106
126
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
109
129
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
112
133