Skip to content

Commit 3172836

Browse files
committed
cleanup
1 parent c93dfa3 commit 3172836

File tree

7 files changed

+60
-81
lines changed

7 files changed

+60
-81
lines changed

src/Agda2Lambox/Compile.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import Agda2Lambox.Compile.Inductive ( compileInductive )
2020
import LambdaBox
2121

2222

23-
compileDefinition :: QName -> CompileM (Maybe (KerName, GlobalDecl))
24-
compileDefinition q = do
25-
defn@Defn{..} <- liftTCM $ getConstInfo q
26-
23+
compileDefinition :: Definition -> CompileM (Maybe (KerName, GlobalDecl))
24+
compileDefinition defn@Defn{..} = do
2725
fmap (qnameToKerName defName,) <$> -- prepend kername
2826
case theDef of
2927
Constructor{conData} -> Nothing <$ requireDef conData

src/Agda2Lambox/Compile/Monad.hs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
module Agda2Lambox.Compile.Monad
44
( CompileM
55
, requireDef
6-
, compile
76
, compileLoop
87
) where
98

@@ -15,8 +14,8 @@ import Data.Set qualified as Set
1514
import Queue.Ephemeral ( EphemeralQueue(..) )
1615
import Queue.Ephemeral qualified as Queue
1716

18-
import Agda.Syntax.Internal ( QName )
19-
import Agda.TypeChecking.Monad.Base ( TCM, MonadTCEnv, MonadTCM, MonadTCState, MonadTCEnv, HasOptions )
17+
import Agda.Compiler.Backend ( QName, Definition, getConstInfo )
18+
import Agda.TypeChecking.Monad.Base ( TCM, MonadTCEnv, MonadTCM(liftTCM), MonadTCState, MonadTCEnv, HasOptions )
2019
import Agda.Utils.List ( mcons )
2120

2221
-- | Backend compilation state.
@@ -32,11 +31,11 @@ data CompileState = CompileState
3231
-- i.e try to compile "related" definitions together.
3332
-- - We use an EphemeralQueue because we don't rely on persistence at all.
3433

