Skip to content

Commit b23c3c6

Browse files
committed
don't compile copies, unfold defs
1 parent 73c208c commit b23c3c6

File tree

12 files changed

+71
-21
lines changed

12 files changed

+71
-21
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ make -f CoqMakefile
3939
## TODO
4040

4141
- [ ] Type aliases (See #3)
42+
- [ ] Improve type compilation
43+
- The (re)implementation of the type translation is currently incomplete.
44+
In particular, when compiling an application, we should retrieve the type of the head and compile the elims with it.
4245
- [ ] Check support for Agda-specific edge cases
43-
- [ ] Module applications
4446
- [ ] Projection-like (See #6)
4547
- [ ] Support primitives (ints and floats)
4648
- [ ] Setup compilation to Wasm/Rust using Certicoq

agda2lambox.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ author: Orestis Melkonian, Carlos Tomé Cortiñas, Bohdan Liesnikov
55
category: Language, Compiler
66
build-type: Simple
77
synopsis: Compiling Agda code to λ-box.
8+
license: MIT
9+
license-file: LICENSE
810

911
extra-doc-files: README.md
1012

@@ -26,6 +28,7 @@ executable agda2lambox
2628
Agda2Lambox.Compile.Function,
2729
Agda2Lambox.Compile.Inductive,
2830
Agda2Lambox.Compile.Type,
31+
Agda2Lambox.Compile.TypeScheme,
2932
Agda2Lambox.Compile,
3033
LambdaBox.Names,
3134
LambdaBox.Term,

src/Agda/Utils.hs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Control.Applicative ( liftA2 )
77
import Data.Bifunctor ( second )
88
import Data.Maybe ( isJust, isNothing )
99

10-
import Agda.Compiler.Backend ( getUniqueCompilerPragma )
10+
import Agda.Compiler.Backend ( getUniqueCompilerPragma, PureTCM )
1111
import Agda.Syntax.Abstract.Name
1212
import Agda.Syntax.Internal
1313
import Agda.TypeChecking.Monad.Base hiding ( conArity )
@@ -27,6 +27,7 @@ import Agda.TypeChecking.Telescope (telView)
2727

2828
import Agda.Utils.EliminateDefaults qualified as TT
2929
import Agda.Utils.Treeless qualified as CustomTT
30+
import Agda.TypeChecking.Reduce (reduceDefCopy)
3031

3132
-- * Miscellaneous
3233

@@ -82,6 +83,20 @@ isRecordProjection d
8283
| otherwise
8384
= Nothing
8485

86+
-- | Try to unfold a definition if introduced by module application.
87+
maybeUnfoldCopy
88+
:: PureTCM m
89+
=> QName -- ^ Name of the definition.
90+
-> Elims
91+
-> (Term -> m a)
92+
-- ^ Callback if the definition is indeed a copy.
93+
-> (QName -> Elims -> m a)
94+
-- ^ Callback if the definition isn't a copy.
95+
-> m a
96+
maybeUnfoldCopy f es onTerm onDef =
97+
reduceDefCopy f es >>= \case
98+
NoReduction () -> onDef f es
99+
YesReduction _ t -> onTerm t
85100

86101
{-
87102
lookupCtx :: MonadTCEnv m => Int -> m (String, Type)

src/Agda/Utils/EliminateDefaults.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ import Agda.Compiler.Treeless.Subst () --instance only
2626

2727

2828
eliminateCaseDefaults :: TTerm -> TCM TTerm
29-
eliminateCaseDefaults term = do
30-
liftIO $ putStrLn "hello"
31-
tr term
29+
eliminateCaseDefaults = tr
3230
where
3331
tr :: TTerm -> TCM TTerm
3432
tr = \case

src/Agda2Lambox/Compile.hs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ import Agda.Syntax.Common (Arg(..))
1212
import Agda.Syntax.Common.Pretty ( prettyShow )
1313
import Agda.TypeChecking.Monad ( liftTCM, getConstInfo )
1414
import Agda.TypeChecking.Pretty
15-
import Agda.Utils.Monad ( whenM, ifM )
15+
import Agda.Utils.Monad ( whenM, ifM, unlessM, ifNotM, orM )
1616

17-
import Agda.Utils ( hasPragma, isDataOrRecDef, treeless )
17+
import Agda.Utils ( hasPragma, isDataOrRecDef, treeless, isArity )
1818

1919
import Agda2Lambox.Compile.Monad
2020
import Agda2Lambox.Compile.Target
2121
import Agda2Lambox.Compile.Utils
22-
import Agda2Lambox.Compile.Term ( compileTerm )
23-
import Agda2Lambox.Compile.Function ( compileFunction )
24-
import Agda2Lambox.Compile.Inductive ( compileInductive )
22+
import Agda2Lambox.Compile.Term ( compileTerm )
23+
import Agda2Lambox.Compile.Function ( compileFunction )
24+
import Agda2Lambox.Compile.Inductive ( compileInductive )
25+
import Agda2Lambox.Compile.TypeScheme ( compileTypeScheme )
2526

2627
import LambdaBox.Names
2728
import LambdaBox.Env (GlobalEnv(..), GlobalDecl(..), ConstantBody(..))
@@ -37,8 +38,12 @@ compileDefinition :: Target t -> Definition -> CompileM (Maybe (KerName, GlobalD
3738
compileDefinition target defn@Defn{..} = setCurrentRange defName do
3839
reportSDoc "agda2lambox.compile" 1 $ "Compiling definition: " <+> prettyTCM defName
3940

40-
-- we skip logical definitions altogether
41-
ifM (liftTCM $ isLogical $ Arg defArgInfo defType) (pure Nothing) do
41+
-- we skip logical definitions altogether,
42+
-- and definitions introduced by module application
43+
ifM
44+
(orM [ pure defCopy
45+
, liftTCM $ isLogical $ Arg defArgInfo defType])
46+
(pure Nothing) do
4247

4348
-- prepend kername
4449
fmap (qnameToKerName defName,) <$> case theDef of
@@ -50,7 +55,13 @@ compileDefinition target defn@Defn{..} = setCurrentRange defName do
5055

5156
Constructor{conData} -> Nothing <$ requireDef conData
5257

53-
Function{} -> compileFunction target defn
58+
Function{} -> do
59+
ifNotM (liftTCM $ isArity defType) (compileFunction target defn) do
60+
-- it's a type scheme
61+
case target of
62+
ToUntyped -> pure Nothing
63+
-- we only compile it with --typed
64+
ToTyped -> Just <$> compileTypeScheme defn
5465

5566
d | isDataOrRecDef d -> compileInductive target defn
5667

src/Agda2Lambox/Compile/Function.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Agda.Syntax.Common ( hasQuantityω )
2020
import Agda.Utils.Monad (guardWithError, whenM)
2121
import Agda.Utils.Lens ( (^.) )
2222

23-
import Agda.Utils ( treeless, pp, isRecordProjection )
23+
import Agda.Utils ( treeless, pp, isRecordProjection, isArity )
2424
import Agda2Lambox.Compile.Target
2525
import Agda2Lambox.Compile.Utils
2626
import Agda2Lambox.Compile.Monad

src/Agda2Lambox/Compile/Monad.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Queue.Ephemeral ( EphemeralQueue(..) )
1818
import Queue.Ephemeral qualified as Queue
1919

2020
import Agda.Syntax.Abstract (QName)
21-
import Agda.Compiler.Backend (getConstInfo)
21+
import Agda.Compiler.Backend (getConstInfo, PureTCM, HasConstInfo, HasBuiltins)
2222
import Agda.TypeChecking.Monad (MonadDebug, MonadTrace, MonadAddContext)
2323
import Agda.TypeChecking.Monad.Debug (MonadDebug, reportSDoc)
2424
import Agda.TypeChecking.Monad.Base hiding (initState)
@@ -51,7 +51,7 @@ newtype CompileM a = Compile (StateT CompileState TCM a)
5151
deriving newtype (Functor, Applicative, Monad)
5252
deriving newtype (MonadIO, MonadFail, MonadDebug, ReadTCState, MonadTrace)
5353
deriving newtype (MonadError TCErr, MonadTCEnv, MonadTCState, HasOptions, MonadTCM)
54-
deriving newtype (MonadAddContext, MonadReduce)
54+
deriving newtype (MonadAddContext, MonadReduce, HasConstInfo, HasBuiltins, PureTCM)
5555

5656
-- | Require a definition to be compiled.
5757
requireDef :: QName -> CompileM ()

src/Agda2Lambox/Compile/Term.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ compileLitPattern = \case
170170

171171
compileCaseType :: CaseType -> C LBox.Inductive
172172
compileCaseType = \case
173-
CTData qn -> do
173+
CTData qn -> do
174174
lift $ requireDef qn
175175
liftTCM $ toInductive qn
176176
CTNat -> do

src/Agda2Lambox/Compile/Type.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import qualified LambdaBox as LBox
2525
import Agda2Lambox.Compile.Utils ( qnameToKName, isLogical )
2626
import Agda2Lambox.Compile.Monad
2727
import Agda.Compiler.Backend (HasConstInfo(getConstInfo), Definition(Defn), AddContext (addContext))
28-
import Agda.Utils (isDataOrRecDef, getInductiveParams, isArity)
28+
import Agda.Utils (isDataOrRecDef, getInductiveParams, isArity, maybeUnfoldCopy)
2929
import Agda.TypeChecking.Substitute (absBody, TelV (theCore))
3030
import Agda.TypeChecking.Telescope (telView)
3131

@@ -133,7 +133,7 @@ compileTypeTerm = \case
133133
TypeVar n -> pure ([], LBox.TVar n)
134134
Other -> pure ([], LBox.TAny )
135135

136-
Def q es -> do
136+
Def q es -> maybeUnfoldCopy q es compileTypeTerm \q es -> do
137137
Defn{theDef = def, defType, defArgInfo, defName} <- liftTCM $ getConstInfo q
138138

139139
isLogicalDef <- liftTCM $ isLogical $ Arg defArgInfo defType
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{-# LANGUAGE DataKinds #-}
2+
module Agda2Lambox.Compile.TypeScheme where
3+
4+
import Control.Monad.IO.Class ( liftIO )
5+
6+
import Agda.Compiler.Backend
7+
import Agda.TypeChecking.Telescope (telView)
8+
import Agda.TypeChecking.Substitute (TelV(TelV))
9+
10+
import Agda2Lambox.Compile.Monad
11+
import Agda2Lambox.Compile.Target
12+
import Agda2Lambox.Compile.Utils
13+
import Agda2Lambox.Compile.Type
14+
import LambdaBox.Env
15+
16+
compileTypeScheme :: Definition -> CompileM (GlobalDecl Typed)
17+
compileTypeScheme Defn{..} = do
18+
TelV tyargs _ <- telView defType
19+
liftIO $ putStrLn "compiling type scheme"
20+
pure $ TypeAliasDecl $ Nothing

0 commit comments

Comments
 (0)