Skip to content

Commit 8705a83

Browse files
committed
added flag for rocq/ast output
1 parent 7f01b67 commit 8705a83

File tree

3 files changed

+51
-34
lines changed

3 files changed

+51
-34
lines changed

demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ <h1>agda2lambox</h1>
157157
{ nat
158158
, bool
159159
, listnat: list(nat)
160-
, listbool: list(bool)
160+
, listbool: list(bool)
161161
, string
162162
}
163163
</script>

src/Agda/Utils.hs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ module Agda.Utils where
55

66
import Control.Applicative ( liftA2 )
77
import Data.Bifunctor ( second )
8-
import Data.Maybe ( isJust, isNothing )
8+
import Data.Maybe ( isJust, isNothing, catMaybes )
99

10-
import Agda.Compiler.Backend ( getUniqueCompilerPragma, PureTCM )
10+
import Agda.Compiler.Backend ( getUniqueCompilerPragma, PureTCM, HasConstInfo (getConstInfo) )
1111
import Agda.Syntax.Abstract.Name
1212
import Agda.Syntax.Internal
1313
import Agda.TypeChecking.Monad.Base hiding ( conArity )
@@ -98,6 +98,31 @@ maybeUnfoldCopy f es onTerm onDef =
9898
NoReduction () -> onDef f es
9999
YesReduction _ t -> onTerm t
100100

101+
-- | Remove all the names defined in where clauses from the list of names.
102+
-- Assumes that the order is the one given by Agda, that is:
103+
-- definitions in (possibly anonymous) modules coming from where clauses appear
104+
-- right after their parent function definition.
105+
filterOutWhere :: [QName] -> TCM [QName]
106+
filterOutWhere [] = pure []
107+
filterOutWhere (q:qs) = do
108+
ws <- getWheres q
109+
let qs' = dropWhile (isChild ws) qs
110+
(q:) <$> filterOutWhere qs'
111+
112+
where
113+
isChild :: [ModuleName] -> QName -> Bool
114+
isChild ws r = any (r `isChildOfMod`) ws
115+
116+
isChildOfMod :: QName -> ModuleName -> Bool
117+
isChildOfMod q mod = qnameModule q `isLeChildModuleOf` mod
118+
119+
getWheres :: QName -> TCM [ModuleName]
120+
getWheres q = do
121+
def <- getConstInfo q
122+
pure case theDef def of
123+
Function{..} -> catMaybes $ map clauseWhereModule funClauses
124+
_ -> []
125+
101126
{-
102127
lookupCtx :: MonadTCEnv m => Int -> m (String, Type)
103128
lookupCtx i = first transcribe . (!! i) {-. reverse-} <$> currentCtx

src/Main.hs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Agda.Syntax.TopLevelModuleName ( TopLevelModuleName, moduleNameToFileName
2828
import Agda.Syntax.Common.Pretty ( pretty, prettyShow )
2929
import Agda.Utils.Monad ( whenM )
3030

31-
import Agda.Utils ( pp, hasPragma, isDataOrRecDef )
31+
import Agda.Utils ( pp, hasPragma, isDataOrRecDef, filterOutWhere )
3232
import Agda2Lambox.Compile.Target
3333
import Agda2Lambox.Compile.Utils
3434
import Agda2Lambox.Compile (compile)
@@ -40,13 +40,18 @@ import LambdaBox.Env
4040
main :: IO ()
4141
main = runAgda [agda2lambox]
4242

43+
data Output = RocqOutput | AstOutput
44+
deriving (Generic, NFData)
45+
4346
-- | Backend options.
4447
data 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.
5257
outdirOpt :: Monad m => FilePath -> Options -> m Options
@@ -55,11 +60,15 @@ outdirOpt dir opts = return opts { optOutDir = Just dir }
5560
typedOpt :: Monad m => Options -> m Options
5661
typedOpt 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.
5967
defaultOptions :: Options
6068
defaultOptions = 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

123110
moduleSetup
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

Comments
 (0)