开发者

How pure and lazy can Scala be?

开发者 https://www.devze.com 2023-04-07 20:21 出处:网络
This is just one of those \"I was wondering...\" questions. Scala has immutable data 开发者_如何学Pythonstructures and (optional) lazy vals etc.

This is just one of those "I was wondering..." questions.

Scala has immutable data 开发者_如何学Pythonstructures and (optional) lazy vals etc.

How close can a Scala program be to one that is fully pure (in a functional programming sense) and fully lazy (or as Ingo points out, can it be sufficiently non-strict)? What values are unavoidably mutable and what evaluation unavoidably greedy?


Regarding lazyness - currently, passing a parameter to a method is by default strict:

def square(a: Int) = a * a

but you use call-by-name parameters:

def square(a: =>Int) = a * a

but this is not lazy in the sense that it computes the value only once when needed:

scala> square({println("calculating");5})
calculating
calculating
res0: Int = 25

There's been some work into adding lazy method parameters, but it hasn't been integrated yet (the below declaration should print "calculating" from above only once):

def square(lazy a: Int) = a * a

This is one piece that is missing, although you could simulate it with a local lazy val:

def square(ap: =>Int) = {
  lazy val a = ap
  a * a
}

Regarding mutability - there is nothing holding you back from writing immutable data structures and avoid mutation. You can do this in Java or C as well. In fact, some immutable data structures rely on the lazy primitive to achieve better complexity bounds, but the lazy primitive can be simulated in other languages as well - at the cost of extra syntax and boilerplate.

You can always write immutable data structures, lazy computations and fully pure programs in Scala. The problem is that the Scala programming model allows writing non pure programs as well, so the type checker can't always infer some properties of the program (such as purity) which it could infer given that the programming model was more restrictive.

For example, in a language with pure expressions the a * a in the call-by-name definition above (a: =>Int) could be optimized to evaluate a only once, regardless of the call-by-name semantics. If the language allows side-effects, then such an optimization is not always applicable.


Scala can be as pure and lazy as you like, but a) the compiler won't keep you honest with regards to purity and b) it will take a little extra work to make it lazy. There's nothing too profound about this; you can even write lazy and pure Java code if you really want to (see here if you dare; achieving laziness in Java requires eye-bleeding amounts of nested anonymous inner classes).

Purity

Whereas Haskell tracks impurities via the type system, Scala has chosen not to go that route, and it's difficult to tack that sort of thing on when you haven't made it a goal from the beginning (and also when interoperability with a thoroughly impure language like Java is a major goal of the language).

That said, some believe it's possible and worthwhile to make the effort to document effects in Scala's type system. But I think purity in Scala is best treated as a matter of self-discipline, and you must be perpetually skeptical about the supposed purity of third-party code.

Laziness

Haskell is lazy by default but can be made stricter with some annotations sprinkled in your code... Scala is the opposite: strict by default but with the lazy keyword and by-name parameters you can make it as lazy as you like.


Feel free to keep things immutable. On the other hand, there's no side effect tracking, so you can't enforce or verify it.

As for non-strictness, here's the deal... First, if you choose to go completely non-strict, you'll be forsaking all of Scala's classes. Even Scalaz is not non-strict for the most part. If you are willing to build everything yourself, you can make your methods non-strict and your values lazy.

Next, I wonder if implicit parameters can be non-strict or not, or what would be the consequences of making them non-strict. I don't see a problem, but I could be wrong.

But, most problematic of all, function parameters are strict, and so are closures parameters.

So, while it is theoretically possible to go fully non-strict, it will be incredibly inconvenient.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号