Skip to content

Commit d9d2958

Browse files
committed
refactor type compilation
1 parent 2b0aba1 commit d9d2958

File tree

7 files changed

+240
-194
lines changed

7 files changed

+240
-194
lines changed

src/Agda/Utils.hs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,8 @@ getInductiveParams Record{recPars} = recPars
5959
-- TODO: consider erased things to be logical
6060
-- so maybe take a Dom Type instead
6161

62-
isLogical, isArity :: Type -> TCM Bool
63-
64-
-- | Whether a type is logical.
65-
isLogical typ = do
66-
tel <- telView typ
67-
let sort = isSort $ unEl $ theCore $ tel
68-
case sort of
69-
Just (Univ UProp _) -> pure True
70-
Just (Inf UProp _) -> pure True
71-
Just LevelUniv -> pure True
72-
Just SizeUniv -> pure True
73-
_ -> pure False
74-
7562
-- | Whether a type is an arity.
63+
isArity :: Type -> TCM Bool
7664
isArity typ = do
7765
tel <- telView typ
7866
pure $ isJust $ isSort $ unEl $ theCore $ tel

src/Agda2Lambox/Compile.hs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE NamedFieldPuns, DataKinds, OverloadedStrings #-}
1+
{-# LANGUAGE NamedFieldPuns, DataKinds, OverloadedStrings, NondecreasingIndentation #-}
22
module Agda2Lambox.Compile
33
( compile
44
) where
@@ -8,10 +8,11 @@ import Data.IORef
88

99
import Agda.Compiler.Backend
1010
import Agda.Syntax.Internal ( QName )
11+
import Agda.Syntax.Common (Arg(..))
1112
import Agda.Syntax.Common.Pretty ( prettyShow )
1213
import Agda.TypeChecking.Monad ( liftTCM, getConstInfo )
1314
import Agda.TypeChecking.Pretty
14-
import Agda.Utils.Monad ( whenM )
15+
import Agda.Utils.Monad ( whenM, ifM )
1516

1617
import Agda.Utils ( hasPragma, isDataOrRecDef, treeless )
1718

@@ -35,25 +36,29 @@ compile t qs = GlobalEnv <$> compileLoop (compileDefinition t) qs
3536
compileDefinition :: Target t -> Definition -> CompileM (Maybe (KerName, GlobalDecl t))
3637
compileDefinition target defn@Defn{..} = setCurrentRange defName do
3738
reportSDoc "agda2lambox.compile" 1 $ "Compiling definition: " <+> prettyTCM defName
38-
fmap (qnameToKerName defName,) <$> -- prepend kername
39-
case theDef of
40-
GeneralizableVar{} -> pure Nothing
4139

42-
Axiom{} -> do
43-
typ <- whenTyped target $ compileTopLevelType defType
44-
pure $ Just $ ConstantDecl $ ConstantBody typ Nothing
40+
-- we skip logical definitions altogether
41+
ifM (liftTCM $ isLogical $ Arg defArgInfo defType) (pure Nothing) do
4542

46-
Constructor{conData} -> Nothing <$ requireDef conData
43+
-- prepend kername
44+
fmap (qnameToKerName defName,) <$> case theDef of
45+
GeneralizableVar{} -> pure Nothing
4746

48-
Function{} -> compileFunction target defn
47+
Axiom{} -> do
48+
typ <- whenTyped target $ compileTopLevelType defType
49+
pure $ Just $ ConstantDecl $ ConstantBody typ Nothing
4950

50-
d | isDataOrRecDef d -> compileInductive target defn
51+
Constructor{conData} -> Nothing <$ requireDef conData
5152

52-
Primitive{..} -> do
53-
reportSDoc "agda2lambox.compile" 5 $
54-
"Found primitive: " <> prettyTCM defName <> ". Compiling it as axiom."
53+
Function{} -> compileFunction target defn
5554

56-
typ <- whenTyped target $ compileTopLevelType defType
57-
pure $ Just $ ConstantDecl $ ConstantBody typ Nothing
55+
d | isDataOrRecDef d -> compileInductive target defn
5856

59-
_ -> genericError $ "Cannot compile: " <> prettyShow defName
57+
Primitive{..} -> do
58+
reportSDoc "agda2lambox.compile" 5 $
59+
"Found primitive: " <> prettyTCM defName <> ". Compiling it as axiom."
60+
61+
typ <- whenTyped target $ compileTopLevelType defType
62+
pure $ Just $ ConstantDecl $ ConstantBody typ Nothing
63+
64+
_ -> genericError $ "Cannot compile: " <> prettyShow defName

src/Agda2Lambox/Compile/Inductive.hs

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE NamedFieldPuns, ImportQualifiedPost, DataKinds, OverloadedStrings #-}
1+
{-# LANGUAGE NamedFieldPuns, ImportQualifiedPost, DataKinds, OverloadedStrings, UnicodeSyntax #-}
22
-- | Convert Agda datatypes to λ□ inductive declarations
33
module Agda2Lambox.Compile.Inductive
44
( compileInductive
@@ -19,15 +19,15 @@ import Agda.TypeChecking.Monad.Base hiding (None)
1919
import Agda.TypeChecking.Monad.Env ( withCurrentModule )
2020
import Agda.TypeChecking.Datatypes ( ConstructorInfo(..), getConstructorInfo, isDatatype )
2121
import Agda.TypeChecking.Pretty
22-
import Agda.TypeChecking.Telescope (telViewUpTo, piApplyM, teleArgs, telView)
22+
import Agda.TypeChecking.Telescope (telViewUpTo, piApplyM, teleArgs, telView, flattenTel)
2323
import Agda.TypeChecking.Substitute (TelView, TelV(theTel), apply)
2424
import Agda.Compiler.Backend ( getConstInfo, lookupMutualBlock, reportSDoc, AddContext (addContext), constructorForm)
2525
import Agda.Syntax.Common.Pretty ( prettyShow )
2626
import Agda.Syntax.Common (Arg)
2727
import Agda.Syntax.Internal
2828
import Agda.Utils.Monad ( unlessM )
2929

30-
import Agda.Utils ( isDataOrRecDef, isLogical, isArity )
30+
import Agda.Utils ( isDataOrRecDef, isArity )
3131
import Agda2Lambox.Compile.Target
3232
import Agda2Lambox.Compile.Utils
3333
import Agda2Lambox.Compile.Monad
@@ -86,6 +86,7 @@ data InductiveBundle = Bundle
8686
, indPars :: Int
8787
}
8888

89+
-- | Gather the shared information for compiling inductives.
8990
getBundle :: Definition -> InductiveBundle
9091
getBundle defn@Defn{defName, defType, theDef} =
9192
case theDef of
@@ -104,53 +105,54 @@ getBundle defn@Defn{defName, defType, theDef} =
104105
, indPars = recPars
105106
}
106107

107-
-- TODO(flupe):
108-
-- actually really unify the compilation of both, they do exactly the same thing
109-
actuallyConvertInductive :: forall t. Target t -> Definition -> CompileM (LBox.OneInductiveBody t)
108+
109+
actuallyConvertInductive :: t. Target t -> Definition -> CompileM (LBox.OneInductiveBody t)
110110
actuallyConvertInductive t defn = do
111111
let Bundle{..} = getBundle defn
112112

113113
params <- theTel <$> telViewUpTo indPars indType
114+
114115
reportSDoc "agda2lambox.compile.inductive" 10 $
115116
"Inductive parameters:" <+> prettyTCM params
116117

117118
let pvars :: [Arg Term] = teleArgs params
118119

119-
-- TODO(flupe): bind params iteratively to correctly figure out the type info
120-
tyvars <- whenTyped t $ forM (toList params) \pdom -> do
121-
let domType = unDom pdom
122-
isParamLogical <- liftTCM $ isLogical domType
123-
isParamArity <- liftTCM $ isArity domType
124-
let isParamSort = isJust $ isSort $ unEl $ domType
125-
pure LBox.TypeVarInfo
126-
{ tvarName = maybe LBox.Anon (LBox.Named . prettyShow) $ domName pdom
127-
, tvarIsLogical = isParamLogical
128-
, tvarIsArity = isParamArity
129-
, tvarIsSort = isParamSort
130-
}
131-
132-
ctors :: [LBox.ConstructorBody t] <-
133-
forM indCons \cname -> do
134-
arity <- liftTCM $ getConstructorInfo cname <&> \case
135-
DataCon arity -> arity
136-
RecordCon _ _ arity _ -> arity
137-
138-
conTypeInfo <- whenTyped t do
139-
conType <- liftTCM $ (`piApplyM` pvars) =<< defType <$> getConstInfo cname
140-
conTel <- toList . theTel <$> telView conType
141-
compileArgs indPars conTel
142-
143-
pure LBox.Constructor
144-
{ cstrName = prettyShow $ qnameName cname
145-
, cstrArgs = arity
146-
, cstrTypes = conTypeInfo
120+
addContext params do
121+
122+
tyvars <- whenTyped t $ forM (flattenTel params) \pdom -> do
123+
let domType = unDom pdom
124+
isParamLogical <- liftTCM $ isLogical pdom
125+
isParamArity <- liftTCM $ isArity domType
126+
let isParamSort = isJust $ isSort $ unEl $ domType
127+
pure LBox.TypeVarInfo
128+
{ tvarName = maybe LBox.Anon (LBox.Named . prettyShow) $ domName pdom
129+
, tvarIsLogical = isParamLogical
130+
, tvarIsArity = isParamArity
131+
, tvarIsSort = isParamSort
147132
}
148133

149-
pure LBox.OneInductive
150-
{ indName = prettyShow $ qnameName indName
151-
, indPropositional = False -- TODO(flupe)
152-
, indKElim = LBox.IntoAny -- TODO(flupe)
153-
, indCtors = ctors
154-
, indProjs = []
155-
, indTypeVars = tyvars
156-
}
134+
ctors :: [LBox.ConstructorBody t] <-
135+
forM indCons \cname -> do
136+
arity <- liftTCM $ getConstructorInfo cname <&> \case
137+
DataCon arity -> arity
138+
RecordCon _ _ arity _ -> arity
139+
140+
conTypeInfo <- whenTyped t do
141+
conType <- liftTCM $ (`piApplyM` pvars) =<< defType <$> getConstInfo cname
142+
conTel <- toList . theTel <$> telView conType
143+
compileArgs indPars conTel
144+
145+
pure LBox.Constructor
146+
{ cstrName = prettyShow $ qnameName cname
147+
, cstrArgs = arity
148+
, cstrTypes = conTypeInfo
149+
}
150+
151+
pure LBox.OneInductive
152+
{ indName = prettyShow $ qnameName indName
153+
, indPropositional = False -- TODO(flupe)
154+
, indKElim = LBox.IntoAny -- TODO(flupe)
155+
, indCtors = ctors
156+
, indProjs = []
157+
, indTypeVars = tyvars
158+
}

0 commit comments

Comments
 (0)