fp-ts: On free monad, tagless-final and error handling
Hi. I’m trying to use Free
to describe a program that exists always in terms of an Either<L, A>
. All my instructions are binary in that way, e.g.
export class CountAvailableUnits<L, A> {
public readonly _tag: "CountAvailableUnits" = "CountAvailableUnits";
public readonly _A!: A;
public readonly _URI!: InstructionURI;
constructor(
readonly id: string;
readonly more: (a: Either<L, number>) => Either<L, A>,
) {}
}
When I attempt to foldFree(either)(interpreter, program).value
I end up with Either<{}, Either<L, A>>
. I can’t figure out the secret sauce to make foldFree
do what I want. It looks like it should, based on the signature of FoldFree2
, but this is beyond my ability to debug. Any ideas?
Thanks
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 34 (20 by maintainers)
Disclaimer: this is pretty long…
Let’s say we want to implement the following program
Scenario 1: using mtl / finally tagless
First of all let’s define the domain model and the capabilities
then the main program
In order to test the program we must define a test instance of
MonadApp
, so first of all let’s define an overloading ofgetMain
for theState
monadNow we can define a instance for
State
Finally we can actually test the program with different initial states
Scenario 2: using concrete types
Let’s still define the capabilities but with concrete types
and then the main program
How can I test this program? We need a test instance for
MonadApp
and I’ll use IORef for this@leemhenson In both scenarios, using Reader instead of passing
MonadApp
(or the other static dictionaries) manually seems a good optionActually the problem with u2 already exist with today s chain implementation.
I mean someone using the current chain api with a Left of
ErrorSubsystem1
and chaining on a function returning aErrorSubsystem2
will detect nothing due to the structural nature of TS like @peterhorne said.So switching to that new Error consolidation scheme would not be a regression.
Is it correct @gcanti ?
In the case of
u2
they are “the same” type due to the fact that Typescript is structurally typed. That doesn’t mean that it’s unsafe, does it?AFAIK finally tagless and mtl are (kind of) synonyms
Yes, I feel your pain, while in Haskell/PureScript (or Scala) looks like a no brainer, in TypeScript there’s a lot of ceremonies. Perhaps it might worth it in a library but it’s a hard sell in the application code
I’m trying to find a good balance as well. I’ll try to collect my thoughts, I need some time though.
Using Reader seems a good option to avoid passing around the static dictionaries by hand, however I think that is tangential to the heart of the problem which is to abstract over the effect
M
of a general effectful program (I might be wrong though, I’ll think about it)