question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reader monad transform one env to another

See original GitHub issue

Description

Hi, I’ d like to chain two reader monads, where one env is subtype of the other, so inner function can access only type it requires and outer function can requires general env param.

Repro steps

type Env = {Now: DateTime; Conn: string}

type InnerEnv = {Now: DateTime}

let inner () =
    monad {
        let! (e:InnerEnv) = Reader.ask
        return doWork e.Now
    }

let outer () =
    monad {
        let! (e:Env) = Reader.ask
        let! m = Reader (fun p -> {Now = p.Now})
        let! r = inner()
        let! m = Reader.local (fun (x:Env) -> {Now = x.Now})
        return r
    }

Expected behavior

Expected result is function outer with signature Reader<Env, Result>

Actual behavior

Doesn’t compile

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
padzikmcommented, Nov 17, 2021

I found solution:

type Env = {Now: DateTime; Conn: string}

type InnerEnv = {Now: DateTime}

let inner () =
    monad {
        let! (e:InnerEnv) = Reader.ask
        return doWork e.Now
    }

let outer () =
    monad {
        let! r1 = inner() |> Reader.local (fun (x:Env) -> {Now = x.Now})
        let! r2 = otherInner() |> Reader.local (fun (x:Env) -> {Conn = x.Conn})
        return r1, r2
    }
0reactions
gustycommented, Nov 17, 2021

I see, it looks like I misunderstood your problem.

I was thinking in having a generic env with some constraints, and I came up once with a solution which requires SRTP accessor functions for each property, which is something like:

let inline getConn x = (^T : (member Conn : _) x)

but as I said, this is not what you need.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get ReaderT to work with another monad ...
1 Answer 1 ... I've tested it, and it at least works. Whether or not this is best practices is unknown to me....
Read more >
Control.Monad.Trans.Reader
Execute a computation in a modified environment (a specialization of withReaderT ). runReader ( withReader f m) = runReader m . f. The...
Read more >
Note to self: reader monad transformer
A Reader is a data type that encapsulates an environment. The runReader function takes an environment (a Reader ) and runs it, producing...
Read more >
Making Sense of Multiple Monads
In a monad transformer, the lift function allows you to run actions in the underlying monad. So using lift in the ReaderT Env...
Read more >
Monad Transformers
Monad transformers allow us to combine the features of different monads in our program. This concept allows us to write extremely flexible programs....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found