wiki:SpecifyPatternBindingSemantics

Version 1 (modified by simonmar@…, 7 years ago) (diff)

summarising discussion from the mailing list

Specify the static semantics of pattern bindings

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.

The proposal is to define the static semantics by the following translation. A binding p = e has the same meaning as the set of bindings

    z = e
    x1 = case z of { p -> x1 }
    ...
    xn = case z of { p -> xn }

where z is fresh, and x1...xn are the variables of the pattern p.

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.

Ambiguity

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

  [(n,s)] = reads t 

which according to the above translation will give us

n :: (Read t) => t 
s :: (Read t) => String

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.

Other issues

  • We should make sure that the specification also takes into account any type signatures for the pattern-bound variables.

Binding terminology

Additionally, we propose to clarify the terminology for bindings. The Haskell 98 report uses the term "simple pattern binding" inconsistently. The new terminology is:

  • variable binding: any binding of the form x = e
  • pattern binding: any binding of the form p = e where p is not a variable
  • function binding: any binding of the form f p1...pn = e where n > 0