@@ -44,7 +44,7 @@ public meta section
4444open Lean Meta Elab Term
4545
4646/-- Compute the chain of delta-unfoldings starting from `e` at default transparency.
47- Returns all intermediate forms including `e` itself (unless `skipHead` is true). -/
47+ Returns all intermediate forms including `e` itself (unless `skipHead` is true). -/
4848private def unfoldChain (e : Expr) (skipHead : Bool := false ) :
4949 MetaM (Array Expr) := do
5050 let mut out : Array Expr := #[]
@@ -57,7 +57,7 @@ private def unfoldChain (e : Expr) (skipHead : Bool := false) :
5757 return out
5858
5959/-- Add all unfoldings of `e` to `acc` as replacement sources mapping to `target`.
60- If `skipHead` is true, the first element (i.e. `e` itself) is not added. -/
60+ If `skipHead` is true, the first element (i.e. `e` itself) is not added. -/
6161private def addUnfoldings (acc : Array (Expr × Expr)) (e target : Expr)
6262 (skipHead : Bool := false ) : MetaM (Array (Expr × Expr)) := do
6363 let chain ← unfoldChain e (skipHead := skipHead)
@@ -68,8 +68,8 @@ private def addUnfoldings (acc : Array (Expr × Expr)) (e target : Expr)
6868 return acc
6969
7070/-- Build the replacement table for differing arguments between `sourceType` and
71- `expectedType`. For each differing argument position, all unfoldings of both the
72- source and expected arguments are mapped to the expected argument. -/
71+ `expectedType`. For each differing argument position, all unfoldings of both the
72+ source and expected arguments are mapped to the expected argument. -/
7373private def buildReplacements (sourceType expectedType : Expr) :
7474 MetaM (Array (Expr × Expr)) := do
7575 let sourceArgs := sourceType.getAppArgs
@@ -84,7 +84,7 @@ private def buildReplacements (sourceType expectedType : Expr) :
8484 return replacements
8585
8686/-- Check whether `e` is defeq (at `default` transparency) to any source expression
87- in `replacements`. Returns the target if found. -/
87+ in `replacements`. Returns the target if found. -/
8888private def matchesAnyDefeq (e : Expr) (replacements : Array (Expr × Expr)) :
8989 MetaM (Option Expr) := do
9090 for (from_, to_) in replacements do
@@ -93,7 +93,7 @@ private def matchesAnyDefeq (e : Expr) (replacements : Array (Expr × Expr)) :
9393 return none
9494
9595/-- Replace binder domains in a chain of lambdas, stopping at the body.
96- Only replaces domains that are defeq to entries in `replacements`. -/
96+ Only replaces domains that are defeq to entries in `replacements`. -/
9797private partial def replaceLamDomains (e : Expr) (replacements : Array (Expr × Expr)) :
9898 MetaM Expr := do
9999 match e with
@@ -103,7 +103,7 @@ private partial def replaceLamDomains (e : Expr) (replacements : Array (Expr ×
103103 | _ => return e
104104
105105/-- WHNF `e` at default transparency and return the constructor info, universe levels,
106- and arguments, or `none` if `e` doesn't reduce to a constructor application. -/
106+ and arguments, or `none` if `e` doesn't reduce to a constructor application. -/
107107private def getCtorApp? (e : Expr) :
108108 MetaM (Option (ConstructorVal × List Level × Array Expr)) := do
109109 let e' ← withDefault <| whnf e
@@ -112,7 +112,7 @@ private def getCtorApp? (e : Expr) :
112112 return some (ci, us, e'.getAppArgs)
113113
114114/-- For each constructor parameter, determine whether it is instance-implicit and
115- whether it is a proof. -/
115+ whether it is a proof. -/
116116private def getFieldInfo (ci : ConstructorVal) : MetaM (Array (Bool × Bool)) :=
117117 withDefault <| forallTelescopeReducing ci.type fun ctorArgs _ =>
118118 ctorArgs.mapM fun arg => do
@@ -123,7 +123,7 @@ private def getFieldInfo (ci : ConstructorVal) : MetaM (Array (Bool × Bool)) :=
123123mutual
124124
125125/-- Process each constructor argument: replace carrier type parameters, recursively
126- normalize instance-implicit fields, and patch lambda binder domains in other fields. -/
126+ normalize instance-implicit fields, and patch lambda binder domains in other fields. -/
127127private partial def normalizeCtorArgs (ci : ConstructorVal) (us : List Level)
128128 (args : Array Expr) (fieldInfo : Array (Bool × Bool))
129129 (replacements : Array (Expr × Expr)) (fuel : Nat) : MetaM Expr := do
@@ -144,10 +144,10 @@ private partial def normalizeCtorArgs (ci : ConstructorVal) (us : List Level)
144144 return mkAppN (.const ci.name us) args
145145
146146/-- Recursively normalize a class instance expression:
147- 1. WHNF at `default` transparency to expose the constructor.
148- 2. Replace the carrier type parameter(s) in the constructor.
149- 3. For each instance-implicit, non-proof field: recurse.
150- 4. For each non-instance function field: replace lambda binder domains only. -/
147+ 1. WHNF at `default` transparency to expose the constructor.
148+ 2. Replace the carrier type parameter(s) in the constructor.
149+ 3. For each instance-implicit, non-proof field: recurse.
150+ 4. For each non-instance function field: replace lambda binder domains only. -/
151151private partial def normalizeInstance (e : Expr) (replacements : Array (Expr × Expr))
152152 (fuel : Nat := 50 ) : MetaM Expr := do
153153 if fuel == 0 then return e
@@ -161,21 +161,21 @@ private partial def normalizeInstance (e : Expr) (replacements : Array (Expr ×
161161end
162162
163163/-- `inferInstanceAs%` — like `inferInstanceAs`, but rewrites internal sub-expressions
164- (e.g. lambda binder domains) to use the expected carrier type instead of
165- intermediate unfoldings that leaked during instance synthesis.
166-
167- When `inferInstanceAs (SomeClass A)` is used to define `SomeClass B` (where
168- `B` is a non-reducible alias for `A`), the synthesized instance may contain
169- sub-expressions referring to `A` or its unfoldings instead of `B`. This
170- causes `isDefEq` failures at `reducibleAndInstances` transparency.
171- `inferInstanceAs%` fixes this by recursively normalizing the constructor
172- tree, patching carrier types and lambda binder domains.
173-
174- Example:
175- ```
176- noncomputable instance : Field (FiniteResidueField K) :=
177- inferInstanceAs% (Field (IsLocalRing.ResidueField _))
178- ```
164+ (e.g. lambda binder domains) to use the expected carrier type instead of
165+ intermediate unfoldings that leaked during instance synthesis.
166+
167+ When `inferInstanceAs (SomeClass A)` is used to define `SomeClass B` (where
168+ `B` is a non-reducible alias for `A`), the synthesized instance may contain
169+ sub-expressions referring to `A` or its unfoldings instead of `B`. This
170+ causes `isDefEq` failures at `reducibleAndInstances` transparency.
171+ `inferInstanceAs%` fixes this by recursively normalizing the constructor
172+ tree, patching carrier types and lambda binder domains.
173+
174+ Example:
175+ ```
176+ noncomputable instance : Field (FiniteResidueField K) :=
177+ inferInstanceAs% (Field (IsLocalRing.ResidueField _))
178+ ```
179179-/
180180elab "inferInstanceAs% " source:term : term <= expectedType => do
181181 let sourceType ← elabType source
0 commit comments