Skip to content

Commit 97f7dc2

Browse files
committed
added basic support for primitive literals
1 parent 267d74b commit 97f7dc2

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

src/Agda2Lambox/Compile/Term.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Control.Monad.Trans
1111
import Data.List ( elemIndex, foldl', singleton )
1212
import Data.Maybe ( fromMaybe, listToMaybe )
1313
import Data.Foldable ( foldrM )
14+
import Data.Text qualified as Text ( unpack )
1415

1516
import Agda.Compiler.Backend ( MonadTCState, HasOptions, canonicalName )
1617
import Agda.Compiler.Backend ( getConstInfo, theDef, pattern Datatype, dataMutual )
@@ -154,16 +155,20 @@ compileTermC = \case
154155
compileLit :: Literal -> C LBox.Term
155156
compileLit = \case
156157

158+
-- TODO(flupe):
159+
-- optionally attempt compiling this to an Int63 Primitive
157160
LitNat i -> do
158161
qn <- liftTCM $ getBuiltinName_ builtinNat
159162
qz <- liftTCM $ getBuiltinName_ builtinZero
160163
qs <- liftTCM $ getBuiltinName_ builtinSuc
161164
lift do
162165
requireDef qn
163-
iterate (toConApp qs . singleton =<<)
164-
(toConApp qz [])
166+
iterate (toConApp qs . singleton =<<)
167+
(toConApp qz [])
165168
!! fromInteger i
166169

170+
LitString s -> pure $ LBox.LPrim $ LBox.PString $ Text.unpack s
171+
167172
l -> genericError $ "unsupported literal: " <> prettyShow l
168173

169174

src/LambdaBox/Term.hs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,61 @@
22
-- | Definition of λ□ terms.
33
module LambdaBox.Term where
44

5+
import Data.Int (Int64)
56
import Data.Bifunctor (first)
67
import Agda.Syntax.Common.Pretty
78
import LambdaBox.Names
89

910

10-
-- | Definition component in a mutual fixpoint.
11+
-- | Definition component in a mutual fixpoint
1112
data Def t = Def
1213
{ dName :: Name
1314
, dBody :: t
1415
, dArgs :: Int
1516
}
1617

17-
-- | Mutual components of a fixpoint.
18+
-- | Mutual components of a fixpoint
1819
type MFixpoint = [Def Term]
1920

21+
-- |
22+
data PrimValue
23+
= PInt Int64
24+
-- NOTE(flupe): ^ Should ensure they are restricted to Int63
25+
| PFloat Int64
26+
| PString String
27+
2028
-- | λ□ terms
2129
data Term
22-
= LBox -- ^ Proofs and erased terms
23-
| LRel Int -- ^ Bound variable, with de Bruijn index
24-
| LLambda Name Term -- ^ Lambda abstraction
25-
| LLetIn Name Term Term
26-
-- ^ Let bindings.
27-
-- Unused in the backend, since Agda itself no longer has let bindings
28-
-- in the concrete syntac.
30+
= LBox -- ^ Proofs and erased terms
31+
| LRel Int -- ^ Bound variable, with de Bruijn index
32+
| LLambda Name Term -- ^ Lambda abstraction
33+
| LLetIn Name Term Term -- ^ Let bindings
2934
| LApp Term Term -- ^ Term application
30-
| LConst KerName -- ^ Named constant.
31-
| LConstruct Inductive Int [Term] -- ^ Inductive constructor.
32-
| LCase -- ^ Pattern-matching case construct.
33-
Inductive -- ^ Inductive type we cae on.
35+
| LConst KerName -- ^ Named constant
36+
| LConstruct Inductive Int [Term] -- ^ Inductive constructor
37+
| LCase -- ^ Pattern-matching case construct
38+
Inductive -- ^ Inductive type we case on
3439
Int -- ^ Number of parameters
3540
Term -- ^ Discriminee
3641
[([Name], Term)] -- ^ Branches
37-
| LFix -- ^ Fixpoint combinator.
42+
| LFix -- ^ Fixpoint combinator
3843
MFixpoint
39-
Int -- ^ Index of the fixpoint we keep.
44+
Int -- ^ Index of the fixpoint we keep
45+
| LPrim PrimValue
46+
-- ^ Primitive literal value
4047

4148

4249
instance Pretty t => Pretty (Def t) where
4350
-- prettyPrec _ (Def s _ _) = pretty s
4451
prettyPrec _ (Def _ t _) = pretty t
4552

53+
54+
instance Pretty PrimValue where
55+
pretty (PInt i) = text $ show i
56+
pretty (PFloat f) = text $ show f
57+
pretty (PString f) = text $ show f
58+
59+
4660
instance Pretty Term where
4761
prettyPrec p v =
4862
case v of
@@ -59,7 +73,6 @@ instance Pretty Term where
5973
in
6074
mparens (p > 0) $
6175
hang ("λ" <+> sep (map pretty (n:ns)) <+> "") 2 $ pretty t'
62-
6376

6477
LLetIn n e t ->
6578
mparens (p > 0) $ sep
@@ -85,3 +98,6 @@ instance Pretty Term where
8598
LFix ds i -> -- FIXME: for mutual recursion
8699
mparens (p > 0) $
87100
hang "μ rec ->" 2 $ pretty $ ds !! i
101+
102+
LPrim p -> pretty p
103+

src/SExpr.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ctor :: Target t -> Text -> [Sexpable t] -> Exp
5050
ctor t n [] = SAtom (ANode n)
5151
ctor t n xs = SList () (SAtom (ANode n) : map (toSexp t) xs)
5252

53+
instance ToSexp t Atom where toSexp _ = SAtom
5354
instance ToSexp t (Sexp Atom) where toSexp _ d = d
5455
instance ToSexp t Int where toSexp _ s = SAtom (AInt s)
5556
instance ToSexp t Bool where toSexp _ s = SAtom (ABool s)
@@ -88,6 +89,20 @@ instance ToSexp t Inductive where
8889
instance ToSexp t d => ToSexp t (Def d) where
8990
toSexp t Def{..} = ctor t "def" [S dName, S dBody, S dArgs]
9091

92+
instance ToSexp t PrimValue where
93+
toSexp t x = toSexp t (primTag x, primVal x)
94+
where
95+
primTag = \case
96+
PInt{} -> ctor t "primInt" []
97+
PFloat{} -> ctor t "primFloat" []
98+
PString{} -> ctor t "primString" []
99+
100+
primVal = \case
101+
PInt i -> AString (show i)
102+
PFloat f -> AString (show f)
103+
PString s -> AString s
104+
105+
91106
instance ToSexp t Term where
92107
toSexp t = \case
93108
LBox -> ctor t "tBox" []
@@ -99,6 +114,7 @@ instance ToSexp t Term where
99114
LConstruct ind i es -> ctor t "tConstruct" [S ind, S i, S es]
100115
LCase ind n u bs -> ctor t "tCase" [S (ind, n), S u, S bs]
101116
LFix mf i -> ctor t "tFix" [S mf, S i]
117+
LPrim p -> ctor t "tPrim" [S p]
102118

103119
instance ToSexp t Type where
104120
toSexp t = \case

test/String.agda

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
open import Agda.Builtin.String
2+
3+
demo : String
4+
demo = "Hello, World!"
5+
{-# COMPILE AGDA2LAMBOX demo #-}

0 commit comments

Comments
 (0)