Changes between Version 1 and Version 2 of OverloadedLists


Ignore:
Timestamp:
Nov 6, 2012 2:40:32 PM (18 months ago)
Author:
giorgidze
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OverloadedLists

    v1 v2  
    111111}}} 
    112112 
    113 == TODO == 
    114  
    115 == Overloading Pattern Matching on Lists == 
    116 ... 
     113== TODO: Overloading Pattern Matching on Lists == 
     114 
     115One way to overload list patterns is to replace the {{{FromList}}} class from 
     116the previous section with the {{{OverloadedLists}}} class defined as follows: 
     117 
     118{{{ 
     119class OverloadedLists l where 
     120  type Item l 
     121  fromList  :: [Item l] -> l 
     122  toList    :: l -> [Item l] 
     123 
     124  fromListN :: Int -> [Item l] -> l 
     125  fromListN _ = fromList 
     126}}} 
     127 
     128This class allows us to view the structure {{{l}}} as a list. Now, the 
     129{{{toList}}} can be used to overload the list notation in patters. 
     130For example, When the {{{OverloadedLists}}} extension is turned on, the 
     131definitions 
     132 
     133{{{ 
     134f [] = ... 
     135g [x,y,z] = ... 
     136}}} 
     137 
     138will be treated as 
     139 
     140{{{ 
     141f (toList -> []) = ... 
     142g (toList -> [x,y,z]) = ... 
     143}}} 
     144 
     145Here, just like for expression, we propose to overload list patterns that use 
     146square brackets. The {{{:}}} infix operator will remain list specific both in 
     147expressions and patterns. In other words, {{{:}}} is not overloaded. 
     148 
     149The instances of the {{{OverloadedLists}}} class should satisfy the following 
     150property: 
     151 
     152{{{ 
     153fromList . toList = id 
     154}}} 
     155 
     156The example {{{FromList}}} instances from the previous section can be extended 
     157into the {{{OverloadedLists}}} instances as follows: 
     158 
     159{{{ 
     160instance OverloadedLists [a] where 
     161  type Item [a] = a 
     162  fromList = id 
     163  toList = id 
     164 
     165instance (Ord a) => OverloadedLists (Set a) where 
     166  type Item (Set a) = a 
     167  fromList = Set.fromList 
     168  toList = Set.toList 
     169 
     170instance (Ord k) => OverloadedLists (Map k v) where 
     171  type Item (Map k v) = (k,v) 
     172  fromList = Map.fromList 
     173  toList = Map.toList 
     174 
     175instance OverloadedLists (IntMap v) where 
     176  type Item (IntMap v) = (Int,v) 
     177  fromList = IntMap.fromList 
     178  toList = IntMap.toList 
     179 
     180instance OverloadedLists Text where 
     181  type Item Text = Char 
     182  fromList = Text.pack 
     183  toList = Text.unpack 
     184 
     185instance OverloadedLists (Vector a) where 
     186  type Item (Vector a) = a 
     187  fromList  = Vector.fromList 
     188  fromListN = Vector.fromListN 
     189  toList = Vector.toList 
     190}}} 
     191 
     192== Further Improvements == 
    117193 
    118194== Defaulting ==