140 | | TODO: |
| 142 | === Patterns === |
| 143 | |
| 144 | We do not allow nested patterns on fields that have polymorphic types. |
| 145 | In other words, when we use a constructor with a polymorphic field as a pattern, |
| 146 | we allow only variable and wild-card patterns in the positions corresponding |
| 147 | to the polymorphic fields. |
| 148 | |
| 149 | Discussion: We could lift this restriction but the resulting behavior |
| 150 | may be more confusing than useful. Consider the following example: |
| 151 | {{{ |
| 152 | data S = C (forall a. [a -> a]) |
| 153 | |
| 154 | test1 (C x) |
| 155 | | null x = ('b', False) |
| 156 | | otherwise = (f 'a', f True) |
| 157 | where f = head x |
| 158 | |
| 159 | test2 (C []) = ('b', False) |
| 160 | test2 (C (f : _)) = (f 'a', f True) |
| 161 | }}} |
| 162 | We may expect that these two definitions are equivalent |
| 163 | but, in fact, the first one is accepted while the |
| 164 | second one is rejected if we perform the usual |
| 165 | translation of patterns. To see what goes wrong, |
| 166 | consider how we desugar patterns: |
| 167 | {{{ |
| 168 | test2 (C x) = case x of |
| 169 | [] -> ('b',False) |
| 170 | f:_ -> (f 'a', f True) |
| 171 | }}} |
| 172 | The use of {{{x}}} in the '''case''' statement instantiates |
| 173 | it to a particular type, which makes {{{f}}} monomorphic |
| 174 | and so we cannot instantiate it to different types. |
| 175 | An alternative might be to perform a generalization after |
| 176 | we pattern match on a polymorphic field but it is not clear |
| 177 | if this extra complexity is useful. |
| 178 | |
| 179 | |
| 180 | == TODO == |
145 | | 1. when you match on ''x'' it instantiates the forall to a monomorphic type as below: |
146 | | {{{ |
147 | | data S = C (forall a. [a]) |
148 | | |
149 | | f (C x) = (show (x::[Int]), show (x::String)) |
150 | | |
151 | | -- f (C []) = ("[]","\"\"") |
152 | | }}} |
153 | | 1. this is not allowed: (see open issue above, iavor thinks GHC tried this and it was really tricky) |
154 | | {{{ |
155 | | f (C []) = True |
156 | | }}} |
157 | | 1. desugaring... |
158 | | {{{ |
159 | | f (C []) = e1 -- illegal |
160 | | }}} |
161 | | would desugar to |
162 | | {{{ |
163 | | f x = case x of |
164 | | C [] -> e 1 -- illegal |
165 | | _ -> error ... |
166 | | }}} |
167 | | would desugar to |
168 | | {{{ |
169 | | case x of |
170 | | C y -> case y of -- NOT illegal |
171 | | [] -> e1 |
172 | | _ -> error... |
173 | | _ -> error... |
174 | | |
175 | | }}} |
176 | | which is a little funny. |
177 | | 1. where is explanation of type checking... |
178 | | 1. where to put the bangs in strict polymorphic fields, hugs and GHC differ - can't figure it out in Hugs |