35-
-- | Initial compile state. Empty queue, no name is known.
36-
initState :: CompileState
37-
initState = CompileState
38-
{ seenDefs = Set.empty
39-
, compileQueue = Queue.empty
34+
-- | Initial compile state, with a set of names required for compilation.
35+
initState :: [QName] -> CompileState
36+
initState qs = CompileState
37+
{ seenDefs = Set.fromList qs
38+
, compileQueue = Queue.fromList qs
4039
}
4140

4241
-- | Backend compilation monad.
@@ -48,10 +47,6 @@ newtype CompileM a = Compile (StateT CompileState TCM a)
4847
-- deriving newtype (MonadState CompileState)
4948
-- NOTE(flupe): safe to not export this, to make sure the queue is indeed ephemeral
5049

51-
-- | Run a compilation unit in TCM.
52-
compile :: CompileM a -> TCM a
53-
compile (Compile c) = evalStateT c initState
54-
5550
-- | Require a definition to be compiled.
5651
requireDef :: QName -> CompileM ()
5752
requireDef q = Compile $ do
@@ -71,8 +66,14 @@ nextUnit = Compile $
7166
Full q queue -> Just q <$ modify \s -> s { compileQueue = queue }
7267

7368
-- | Run the processing function as long as there are names to be compiled.
74-
compileLoop :: (QName -> CompileM (Maybe a)) -> CompileM [a]
75-
compileLoop step =
76-
nextUnit >>= \case
69+
compileLoop
70+
:: (Definition -> CompileM (Maybe a)) -- ^ The compilation function
71+
-> [QName] -- ^ Names to compile
72+
-> TCM [a]
73+
compileLoop step = evalStateT unloop . initState
74+
where
75+
loop@(Compile unloop) = nextUnit >>= \case
7776
Nothing -> pure []
78-
Just q -> mcons <$> step q <*> compileLoop step
77+
Just q -> do
78+
mr <- step =<< (liftTCM $ getConstInfo q)
79+
mcons mr <$> loop

src/Agda2Lambox/Compile/Term.hs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module Agda2Lambox.Compile.Term
33
( compileTerm
44
) where
55

6-
import Control.Monad ( mapM )
76
import Control.Monad.Fail ( MonadFail )
87
import Control.Monad.IO.Class (MonadIO)
98
import Control.Monad.Reader.Class ( MonadReader, ask )
@@ -17,6 +16,7 @@ import Agda.Compiler.Backend ( getConstInfo, theDef, pattern Datatype, dataMutua
1716
import Agda.Syntax.Abstract.Name ( ModuleName(..), QName(..) )
1817
import Agda.Syntax.Common ( Erased(..) )
1918
import Agda.Syntax.Common.Pretty ( prettyShow )
19+
import Agda.Syntax.Literal
2020
import Agda.Syntax.Treeless ( TTerm(..), TAlt(..), CaseInfo(..), CaseType(..) )
2121
import Agda.TypeChecking.Datatypes ( getConstructorData, getConstructors )
2222
import Agda.TypeChecking.Monad.Base ( TCM , liftTCM, MonadTCEnv, MonadTCM )
@@ -47,14 +47,11 @@ initEnv = CompileEnv
4747
}
4848

4949
-- | Compilation monad.
50-
newtype C a = C (ReaderT CompileEnv CompileM a)
51-
deriving newtype (Functor, Applicative, Monad, MonadIO)
52-
deriving newtype (MonadFail, MonadReader CompileEnv)
53-
deriving newtype (MonadTCEnv, MonadTCState, HasOptions, MonadTCM)
50+
type C a = ReaderT CompileEnv CompileM a
5451

5552
-- | Run a compilation unit in @TCM@, in the initial environment.
5653
runC :: C a -> CompileM a
57-
runC (C m) = runReaderT m initEnv
54+
runC m = runReaderT m initEnv
5855

5956
-- | Increase the number of locally-bound variables.
6057
underBinders :: Int -> C a -> C a
@@ -69,10 +66,6 @@ underBinder = underBinders 1
6966
withMutuals :: [QName] -> C a -> C a
7067
withMutuals ms = local \e -> e { mutuals = reverse ms }
7168

72-
-- | Require a name to be compiled
73-
require :: QName -> C ()
74-
require = C . lift . requireDef
75-
7669
-- * Term conversion
7770

7871
-- | Convert a treeless term to its λ□ equivalent.
@@ -92,21 +85,26 @@ compileTermC = \case
9285
TDef qn -> do
9386
CompileEnv{mutuals, boundVars} <- ask
9487
case qn `elemIndex` mutuals of
95-
Nothing -> do require qn; pure $ LConst $ qnameToKName qn
88+
Nothing -> do lift $ requireDef qn; pure $ LConst $ qnameToKName qn
9689
Just i -> pure $ LRel $ i + boundVars
9790

98-
TCon q -> do require q; liftTCM (toConApp q [])
99-
TApp (TCon q) args -> do require q; liftTCM . toConApp q =<< mapM compileTermC args
91+
TCon q -> do
92+
lift $ requireDef q
93+
liftTCM $ toConApp q []
94+
95+
TApp (TCon q) args -> do
96+
lift $ requireDef q
97+
liftTCM . toConApp q =<< traverse compileTermC args
10098
-- ^ For dealing with fully-applied constructors
10199

102100
TApp u es -> do
103101
cu <- compileTermC u
104-
ces <- mapM compileTermC es
102+
ces <- traverse compileTermC es
105103
pure $ foldl' LApp cu ces
106104

107105
TLam t -> underBinder $ LLam <$> compileTermC t
108106

109-
TLit l -> fail "literals not supported"
107+
TLit l -> compileLit l
110108

111109
TLet u v -> LLet <$> compileTermC u
112110
<*> underBinder (compileTermC v)
@@ -116,7 +114,7 @@ compileTermC = \case
116114
Erased _ -> fail "Erased matches not supported."
117115
NotErased _ -> do
118116
cind <- compileCaseType caseType
119-
LCase cind 0 (LRel n) <$> mapM compileAlt talts
117+
LCase cind 0 (LRel n) <$> traverse compileAlt talts
120118

121119
TUnit -> return LBox
122120
TSort -> return LBox
@@ -125,10 +123,14 @@ compileTermC = \case
125123
TCoerce tt -> fail "Coerces not supported."
126124
TError terr -> fail "Errors not supported."
127125

126+
compileLit :: Literal -> C LBox.Term
127+
compileLit = \case
128+
l -> fail $ "unsupported literal: " <> prettyShow l
128129

129130
compileCaseType :: CaseType -> C LBox.Inductive
130131
compileCaseType = \case
131-
CTData qn -> do require qn; liftTCM $ toInductive qn
132+
CTData qn -> do lift $ requireDef qn
133+
liftTCM $ toInductive qn
132134
_ -> fail "case type not supported"
133135

134136

src/Main.hs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- | The agda2lambox Agda backend
33
module Main (main) where
44

5-
import Control.Monad ( unless, when, forM_ )
5+
import Control.Monad ( unless, when, forM_, filterM )
66
import Control.Monad.IO.Class ( liftIO )
77
import Control.DeepSeq ( NFData )
88
import Data.Function ( (&) )
@@ -25,16 +25,15 @@ import Agda.Utils.Monad ( whenM )
2525

2626
import Agda.Utils ( pp, hasPragma, isDataOrRecDef )
2727
import Agda2Lambox.Compile.Utils (qnameToKName)
28-
import Agda2Lambox.Compile.Monad (compile, compileLoop, requireDef)
29-
import Agda2Lambox.Compile (compileDefinition)
28+
import Agda2Lambox.Compile.Monad (compileLoop)
29+
import Agda2Lambox.Compile (compileDefinition)
3030
import CoqGen ( ToCoq(ToCoq) )
3131
import LambdaBox ( KerName, GlobalDecl, qnameToKerName, CoqModule(..) )
3232

3333

3434
main :: IO ()
3535
main = runAgda [agda2lambox]
3636

37-
3837
-- | Backend options.
3938
data Options = Options { optOutDir :: Maybe FilePath }
4039
deriving (Generic, NFData)
@@ -48,11 +47,7 @@ defaultOptions :: Options
4847
defaultOptions = Options { optOutDir = Nothing }
4948

5049
-- | Backend module environments.
51-
data ModuleEnv = ModuleEnv
52-
{ modProgs :: IORef [QName]
53-
-- ^ Names of programs to extract in a module
54-
}
55-
50+
type ModuleEnv = ()
5651
type ModuleRes = ()
5752

5853
-- | The adga2lambox backend.
@@ -73,7 +68,7 @@ agda2lambox = Backend backend
7368
, postCompile = \ _ _ _ -> return ()
7469
, preModule = moduleSetup
7570
, postModule = writeModule
76-
, compileDef = compileDef'
71+
, compileDef = \ _ _ _ -> pure . defName
7772
, scopeCheckingSuffices = False
7873
, mayEraseType = \ _ -> return True
7974
}
@@ -84,36 +79,18 @@ moduleSetup
8479
-> TCM (Recompile ModuleEnv ModuleRes)
8580
moduleSetup _ _ m _ = do
8681
setScope . iInsideScope =<< curIF
87-
Recompile . ModuleEnv <$> liftIO (newIORef [])
88-
82+
pure $ Recompile ()
8983

