Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Fabio Labella
@SystemFw
but yeah, at the end of the day you need suspension
because you want to separate evaluation and execution
that being said, this is trying to understand (imho) how IO is implemented
rather than its semantics
Vinayak Pathak
@vinayakpathak
i see
Drew Boardman
@drewboardman
is he saying def is not RT because it's lazy and not memoized?
because I don't see how that follows
Fabio Labella
@SystemFw
no
we're trying to discuss where def is enough to ensure referential transparency, which it isn't
Vinayak Pathak
@vinayakpathak
def + by value is not referentially transparent because you will evaluate the arguments prematurely
Fabio Labella
@SystemFw
exactly
def + by name is, and in that model val is the equivalent of <-
but the whole thing is then easier to understand by considering IO[A] = () => A , which has the limitations outlined above
(you can replace all def t: A with val t: () => A)
Paul Snively
@paul-snively
Maybe it's worth looking at the implementation of Id, which you might think of as "the fake monad."
Drew Boardman
@drewboardman
Id is a datatype
Paul Snively
@paul-snively
So is IO, and they both have Monad instances.
Among others.
The point is, you can compare what happens in various scenarios in code using Id and in code using IO, and see how they differ (because the latter takes great care to satisfy various laws under a broad range of operational conditions, and the former doesn't).
Fabio Labella
@SystemFw

https://github.com/SystemFw/Scala-World-2019/blob/master/Examples.scala

Here there are a few implementations of IO, the first one of which is essentially with () => A. But I do think that looking at the implementation is not the right way to understand IO at first

which is great, it means we actually have an abstraction
Christopher Davenport
@ChristopherDavenport
I’m missing a lot of context. But Id is by no means a fake monad.
Paul Snively
@paul-snively
Looking at the implementation of IO is a horrible way to understand it, I think.
Vinayak Pathak
@vinayakpathak
@SystemFw nice, that's great. that answers a lot of my questions. I'm probably going to start thinking of IO as () => A until I realize it's a huge simplification
Fabio Labella
@SystemFw
well, I wouldn't recommend that :)
Paul Snively
@paul-snively
@ChristopherDavenport: Sure. I should have put "fake" in scare-quotes, at the very least.
Fabio Labella
@SystemFw
this is a general point about FP btw
trying to start using algebraic thinking
it's actually the biggest benefit imho, one of the ultimate goals
and it's hard to get your head around that at first
switching mindset from executing actions to composing programs
and I think that the earlier you start trying, the better
Paul Snively
@paul-snively
@vinayakpathak: Just to give one example: the () => A model certainly doesn't give you IO(1) |+| IO(1).
Christopher Davenport
@ChristopherDavenport
Or race, or async, or continual. Without even considering +Effect
Or for that matter Bracket
Fabio Labella
@SystemFw
IO[A] is a program. Option[A] is a program. These programs can be composed using certain operations. You build your code by building mini-programs, assembling bigger programs out of small ones, and translating programs from one "language" to another (eg. fs2.Stream to IO, or Option to Either)
Vinayak Pathak
@vinayakpathak
@SystemFw yeah, but with IO you get to write things that look like they are about executing actions but they're really about composing programs... so it's a little confusing
Fabio Labella
@SystemFw
I have a suggestion for that
avoid for
completely avoid it for the time being
Vinayak Pathak
@vinayakpathak
ah interesting
Fabio Labella
@SystemFw
use explicit flatMap and other combinators
for prioritises brevity over clarity, and when trying to form a clear semantic mental model it's unhelpful
Vinayak Pathak
@vinayakpathak
i'm gonna try that
Fabio Labella
@SystemFw
(note that the same ambiguity happens with something as simple as Option as well, but one only starts thinking about it with more powerful types like IO etc.)
but do try that, and do try to forget entirely how things are implemented
(for the time being at least)
you can actually do it with something as simple as Option first
Paul Snively
@paul-snively
Also, avoiding monads is helpful. Then you can't use for.
Fabio Labella
@SystemFw
forget about Option being a datatype made by Some and None, and don't use pattern matching