@@ -10,6 +10,7 @@ public import Lean.Compiler.ClosedTermCache
1010public import Lean.Compiler.NeverExtractAttr
1111public import Lean.Compiler.LCNF.Internalize
1212public import Lean.Compiler.LCNF.ToExpr
13+ import Lean.Compiler.LCNF.PrettyPrinter
1314
1415public section
1516
@@ -79,25 +80,26 @@ partial def shouldExtractLetValue (isRoot : Bool) (v : LetValue) : M Bool := do
7980 | .lit (.nat v) => return !isRoot || v >= Nat.pow 2 63
8081 | .lit _ | .erased => return !isRoot
8182 | .const name _ args =>
83+ -- TODO: overapproximating, like this we never extract closed terms from our SCC even if the SCC
84+ -- has actually been split open by now
8285 if (← read).sccDecls.any (·.name == name) then
8386 return false
8487 -- TODO: cleanup never extract annotations in core
8588 if hasNeverExtractAttribute (← getEnv) name then
8689 return false
90+ let isPap := (← getMonoDecl? name).map (decide <| args.size < ·.getArity ) |>.getD false
8791 if let some constInfo := (← getEnv).find? name then
8892 let shouldExtractCtor :=
8993 constInfo.isCtor &&
9094 !(isExtern (← getEnv) name) &&
9195 (!(args.all isIrrelevantArg) || !isRoot)
9296 -- TODO: check fully applied ctor
93- let shouldExtract := shouldExtractCtor || extractionAllowlist.contains name
97+ let shouldExtract := shouldExtractCtor || extractionAllowlist.contains name || isPap
9498 if !shouldExtract then
9599 return false
96100 else
97- if let some decl := ← getMonoDecl? name then
98- if args.size >= decl.getArity then
99- return false
100- if !(!isRoot && isClosedTermName (← getEnv) name) then
101+ let isClosedApp := (!isRoot && isClosedTermName (← getEnv) name)
102+ if !(isClosedApp || isPap) then
101103 return false
102104 args.allM shouldExtractArg
103105 | .fvar fnVar args =>
@@ -112,7 +114,8 @@ partial def shouldExtractArg (arg : Arg) : M Bool := do
112114
113115partial def shouldExtractFVar (fvarId : FVarId) : M Bool := do
114116 if let some letDecl ← findLetDecl? fvarId then
115- shouldExtractLetValue false letDecl.value
117+ let ret ← shouldExtractLetValue false letDecl.value
118+ return ret
116119 else
117120 return false
118121
@@ -123,7 +126,9 @@ mutual
123126partial def visitCode (code : Code) : M Code := do
124127 match code with
125128 | .let decl k =>
126- if (← shouldExtractLetValue true decl.value) then
129+ let should ← shouldExtractLetValue true decl.value
130+ trace[Meta.debug] m! "Should extract: { ← ppLetValue decl.value} ? { should} "
131+ if should then
127132 let ⟨_, decls⟩ ← extractLetValue decl.value |>.run {}
128133 let decls := decls.reverse.push (.let decl)
129134 let decls ← decls.mapM Internalize.internalizeCodeDecl |>.run' {}
0 commit comments