@@ -7,11 +7,12 @@ module Agda2Lambox.Convert.Function
77import Control.Monad.Reader ( ask , liftIO )
88import Control.Monad ( forM )
99import Data.List ( elemIndex )
10+ import Data.Maybe ( isNothing , isJust )
1011
1112import Utils
1213
1314import Agda ( liftTCM )
14- import Agda.Lib ()
15+ import Agda.Lib ( (^.) , funInline )
1516import Agda.Utils
1617import Agda.Syntax.Abstract.Name ( qnameModule )
1718import Agda.TypeChecking.Monad.Base
@@ -20,6 +21,7 @@ import Agda.Compiler.ToTreeless ( toTreeless )
2021import Agda.Compiler.Backend ( getConstInfo )
2122import Agda.Syntax.Treeless ( EvaluationStrategy (EagerEvaluation ) )
2223import Agda.Syntax.Common.Pretty ( prettyShow )
24+ import Agda.Syntax.Common ( hasQuantityω )
2325
2426import LambdaBox
2527
@@ -40,14 +42,29 @@ convertFunctionBody Defn{defName} =
4042 Just t <- liftTCM $ toTreeless EagerEvaluation defName
4143 convert t
4244
45+ -- | Whether a function is a (true) record projection.
46+ isProjection :: Either ProjectionLikenessMissing Projection -> Bool
47+ isProjection (Left _) = False
48+ isProjection (Right Projection {projProper}) = isJust projProper
49+
50+ -- | Whether to compile a function definition to λ□.
51+ shouldCompileFunction :: Definition -> Bool
52+ shouldCompileFunction def@ Defn {theDef} | Function {.. } <- theDef
53+ = not (theDef ^. funInline) -- not inlined (from module application)
54+ && isNothing funExtLam -- not a pattern-lambda-generated function NOTE(flupe): ?
55+ && isNothing funWith -- not a with-generated function NOTE(flupe): ?
56+ && not (isProjection funProjection) -- not a record projection
57+ && hasQuantityω def -- non-erased
58+
4359-- | Convert a function definition to a λ□ declaration.
44- convertFunction :: Definition :~> GlobalDecl
60+ convertFunction :: Definition :~> Maybe GlobalDecl
61+ convertFunction defn | not (shouldCompileFunction defn) = return Nothing
4562convertFunction defn@ Defn {defName, theDef} =
4663 withCurrentModule (qnameModule defName) do
4764 let Function {funMutual = Just ms} = theDef
4865
4966 if null ms then
50- ConstantDecl . Just <$> convertFunctionBody defn
67+ Just . ConstantDecl . Just <$> convertFunctionBody defn
5168 else do
5269 mdefs :: [Definition ] <- mapM getConstInfo ms
5370
@@ -59,7 +76,7 @@ convertFunction defn@Defn{defName, theDef} =
5976 -- it's unclear in which order mutual fixpoints are added to the local context
6077 let Just k = elemIndex defName ms
6178
62- ConstantDecl . Just . flip LFix k <$>
79+ Just . ConstantDecl . Just . flip LFix k <$>
6380 forM mdefs \ def@ Defn {defName} -> do
6481 body <- convertFunctionBody def
6582 return Def
0 commit comments