/-
Copyright (c) 2022 E.W.Ayers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: E.W.Ayers
-/
import Lean
import Mathlib.Lean.Expr.Basic
/-!
# Traversal functions for expressions.
-/
namespace Lean.Expr
/-- Maps `f` on each immediate child of the given expression. -/
def traverseChildren [Applicative M: ?m.5
M] (f : Expr: Type
Expr → M: ?m.5
M Expr: Type
Expr) : Expr: Type
Expr → M: ?m.5
M Expr: Type
Expr
| e: Expr
e@(forallE: Name → Expr → Expr → BinderInfo → Expr
forallE _ d: Expr
d b: Expr
b _) => pure e: Expr
e.updateForallE! <*> f d: Expr
d <*> f b: Expr
b
| e: Expr
e@(lam: Name → Expr → Expr → BinderInfo → Expr
lam _ d: Expr
d b: Expr
b _) => pure e: Expr
e.updateLambdaE! <*> f d: Expr
d <*> f b: Expr
b
| e: Expr
e@(mdata _ b: Expr
b) => e: Expr
e.updateMData! <$> f b: Expr
b
| e: Expr
e@(letE _ t: Expr
t v: Expr
v b: Expr
b _) => pure e: Expr
e.updateLet! <*> f t: Expr
t <*> f v: Expr
v <*> f b: Expr
b
| e: Expr
e@(app l: Expr
l r: Expr
r) => pure e: Expr
e.updateApp! <*> f l: Expr
l <*> f r: Expr
r
| e: Expr
e@(proj _ _ b: Expr
b) => e: Expr
e.updateProj! <$> f b: Expr
b
| e: Expr
e => pure e: Expr
e
/-- `e.foldlM f a` folds the monadic function `f` over the subterms of the expression `e`,
with initial value `a`. -/
def foldlM {α: Type
α : Type: Type 1
Type} {m: ?m.2091
m} [Monad m: ?m.2091
m] (f: α → Expr → m α
f : α: Type
α → Expr: Type
Expr → m: ?m.2091
m α: Type
α) (x: α
x : α: Type
α) (e: Expr
e : Expr: Type
Expr) : m: ?m.2091
m α: Type
α :=
Prod.snd <$> (StateT.run (e: Expr
e.traverseChildren $ fun e': ?m.2170
e' =>
Functor.mapConst e': ?m.2170
e' (get: {σ : outParam (Type ?u.2257)} → {m : Type ?u.2257 → Type ?u.2256} → [self : MonadState σ m] → m σ
get >>= monadLift ∘ flip f: α → Expr → m α
f e': ?m.2170
e' >>= set: {σ : semiOutParam (Type ?u.2412)} → {m : Type ?u.2412 → Type ?u.2411} → [self : MonadStateOf σ m] → σ → m PUnit
set)) x: α
x : m _: Type
_)
end Lean.Expr