[Haskell] ANNOUNCE: HaRP (Haskell Regular Patterns) version 0.1

Niklas Broberg n_broberg at hotmail.com
Sun May 16 10:37:22 EDT 2004


Dylan Thurston wrote:
>So I guess that in
>
>foo [/ a? 2 b /] = (a,b)
>
>the type of a is '[Int]', not 'Maybe Int', right?

Aye. If you wanted Maybe Int, you would need to do

foo [/ a at _? 2 b /] = (a,b)

I wrote:
> > I'm starting to think maybe our context dependent approach to implicit
> > bindings isn't very good after all since it seems to confuse a lot of
> > people. Perhaps variables bound inside regular patterns should always be
> > non-linear... of course that would still be context dependent when 
>compared
> > to normal Haskell patterns, but perhaps still less confusing?

Dylan Thurston wrote:
>Alternatively, you could forbid the use of simple variables nonlinearly,
>since there's an alternative way to write it.  Or, you could make
>variables (and other pattern binding) more context dependent, recording
>all the relevant parts of the context (and not just whether the context
>is linear or not), if that makes sense.
>
>Interesting issues, anyway!

Indeed they are.
We basically have two reasons for not wanting to record the context in bound 
variables.
The first is that it would not correspond very well with ordinary pattern 
matching, and we sought to minimize the differences by only adding the 
context dependence that was impossible to avoid.
The second is that there is a way to get at that context information 
already, by explicitly binding larger patterns (cf. the example above). In 
contrast, were we to record contexts in all bound variables there would be 
no way to avaid the context information should you want to. As an example

catMaybes :: [Maybe a] -> [a]
catMaybes [/ (Just a | Nothing)* /] = a

If we were to record the context in which a appears, we would have removed 
one data constructor (Just) but added another (Left).
Perhaps even more illustrating would be

catEithers :: [Either a a] -> [a]
catEithers [/ (Left a | Right a)* /] = a

Dylan Thurston wrote:
>By the way, are nested regular expression matches allowed?  Something
>like:
>
>foo :: [[Int]] -> Int
>foo [/ _* [/ 5* a 6* /] _* /] = a

Absolutely. We've added regular patterns as a new pattern category in 
Haskell, so anywhere that you can use a standard pattern you can use a 
regular pattern, including inside a regular pattern.

Dylan Thurston wrote:
>?  If so, what is the type of a in
>
>foo [/ _* [/ 5* a* /]* _* /]
>
>?

It will be [[Int]]. a has type [Int] in the inner pattern, and in the outer 
pattern it appears in non-linear context (inside the *). Compare to

foo [/ _* (Just a@(_:_))* _* /] = a

Here the a clearly binds to a list in the inner expression, and in the 
regular pattern it appears in non-linear context. So here foo :: [Maybe [a]] 
-> [[a]]
As an aside, the patterns above will always give a the empty list since the 
first _* will "eat" the entire input, being greedy and all. But of course 
that won't affect the types...

With the package you can try out the types yourself. Just write these 
functions in a source file and load it into ghci following the guidelines on 
the homepage (http://www.dtek.chalmers.se/~d00nibro/harp/). Then you can let 
ghci infer the types for you. =)

/Niklas

_________________________________________________________________
Add photos to your e-mail with MSN 8. Get 2 months FREE*. 
http://join.msn.com/?page=features/featuredemail



More information about the Haskell mailing list