@@ -33,16 +33,15 @@ This module is written in a rather manual style, constructing the `Expr` directl
3333read with the expected output to the side.
3434-/
3535
36- namespace Lean
36+ namespace Lean.NoConfusionLinear
3737
3838open Meta
3939
4040/--
4141List of constants that the linear `noConfusionType` construction depends on.
4242-/
43- def linearNoConfusionDeps : Array Lean.Name :=
44- #[ ``Nat.lt, ``cond, ``OfNat.ofNat, ``instOfNatNat, ``Nat, ``PUnit, ``Eq, ``Not, ``dite,
45- ``Nat.decEq, ``Nat.blt ]
43+ def deps : Array Lean.Name :=
44+ #[ ``Nat.lt, ``cond, ``Nat, ``PUnit, ``Eq, ``Not, ``dite, ``Nat.decEq, ``Nat.blt ]
4645
4746def mkNatLookupTable (n : Expr) (es : Array Expr) (default : Expr) : MetaM Expr := do
4847 let type ← inferType default
@@ -54,12 +53,21 @@ def mkNatLookupTable (n : Expr) (es : Array Expr) (default : Expr) : MetaM Expr
5453 let mid := (start + stop ) / 2
5554 let low ← go start mid
5655 let high ← go mid stop
57- return mkApp4 (mkConst ``cond [u]) type (mkApp2 (mkConst ``Nat.blt) n (mkNatLit mid)) low high
56+ return mkApp4 (mkConst ``cond [u]) type (mkApp2 (mkConst ``Nat.blt) n (mkRawNatLit mid)) low high
5857 if h : es.size = 0 then
5958 pure default
6059 else
6160 go 0 es.size
6261
62+ def mkWithCtorTypeName (indName : Name) : Name :=
63+ Name.str indName "noConfusionType" |>.str "withCtorType"
64+
65+ def mkWithCtorName (indName : Name) : Name :=
66+ Name.str indName "noConfusionType" |>.str "withCtor"
67+
68+ def mkNoConfusionTypeName (indName : Name) : Name :=
69+ Name.str indName "noConfusionType"
70+
6371def mkWithCtorType (indName : Name) : MetaM Unit := do
6472 let ConstantInfo.inductInfo info ← getConstInfo indName | unreachable!
6573 let casesOnName := mkCasesOnName indName
@@ -81,7 +89,7 @@ def mkWithCtorType (indName : Name) : MetaM Unit := do
8189 let e ← mkNatLookupTable ctorIdx es default
8290 mkLambdaFVars ((xs.push P).push ctorIdx) e
8391
84- let declName := Name.str indName "noConfusionType" |>.str "withCtorType"
92+ let declName := mkWithCtorTypeName indName
8593 addAndCompile (.defnDecl (← mkDefinitionValInferrringUnsafe
8694 (name := declName)
8795 (levelParams := casesOnInfo.levelParams)
@@ -95,7 +103,7 @@ def mkWithCtorType (indName : Name) : MetaM Unit := do
95103
96104def mkWithCtor (indName : Name) : MetaM Unit := do
97105 let ConstantInfo.inductInfo info ← getConstInfo indName | unreachable!
98- let withCtorTypeName := Name.str indName "noConfusionType" |>.str "withCtorType"
106+ let withCtorTypeName := mkWithCtorTypeName indName
99107 let casesOnName := mkCasesOnName indName
100108 let casesOnInfo ← getConstVal casesOnName
101109 let v::us := casesOnInfo.levelParams.map mkLevelParam | panic! "unexpected universe levels on `casesOn`"
@@ -123,7 +131,7 @@ def mkWithCtor (indName : Name) : MetaM Unit := do
123131 let ctor := mkAppN (mkConst ctorName us) xs
124132 let ctorType ← inferType ctor
125133 forallTelescope ctorType fun zs _ => do
126- let heq := mkApp3 (mkConst ``Eq [1 ]) (mkConst ``Nat) ctorIdx (mkNatLit i)
134+ let heq := mkApp3 (mkConst ``Eq [1 ]) (mkConst ``Nat) ctorIdx (mkRawNatLit i)
127135 let «then » ← withLocalDeclD `h heq fun h => do
128136 let e ← mkEqNDRec (motive := withCtorTypeNameApp) k h
129137 let e := mkApp e (mkConst ``PUnit.unit [indLevel])
@@ -133,13 +141,13 @@ def mkWithCtor (indName : Name) : MetaM Unit := do
133141 let «else » ← withLocalDeclD `h (mkNot heq) fun h =>
134142 mkLambdaFVars #[h] k'
135143 let alt := mkApp5 (mkConst ``dite [v.succ])
136- P heq (mkApp2 (mkConst ``Nat.decEq) ctorIdx (mkNatLit i))
144+ P heq (mkApp2 (mkConst ``Nat.decEq) ctorIdx (mkRawNatLit i))
137145 «then » «else »
138146 mkLambdaFVars zs alt
139147 let e := mkAppN e alts
140148 mkLambdaFVars (xs ++ #[P, ctorIdx, k, k'] ++ ys ++ #[x]) e
141149
142- let declName := Name.str indName "noConfusionType" |>.str "withCtor"
150+ let declName := mkWithCtorName indName
143151 -- not compiled to avoid old code generator bug #1774
144152 addDecl (.defnDecl (← mkDefinitionValInferrringUnsafe
145153 (name := declName)
@@ -153,7 +161,7 @@ def mkWithCtor (indName : Name) : MetaM Unit := do
153161 setReducibleAttribute declName
154162
155163def mkNoConfusionTypeLinear (indName : Name) : MetaM Unit := do
156- let declName := .str indName "noConfusionType"
164+ let declName := mkNoConfusionTypeName indName
157165 let ConstantInfo.inductInfo info ← getConstInfo indName | unreachable!
158166 let casesOnName := mkCasesOnName indName
159167 let casesOnInfo ← getConstVal casesOnName
@@ -179,10 +187,10 @@ def mkNoConfusionTypeLinear (indName : Name) : MetaM Unit := do
179187 let alts' ← alts.mapIdxM fun i alt => do
180188 let altType ← inferType alt
181189 forallTelescope altType fun zs1 _ => do
182- let alt := mkConst (Name.str declName "withCtor" ) (v :: us)
190+ let alt := mkConst (mkWithCtorName indName ) (v :: us)
183191 let alt := mkAppN alt xs
184192 let alt := mkApp alt PType
185- let alt := mkApp alt (mkNatLit i)
193+ let alt := mkApp alt (mkRawNatLit i)
186194 let k ← forallTelescopeReducing (← inferType alt).bindingDomain! fun zs2 _ => do
187195 let eqs ← (Array.zip zs1 zs2[1 :]).filterMapM fun (z1,z2) => do
188196 if (← isProof z1) then
@@ -214,3 +222,5 @@ def mkNoConfusionTypeLinear (indName : Name) : MetaM Unit := do
214222 modifyEnv fun env => addToCompletionBlackList env declName
215223 modifyEnv fun env => addProtected env declName
216224 setReducibleAttribute declName
225+
226+ end Lean.NoConfusionLinear
0 commit comments