Comments (4)
Shortly speaking, it's all about trampolining.
A bit more details:
I've done some archaeology, and found the initial PR that introduced StateT as F[S => F[(S, A)]]
. After many years of development, it transformed into IndexedStateT
having the F[(SA) ⇒ F[(SB, A)]]
signature. That PR has some reasoning about this outer effect.
State wraps F[S => F[(S, A)]] instead of the more familiar S => F[(S, A)]. This allows for effective trampolining.
Also, you might want to dig into the original issue where there was quite a detailed discussion on that outer effect.
It's better to have veterans answer this, but I may guess there wasn't an objective to have aligned implementation with the theory. In that sense, in cats could be found many other occurrences.
from cats.
I think it was 100% to improve stack safety.
The outer F[_]
is always the same as pure in our constructions I think. We do test the laws, but of course it's possible we've missed something.
For our State monad instance we use this except with F = Eval which is a somewhat optimized implementation of a free monad.
from cats.
The main issue I have is that while it is probably sound to use it for trampolining, any other effects in that outer F will be applied rather out of order, and you don't really have any laws locking you out of doing such at present, no? Would it make sense to introduce a base trampolining monad to use in that place instead rather than force everything through the entire underlying monad stack twice without using any of the other parts? e.g. error handling, etc.
from cats.
I think the first step might be to construct a law or test we think should pass and doesn't. From that concrete example we might have more ideas.
We are under a few constraints:
- due to the way the scala/jvm ecosystem works, generally we compose classpaths from large sets of transitive dependencies, often which are not on a single version of cats. So, this is why we highly value not breaking binary compatibility: so when you use libraries that already depend on cats together they tend to work. Thus, we can add laws to our testing or laws package somewhat easily, but we can't easily change type signatures since they often invalidate the binary compatibility.
- we are very interested in actually using the code, so the practical issue of stack safety it very important to us. Long ago, before various stack safety tricks had been found, using functional patterns on the JVM often resulted in stack overflow errors.
If we were unconstrained by 1. we could make the constructor for the class private, so users could never observe the outer F[_]
.
That said, I'm not sure how it being there harms anything.
As to your point:
Would it make sense to introduce a base trampolining monad to use in that place instead rather than force everything through the entire underlying monad stack twice without using any of the other parts?
We definitely could have done another direct encoding I think, by using a Free structure here and running an interpreter on that in the end, but at this time point 2 from above will be an issue: that will almost certainly break binary compatibility now.
I think withing our constraints (binary compatibility, stack safety and then performance, mostly in that order), we are open to making improvements.
from cats.
Related Issues (20)
- `Apply` syntax is allocating HOT 2
- add `sizeIs` method like std-lib to `cats.Reducible` and/or NonEmpty collections HOT 1
- Maybe you are looking for `as`? :) HOT 2
- Fix documentation (or design) of Free Monad
- Type-classes for Prepend (Cons) and Append (Snoc) operations HOT 5
- Scala 3: No given instance of type cats.Applicative[[_] =>> Any] was found for parameter F of method apply in class PurePartiallyApplied
- Unusual EqLaws definition HOT 4
- Outdated Link in Docs - Seeking Guidance for Fix HOT 1
- algebra-core/src HOT 1
- Option size method implicitly selected from UnorderedFoldable HOT 2
- .splitWhen
- Difference in the Applicative inferred for Seq[Seq[?]]#sequence between Scala 2 and 3
- Inconsistent behaviour when using Eval as Applicative
- `Tuple1SemigroupalOps` methods have different names from other `TupleNSemigroupalOps` classes HOT 3
- Instances for Currency HOT 7
- EitherT[Option, ?, ?]] can't be used as a bifunctor HOT 5
- trait EuclidianRing should not be a Ring HOT 4
- OutOfMemoryError when IO.uncancelable is used in recursive function HOT 1
- ambiguous implicit resolution of `Show.ContravariantShow[immutable.SortedMap[K, V]]` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cats.