[Haskell-cafe] Re: [Haskell] lazy constructors

Ben Rudiak-Gould br276 at cl.cam.ac.uk
Wed Oct 13 10:31:05 EDT 2004


Serge D. Mechveliani wrote:

>  As the types are resolved before the computation, as the above
>  program shows, how can addToSPair expect anything besides a string
>  pair? Why tilda is not a default?

Haskell pairs are "lifted", meaning that they also include an extra 
value (bottom) which doesn't match (x,y). Not everyone is convinced that 
this is a good idea, but it's the way things are at present.

>  The only doubt may be the matching of (xs, ys) against bottom ::
>  (String, String) Is not this natural to have a result as (bottom ::
>  String, bottom :: String) -- = xs = ys and compute further?

Possibly in this case, yes. But not in general, since there might be 
other data constructors also.

>  What will occur if all the Haskell programmers set `~' before each
>  data constructor in each pattern in their existing programs?

Things won't work as you'd expect. For example, if you defined

   null :: [a] -> Bool
   null list =
     case list of
       ~(x:xs) -> False
       ~[]     -> True

you would find that (null []) is False, not True, because the first 
pattern matches irrefutably. Pattern matching needs to be strict so that 
case statements like this work sensibly.

>  As the types are recognized before the computation, the programs will
>  remain safe and will become considerably faster. For example, the
>  above program of g1 becomes a billion times faster. I wonder.

Actually using irrefutable patterns tends to make a program slower, not 
faster, because it makes the program less strict.

Good questions!

-- Ben



More information about the Haskell-Cafe mailing list