@@ -3,7 +3,6 @@ module Agda2Lambox.Compile.Term
33 ( compileTerm
44 ) where
55
6- import Control.Monad ( mapM )
76import Control.Monad.Fail ( MonadFail )
87import Control.Monad.IO.Class (MonadIO )
98import Control.Monad.Reader.Class ( MonadReader , ask )
@@ -17,6 +16,7 @@ import Agda.Compiler.Backend ( getConstInfo, theDef, pattern Datatype, dataMutua
1716import Agda.Syntax.Abstract.Name ( ModuleName (.. ), QName (.. ) )
1817import Agda.Syntax.Common ( Erased (.. ) )
1918import Agda.Syntax.Common.Pretty ( prettyShow )
19+ import Agda.Syntax.Literal
2020import Agda.Syntax.Treeless ( TTerm (.. ), TAlt (.. ), CaseInfo (.. ), CaseType (.. ) )
2121import Agda.TypeChecking.Datatypes ( getConstructorData , getConstructors )
2222import 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.
5653runC :: 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.
6057underBinders :: Int -> C a -> C a
@@ -69,10 +66,6 @@ underBinder = underBinders 1
6966withMutuals :: [QName ] -> C a -> C a
7067withMutuals 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
129130compileCaseType :: CaseType -> C LBox. Inductive
130131compileCaseType = \ 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
0 commit comments