lean4-htt/src/Init/Control
Leonardo de Moura 9af0a0e18b feat: add withReader method
@Kha `withReader` is a well-behaved version of `adaptReader`. `adaptReader` is
too general, and it often produces counterintuitive elaboration
errors.

Here are two super annoying issues I hit all the time:
1- `adaptReader` + polymorphic code
```
def ex1 : ReaderT Nat IO Unit :=
adaptReader (fun x => x + 1) $
  IO.println "foo" -- 3 Errors here failed to synthesize `Monad ?m` and  `MonadIO ?m`, and don't know how to synthesize `Type → Type`
```

2- `adaptReader` and notation that requires the expected type
```
structure Context :=
(x y : Nat)

def ex2 : ReaderT Context IO Nat :=
adaptReader (fun s => { s with x := 10 }) $ -- Error at the structure instance
  ...
```
In the example above, I have to write `fun (s : Context) => ...` to
fix the problem.

The two problems above happen in the old and new frontends. However,
there is a new problem specific for the new frontend. In the new
frontend, a `do` is only elaborated when the expected type is known.
So, `adaptReader (fun ctx => ...) do ...` seldom works :(

As I said above, the issue is that `adaptReader` is too general. Its
type is
```
  {ρ ρ' : Type u_1} → {m m' : Type u_1 → Type u_2} → [MonadReaderAdapter ρ ρ' m m'] → {α : Type u_1} → (ρ' → ρ) → m α → m' α
```

`withReader` is a simpler version of `adaptReader`
```
withReader : {ρ : Type u_1} → {m : Type u_1 → Type u_2} → [MonadWithReader ρ m] → {α : Type u_1} → (ρ → ρ) → m α → m α
```
It doesn't have any of the problems above. Moreover, I managed to replace
every single instance of `adaptReader` with `withReader` at the stdlib
and tests. We don't need the `adaptReader` generality.
2020-10-13 15:00:17 -07:00
..
Alternative.lean feat: add missing features to do notation parser 2020-09-30 06:51:25 -07:00
Applicative.lean chore: fix stdlib 2020-05-12 15:02:03 -07:00
Conditional.lean chore: avoid ^do ... 2019-12-11 06:19:12 -08:00
EState.lean feat: add MonadFinally 2020-08-25 17:58:35 -07:00
Except.lean feat: add interpolated string for toString 2020-10-09 14:38:24 -07:00
Functor.lean chore: remove <$ and $> notation 2020-06-15 14:52:31 -07:00
Id.lean chore: add helper instance 2020-10-05 11:24:35 -07:00
Monad.lean fix: remove bad instances 2020-09-17 17:31:09 -07:00
MonadControl.lean chore: split Lift.lean into MonadLift.lean, MonadFunctor.lean, and MonadRun.lean 2020-08-26 08:34:35 -07:00
MonadFunctor.lean chore: split Lift.lean into MonadLift.lean, MonadFunctor.lean, and MonadRun.lean 2020-08-26 08:34:35 -07:00
MonadLift.lean chore: split Lift.lean into MonadLift.lean, MonadFunctor.lean, and MonadRun.lean 2020-08-26 08:34:35 -07:00
MonadRun.lean chore: split Lift.lean into MonadLift.lean, MonadFunctor.lean, and MonadRun.lean 2020-08-26 08:34:35 -07:00
Option.lean chore: split Lift.lean into MonadLift.lean, MonadFunctor.lean, and MonadRun.lean 2020-08-26 08:34:35 -07:00
Reader.lean feat: add withReader method 2020-10-13 15:00:17 -07:00
State.lean feat: add MonadFinally 2020-08-25 17:58:35 -07:00
StateRef.lean feat: add MonadFinally 2020-08-25 17:58:35 -07:00