Skip to content

Commit 6aba0c6

Browse files
committed
backup
1 parent db449e3 commit 6aba0c6

File tree

5 files changed

+64
-35
lines changed

5 files changed

+64
-35
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ cabal run agda2lambox -- --out-dir build -itest test/Nat.agda
3131
- [x] Evaluate λ□ programs from inside Coq to start testing
3232
- [x] Using the λ□-Mut from CertiCoq
3333
- [ ] Support mutual inductives
34+
- [ ] Refactor backend
35+
- Get rid of the Convert class.
36+
- Possibly put everything in a single module.
37+
- Unify compilation of records and datatypes.
3438
- [x] Support (one-inductive) records
3539
- [x] Properly translate projections in terms (by actually generating projections)
3640
- [ ] Support mutual (possibly inductive) records

agda2lambox.cabal

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ executable agda2lambox
3131
CoqGen,
3232
Paths_agda2lambox
3333
autogen-modules: Paths_agda2lambox
34-
build-depends: base >= 4.10 && < 4.22,
35-
Agda >= 2.7 && <= 2.8,
36-
deepseq >= 1.4.4 && < 1.6,
34+
build-depends: base >= 4.10 && < 4.22,
35+
Agda >= 2.7 && <= 2.8,
36+
deepseq >= 1.4.4 && < 1.6,
37+
containers >= 0.7 && < 0.8,
3738
pretty-show,
3839
directory,
3940
filepath,

