Changes between Version 6 and Version 7 of OverloadedLists
- Timestamp:
- 11/10/12 02:52:07 (7 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
OverloadedLists
v6 v7 116 116 117 117 One way to overload list patterns is to replace the {{{FromList}}} class from 118 the previous section with the {{{ ListRep}}} class defined as follows:119 120 {{{ 121 class ListRepl where118 the previous section with the {{{IsList}}} class defined as follows: 119 120 {{{ 121 class IsList l where 122 122 type Item l 123 123 fromList :: [Item l] -> l … … 149 149 expressions and patterns. In other words, {{{:}}} is not overloaded. 150 150 151 The instances of the {{{ ListRep}}} class should satisfy the following151 The instances of the {{{IsList}}} class should satisfy the following 152 152 property: 153 153 … … 157 157 158 158 The example {{{FromList}}} instances from the previous section can be extended 159 into the {{{ ListRep}}} instances as follows:160 161 {{{ 162 instance ListRep[a] where159 into the {{{IsList}}} instances as follows: 160 161 {{{ 162 instance IsList [a] where 163 163 type Item [a] = a 164 164 fromList = id 165 165 toList = id 166 166 167 instance (Ord a) => ListRep(Set a) where167 instance (Ord a) => IsList (Set a) where 168 168 type Item (Set a) = a 169 169 fromList = Set.fromList 170 170 toList = Set.toList 171 171 172 instance (Ord k) => ListRep(Map k v) where172 instance (Ord k) => IsList (Map k v) where 173 173 type Item (Map k v) = (k,v) 174 174 fromList = Map.fromList 175 175 toList = Map.toList 176 176 177 instance ListRep(IntMap v) where177 instance IsList (IntMap v) where 178 178 type Item (IntMap v) = (Int,v) 179 179 fromList = IntMap.fromList 180 180 toList = IntMap.toList 181 181 182 instance ListRepText where182 instance IsList Text where 183 183 type Item Text = Char 184 184 fromList = Text.pack 185 185 toList = Text.unpack 186 186 187 instance ListRep(Vector a) where187 instance IsList (Vector a) where 188 188 type Item (Vector a) = a 189 189 fromList = Vector.fromList … … 191 191 toList = Vector.toList 192 192 }}} 193 194 An alternative name for the {{{IsList}}} class could be {{{ListRep}}} (meaning 195 representable as list). This is to emphasise that its instances should be 196 representable as lists, it is not required for them to be lists. 193 197 194 198 == Further GHC improvements/extensions that may benefit {{{OverloadedLists}}} ==
