Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would pattern matching look like in a strict Haskell?

Tags:

haskell

As a research experiment, I've recently worked on implementing strict-by-default Haskell modules. Instead of being lazy-by-default and having ! as an escape hatch, we're strict-by-default and have ~ as an escape hatch. This behavior is enabled using a {-# LANGUAGE Strict #-} pragma.

While working on making patterns strict I came up on an interesting question: should patterns be strict in the "top-level" only or in all bind variables. For example, if we have

f x = case x of
  y -> ...

we will force y even though Haskell would not do so. The more tricky case is

f x = case x of
  Just y -> ...

Should we interpret that as

f x = case x of
  Just y -> ...  -- already strict in 'x' but not in `y`

or

f x = case x of
  Just !y -> ...  -- now also strict in 'y'

(Note that we're using the normal, lazy Haskell Just here.)

One design constraint that might of value is this: I want the pragma to be modular. For example, even with Strict turned on we don't evaluate arguments to functions defined in other modules. That would make it non-modular.

Is there any prior art here?

like image 920
tibbe Avatar asked Nov 06 '13 13:11

tibbe


People also ask

How is pattern matching done in Haskell?

Overview. We use pattern matching in Haskell to simplify our codes by identifying specific types of expression. We can also use if-else as an alternative to pattern matching. Pattern matching can also be seen as a kind of dynamic polymorphism where, based on the parameter list, different methods can be executed.

What is pattern matching in Haskell with example?

Pattern matching consists of specifying patterns to which some data should conform and then checking to see if it does and deconstructing the data according to those patterns. When defining functions, you can define separate function bodies for different patterns.

Can you pattern match in guards Haskell?

The PatternGuards extension, now officially incorporated into the Haskell 2010 language, expands guards to allow arbitrary pattern matching and condition chaining. The existing syntax for guards then becomes a special case of the new, much more general form. You start a guard in the same way as always, with a | .

Is Haskell strict?

Haskell is a non-strict language, and most implementations use a strategy called laziness to run your program.


1 Answers

As far as I understand things, refutable patterns are always strict at least on the outer level. Which is another way to say that the scrutinized expression must have been evaluated to WHNF, otherwise you couldn't see if it is a 'Just' or a 'Nothing'.

Hence your

!(Just y) -> ...

notation appears useless.

OTOH, since in a strict language, the argument to Just must already have been evaluated, the notation

Just !y ->

doesn't make sense either.

like image 123
Ingo Avatar answered Nov 07 '22 16:11

Ingo