src/Agda2Lambox/Convert/Data.hs

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
{-# LANGUAGE NamedFieldPuns #-}
1+
{-# LANGUAGE NamedFieldPuns, ImportQualifiedPost #-}
22
-- | Convert Agda datatypes to λ□ inductive declarations
33
module Agda2Lambox.Convert.Data
44
( convertDatatype
55
) where
66

77
import Control.Monad.Reader ( ask, liftIO )
8-
import Control.Monad ( forM, when, unless )
8+
import Control.Monad ( forM, when, unless, (>=>) )
9+
import Data.Traversable ( mapM )
10+
import Data.Foldable ( toList )
911
import Data.List ( elemIndex )
1012
import Data.Maybe ( isJust )
1113

@@ -17,9 +19,9 @@ import Agda.Utils
1719
import Agda.Syntax.Abstract.Name ( qnameModule, qnameName )
1820
import Agda.TypeChecking.Monad.Base
1921
import Agda.TypeChecking.Monad.Env ( withCurrentModule )
20-
import Agda.TypeChecking.Datatypes ( ConstructorInfo(..), getConstructorInfo )
22+
import Agda.TypeChecking.Datatypes ( ConstructorInfo(..), getConstructorInfo, isDatatype )
2123
import Agda.Compiler.ToTreeless ( toTreeless )
22-
import Agda.Compiler.Backend ( getConstInfo )
24+
import Agda.Compiler.Backend ( getConstInfo, lookupMutualBlock )
2325
import Agda.Syntax.Treeless ( EvaluationStrategy(EagerEvaluation) )
2426
import Agda.Syntax.Common.Pretty ( prettyShow )
2527

@@ -30,16 +32,46 @@ import Agda2Lambox.Convert.Class
3032
import Agda.Utils.Monad (guardWithError)
3133

3234

33-
-- | Convert a datatype definition to a Lambdabox declaration.
34-
convertDatatype :: Definition :~> GlobalDecl
35-
convertDatatype defn@Defn{defName, theDef, defMutual} =
36-
withCurrentModule (qnameModule defName) do
37-
let Datatype{..} = theDef
35+
-- | Toplevel conversion from a datatype definition to a Lambdabox declaration.
36+
convertDatatype :: Definition :~> Maybe GlobalDecl
37+
convertDatatype defn@Defn{defName, defMutual} = do
38+
-- we lookup names in the mutual block
39+
MutualBlock{mutualNames} <- lookupMutualBlock defMutual
40+
41+
let names = toList mutualNames
42+
43+
-- we consider that the *lowest name* in the mutual block
44+
-- is the *representative* of the mutual block
45+
-- i.e that's when we trigger the compilation of the mutual block
46+
47+
if defName /= head names then return Nothing
48+
else do
49+
50+
-- when it's time to compile the mutual block
51+
-- we make sure that all definitions in the block are datatypes (for now)
52+
53+
onlyDatas :: Bool <- and <$> mapM (liftTCM . isDatatype) names
54+
55+
unless onlyDatas $ fail "not supported: mutual datatypes with non-datatypes"
56+
57+
bodies <- forM names $ liftTCM . getConstInfo >=> actuallyConvertDatatype
58+
59+
return $ Just $ InductiveDecl $ MutualInductive
60+
{ indFinite = Finite
61+
-- NOTE(flupe): Agda's datatypes are *always* finite?
62+
-- Co-induction is restricted to records.
63+
-- We may want to set BiFinite for non-recursive datatypes, but I don't know yet.
64+
-- in anycase, once we also accept coinductive records in the mix, probably we should pick CoFinite
65+
, indPars = 0
66+
, indBodies = bodies
67+
}
3868

39-
let Just muts = dataMutual
4069

41-
unless (length muts < 2) $
42-
fail $ "mututal datatypes not supported" <> prettyShow dataMutual
70+
71+
actuallyConvertDatatype :: Definition :~> OneInductiveBody
72+
actuallyConvertDatatype defn@Defn{defName, theDef, defMutual} =
73+
withCurrentModule (qnameModule defName) do
74+
let Datatype{..} = theDef
4375

4476
ctors :: [ConstructorBody]
4577
<- forM dataCons \cname -> do
@@ -49,22 +81,14 @@ convertDatatype defn@Defn{defName, theDef, defMutual} =
4981
, ctorArgs = arity
5082
}
5183

52-
let
53-
inductive = OneInductive
54-
{ indName = prettyShow $ qnameName defName
55-
, indPropositional = False
56-
-- TODO(flupe): ^ take care of this (use datatypeSort to figure this out)
57-
, indKElim = IntoAny
58-
-- TODO(flupe): also take care of this (with the Sort)
59-
, indCtors = ctors
60-
, indProjs = []
61-
}
62-
63-
return $ InductiveDecl $ MutualInductive
64-
{ indFinite = Finite
65-
-- NOTE(flupe): Agda's datatypes are *always* finite?
66-
-- Co-induction is restricted to records.
67-
-- We may want to set BiFinite for non-recursive datatypes, but I don't know yet.
68-
, indPars = 0
69-
, indBodies = [inductive]
84+
return OneInductive
85+
{ indName = prettyShow $ qnameName defName
86+
, indPropositional = False
87+
-- TODO(flupe): ^ take care of this (use datatypeSort to figure this out)
88+
, indKElim = IntoAny
89+
-- TODO(flupe): also take care of this (with the Sort)
90+
, indCtors = ctors
91+
, indProjs = []
7092
}
93+
94+

src/Agda2Lambox/Convert/Function.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ convertFunction defn@Defn{defName, theDef} =
5757
withCurrentModule (qnameModule defName) do
5858
let Function{funMutual = Just ms} = theDef
5959

60-
if null ms then
60+
if length ms < 2 then
6161
Just. ConstantDecl . Just <$> convertFunctionBody defn
6262
else do
6363
mdefs :: [Definition] <- mapM getConstInfo ms

src/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ compile opts menv _ def@Defn{..} =
9090

9191
runC0 (convertFunction def)
9292

93-
Datatype{} -> Just <$> runC0 (convertDatatype def)
93+
Datatype{} -> runC0 (convertDatatype def)
9494

9595
Record{} -> Just <$> runC0 (convertRecord def)
9696

0 commit comments

Comments
 (0)