@@ -23,6 +23,7 @@ import Paths_agda2lambox ( version )
2323import Agda.Compiler.Common
2424import Agda.Compiler.Backend
2525import Agda.Main ( runAgda )
26+ import Agda.Syntax.Internal ( clauseWhereModule )
2627import Agda.Syntax.TopLevelModuleName ( TopLevelModuleName , moduleNameToFileName )
2728import Agda.Syntax.Common.Pretty ( pretty , prettyShow )
2829import Agda.Utils.Monad ( whenM )
@@ -93,14 +94,39 @@ agda2lambox = Backend backend
9394 }
9495
9596
97+
98+ -- | Remove all the names defined in where clauses from the list of names.
99+ -- Assumes that the order is the one given by Agda, that is:
100+ -- definitions in (possibly anonymous) modules coming from where clauses appear
101+ -- right after their parent function definition.
102+ filterOutWhere :: [QName ] -> TCM [QName ]
103+ filterOutWhere [] = pure []
104+ filterOutWhere (q: qs) = do
105+ ws <- getWheres q
106+ let qs' = dropWhile (isChild ws) qs
107+ (q: ) <$> filterOutWhere qs'
108+
109+ where
110+ isChild :: [ModuleName ] -> QName -> Bool
111+ isChild ws r = any (r `isChildOfMod` ) ws
112+
113+ isChildOfMod :: QName -> ModuleName -> Bool
114+ isChildOfMod q mod = qnameModule q `isLeChildModuleOf` mod
115+
116+ getWheres :: QName -> TCM [ModuleName ]
117+ getWheres q = do
118+ def <- getConstInfo q
119+ pure case theDef def of
120+ Function {.. } -> catMaybes $ map clauseWhereModule funClauses
121+ _ -> []
122+
96123moduleSetup
97124 :: Options -> IsMain -> TopLevelModuleName -> Maybe FilePath
98125 -> TCM (Recompile ModuleEnv ModuleRes )
99126moduleSetup _ _ m _ = do
100127 setScope . iInsideScope =<< curIF
101128 pure $ Recompile ()
102129
103-
104130writeModule
105131 :: Options -> ModuleEnv -> IsMain -> TopLevelModuleName
106132 -> [QName ]
@@ -109,7 +135,10 @@ writeModule opts menv NotMain _ _ = pure ()
109135writeModule Options {.. } menv IsMain m defs = do
110136 programs <- filterM hasPragma defs
111137 outDir <- flip fromMaybe optOutDir <$> compileDir
112- env <- compile optTarget $ reverse defs
138+
139+ defs' <- filterOutWhere defs
140+
141+ env <- compile optTarget $ reverse defs'
113142
114143 liftIO $ createDirectoryIfMissing True outDir
115144
0 commit comments