3 ms·
> It doesn't tell me which of the calls is effectful and which is pure. But it does because their type says so: String read() effect IO (where `effect` is
by pron 11y ago
> It doesn't tell me which of the calls is effectful and which is pure.
But it does because their type says so:
String read() effect IO
(where `effect` is just like Java's checked `throws`)
> what is the type of a? "(String effects IO)" or some such?
Right. It is as I said, a type on read, not on a. `a` is still `String`.
> only allowed inside the scope of a method with IO as an effect then it's the same problem as checked exceptions.
But that's the opposite of a problem. That's like using `IO a` outside of an IO monad. You want the function to only be called in the right context (an appropriate handler is in effect).
> it doesn't match my experience.
Fair enough, and you're not alone, but here's the relevant discussion (about 4 minutes): https://youtu.be/iHHSa39p48I?t=8h14m11s https://youtu.be/iHHSa39p48I?t=8h14m11s
> Since you're emphasizing it I'll ask: how do you express effects that don't commute?
The same way:
void foo() effects Either<Writer<Log>>
etc.. (this doesn't work with Java's current type system, which doesn't allow generic exception types, though).
I'm not sure if the same effects would make sense, though. For example, Either is a monadic value which doesn't make sense in an imperative setting.
This works because you can have:
<T> T check(T x) effects MyEffect<T>
- lmm 11y ago> But it does because their type says so: Sure. But I remember doing this with checked exceptions, mousing over each method call one by one to figure out which ones could or couldn't throw. It's much nicer if you can see which methods are the throwey ones immediately - =/<- is a real sweet spot in terms of having the difference be visible but not overly intrusive. > But that's the opposite of a problem. That's like using `IO a` outside of an IO monad. You want the function to only be called in the right context (an appropriate handler is in effect). Disagree. I want to be able to have a value of type IO a outside a method - e.g. in a static field. More generally it's really huge to be able to just treat these things as ordinary values that I can manipulate using the ordinary rules of the language. > I'm not sure if the same effects would make sense, though. For example, Either is a monadic value which doesn't make sense in an imperative setting. The two effects are "possibly skip the rest of the computation, returning an Error instead" and "record a programatically-accessible Log" - I think those both make sense. The point is the reason you have to use the horrible monad transformers is that the order in which you evaluate these effects matters - if there's an early error, are later log statements recorded, or not? There are more complicated cases when e.g. catching I/O errors.
- pron 11y ago> It's much nicer if you can see which methods are the throwey ones immediately - =/<- is a real sweet spot in terms of having the difference be visible but not overly intrusive. Well, I guess that's a matter of taste, but 1/ what information does "effectful" convey if you don't know what the effect is until you mouse-over? 2/ I think that's a very, very low price (if it is a price at all) to pay for something that fits much better with non-pure languages. > I want to be able to have a value of type IO a outside a method - e.g. in a static field But continuation effects don't have `IO a`, just `a`. The effect comes with the context. An `IO a` would translate to a lazy value, namely a function, which could be a continuation. There's nothing stopping you from storing a continuation in a field. You can manipulate the `a` just like any value, and the "IO" with handlers -- just as you do with monads. > possibly skip the rest of the computation, returning an Error instead Yes, but that's not all Either says. It says "return an error or a value of type a". In imperative code that makes less sense. What you want to say is: "throw an exception or don't (and return whatever value it is that the function returns)". The type of the normal return value is not recorded in the Either. > The point is the reason you have to use the horrible monad transformers is that the order in which you evaluate these effects matters You need monad transformers not because of ordering, but because monads don't generally compose. Or it's a bit reversed: because monads don't compose they are sensitive to accidental ordering (whether you care about the order or not), which makes monad transformers tricky. With continuations you have the choice of when to be sensitive to order and when not to. For example, in the imperative case, you could encode the information "if there's an early error, are later log statements recorded, or not?" directly in the type, (I think) but I don't see any reason to. In Haskell you must because the types need to be nested in one particular way according to the transformer. In any case, my claim is as follows: if you generally prefer the imperative way of doing effects (like in Java, OCaml, F#, Scheme, JS, Python, Clojure, Erlang etc.) -- as I do -- then continuations are far more natural than monads. If you prefer the PFP way, you should stick to monads (or if using Haskell, must stick to monads).
- lmm 11y ago> An `IO a` would translate to a lazy value, namely a function, which could be a continuation. There's nothing stopping you from storing a continuation in a field. You can manipulate the `a` just like any value, and the "IO" with handlers -- just as you do with monads. I think the Java world is finally accepting that methods are not a substitute for first-class values; one of the biggest remaining problems with Java lambdas is that it's awkward to use them with checked exceptions (e.g. do you to create a named @FunctionalInterface for each input/output/exception combination you want to store? You can use generics but they don't let you abstract over arity). Handling effects this way would presumably have the same problems? Put it this way: Either[MyError, A] is much more useful to me than a checked MyException, because I can use the former as a value whereas the latter I can only throw from methods. That's the difference I'm worried about here. > You need monad transformers not because of ordering, but because monads don't generally compose. Or it's a bit reversed: because monads don't compose they are sensitive to accidental ordering (whether you care about the order or not), which makes monad transformers tricky. With continuations you have the choice of when to be sensitive to order and when not to. What does the choice look like in code then? Under monads I'd write: Left(MyError()).liftM[WriterT[...]] WriterT.tell("Here".point[...]) or EitherT.left(MyError().point[...]) Writer.tell("Here").liftM[EitherT[...]] and while it's clunky it's clear which one's which. Under your approach if I write: throwc(new MyException()); add(Writer.class, "Here"); then which semantics do I get, and how do I get the other one?