Lazy Patterns (was: Curry module system and other proposals)

From: Wolfgang Lux <>
Date: Tue, 28 Feb 2006 13:50:25 +0100

Michael Hanus wrote:

> Moreover, I am not sure whether the semantic consequences of
> the little tilde in definitions like
> f x ~(False:ys) = if x==0 then ys else []
> f x (True:ys) = ys
> (note that without the tilde f is inductively sequential, whereas
> with the tilde f is overlapping) should be made explicit by
> a let declaration as in
> f x y = let (False:ys)=y in if x==0 then ys else []
> f x (True:ys) = ys

I agree that in the latter definition it is more obvious that f has
overlapping patterns, but I doubt that anybody would intend to write
such as definition.

> Do you have examples showing that it is worth to hide these
> consequences
> in a tilde annotation instead of a let?

Quite obviously, lazy patterns are most useful in declarations with
only a single rule (because otherwise they are likely to give rise to
unintended non-determinism).

One kind of such examples is tuples. For instance, you can define the
prelude function unzip with a simple fold and a lambda as follows

   unzip = foldr (\(x,y) ~(xs,ys) -> (x:xs,y:ys)) ([],[])

using a let declaration in the lambda expression

   unzip = foldr (\(x,y) xys -> let (xs,ys) = xys in (x:xs,y:ys)) ([],

looks a bit clumsy to me.

Another kind of examples where lazy patterns are useful are lazy stream
processors. As an example, consider a stream of integers where after
third integer you want to a insert a sum of those numbers, but at the
same time want to be as lazy as possible so that
   head (process (1:undefined)) = 1
With lazy patterns you would do this as follows

   process (x : ~(y : ~(z : rest)) = x : y : z : (x+y+z) : process rest

Without lazy patterns, you would have to use something like

   process (x : rest1) = x : y : z : (x+y+z) : process rest3
     where y : rest2 = rest1
           z : rest3 = rest2

which, IMHO, is considerably less readable than the version using lazy
patterns and also in danger of mixing up the rest? variables.


curry mailing list
Received on Di Feb 28 2006 - 16:59:33 CET

This archive was generated by hypermail 2.3.0 : So Feb 05 2023 - 07:15:08 CET