Skip to content

Commit d781ebe

Browse files
committed
Throw error if module under Haskell. namespace produces any Haskell output
1 parent e4047f6 commit d781ebe

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/Agda2Hs/Compile.hs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Agda2Hs.Compile where
22

3+
import Prelude hiding (null)
4+
35
import Control.Monad.Trans.RWS.CPS ( evalRWST )
46
import Control.Monad.State ( gets )
57
import Control.Arrow ((>>>))
@@ -17,17 +19,18 @@ import Agda.TypeChecking.Monad.Signature ( isInlineFun )
1719
import Agda.Utils.Impossible
1820
import Agda.Utils.List
1921
import Agda.Utils.Null
20-
import Agda.Utils.Monad ( whenM, anyM, when )
22+
import Agda.Utils.Monad ( whenM, anyM, when, unless )
2123

2224
import qualified Language.Haskell.Exts.Extension as Hs
2325

2426
import Agda2Hs.Compile.ClassInstance ( compileInstance )
2527
import Agda2Hs.Compile.Data ( compileData )
2628
import Agda2Hs.Compile.Function ( compileFun, checkTransparentPragma, checkInlinePragma )
29+
import Agda2Hs.Compile.Name ( hsTopLevelModuleName )
2730
import Agda2Hs.Compile.Postulate ( compilePostulate )
2831
import Agda2Hs.Compile.Record ( compileRecord, checkUnboxPragma )
2932
import Agda2Hs.Compile.Types
30-
import Agda2Hs.Compile.Utils ( setCurrentRangeQ, tellExtension, primModules, isClassName )
33+
import Agda2Hs.Compile.Utils ( setCurrentRangeQ, tellExtension, primModules, isPrimModule, isHsModule, isClassName )
3134
import Agda2Hs.Pragma
3235
import qualified Language.Haskell.Exts.Syntax as Hs
3336
import qualified Language.Haskell.Exts.Pretty as Hs
@@ -54,9 +57,6 @@ runC tlm rewrites c = evalRWST c (initCompileEnv tlm rewrites) initCompileState
5457

5558
moduleSetup :: Options -> IsMain -> TopLevelModuleName -> Maybe FilePath -> TCM (Recompile ModuleEnv ModuleRes)
5659
moduleSetup _ _ m _ = do
57-
-- we never compile primitive modules
58-
if any (`isPrefixOf` prettyShow m) primModules then pure $ Skip ()
59-
else do
6060
reportSDoc "agda2hs.compile" 3 $ text "Compiling module: " <+> prettyTCM m
6161
setScope . iInsideScope =<< curIF
6262
return $ Recompile m
@@ -120,6 +120,7 @@ verifyOutput ::
120120
verifyOutput _ _ _ m ls = do
121121
reportSDoc "agda2hs.compile" 5 $ text "Checking generated output before rendering: " <+> prettyTCM m
122122
ensureUniqueConstructors
123+
ensureNoOutputFromPrimModules
123124
where
124125
ensureUniqueConstructors = do
125126
let allCons = do
@@ -134,3 +135,18 @@ verifyOutput _ _ _ m ls = do
134135
duplicateCons = filter ((> 1) . length) . group . sort $ allCons
135136
when (length duplicateCons > 0) $
136137
genericDocError =<< vcat (map (\x -> text $ "Cannot generate multiple constructors with the same identifier: " <> Hs.prettyPrint (headWithDefault __IMPOSSIBLE__ x)) duplicateCons)
138+
139+
ensureNoOutputFromPrimModules = unless (null $ concat $ map fst ls) $ do
140+
let hsModName = hsTopLevelModuleName m
141+
when (isPrimModule hsModName) $ do
142+
reportSDoc "agda2hs.compile" 10 $ text "Primitive module" <+> prettyTCM m <+> text "has non-null output."
143+
if isHsModule hsModName
144+
then genericDocError =<< hsep
145+
( pwords "The `Haskell.` namespace are reserved for binding existing Haskell modules, so the module"
146+
++ [text "`" <> prettyTCM m <> text "`"]
147+
++ pwords "should not contain any"
148+
++ [text "`{-# COMPILE AGDA2HS ... #-}`"]
149+
++ pwords "pragmas that produce Haskell code."
150+
)
151+
else do
152+
__IMPOSSIBLE_VERBOSE__ "Primitive Agda module should not have any COMPILE AGDA2HS pragmas!"

src/Agda2Hs/Compile/Name.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ compileQName f
115115
existsInHaskell <- orM
116116
[ pure $ isJust special
117117
, pure $ isPrimModule mod
118-
, pure $ isHsModule mod
119118
, hasCompilePragma f
120119
, isClassFunction f
121120
, isWhereFunction f

src/Agda2Hs/Compile/Utils.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ import Agda.Utils.Impossible ( __IMPOSSIBLE__ )
5757
primModules =
5858
[ "Agda.Builtin"
5959
, "Agda.Primitive"
60-
, "Haskell.Prim"
61-
, "Haskell.Prelude"
62-
, "Haskell.Law"
60+
, "Haskell"
6361
]
6462

6563
isPrimModule :: Hs.ModuleName () -> Bool

0 commit comments

Comments
 (0)