Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set priority in monadic class instances #6725

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/examples/widgets.lean
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def getType (params : GetTypeParams) : RequestM (RequestTask CodeWithInfos) :=
runTermElabM snap do
let name ← resolveGlobalConstNoOverloadCore params.name
let c ← try getConstInfo name
catch _ => throwThe RequestError ⟨.invalidParams, s!"no constant named '{name}'"⟩
catch _ : Exception => throwThe RequestError ⟨.invalidParams, s!"no constant named '{name}'"⟩
Widget.ppExprTagged c.type

/-!
Expand Down
2 changes: 1 addition & 1 deletion src/Init/Control/Except.lean
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ instance (m : Type u → Type v) (ε₁ : Type u) (ε₂ : Type u) [MonadExceptO
tryCatch x handle := ExceptT.mk <| tryCatchThe ε₁ x handle

@[always_inline]
instance (m : Type u → Type v) (ε : Type u) [Monad m] : MonadExceptOf ε (ExceptT ε m) where
instance (priority := high) (m : Type u → Type v) (ε : Type u) [Monad m] : MonadExceptOf ε (ExceptT ε m) where
throw e := ExceptT.mk <| pure (Except.error e)
tryCatch := ExceptT.tryCatch

Expand Down
2 changes: 1 addition & 1 deletion src/Init/Control/ExceptCps.lean
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ instance : Monad (ExceptCpsT ε m) where
instance : LawfulMonad (ExceptCpsT σ m) := by
refine LawfulMonad.mk' _ ?_ ?_ ?_ <;> intros <;> rfl

instance : MonadExceptOf ε (ExceptCpsT ε m) where
instance (priority := high) : MonadExceptOf ε (ExceptCpsT ε m) where
throw e := fun _ _ k => k e
tryCatch x handle := fun _ k₁ k₂ => x _ k₁ (fun e => handle e _ k₁ k₂)

Expand Down
2 changes: 1 addition & 1 deletion src/Init/Control/Option.lean
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ instance : MonadFunctor m (OptionT m) := ⟨fun f x => f x⟩
let some a ← x | handle ()
pure a

instance : MonadExceptOf Unit (OptionT m) where
instance (priority := low) : MonadExceptOf Unit (OptionT m) where
throw := fun _ => OptionT.fail
tryCatch := OptionT.tryCatch

Expand Down
2 changes: 1 addition & 1 deletion src/Init/Control/State.lean
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def ForM.forIn [Monad m] [ForM (StateT β (ExceptT β m)) ρ α]
section
variable {σ : Type u} {m : Type u → Type v}

instance [Monad m] : MonadStateOf σ (StateT σ m) where
instance (priority := high) [Monad m] : MonadStateOf σ (StateT σ m) where
get := StateT.get
set := StateT.set
modifyGet := StateT.modifyGet
Expand Down
13 changes: 7 additions & 6 deletions src/Init/Prelude.lean
Original file line number Diff line number Diff line change
Expand Up @@ -2997,7 +2997,8 @@ variable {ε : Type u} {m : Type v → Type w}
@[inline] protected def orElse [MonadExcept ε m] {α : Type v} (t₁ : m α) (t₂ : Unit → m α) : m α :=
tryCatch t₁ fun _ => t₂ ()

instance [MonadExcept ε m] {α : Type v} : OrElse (m α) where
/- (priority := low) -/
instance (priority := 100) [MonadExcept ε m] {α : Type v} : OrElse (m α) where
orElse := MonadExcept.orElse

end MonadExcept
Expand Down Expand Up @@ -3027,7 +3028,7 @@ namespace ReaderT
section
variable {ρ : Type u} {m : Type u → Type v} {α : Type u}

instance : MonadLift m (ReaderT ρ m) where
instance : MonadLift m (ReaderT ρ m) where
monadLift x := fun _ => x

@[always_inline]
Expand Down Expand Up @@ -3123,7 +3124,8 @@ instance (ρ : Type u) (m : Type u → Type v) [MonadReaderOf ρ m] : MonadReade
instance {ρ : Type u} {m : Type u → Type v} {n : Type u → Type w} [MonadLift m n] [MonadReaderOf ρ m] : MonadReaderOf ρ n where
read := liftM (m := m) read

instance {ρ : Type u} {m : Type u → Type v} [Monad m] : MonadReaderOf ρ (ReaderT ρ m) where
/- (priority := high) -/
instance (priority := 10000) {ρ : Type u} {m : Type u → Type v} [Monad m] : MonadReaderOf ρ (ReaderT ρ m) where
read := ReaderT.read

/--
Expand Down Expand Up @@ -3160,7 +3162,8 @@ instance (ρ : Type u) (m : Type u → Type v) [MonadWithReaderOf ρ m] : MonadW
instance {ρ : Type u} {m : Type u → Type v} {n : Type u → Type v} [MonadFunctor m n] [MonadWithReaderOf ρ m] : MonadWithReaderOf ρ n where
withReader f := monadMap (m := m) (withTheReader ρ f)

instance {ρ : Type u} {m : Type u → Type v} : MonadWithReaderOf ρ (ReaderT ρ m) where
/- (priority := high) -/
instance (priority := 10000) {ρ : Type u} {m : Type u → Type v} : MonadWithReaderOf ρ (ReaderT ρ m) where
withReader f x := fun ctx => x (f ctx)

/--
Expand Down Expand Up @@ -3246,8 +3249,6 @@ of the state. It is equivalent to `get <* modify f` but may be more efficient.
def getModify {σ : Type u} {m : Type u → Type v} [MonadState σ m] (f : σ → σ) : m σ :=
modifyGet fun s => (s, f s)

-- NOTE: The Ordering of the following two instances determines that the top-most `StateT` Monad layer
-- will be picked first
@[always_inline]
instance {σ : Type u} {m : Type u → Type v} {n : Type u → Type w} [MonadLift m n] [MonadStateOf σ m] : MonadStateOf σ n where
get := liftM (m := m) MonadStateOf.get
Expand Down
8 changes: 4 additions & 4 deletions src/Lean/Elab/Match.lean
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ open Lean.Elab.Term.Quotation in
for (pats, rhs) in patss.zip rhss do
let vars ← try
getPatternsVars pats
catch | _ => return -- can happen in case of pattern antiquotations
catch _ => return -- can happen in case of pattern antiquotations
Quotation.withNewLocals (getPatternVarNames vars) <| precheck rhs
| _ => throwUnsupportedSyntax

Expand Down Expand Up @@ -346,9 +346,9 @@ private def elabPatterns (patternStxs : Array Syntax) (matchType : Expr) : Excep
| some path =>
restoreState s
-- Wrap the type mismatch exception for the "discriminant refinement" feature.
throwThe PatternElabException { ex := ex, patternIdx := idx, pathToIndex := path }
| none => restoreState s; throw ex
| none => throw ex
throw { ex := ex, patternIdx := idx, pathToIndex := path }
| none => restoreState s; throwThe Exception ex
| none => throwThe Exception ex
matchType := b.instantiate1 pattern
patterns := patterns.push pattern
| _ => throwError "unexpected match type"
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Elab/PreDefinition/Eqns.lean
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private def shouldUseSimpMatch (e : Expr) : MetaM Bool := do
let args := e.getAppArgs
for discr in args[info.getFirstDiscrPos : info.getFirstDiscrPos + info.numDiscrs] do
if (← Meta.isConstructorApp discr) then
throwThe Unit ()
throw ()
return (← (find e).run) matches .error _

partial def mkEqnTypes (declNames : Array Name) (mvarId : MVarId) : MetaM (Array Expr) := do
Expand Down
4 changes: 2 additions & 2 deletions src/lake/Lake/Util/Cycle.lean
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ instance inhabitedOfMonadCycle [MonadCycle κ m] : Inhabited (m α) := ⟨throwC
/-- A transformer that equips a monad with a `CallStack`. -/
abbrev CallStackT κ m := ReaderT (CallStack κ) m

instance [Monad m] : MonadCallStackOf κ (CallStackT κ m) where
instance (priority := high) [Monad m] : MonadCallStackOf κ (CallStackT κ m) where
getCallStack := read
withCallStack s x := x s

/-- A transformer that equips a monad with a `CallStack` to detect cycles. -/
abbrev CycleT κ m := CallStackT κ <| ExceptT (Cycle κ) m

instance [Monad m] : MonadCycleOf κ (CycleT κ m) where
instance (priority := high) [Monad m] : MonadCycleOf κ (CycleT κ m) where
throwCycle := throw

/--
Expand Down
4 changes: 2 additions & 2 deletions src/lake/Lake/Util/EStateT.lean
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ protected def get [Pure m] : EStateT ε σ m σ := fun s =>
protected def modifyGet [Pure m] (f : σ → Prod α σ) : EStateT ε σ m α := fun s =>
match f s with | (a, s) => pure <| .ok a s

instance [Pure m] : MonadStateOf σ (EStateT ε σ m) where
instance (priority := high) [Pure m] : MonadStateOf σ (EStateT ε σ m) where
set := EStateT.set
get := EStateT.get
modifyGet := EStateT.modifyGet
Expand All @@ -201,7 +201,7 @@ protected def tryCatch [Monad m] (x : EStateT ε σ m α) (handle : ε → EStat
| .error e s => handle e s
| ok => pure ok

instance [Monad m] : MonadExceptOf ε (EStateT ε σ m) where
instance (priority := high) [Monad m] : MonadExceptOf ε (EStateT ε σ m) where
throw := EStateT.throw
tryCatch := EStateT.tryCatch

Expand Down
4 changes: 2 additions & 2 deletions tests/lean/run/catchThe.lean
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def testM {α} [BEq α] [ToString α] (x : M α) (expected : α) : MetaM Unit :
| Except.error e => throwError m!"FAILED: {e}"

@[noinline] def act1 : M Nat :=
throw <| Exception.error Syntax.missing "Error at act1"
throwThe Exception <| Exception.error Syntax.missing "Error at act1"

def g1 : M Nat :=
tryCatchThe Exception
Expand All @@ -25,7 +25,7 @@ def g1 : M Nat :=
#eval testM g1 200

@[noinline] def act2 : M Nat :=
throwThe String "hello world"
throw "hello world"

def g2 : M Nat :=
tryCatchThe Exception
Expand Down
Loading