@@ -28,7 +28,7 @@ import Agda.Syntax.TopLevelModuleName ( TopLevelModuleName, moduleNameToFileName
2828import Agda.Syntax.Common.Pretty ( pretty , prettyShow )
2929import Agda.Utils.Monad ( whenM )
3030
31- import Agda.Utils ( pp , hasPragma , isDataOrRecDef )
31+ import Agda.Utils ( pp , hasPragma , isDataOrRecDef , filterOutWhere )
3232import Agda2Lambox.Compile.Target
3333import Agda2Lambox.Compile.Utils
3434import Agda2Lambox.Compile (compile )
@@ -40,13 +40,18 @@ import LambdaBox.Env
4040main :: IO ()
4141main = runAgda [agda2lambox]
4242
43+ data Output = RocqOutput | AstOutput
44+ deriving (Generic , NFData )
45+
4346-- | Backend options.
4447data Options = forall t . Options
4548 { optOutDir :: Maybe FilePath
4649 , optTarget :: Target t
50+ , optOutput :: Output
4751 }
4852
49- instance NFData Options where rnf (Options m t) = rnf m `seq` rnf t
53+ instance NFData Options where
54+ rnf (Options m t o) = rnf m `seq` rnf t `seq` rnf o
5055
5156-- | Setter for output directory option.
5257outdirOpt :: Monad m => FilePath -> Options -> m Options
@@ -55,11 +60,15 @@ outdirOpt dir opts = return opts { optOutDir = Just dir }
5560typedOpt :: Monad m => Options -> m Options
5661typedOpt opts = return opts { optTarget = ToTyped }
5762
63+ rocqOpt :: Monad m => Options -> m Options
64+ rocqOpt opts = return opts { optOutput = RocqOutput }
65+
5866-- | Default backend options.
5967defaultOptions :: Options
6068defaultOptions = Options
6169 { optOutDir = Nothing
6270 , optTarget = ToUntyped
71+ , optOutput = AstOutput
6372 }
6473
6574-- | Backend module environments.
@@ -82,6 +91,8 @@ agda2lambox = Backend backend
8291 " Write output files to DIR. (default: project root)"
8392 , Option [' t' ] [" typed" ] (NoArg typedOpt)
8493 " Compile to typed λ□ environments."
94+ , Option [' c' ] [" rocq" ] (NoArg typedOpt)
95+ " Output a Rocq file."
8596 ]
8697 , isEnabled = \ _ -> True
8798 , preCompile = return
@@ -95,30 +106,6 @@ agda2lambox = Backend backend
95106
96107
97108
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- _ -> []
122109
123110moduleSetup
124111 :: Options -> IsMain -> TopLevelModuleName -> Maybe FilePath
@@ -146,13 +133,18 @@ writeModule Options{..} menv IsMain m defs = do
146133 coqMod = CoqModule env (map qnameToKName programs)
147134
148135 liftIO do
149- putStrLn $ " Writing " <> fileName " .{v, txt,ast} "
136+ putStrLn $ " Writing " <> fileName " .txt"
150137
151138 pp coqMod <> " \n "
152139 & writeFile (fileName " .txt" )
153140
154- prettyCoq optTarget coqMod <> " \n "
155- & writeFile (fileName " .v" )
141+ case optOutput of
142+ RocqOutput -> do
143+ putStrLn $ " Writing " <> fileName " .v"
144+ prettyCoq optTarget coqMod <> " \n "
145+ & writeFile (fileName " .v" )
156146
157- prettySexp optTarget coqMod <> " \n "
158- & LText. writeFile (fileName " .ast" )
147+ AstOutput -> do
148+ putStrLn $ " Writing " <> fileName " .ast"
149+ prettySexp optTarget coqMod <> " \n "
150+ & LText. writeFile (fileName " .ast" )
0 commit comments