3 ms·
> I think the resource management issue is mostly solved in practice when using pure functional programming. Well, in the sense that garbage collectors solve m
by eru 3y ago
> I think the resource management issue is mostly solved in practice when using pure functional programming.
Well, in the sense that garbage collectors solve memory management. But pure functional programming in the Haskell sense doesn't really manage file handles for you, or database connections.
You could have more predictable performance in Haskell, if you didn't have to deal with mandatory laziness for some functions.
(Basically, in this hypothetical variant of Haskell, by default functions would be safe to be evaluated in any order, strict or lazy or whatever. If you want functions that need to be evaluated lazily, then you'd need to declare that; just like today you already need to declare that your function might have side effects.)
The compiler would then be free to re-order evaluation a lot more, and pick predictable, fast performance.
- valenterry 3y ago> Well, in the sense that garbage collectors solve memory management. But pure functional programming in the Haskell sense doesn't really manage file handles for you, or database connections. No, I was actually talking about the latter and not about memory management at all. In Scala we even achieve this on the library level. (e.g. https://zio.dev/reference/resource/ https://zio.dev/reference/resource/ which also compares the existing problematic try/catch with its own approach) And despite Scala being strict, the GC still makes it unsuitable for many cases where you just need something like Haskell.
- mrkeen 3y ago> If you want functions that need to be evaluated lazily, then you'd need to declare that Not a sensible default. You cannot ask a strict function to behave lazily. But you can ask a lazy function to behave strictly.
- throwaway17_17 3y agoI am unsure where the proof, or even supporting argument, would come from for such a statement. The literature is lengthy and deep, but I would sum up the current understanding and most popular argument against your stated position by citing Robert Harper (who I admit is notoriously anti-Laziness by default) in his commentary to the 2nd Edition to Practical Foundations of Programming Languages: "The problem is the very idea of a lazy language, which imposes a ruinous semantics on types. An eager semantics supports the expected semantics of types (for example, the natural numbers are the natural numbers) and, moreover, admits definition of the lazy forms of these types using suspension types (Chapter 36). A value of a suspension type is either a value of the underlying type, or a computation of such a value that may diverge. There being no representation of the eager types in a lazy language, it follows that, eager languages are strictly more expressive than lazy languages." [1] This paragraph is then followed by a link to Harper's "PCF by Value" supplement/errata to the above referenced PFPL, where such a statement is reinforced. [2] [1] - https://www.cs.cmu.edu/~rwh/pfpl/supplements/commentary.pdf https://www.cs.cmu.edu/~rwh/pfpl/supplements/commentary.pdf [2] - https://www.cs.cmu.edu/~rwh/pfpl/supplements/pcfv.pdf https://www.cs.cmu.edu/~rwh/pfpl/supplements/pcfv.pdf
- mrkeen 3y ago> I am unsure where the proof, or even supporting argument You make a lazy function strict by asking for its output now rather than later. > An eager semantics supports the expected semantics of types (for example, the natural numbers are the natural numbers). What a (counter)example! In lazy semantics the natural numbers are the natural numbers. In eager semantics the natural numbers are stack overflow. > moreover, admits definition of the lazy forms of these types using suspension types This is the required modification that your library maintainer would need to make if he had produced a strict library that you wanted to use lazily. Hence "You cannot ask a strict function to behave lazily." > There being no representation of the eager types in a lazy language Because the lazy representation is the same as the eager representation in the source code. It's up to the caller to demand values now, or later, hence "But you can ask a lazy function to behave strictly."
- eru 3y ago> You make a lazy function strict by asking for its output now rather than later. That doesn't mean you can evaluate the function strictly. Have a look at this example: f = head . map (2+) x = f [5, undefined] You can ask for x right away (it's 7), but if you evaluate f strictly, it still diverges. In any case, my suggestion is not to deal with arbitrary functions; but to restrict the set of untagged functions to those where any evaluation order works. 'Works' in the sense that any evaluation order takes a finite amount of time.
- eru 3y ago> You cannot ask a strict function to behave lazily. But you can ask a lazy function to behave strictly. Huh? I suggest that when you don't annotate anything, you can only use constructs that can be evaluated in any order (including both lazy and strict or any other order). So the compiler has maximal freedom. So your supposed impossibility doesn't come into play: we are not dealing with arbitrary functions. If your function needs a specific evaluation order, you'd need to tag that.
- mrkeen 3y agoYou can walk an expression and replace its thunks with the values that those thunks deferred the evaluation of. To do the reverse would require uncomputation. You can't turn all the natural numbers back into the expression [1..].
- marcosdumay 3y ago> But pure functional programming in the Haskell sense doesn't really manage file handles for you, or database connections. That's what monads are for. Haskell doesn't require that you use automatic management. But it does absolutely have a solution.
- eru 3y agoHow are monads useful for managing file handles or database connections? You might want to look into 'indexed monads', they can perhaps help with that. But they aren't a type monads (it's the other way round, vanilla monads can be seen as a trivial kind of 'indexed monads', I guess).
- valenterry 3y agoThe encapsulate direct access to those resource and therefore allow a properly controlled lifecycle because usage is then local and cleanup in case of errors is automatic. The caller pretty much can't do anything wrong. No linear types or indexed monads needed actually.