Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always drop "Haskell." prefix from module names #379

Merged
merged 5 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions lib/Haskell/Extra/Loop.agda

This file was deleted.

29 changes: 22 additions & 7 deletions src/Agda2Hs/Compile.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Agda2Hs.Compile where

import Prelude hiding (null)

import Control.Monad.Trans.RWS.CPS ( evalRWST )
import Control.Monad.State ( gets )
import Control.Arrow ((>>>))
Expand All @@ -17,17 +19,18 @@ import Agda.TypeChecking.Monad.Signature ( isInlineFun )
import Agda.Utils.Impossible
import Agda.Utils.List
import Agda.Utils.Null
import Agda.Utils.Monad ( whenM, anyM, when )
import Agda.Utils.Monad ( whenM, anyM, when, unless )

import qualified Language.Haskell.Exts.Extension as Hs

import Agda2Hs.Compile.ClassInstance ( compileInstance )
import Agda2Hs.Compile.Data ( compileData )
import Agda2Hs.Compile.Function ( compileFun, checkTransparentPragma, checkInlinePragma )
import Agda2Hs.Compile.Name ( hsTopLevelModuleName )
import Agda2Hs.Compile.Postulate ( compilePostulate )
import Agda2Hs.Compile.Record ( compileRecord, checkUnboxPragma )
import Agda2Hs.Compile.Types
import Agda2Hs.Compile.Utils ( setCurrentRangeQ, tellExtension, primModules, isClassName )
import Agda2Hs.Compile.Utils ( setCurrentRangeQ, tellExtension, primModules, isPrimModule, isHsModule, isClassName )
import Agda2Hs.Pragma
import qualified Language.Haskell.Exts.Syntax as Hs
import qualified Language.Haskell.Exts.Pretty as Hs
Expand All @@ -54,9 +57,6 @@ runC tlm rewrites c = evalRWST c (initCompileEnv tlm rewrites) initCompileState

moduleSetup :: Options -> IsMain -> TopLevelModuleName -> Maybe FilePath -> TCM (Recompile ModuleEnv ModuleRes)
moduleSetup _ _ m _ = do
-- we never compile primitive modules
if any (`isPrefixOf` prettyShow m) primModules then pure $ Skip ()
else do
reportSDoc "agda2hs.compile" 3 $ text "Compiling module: " <+> prettyTCM m
setScope . iInsideScope =<< curIF
return $ Recompile m
Expand Down Expand Up @@ -116,10 +116,11 @@ compile opts tlm _ def =

verifyOutput ::
Options -> ModuleEnv -> IsMain -> TopLevelModuleName
-> [(CompiledDef, CompileOutput)] -> TCM Bool
-> [(CompiledDef, CompileOutput)] -> TCM ()
verifyOutput _ _ _ m ls = do
reportSDoc "agda2hs.compile" 5 $ text "Checking generated output before rendering: " <+> prettyTCM m
ensureUniqueConstructors
ensureNoOutputFromPrimModules
where
ensureUniqueConstructors = do
let allCons = do
Expand All @@ -134,4 +135,18 @@ verifyOutput _ _ _ m ls = do
duplicateCons = filter ((> 1) . length) . group . sort $ allCons
when (length duplicateCons > 0) $
genericDocError =<< vcat (map (\x -> text $ "Cannot generate multiple constructors with the same identifier: " <> Hs.prettyPrint (headWithDefault __IMPOSSIBLE__ x)) duplicateCons)
return (length duplicateCons == 0)

ensureNoOutputFromPrimModules = unless (null $ concat $ map fst ls) $ do
let hsModName = hsTopLevelModuleName m
when (isPrimModule hsModName) $ do
reportSDoc "agda2hs.compile" 10 $ text "Primitive module" <+> prettyTCM m <+> text "has non-null output."
if isHsModule hsModName
then genericDocError =<< hsep
( pwords "The `Haskell.` namespace are reserved for binding existing Haskell modules, so the module"
++ [text "`" <> prettyTCM m <> text "`"]
++ pwords "should not contain any"
++ [text "`{-# COMPILE AGDA2HS ... #-}`"]
++ pwords "pragmas that produce Haskell code."
)
else do
__IMPOSSIBLE_VERBOSE__ "Primitive Agda module should not have any COMPILE AGDA2HS pragmas!"
9 changes: 7 additions & 2 deletions src/Agda2Hs/Compile/Name.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Control.Monad.Reader

import Data.Functor ( (<&>) )
import Data.Bifunctor ( bimap )
import Data.List ( intercalate, isPrefixOf )
import Data.List ( intercalate, isPrefixOf, stripPrefix )
import Data.Text ( unpack )
import qualified Data.Map.Strict as Map

Expand Down Expand Up @@ -210,7 +210,12 @@ compileQName f
in (mod', Just (Import mod' qual Nothing hf maybeIsType))
else (mod, Nothing)
| otherwise
= (mod, Just (Import mod qual par hf maybeIsType))
= let mod' = dropHaskellPrefix mod
in (mod', Just (Import mod' qual par hf maybeIsType))

dropHaskellPrefix :: Hs.ModuleName () -> Hs.ModuleName ()
dropHaskellPrefix (Hs.ModuleName l s) =
Hs.ModuleName l $ fromMaybe s $ stripPrefix "Haskell." s

isWhereFunction :: QName -> C Bool
isWhereFunction f = do
Expand Down
7 changes: 4 additions & 3 deletions src/Agda2Hs/Compile/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ import Agda.Utils.Impossible ( __IMPOSSIBLE__ )
primModules =
[ "Agda.Builtin"
, "Agda.Primitive"
, "Haskell.Prim"
, "Haskell.Prelude"
, "Haskell.Law"
, "Haskell"
]

isPrimModule :: Hs.ModuleName () -> Bool
isPrimModule mod = any (`isPrefixOf` pp mod) primModules

isHsModule :: Hs.ModuleName () -> Bool
isHsModule mod = "Haskell." `isPrefixOf` pp mod

concatUnzip :: [([a], [b])] -> ([a], [b])
concatUnzip = (concat *** concat) . unzip

Expand Down
Loading