@@ -13,12 +13,13 @@ import Agda.TypeChecking.Telescope
1313
1414import Agda.Utils.Maybe
1515import Agda.Utils.Monad
16+ import Agda.Utils.Singleton
1617import Agda.Utils.Impossible ( __IMPOSSIBLE__ )
1718
1819import Agda2Hs.AgdaUtils
1920
2021import Agda2Hs.Compile.Name
21- import Agda2Hs.Compile.Type ( compileDomType , compileTeleBinds , compileDom , DomOutput (.. ) )
22+ import Agda2Hs.Compile.Type ( compileType , compileDomType , compileTeleBinds , compileDom , DomOutput (.. ) )
2223import Agda2Hs.Compile.Types
2324import Agda2Hs.Compile.Utils
2425
@@ -34,8 +35,8 @@ checkNewtype name cs = do
3435 (Hs. QualConDecl () _ _ (Hs. ConDecl () cName types): _) -> checkNewtypeCon cName types
3536 _ -> __IMPOSSIBLE__
3637
37- compileData :: AsNewType -> [Hs. Deriving () ] -> Definition -> C [Hs. Decl () ]
38- compileData newtyp ds def = do
38+ compileData :: DataTarget -> [Hs. Deriving () ] -> Definition -> C [Hs. Decl () ]
39+ compileData target ds def = do
3940 let d = hsName $ prettyShow $ qnameName $ defName def
4041 checkValidTypeName d
4142 let Datatype {dataPars = n, dataIxs = numIxs, dataCons = cs} = theDef def
@@ -48,12 +49,22 @@ compileData newtyp ds def = do
4849 -- TODO: filter out erased constructors
4950 cs <- mapM (compileConstructor params) cs
5051 let hd = foldl (Hs. DHApp () ) (Hs. DHead () d) binds
52+ let htarget = toDataTarget target
5153
52- let target = if newtyp then Hs. NewType () else Hs. DataType ( )
54+ when ( target == ToNewType ) (checkNewtype d $ map fst cs )
5355
54- when newtyp (checkNewtype d cs)
56+ return . singleton $ case target of
57+ ToGadt -> Hs. GDataDecl () htarget Nothing hd Nothing (map (uncurry conToGadtCon) cs) ds
58+ _ -> Hs. DataDecl () htarget Nothing hd (map fst cs) ds
5559
56- return [Hs. DataDecl () target Nothing hd cs ds]
60+ where
61+ conToGadtCon :: Hs. QualConDecl () -> Hs. Type () -> Hs. GadtDecl ()
62+ conToGadtCon (Hs. QualConDecl _ tys ctx con) rt = case con of
63+ Hs. ConDecl () c ts ->
64+ Hs. GadtDecl () c tys ctx Nothing $
65+ foldr (Hs. TyFun () ) rt ts
66+ Hs. InfixConDecl {} -> __IMPOSSIBLE__
67+ Hs. RecDecl {} -> __IMPOSSIBLE__
5768
5869allIndicesErased :: Type -> C ()
5970allIndicesErased t = reduce (unEl t) >>= \ case
@@ -64,19 +75,20 @@ allIndicesErased t = reduce (unEl t) >>= \case
6475 DomForall {} -> agda2hsError " Not supported: indexed datatypes"
6576 _ -> return ()
6677
67- compileConstructor :: [Arg Term ] -> QName -> C (Hs. QualConDecl () )
78+ compileConstructor :: [Arg Term ] -> QName -> C (Hs. QualConDecl () , Hs. Type () )
6879compileConstructor params c = do
6980 reportSDoc " agda2hs.data.con" 15 $ text " compileConstructor" <+> prettyTCM c
7081 reportSDoc " agda2hs.data.con" 20 $ text " params = " <+> prettyTCM params
7182 ty <- defType <$> getConstInfo c
7283 reportSDoc " agda2hs.data.con" 30 $ text " ty (before piApply) = " <+> prettyTCM ty
7384 ty <- ty `piApplyM` params
7485 reportSDoc " agda2hs.data.con" 20 $ text " ty = " <+> prettyTCM ty
75- TelV tel _ <- telView ty
86+ TelV tel ret <- telView ty
7687 let conName = hsName $ prettyShow $ qnameName c
7788 checkValidConName conName
7889 args <- compileConstructorArgs tel
79- return $ Hs. QualConDecl () Nothing Nothing $ Hs. ConDecl () conName args
90+ hret <- addContext tel $ compileType $ unEl ret
91+ return (Hs. QualConDecl () Nothing Nothing $ Hs. ConDecl () conName args, hret)
8092
8193compileConstructorArgs :: Telescope -> C [Hs. Type () ]
8294compileConstructorArgs EmptyTel = return []
@@ -132,12 +144,13 @@ checkCompileToDataPragma def s = noCheckNames $ do
132144 unless (length rcons == length dcons) $ fail
133145 " they have a different number of constructors"
134146 forM_ (zip dcons rcons) $ \ (c1, c2) -> do
135- Hs. QualConDecl _ _ _ (Hs. ConDecl _ hsC1 args1) <-
147+ ( Hs. QualConDecl _ _ _ (Hs. ConDecl _ hsC1 args1) , rt1 ) <-
136148 addContext dtel $ compileConstructor (teleArgs dtel) c1
137149 -- rename parameters of r to match those of d
138150 rtel' <- renameParameters dtel rtel
139- Hs. QualConDecl _ _ _ (Hs. ConDecl _ hsC2 args2) <-
151+ ( Hs. QualConDecl _ _ _ (Hs. ConDecl _ hsC2 args2) , rt2 ) <-
140152 addContext rtel' $ compileConstructor (teleArgs rtel') c2
153+ -- TODO: check that rt1 and rt2 are equal?
141154 unless (hsC1 == hsC2) $ fail $
142155 " name of constructor" <+> text (Hs. pp hsC1) <+>
143156 " does not match" <+> text (Hs. pp hsC2)
0 commit comments