90-
compileDef'
91-
:: Options -> ModuleEnv -> IsMain
92-
-> Definition
93-
-> TCM QName
94-
compileDef' opts menv ismain Defn{defName} = do
95-
-- if it has a pragma, that's a program!
96-
when (ismain == IsMain) $
97-
whenM (hasPragma defName) $
98-
liftIO $ modifyIORef' (modProgs menv) (defName :)
9984

100-
pure defName
101-
102-
103-
-- | Collect global definitions and programs in a module
104-
-- and write them to disk.
10585
writeModule
10686
:: Options -> ModuleEnv -> IsMain -> TopLevelModuleName
10787
-> [QName]
10888
-> TCM ModuleRes
109-
writeModule opts menv NotMain _ _ = pure ()
89+
writeModule opts menv NotMain _ _ = pure ()
11090
writeModule opts menv IsMain m defs = do
111-
programs <- liftIO $ readIORef $ modProgs menv
112-
outDir <- flip fromMaybe (optOutDir opts) <$> compileDir
113-
114-
decls <- compile do
115-
forM_ (reverse defs) requireDef
116-
compileLoop compileDefinition
91+
programs <- filterM hasPragma defs
92+
outDir <- flip fromMaybe (optOutDir opts) <$> compileDir
93+
decls <- compileLoop compileDefinition $ reverse defs
11794

11895
liftIO $ createDirectoryIfMissing True outDir
11996

test/Imports.agda

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
open import Agda.Builtin.Bool
2+
open import Agda.Builtin.List
23

3-
test : Bool
4-
test = true
4+
test : List Bool
5+
test = true ∷ false ∷ []

test/Map.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ data List (A : Set) : Set where
1212
_::_ : A -> List A -> List A
1313

1414
xs : List Nat
15-
xs = zero :: succ zero :: succ (succ zero) :: []
15+
xs = zero :: []
1616

1717
map : {A B : Set} -> (A -> B) -> List A -> List B
1818
map f [] = []

test/Nat.agda

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
data Nat : Set where
2-
zero : Nat
3-
succ : Nat Nat
1+
open import Agda.Builtin.Nat
42

53
one : Nat
6-
one = succ zero
4+
one = suc zero
75

6+
{-
87
add : Nat → Nat → Nat
98
add zero y = y
10-
add (succ x) y = succ (add x y)
9+
add (suc x) y = suc (add x y)
1110
1211
incr : Nat → Nat
13-
incr = succ
12+
incr = suc
1413
1514
deux trois : Nat
16-
deux = succ (succ zero)
17-
trois = succ (succ (succ zero))
15+
deux = suc (suc zero)
16+
trois = suc (suc (suc zero))
1817
1918
prog : Nat
2019
prog = add trois deux
2120
{-# COMPILE AGDA2LAMBOX prog #-}
21+
-}

0 commit comments

Comments
 (0)