Zulip Chat Archive

Stream: lean4

Topic: nested ReaderT


David Renshaw (Oct 23 2022 at 00:52):

If I'm in the TacticM monad, I get access the Tactic.Context by doing ← read.
But how do I access the inner Term.Context, from the inner TermElabM monad?

Mario Carneiro (Oct 23 2022 at 01:33):

let termCtx <- readThe Term.Context

Mario Carneiro (Oct 23 2022 at 01:34):

alternatively <- liftM read

Mario Carneiro (Oct 23 2022 at 01:35):

Also, many fields of the context are provided as separate functions like getInfoTrees, and those work even if there are more reader layers afterward

David Renshaw (Oct 23 2022 at 01:38):

Cool, this works.

     let context  readThe Lean.Elab.Term.Context

David Renshaw (Oct 23 2022 at 01:38):

But <- liftM read does not seem to work.

David Renshaw (Oct 23 2022 at 01:38):

ah, need parens

David Renshaw (Oct 23 2022 at 01:39):

let context (← liftM) read

Mario Carneiro (Oct 23 2022 at 01:39):

wait that looks wrong

Mario Carneiro (Oct 23 2022 at 01:39):

let context <- liftM read

David Renshaw (Oct 23 2022 at 01:40):

oh, right, my liftM thing doesn't work. There's a red squiggle on the next line now...

Mario Carneiro (Oct 23 2022 at 01:40):

Note that this will just get the second reader in the stack, which is probably the TermElabM context

Mario Carneiro (Oct 23 2022 at 01:40):

you have to do let context <- liftM <| liftM <| liftM read to go deeper

David Renshaw (Oct 23 2022 at 01:41):

For yours, I get the error typeclass instance problem is stuck, it is often due to metavariables

Mario Carneiro (Oct 23 2022 at 01:41):

which typeclass problem? what code?

Mario Carneiro (Oct 23 2022 at 01:42):

#mwe

David Renshaw (Oct 23 2022 at 01:46):

import Lean

open Lean Elab.Tactic Parser.Tactic Lean.Meta

def foo : TacticM Unit :=
  do let ctx  readThe Elab.Term.Context -- works
     let ctx  liftM read
-- typeclass instance problem is stuck, it is often due to metavariables
--  MonadLiftT (?m.265 ctx) TacticM
--
     pure ()

Mario Carneiro (Oct 23 2022 at 01:48):

hm, I thought that worked without additional hints. You can do it (even less, in fact) with a type ascription:

import Lean

open Lean Elab.Tactic Elab.Term Parser.Tactic Lean.Meta

def foo : TacticM Unit := do
  let ctx  (read : TermElabM _)

David Renshaw (Oct 23 2022 at 01:50):

hah, I had tried this unsuccessfully:

     let (ctx: Elab.Term.Context)  read

Mario Carneiro (Oct 23 2022 at 01:50):

yeah you need to use readThe for that version to work

David Renshaw (Oct 23 2022 at 01:52):

Ah, and I see I can add the liftM if I really want to

  let ctx  liftM (read : Elab.TermElabM _)

David Renshaw (Oct 23 2022 at 01:52):

I like readThe. :thumbs_up:


Last updated: Dec 20 2023 at 11:08 UTC