1- {-# LANGUAGE NamedFieldPuns, ImportQualifiedPost, DataKinds, OverloadedStrings #-}
1+ {-# LANGUAGE NamedFieldPuns, ImportQualifiedPost, DataKinds, OverloadedStrings, UnicodeSyntax #-}
22-- | Convert Agda datatypes to λ□ inductive declarations
33module Agda2Lambox.Compile.Inductive
44 ( compileInductive
@@ -19,15 +19,15 @@ import Agda.TypeChecking.Monad.Base hiding (None)
1919import Agda.TypeChecking.Monad.Env ( withCurrentModule )
2020import Agda.TypeChecking.Datatypes ( ConstructorInfo (.. ), getConstructorInfo , isDatatype )
2121import Agda.TypeChecking.Pretty
22- import Agda.TypeChecking.Telescope (telViewUpTo , piApplyM , teleArgs , telView )
22+ import Agda.TypeChecking.Telescope (telViewUpTo , piApplyM , teleArgs , telView , flattenTel )
2323import Agda.TypeChecking.Substitute (TelView , TelV (theTel ), apply )
2424import Agda.Compiler.Backend ( getConstInfo , lookupMutualBlock , reportSDoc , AddContext (addContext ), constructorForm )
2525import Agda.Syntax.Common.Pretty ( prettyShow )
2626import Agda.Syntax.Common (Arg )
2727import Agda.Syntax.Internal
2828import Agda.Utils.Monad ( unlessM )
2929
30- import Agda.Utils ( isDataOrRecDef , isLogical , isArity )
30+ import Agda.Utils ( isDataOrRecDef , isArity )
3131import Agda2Lambox.Compile.Target
3232import Agda2Lambox.Compile.Utils
3333import Agda2Lambox.Compile.Monad
@@ -86,6 +86,7 @@ data InductiveBundle = Bundle
8686 , indPars :: Int
8787 }
8888
89+ -- | Gather the shared information for compiling inductives.
8990getBundle :: Definition -> InductiveBundle
9091getBundle 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 )
110110actuallyConvertInductive 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