Skip to content

Commit 73c208c

Browse files
committed
drop where defs at first
1 parent ff437c6 commit 73c208c

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/Main.hs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Paths_agda2lambox ( version )
2323
import Agda.Compiler.Common
2424
import Agda.Compiler.Backend
2525
import Agda.Main ( runAgda )
26+
import Agda.Syntax.Internal ( clauseWhereModule )
2627
import Agda.Syntax.TopLevelModuleName ( TopLevelModuleName, moduleNameToFileName )
2728
import Agda.Syntax.Common.Pretty ( pretty, prettyShow )
2829
import 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+
96123
moduleSetup
97124
:: Options -> IsMain -> TopLevelModuleName -> Maybe FilePath
98125
-> TCM (Recompile ModuleEnv ModuleRes)
99126
moduleSetup _ _ m _ = do
100127
setScope . iInsideScope =<< curIF
101128
pure $ Recompile ()
102129

103-
104130
writeModule
105131
:: Options -> ModuleEnv -> IsMain -> TopLevelModuleName
106132
-> [QName]
@@ -109,7 +135,10 @@ writeModule opts menv NotMain _ _ = pure ()
109135
writeModule 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

Comments
 (0)