| 1 | = Specify the static semantics of pattern bindings = |
| 2 | |
| 3 | Currently the Haskell 98 report doesn't specify clearly the static semantics of pattern bindings. Tricky cases arise when considering whether a pattern binding can define polymorphic values (see also MonomorphicPatternBindings), or when the binding is overloaded. In Haskell 98, overloaded pattern bindings cannot declare polymorphic values, due to the monomorphism restriction, but if we were to lift the monomorphism restriction we have to consider carefully what an overloaded polymorphic pattern binding would mean. |
| 4 | |
| 5 | The proposal is to define the static semantics by the following |
| 6 | translation. A binding `p = e` has the same meaning as the set of |
| 7 | bindings |
| 8 | |
| 9 | {{{ |
| 10 | z = e |
| 11 | x1 = case z of { p -> x1 } |
| 12 | ... |
| 13 | xn = case z of { p -> xn } |
| 14 | }}} |
| 15 | |
| 16 | where `z` is fresh, and `x1...xn` are the variables of the pattern p. |
| 17 | |
| 18 | Note that this specification addresses one of the main concerns of MonomorphicPatternBindings, namely that it is not clear what (overloaded, polymorphic) pattern bindings should mean. |
| 19 | |
| 20 | == Ambiguity == |
| 21 | |
| 22 | It is easy to end up with ambiguity when using overloaded pattern bindings. The example from the Haskell 98 report which is used to justify the monomorphism restriction is |
| 23 | |
| 24 | {{{ |
| 25 | [(n,s)] = reads t |
| 26 | }}} |
| 27 | |
| 28 | which according to the above translation will give us |
| 29 | |
| 30 | {{{ |
| 31 | n :: (Read t) => t |
| 32 | s :: (Read t) => String |
| 33 | }}} |
| 34 | |
| 35 | there is no way to refer to `s`, and a type signature for `s` cannot be given. Neither can this cannot be resolved by giving a type signature for `n` (unless perhaps we have ScopedTypeVariables with type signatures on pattern variables and we put a signature on `n` in the pattern). We may want to mention this issue in the report. |
| 36 | |
| 37 | == Other issues == |
| 38 | |
| 39 | * We should make sure that the specification also takes into account any type signatures for the pattern-bound variables. |
| 40 | |
| 41 | == Binding terminology == |
| 42 | |
| 43 | Additionally, we propose to clarify the terminology for bindings. The Haskell 98 report uses the term "simple pattern binding" inconsistently. The new terminology is: |
| 44 | |
| 45 | * variable binding: any binding of the form `x = e` |
| 46 | * pattern binding: any binding of the form `p = e` where `p` is not a variable |
| 47 | * function binding: any binding of the form `f p1...pn = e` where `n > 0` |
| 48 | |
| 49 | |