9191 k hs
9292
9393/-- Given a list of `AltLHS`, create a minor premise for each one, convert them into `Alt`, and then execute `k` -/
94- private def withAlts {α} (motive : Expr) (discrs : Array Expr) (discrInfos : Array DiscrInfo) (lhss : List AltLHS) (k : List Alt → Array (Expr × Nat) → MetaM α) : MetaM α :=
95- loop lhss [] #[]
94+ private def withAlts {α} (motive : Expr) (discrs : Array Expr) (discrInfos : Array DiscrInfo)
95+ (lhss : List AltLHS) (k : List Alt → Array Expr → Array AltParamInfo → MetaM α) : MetaM α :=
96+ loop lhss [] #[] #[]
9697where
9798 mkMinorType (xs : Array Expr) (lhs : AltLHS) : MetaM Expr :=
9899 withExistingLocalDecls lhs.fvarDecls do
@@ -101,23 +102,24 @@ where
101102 withEqs discrs args discrInfos fun eqs => do
102103 mkForallFVars (xs ++ eqs) minorType
103104
104- loop (lhss : List AltLHS) (alts : List Alt) (minors : Array ( Expr × Nat) ) : MetaM α := do
105+ loop (lhss : List AltLHS) (alts : List Alt) (minors : Array Expr) (altInfos : Array AltParamInfo ) : MetaM α := do
105106 match lhss with
106- | [] => k alts.reverse minors
107+ | [] => k alts.reverse minors altInfos
107108 | lhs::lhss =>
108109 let xs := lhs.fvarDecls.toArray.map LocalDecl.toExpr
109110 let minorType ← mkMinorType xs lhs
110111 let hasParams := !xs.isEmpty || discrInfos.any fun info => info.hName?.isSome
111- let ( minorType, minorNumParams) := if hasParams then ( minorType, xs.size) else ( mkSimpleThunkType minorType, 1 )
112+ let minorType := if hasParams then minorType else mkSimpleThunkType minorType
112113 let idx := alts.length
113114 let minorName := (`h).appendIndexAfter (idx+1 )
114115 trace[Meta.Match.debug] "minor premise {minorName} : {minorType}"
115116 withLocalDeclD minorName minorType fun minor => do
116117 let rhs := if hasParams then mkAppN minor xs else mkApp minor (mkConst `Unit.unit)
117- let minors := minors.push (minor, minorNumParams)
118+ let minors := minors.push minor
119+ let altInfos := altInfos.push { numFields := xs.size, numOverlaps := 0 , hasUnitThunk := !hasParams }
118120 let fvarDecls ← lhs.fvarDecls.mapM instantiateLocalDeclMVars
119121 let alts := { ref := lhs.ref, idx := idx, rhs := rhs, fvarDecls := fvarDecls, patterns := lhs.patterns, cnstrs := [] } :: alts
120- loop lhss alts minors
122+ loop lhss alts minors altInfos
121123
122124structure State where
123125 /-- Used alternatives -/
@@ -1094,7 +1096,6 @@ where `v` is a universe parameter or 0 if `B[a_1, ..., a_n]` is a proposition.
10941096def mkMatcher (input : MkMatcherInput) : MetaM MatcherResult := withCleanLCtxFor input do
10951097 let ⟨matcherName, matchType, discrInfos, lhss⟩ := input
10961098 let numDiscrs := discrInfos.size
1097- let numEqs := getNumEqsFromDiscrInfos discrInfos
10981099 checkNumPatterns numDiscrs lhss
10991100 forallBoundedTelescope matchType numDiscrs fun discrs matchTypeBody => do
11001101 /- We generate an matcher that can eliminate using different motives with different universe levels.
@@ -1103,9 +1104,8 @@ def mkMatcher (input : MkMatcherInput) : MetaM MatcherResult := withCleanLCtxFor
11031104 This is useful for implementing `MatcherApp.addArg` because it may have to change the universe level. -/
11041105 let uElim ← getLevel matchTypeBody
11051106 let uElimGen ← if uElim == levelZero then pure levelZero else mkFreshLevelMVar
1106- let mkMatcher (type val : Expr) (minors : Array (Expr × Nat) ) (s : State) : MetaM MatcherResult := do
1107+ let mkMatcher (type val : Expr) (altInfos : Array AltParamInfo ) (s : State) : MetaM MatcherResult := do
11071108 trace[Meta.Match.debug] "matcher value: {val}\n type: {type}"
1108- trace[Meta.Match.debug] "minors num params: {minors.map (·.2)}"
11091109 /- The option `bootstrap.gen_matcher_code` is a helper hack. It is useful, for example,
11101110 for compiling `src/Init/Data/Int`. It is needed because the compiler uses `Int.decLt`
11111111 for generating code for `Int.casesOn` applications, but `Int.casesOn` is used to
@@ -1125,7 +1125,7 @@ def mkMatcher (input : MkMatcherInput) : MetaM MatcherResult := withCleanLCtxFor
11251125 match addMatcher with
11261126 | some addMatcher => addMatcher <|
11271127 { numParams := matcher.getAppNumArgs
1128- altNumParams := minors.map fun minor => minor. 2 + numEqs
1128+ altInfos
11291129 discrInfos
11301130 numDiscrs
11311131 uElimPos?
@@ -1153,7 +1153,7 @@ def mkMatcher (input : MkMatcherInput) : MetaM MatcherResult := withCleanLCtxFor
11531153 let isEqMask ← eqs.mapM fun eq => return (← inferType eq).isEq
11541154 return (mvarType, isEqMask)
11551155 trace[Meta.Match.debug] "target: {mvarType}"
1156- withAlts motive discrs discrInfos lhss fun alts minors => do
1156+ withAlts motive discrs discrInfos lhss fun alts minors altInfos => do
11571157 let mvar ← mkFreshExprMVar mvarType
11581158 trace[Meta.Match.debug] "goal\n {mvar.mvarId!}"
11591159 let examples := discrs'.toList.map fun discr => Example.var discr.fvarId!
@@ -1170,21 +1170,21 @@ def mkMatcher (input : MkMatcherInput) : MetaM MatcherResult := withCleanLCtxFor
11701170 rfls := rfls.push (← mkHEqRefl discr)
11711171 isEqMaskIdx := isEqMaskIdx + 1
11721172 let val := mkAppN (mkAppN val discrs) rfls
1173- let args := #[motive] ++ discrs ++ minors.map Prod.fst
1173+ let args := #[motive] ++ discrs ++ minors
11741174 let val ← mkLambdaFVars args val
11751175 let type ← mkForallFVars args (mkAppN motive discrs)
1176- mkMatcher type val minors s
1176+ mkMatcher type val altInfos s
11771177 else
11781178 let mvarType := mkAppN motive discrs
11791179 trace[Meta.Match.debug] "target: {mvarType}"
1180- withAlts motive discrs discrInfos lhss fun alts minors => do
1180+ withAlts motive discrs discrInfos lhss fun alts minors altInfos => do
11811181 let mvar ← mkFreshExprMVar mvarType
11821182 let examples := discrs.toList.map fun discr => Example.var discr.fvarId!
11831183 let (_, s) ← (process { mvarId := mvar.mvarId!, vars := discrs.toList, alts := alts, examples := examples }).run {}
1184- let args := #[motive] ++ discrs ++ minors.map Prod.fst
1184+ let args := #[motive] ++ discrs ++ minors
11851185 let type ← mkForallFVars args mvarType
11861186 let val ← mkLambdaFVars args mvar
1187- mkMatcher type val minors s
1187+ mkMatcher type val altInfos s
11881188
11891189def getMkMatcherInputInContext (matcherApp : MatcherApp) : MetaM MkMatcherInput := do
11901190 let matcherName := matcherApp.matcherName
0 commit comments