10 ms·
> So a theoretical type system extension that Kotlin doesn't actually have could solve this There is nothing to solve unless a problem is identified, which, to
by pron 9y ago
> So a theoretical type system extension that Kotlin doesn't actually have could solve this
There is nothing to solve unless a problem is identified, which, to date, none has (except within the context of pure FP).
> and looks ok in simple examples
Kiselyov treats general cases, and AFICT, his work on algebraic effect systems uses exactly the same approach.
> it hasn't caused a bug yet; the community and culture can mostly work around the lack of hard restrictions at the language level.
Well, you were talking about language support. The fact is that monadic style and the imperative control structures provided by Scala simply do not mix well, so why does Scala have both? Anyway, Kotlin doesn't want to buy into that programming paradigm. That's just your aesthetic choice.
> There are now ways to avoid having to copy-paste in that situation e.g. MTL-style code ("final tagless") or free coproduct style.
While I'm only a little familiar with free algebras from a theoretical POV, and all I know about "final tagless" is that it aims to achieve something similar to object algebras in OOP code, I believe those "free interpreter" styles are both expensive performance-wise (they require to represent code as a runtime data structure), and just expose an underlying fundamental problem in the language (there is also the deep problem -- which is mostly theoretical, but I'm also interested in theory, and find the FP theory to be both weak and complex compared to newer alternatives -- that monadic code can be computationally identical to non-monadic code, e.g. simple recursion vs. running in a state monad, yet the two have completely different denotations and inhabit incomparable types; if monadic code is the more general form, there are theories that do something similar, but in a way that's more uniform, simpler, and much more amenable to formal analysis; but I digress).
In general, one could say that any language with extensive DSL capabilities can fix any problem, but there are very good reasons to avoid powerful DSL support. This is one of the things I find most interesting in Kotlin. It offers some DSL support (inline functions, builders), which seems powerful, yet still doesn't take embedding too far. Is this a sweet spot? Does it do harm by adding features that don't solve hard problems yet introduce complexity? I don't know, but it will be interesting to see.
- lmm 9y ago> The fact is that monadic style and the imperative control structures provided by Scala simply do not mix well, so why does Scala have both? Which control structures? "for" extends very nicely to monadic style, as do if/else expressions. I guess do/while and throw/catch don't, but the language syntax is flexible enough that it doesn't really matter, because these builtin control structures are only barely more privileged than user-defined functions. (To answer your question, probably Java compatibility). I mean, don't get me wrong, I'd like to see them removed from the language, but it's not like anything would go wrong if you used while in a monadic context. throw I will grant you doesn't mix well with the rest of the language; the JVM means the possibility of exceptions is something we have to live with, but from a pure language design point of view it would be better to be without it, sure. > That's just your aesthetic choice. Aesthetics have nothing to do with it; it's my experience of what does and doesn't produce reliable, maintainable code. > "free interpreter" styles are both expensive performance-wise Free interpreter style can be expensive performance-wise; final tagless doesn't have that problem (indeed I see a lot of similarity between that style and various effect/ambient handler styles, for better and for worse). > that monadic code can be computationally identical to non-monadic code, e.g. simple recursion vs. running in a state monad, yet the two have completely different denotations and inhabit incomparable types What do state and recursion have to do with each other? Are you assuming some unmodelled language-level feature like mutable variables?
- pron 9y ago> don't get me wrong, I'd like to see them removed from the language Why? Because you are drawn to the pure FP style. That's great, but that's an aesthetic choice, and certainly not one shared among all programmers. > but it's not like anything would go wrong if you used while in a monadic context. throw I will grant you doesn't mix well with the rest of the language Eh. You can't use while to loop over some effectful monad. > Aesthetics have nothing to do with it; it's my experience of what does and doesn't produce reliable, maintainable code. Yes, but seeing that that experience is far from universal, that makes it an aesthetic choice. It makes you more productive because you are more comfortable with that style. > What do state and recursion have to do with each other? You could translate any recursive function into one making use of a state monad. While the two express the same computation, I think their denotation is too different to construct an isomorphism between the two; this is an essential problem with the lambda calculus (in all its manifestations), that can express computations as either functions or monads. This is not a problem with simpler, more composable paradigms, that express computation in a way more amenable to mathematical manipulation (e.g., synchronous programming a-la Eve), and eschew the problematic representation of computation as functions.
- lmm 9y ago> Why? Because you are drawn to the pure FP style. That's great, but that's an aesthetic choice, and certainly not one shared among all programmers. No, because they're special cases at the language level that don't need to be there. It shouldn't be a language builtin if we can achieve the same thing with a plain old library function. That's not an aesthetic preference, that's language design. > You could translate any recursive function into one making use of a state monad. What and how would you translate? I can't see what you mean at all, particularly because it's very normal to write a recursive function that uses the state monad - I do this quite a lot.
- pron 9y ago> It shouldn't be a language builtin if we can achieve the same thing with a plain old library function. That is an aesthetic preference, and this is why: sometimes you don't want a general abstraction that allows you to express certain constructs because that same general abstraction may be used to express constructs that you don't want your programmers to create. In that case, you want an ad hoc builtin construct instead of a more general abstraction. The things that you want or don't want in order to maximize some value function are exactly what aesthetics is. There is no mathematical theorem, or no natural law, determining which value function I aim to optimize, and therefore no objective rule for deciding which language features are "correct", and no absolute measure for what constitutes good design. The only measure for good design is whether or not the product optimizes the value function that you've chosen on aesthetic grounds. > What and how would you translate? Think of writing a factorial function recursively and one using some looping monad. The two have isomorphic operational semantics, but completely different denotations. Monads basically let you express any kind of computation, including those that you can express with plain lambda terms. In addition -- and this is important -- some computations can _only_ be expressed with monads (assuming you don't have linear types). This means that lambda calculus contains two different, and incompatible, ways of expressing computation. This makes program analysis unnecessarily complicated (unless your program is just a sequential computation, in which case lambda calculus is a nice formalism because that's the usage it's been invented for). Formalisms invented for general (i.e., not necessarily sequential) computation, including sequential, concurrent and interactive, don't have this problem, and have just a single, simple and uniform way to express any kind of computation. This makes them amenable to mathematical analysis, something the pure-FP model has difficulty with (you'll note that when analyzing pure FP, various ad-hoc frameworks need to be invented, e.g., a message-passing model for concurrency etc.).