... | ... | @@ -73,14 +73,19 @@ The only special stuff is in the pattern. |
|
|
The sole change is this: add a single new sort of pattern, of the
|
|
|
form
|
|
|
|
|
|
|
|
|
>
|
|
|
>
|
|
|
> (*expr* `->` *pat*)
|
|
|
>
|
|
|
>
|
|
|
|
|
|
|
|
|
where *expr* is an arbitrary Haskell expression. I'll call a pattern
|
|
|
of this form a **view pattern**.
|
|
|
|
|
|
|
|
|
|
|
|
From a **scoping** point of view, the variables bound by the pattern (*expr* `->` *pat*)
|
|
|
are simply the variables bound by ``pat``.
|
|
|
Any variables in ``expr`` are bound occurrences.
|
... | ... | @@ -99,6 +104,7 @@ The expression *expr* must have type |
|
|
*t1 `-> Maybe` t2*. Then the pattern *pat* must have type *t2*, and the
|
|
|
whole pattern (*expr* `->` *pat*) has type *t1*.
|
|
|
|
|
|
|
|
|
### Features
|
|
|
|
|
|
|
... | ... | @@ -336,6 +342,7 @@ Of course, the real power unfolds when the extra parameter can be given at runti |
|
|
|
|
|
In the proposed view pattern (*expr* `->` *pat*), *expr* is an arbitrary Haskell expression. Thus, the lightweight proposal has the **value input feature**. For another example, suppose you wrote a regular expression matching function:
|
|
|
|
|
|
|
|
|
```wiki
|
|
|
regexp :: String -> String -> Maybe (String, String)
|
|
|
-- (regexp r s) parses a string matching regular expression r
|
... | ... | @@ -992,6 +999,7 @@ The examples at the top of this page would look like this with first class patte |
|
|
### First class abstractions
|
|
|
|
|
|
|
|
|
|
|
|
Several proposals suggest first class *abstractions* rather that first-class *patterns*. By a "first class abstraction" I mean a value of type
|
|
|
(*something* `->` *something*)
|
|
|
with a syntax something like
|
... | ... | |