Skip to content

Commit 93e5cc8

Browse files
committed
add support for nat literals
1 parent d0300fa commit 93e5cc8

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

agda2lambox.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ executable agda2lambox
2828
Paths_agda2lambox
2929
autogen-modules: Paths_agda2lambox
3030
build-depends: base >= 4.10 && < 4.22,
31-
Agda >= 2.7 && <= 2.9,
31+
Agda >= 2.8 && < 2.9,
3232
deepseq >= 1.4.4 && < 1.6,
3333
queues >= 1.0.0 && < 1.2,
3434
pretty-show,
3535
containers,
3636
directory,
3737
filepath,
38+
text,
3839
mtl
3940
default-language: Haskell2010
4041
default-extensions:

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
packages: .
1+
packages: agda2lambox.cabal
22

33
source-repository-package
44
type: git

src/Agda2Lambox/Compile/Function.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Agda.Syntax.Common ( hasQuantityω )
1919
import Agda.Utils.Monad (guardWithError, whenM)
2020
import Agda.Utils.Lens ( (^.) )
2121

22-
import Agda.Utils ( etaExpandCtor )
22+
import Agda.Utils ( etaExpandCtor, treeless, pp )
2323
import Agda2Lambox.Compile.Utils
2424
import Agda2Lambox.Compile.Monad
2525
import Agda2Lambox.Compile.Term ( compileTerm )
@@ -35,7 +35,7 @@ isFunction _ = False
3535
-- | Convert a function body to a Lambdabox term.
3636
compileFunctionBody :: [QName] -> Definition -> CompileM LBox.Term
3737
compileFunctionBody ms Defn{defName, theDef} = do
38-
Just t <- liftTCM $ toTreeless EagerEvaluation defName
38+
Just t <- liftTCM $ treeless defName
3939
compileTerm ms =<< liftTCM (etaExpandCtor t)
4040

4141

src/Agda2Lambox/Compile/Term.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ import Control.Monad.IO.Class (MonadIO)
88
import Control.Monad.Reader.Class ( MonadReader, ask )
99
import Control.Monad.Reader ( ReaderT(runReaderT), local )
1010
import Control.Monad.Trans
11-
import Data.List ( elemIndex, foldl' )
11+
import Data.List ( elemIndex, foldl', singleton )
1212
import Data.Maybe ( fromMaybe, listToMaybe )
13+
import Data.Foldable ( foldrM )
1314

1415
import Agda.Compiler.Backend ( MonadTCState, HasOptions )
1516
import Agda.Compiler.Backend ( getConstInfo, theDef, pattern Datatype, dataMutual )
1617
import Agda.Syntax.Abstract.Name ( ModuleName(..), QName(..) )
18+
import Agda.Syntax.Builtin ( builtinZero, builtinSuc )
1719
import Agda.Syntax.Common ( Erased(..) )
1820
import Agda.Syntax.Common.Pretty ( prettyShow )
1921
import Agda.Syntax.Literal
2022
import Agda.Syntax.Treeless ( TTerm(..), TAlt(..), CaseInfo(..), CaseType(..) )
2123
import Agda.TypeChecking.Datatypes ( getConstructorData, getConstructors )
2224
import Agda.TypeChecking.Monad.Base ( TCM , liftTCM, MonadTCEnv, MonadTCM )
25+
import Agda.TypeChecking.Monad.Builtin ( getBuiltinName_ )
2326

2427
import LambdaBox ( Term(..) )
2528
import LambdaBox qualified as LBox
@@ -125,7 +128,15 @@ compileTermC = \case
125128

126129
compileLit :: Literal -> C LBox.Term
127130
compileLit = \case
128-
l -> fail $ "unsupported literal: " <> prettyShow l
131+
132+
LitNat i -> do
133+
qz <- liftTCM $ getBuiltinName_ builtinZero
134+
qs <- liftTCM $ getBuiltinName_ builtinSuc
135+
z <- liftTCM $ toConApp qz []
136+
let ss = take (fromInteger i) $ repeat (toConApp qs . singleton)
137+
liftTCM $ foldrM ($) z ss
138+
139+
l -> fail $ "unsupported literal: " <> prettyShow l
129140

130141
compileCaseType :: CaseType -> C LBox.Inductive
131142
compileCaseType = \case

src/Main.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE DeriveGeneric, DeriveAnyClass, NamedFieldPuns #-}
1+
{-# LANGUAGE DeriveGeneric, DeriveAnyClass, NamedFieldPuns, OverloadedStrings #-}
22
-- | The agda2lambox Agda backend
33
module Main (main) where
44

@@ -9,6 +9,7 @@ import Data.Function ( (&) )
99
import Data.IORef ( IORef, newIORef, readIORef, modifyIORef' )
1010
import Data.Maybe ( fromMaybe, catMaybes )
1111
import Data.Version ( showVersion )
12+
import Data.Text ( pack )
1213
import GHC.Generics ( Generic )
1314
import System.Console.GetOpt ( OptDescr(Option), ArgDescr(ReqArg) )
1415
import System.Directory ( createDirectoryIfMissing )
@@ -57,7 +58,9 @@ agda2lambox = Backend backend
5758
backend :: Backend' Options Options ModuleEnv ModuleRes QName
5859
backend = Backend'
5960
{ backendName = "agda2lambox"
60-
, backendVersion = Just $ showVersion version
61+
, backendInteractTop = Nothing
62+
, backendInteractHole = Nothing
63+
, backendVersion = Just $ pack $ showVersion version
6164
, options = defaultOptions
6265
, commandLineFlags =
6366
[ Option ['o'] ["out-dir"] (ReqArg outdirOpt "DIR")

test/Imports.agda

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
open import Agda.Builtin.Bool
22
open import Agda.Builtin.List
3+
open import Agda.Builtin.Nat
34

45
test : List Bool
56
test = true ∷ false ∷ []
7+
8+
map : {A B : Set} (A B) List A List B
9+
map f [] = []
10+
map f (x ∷ xs) = f x ∷ map f xs
11+
12+
test2 : List Nat
13+
test2 = map suc (1 ∷ [])

0 commit comments

Comments
 (0)