@@ -2,7 +2,7 @@ module Agda2Hs.Compile.Data where
2
2
3
3
import qualified Language.Haskell.Exts.Syntax as Hs
4
4
5
- import Control.Monad ( when )
5
+ import Control.Monad ( unless , when )
6
6
import Agda.Compiler.Backend
7
7
import Agda.Syntax.Common
8
8
import Agda.Syntax.Internal
@@ -14,8 +14,9 @@ import Agda.TypeChecking.Substitute
14
14
import Agda.TypeChecking.Telescope
15
15
16
16
import Agda.Utils.Impossible ( __IMPOSSIBLE__ )
17
+ import Agda.Utils.Monad ( mapMaybeM )
17
18
18
- import Agda2Hs.Compile.Type ( compileDomType , compileTeleBinds )
19
+ import Agda2Hs.Compile.Type ( compileDomType , compileTeleBinds , compileDom , DomOutput ( .. ) )
19
20
import Agda2Hs.Compile.Types
20
21
import Agda2Hs.Compile.Utils
21
22
import Agda2Hs.HsUtils
@@ -46,15 +47,15 @@ compileData newtyp ds def = do
46
47
when newtyp (checkNewtype d cs)
47
48
48
49
return [Hs. DataDecl () target Nothing hd cs ds]
49
- where
50
- allIndicesErased :: Type -> C ()
51
- allIndicesErased t = reduce (unEl t) >>= \ case
52
- Pi dom t -> compileDomType (absName t) dom >>= \ case
53
- DomDropped -> allIndicesErased (unAbs t)
54
- DomType {} -> genericDocError =<< text " Not supported: indexed datatypes"
55
- DomConstraint {} -> genericDocError =<< text " Not supported: constraints in types"
56
- DomForall {} -> genericDocError =<< text " Not supported: indexed datatypes"
57
- _ -> return ()
50
+
51
+ allIndicesErased :: Type -> C ()
52
+ allIndicesErased t = reduce (unEl t) >>= \ case
53
+ Pi dom t -> compileDomType (absName t) dom >>= \ case
54
+ DomDropped -> allIndicesErased (unAbs t)
55
+ DomType {} -> genericDocError =<< text " Not supported: indexed datatypes"
56
+ DomConstraint {} -> genericDocError =<< text " Not supported: constraints in types"
57
+ DomForall {} -> genericDocError =<< text " Not supported: indexed datatypes"
58
+ _ -> return ()
58
59
59
60
compileConstructor :: [Arg Term ] -> QName -> C (Hs. QualConDecl () )
60
61
compileConstructor params c = do
@@ -77,3 +78,42 @@ compileConstructorArgs (ExtendTel a tel) = compileDomType (absName tel) a >>= \c
77
78
DomConstraint hsA -> genericDocError =<< text " Not supported: constructors with class constraints"
78
79
DomDropped -> underAbstraction a tel compileConstructorArgs
79
80
DomForall {} -> __IMPOSSIBLE__
81
+
82
+
83
+ checkUnboxedDataPragma :: Definition -> C ()
84
+ checkUnboxedDataPragma def = do
85
+ let Datatype {.. } = theDef def
86
+
87
+ -- unboxed datatypes shouldn't be recursive
88
+ unless (all null dataMutual) $ genericDocError
89
+ =<< text " Unboxed datatype" <+> prettyTCM (defName def)
90
+ <+> text " cannot be recursive"
91
+
92
+ TelV tel t <- telViewUpTo dataPars (defType def)
93
+ let params :: [Arg Term ] = teleArgs tel
94
+
95
+ allIndicesErased t
96
+
97
+ case dataCons of
98
+ [con] -> do
99
+ info <- getConstInfo con
100
+ let Constructor {.. } = theDef info
101
+ ty <- defType info `piApplyM` params
102
+ TelV conTel _ <- telView ty
103
+ args <- nonErasedArgs conTel
104
+ unless (length args == 1 ) $ genericDocError
105
+ =<< text " Unboxed datatype" <+> prettyTCM (defName def)
106
+ <+> text " should have a single constructor with exactly one non-erased argument."
107
+
108
+ _ -> genericDocError =<< text " Unboxed datatype" <+> prettyTCM (defName def)
109
+ <+> text " must have exactly one constructor."
110
+
111
+ where
112
+ nonErasedArgs :: Telescope -> C [String ]
113
+ nonErasedArgs EmptyTel = return []
114
+ nonErasedArgs (ExtendTel a tel) = compileDom a >>= \ case
115
+ DODropped -> underAbstraction a tel nonErasedArgs
116
+ DOType -> genericDocError =<< text " Type argument in unboxed datatype not supported"
117
+ DOInstance -> genericDocError =<< text " Instance argument in unboxed datatype not supported"
118
+ DOTerm -> (absName tel: ) <$> underAbstraction a tel nonErasedArgs
119
+
0 commit comments