Changes between Version 7 and Version 8 of OverloadedLists


Ignore:
Timestamp:
Jan 30, 2013 12:01:11 PM (2 years ago)
Author:
guest
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OverloadedLists

    v7 v8  
    5757}}}
    5858
    59 GHC, during the typechecking and desugaring phases, uses whatever is in scope
    60 with the names of {{{fromList}}} and {{{fromListN}}} (i.e., {{{fromList}}} and
    61 {{{fromListN}}} are rebindable).
    62 
    63 That said, the {{{GHC.Exts}}} module exports the {{{FromList}}} class that can
    64 be used to overload {{{fromListN}}} and {{{fromListN}}} for different
    65 structures. The type class is defined as follows:
    66 
    67 {{{
    68 class FromList l where
    69   type Item l
    70   fromList  :: [Item l] -> l
    71   fromListN :: Int -> [Item l] -> l
    72   fromListN _ = fromList
    73 }}}
    74 
    75 The {{{FromList}}} class and its methods are intended to be used in
    76 conjunction with the {{{OverloadedLists}}} extension. The {{{Item}}} type
    77 function returns the type of items of the structure {{{l}}}. The fromList
    78 function constructs the structure {{{l}}} from the given list of {{{Item l}}}.
    79 The {{{fromListN}}} function takes the input list's length as a hint. Its
    80 behaviour should be equivalent to {{{fromList}}}. The hint can be used for
    81 more efficient construction of the structure {{{l}}} compared to
    82 {{{fromList}}}. If the given hint is not equal to the input list's length the
    83 behaviour of {{{fromListN}}} is not specified.
    84 
    85 In the following, we give several example instances of the {{{FromList}}} type
    86 class:
    87 
    88 {{{
    89 instance FromList [a] where
    90   type Item [a] = a
    91   fromList = id
    92 
    93 instance (Ord a) => FromList (Set a) where
    94   type Item (Set a) = a
    95   fromList = Set.fromList
    96 
    97 instance (Ord k) => FromList (Map k v) where
    98   type Item (Map k v) = (k,v)
    99   fromList = Map.fromList
    100 
    101 instance FromList (IntMap v) where
    102   type Item (IntMap v) = (Int,v)
    103   fromList = IntMap.fromList
    104 
    105 instance FromList Text where
    106   type Item Text = Char
    107   fromList = Text.pack
    108 
    109 instance FromList (Vector a) where
    110   type Item (Vector a) = a
    111   fromList  = Vector.fromList
    112   fromListN = Vector.fromListN
    113 }}}
    114 
    115 == TODO: Overloading Pattern Matching on Lists ==
    116 
    117 One way to overload list patterns is to replace the {{{FromList}}} class from
    118 the previous section with the {{{IsList}}} class defined as follows:
    119 
    120 {{{
    121 class IsList l where
    122   type Item l
    123   fromList  :: [Item l] -> l
    124   toList    :: l -> [Item l]
    125 
    126   fromListN :: Int -> [Item l] -> l
    127   fromListN _ = fromList
    128 }}}
    129 
    130 This class allows us to view the structure {{{l}}} as a list. Now, the
    131 {{{toList}}} can be used to overload the list notation in patters.
    132 For example, When the {{{OverloadedLists}}} extension is turned on, the
     59List patterns are also overloaded. When the {{{OverloadedLists}}} extension is turned on, the
    13360definitions
    13461
     
    14572}}}
    14673
    147 Here, just like for expressions, we propose to overload list patterns that use
    148 square brackets. The {{{:}}} infix operator will remain list specific both in
    149 expressions and patterns. In other words, {{{:}}} is not overloaded.
     74GHC, during the typechecking and desugaring phases, uses whatever is in scope
     75with the names of {{{fromList}}} and {{{fromListN}}} (i.e., {{{fromList}}} and
     76{{{fromListN}}} are rebindable).
     77
     78That said, the {{{GHC.Exts}}} module exports the {{{IsList}}} class that can
     79be used to overload {{{fromListN}}} and {{{fromListN}}} for different
     80structures. The type class is defined as follows:
     81
     82{{{
     83class IsList l where
     84  type Item l
     85  fromList  :: [Item l] -> l
     86  toList    :: l -> [Item l]
     87
     88  fromListN :: Int -> [Item l] -> l
     89  fromListN _ = fromList 
     90}}}
     91
     92The {{{IsList}}} class and its methods are intended to be used in
     93conjunction with the {{{OverloadedLists}}} extension. The {{{Item}}} type
     94function returns the type of items of the structure {{{l}}}. The fromList
     95function constructs the structure {{{l}}} from the given list of {{{Item l}}}.
     96The {{{fromListN}}} function takes the input list's length as a hint. Its
     97behaviour should be equivalent to {{{fromList}}}. The hint can be used for
     98more efficient construction of the structure {{{l}}} compared to
     99{{{fromList}}}. If the given hint is not equal to the input list's length the
     100behaviour of {{{fromListN}}} is not specified.
    150101
    151102The instances of the {{{IsList}}} class should satisfy the following
     
    156107}}}
    157108
    158 The example {{{FromList}}} instances from the previous section can be extended
    159 into the {{{IsList}}} instances as follows:
     109In the following, we give several example instances of the {{{IsList}}} type
     110class:
    160111
    161112{{{
     
    163114  type Item [a] = a
    164115  fromList = id
    165   toList = id
     116  toList   = id
    166117
    167118instance (Ord a) => IsList (Set a) where
    168119  type Item (Set a) = a
    169120  fromList = Set.fromList
    170   toList = Set.toList
     121  toList   = Set.toList
    171122
    172123instance (Ord k) => IsList (Map k v) where
    173124  type Item (Map k v) = (k,v)
    174125  fromList = Map.fromList
    175   toList = Map.toList
     126  toList   = Map.toList
    176127
    177128instance IsList (IntMap v) where
    178129  type Item (IntMap v) = (Int,v)
    179130  fromList = IntMap.fromList
    180   toList = IntMap.toList
     131  toList   = IntMap.toList
    181132
    182133instance IsList Text where
    183134  type Item Text = Char
    184135  fromList = Text.pack
    185   toList = Text.unpack
     136  toList   = Text.unpack
    186137
    187138instance IsList (Vector a) where
    188139  type Item (Vector a) = a
    189140  fromList  = Vector.fromList
     141  toList    = Vector.toList
    190142  fromListN = Vector.fromListN
    191   toList = Vector.toList
     143
    192144}}}
    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.
    197145
    198146== Further GHC improvements/extensions that may benefit {{{OverloadedLists}}} ==