| 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 |
| | 59 | List patterns are also overloaded. When the {{{OverloadedLists}}} extension is turned on, the |
| 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. |
| | 74 | GHC, during the typechecking and desugaring phases, uses whatever is in scope |
| | 75 | with the names of {{{fromList}}} and {{{fromListN}}} (i.e., {{{fromList}}} and |
| | 76 | {{{fromListN}}} are rebindable). |
| | 77 | |
| | 78 | That said, the {{{GHC.Exts}}} module exports the {{{IsList}}} class that can |
| | 79 | be used to overload {{{fromListN}}} and {{{fromListN}}} for different |
| | 80 | structures. The type class is defined as follows: |
| | 81 | |
| | 82 | {{{ |
| | 83 | class 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 | |
| | 92 | The {{{IsList}}} class and its methods are intended to be used in |
| | 93 | conjunction with the {{{OverloadedLists}}} extension. The {{{Item}}} type |
| | 94 | function returns the type of items of the structure {{{l}}}. The fromList |
| | 95 | function constructs the structure {{{l}}} from the given list of {{{Item l}}}. |
| | 96 | The {{{fromListN}}} function takes the input list's length as a hint. Its |
| | 97 | behaviour should be equivalent to {{{fromList}}}. The hint can be used for |
| | 98 | more efficient construction of the structure {{{l}}} compared to |
| | 99 | {{{fromList}}}. If the given hint is not equal to the input list's length the |
| | 100 | behaviour of {{{fromListN}}} is not specified. |