Skip to content

Commit 55841cf

Browse files
committed
Use CustomBackendError instead of Generic(Doc)Error
1 parent 43e0979 commit 55841cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+198
-172
lines changed

src/Agda2Hs/Compile.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ compile opts tlm _ def =
106106
(DefaultPragma _ , Function{}) -> compileFun True def
107107
(DefaultPragma ds , Record{} ) -> pure <$> compileRecord (ToRecord False ds) def
108108

109-
_ -> genericDocError =<< text "Don't know how to compile" <+> prettyTCM (defName def)
109+
_ -> agda2hsErrorM $ text "Don't know how to compile" <+> prettyTCM (defName def)
110110

111111
postCompile :: C ()
112112
postCompile = whenM (gets $ lcaseUsed >>> (> 0)) $ tellExtension Hs.LambdaCase
@@ -131,14 +131,14 @@ verifyOutput _ _ _ m ls = do
131131
Hs.RecDecl _ n _ -> n
132132
duplicateCons = filter ((> 1) . length) . group . sort $ allCons
133133
when (length duplicateCons > 0) $
134-
genericDocError =<< vcat (map (\x -> text $ "Cannot generate multiple constructors with the same identifier: " <> Hs.prettyPrint (headWithDefault __IMPOSSIBLE__ x)) duplicateCons)
134+
agda2hsErrorM $ vcat (map (\x -> text $ "Cannot generate multiple constructors with the same identifier: " <> Hs.prettyPrint (headWithDefault __IMPOSSIBLE__ x)) duplicateCons)
135135

136136
ensureNoOutputFromHsModules = unless (null $ concat $ map fst ls) $ do
137137
let hsModName = hsTopLevelModuleName m
138138
case hsModuleKind hsModName of
139139
HsModule -> do
140140
reportSDoc "agda2hs.compile" 10 $ text "Haskell module" <+> prettyTCM m <+> text "has non-null output."
141-
genericDocError =<< hsep
141+
agda2hsErrorM $ hsep
142142
( pwords "The `Haskell.` namespace are reserved for binding existing Haskell modules, so the module"
143143
++ [text "`" <> prettyTCM m <> text "`"]
144144
++ pwords "should not contain any"

src/Agda2Hs/Compile/ClassInstance.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ compileInstance ToDefinition def@Defn{..} =
9494
text "compileInstance compiled clauses: " :
9595
map (nest 2 . text . pp) ds
9696
when (length (nub rs) > 1) $
97-
genericDocError =<< fsep (pwords "More than one minimal record used.")
97+
agda2hsErrorM $ fsep (pwords "More than one minimal record used.")
9898
return $ Hs.InstDecl () Nothing ir (Just ds)
9999
where Function{..} = theDef
100100

@@ -138,7 +138,7 @@ compileInstRule cs ty = do
138138

139139
etaExpandClause :: Clause -> C [Clause]
140140
etaExpandClause cl@Clause{clauseBody = Nothing} =
141-
genericError "Instance definition with absurd pattern!"
141+
agda2hsError "Instance definition with absurd pattern!"
142142
etaExpandClause cl@Clause{namedClausePats = ps, clauseBody = Just t} = do
143143
case t of
144144
Con c _ _ -> do
@@ -147,7 +147,7 @@ etaExpandClause cl@Clause{namedClausePats = ps, clauseBody = Just t} = do
147147
clauseBody = Just $ t `applyE` [Proj ProjSystem $ unArg f] }
148148
| f <- fields ]
149149
return cls
150-
_ -> genericDocError =<< fsep (pwords $
150+
_ -> agda2hsErrorM $ fsep (pwords $
151151
"Type class instances must be defined using copatterns (or top-level" ++
152152
" records) and cannot be defined using helper functions.")
153153

@@ -221,9 +221,9 @@ compileInstanceClause' curModule ty (p:ps) c
221221
if
222222
-- Instance field: check canonicity.
223223
| isInstance arg -> do
224-
unless (null ps) $ genericDocError =<< text "not allowed: explicitly giving superclass"
224+
unless (null ps) $ agda2hsError "not allowed: explicitly giving superclass"
225225
body <- case clauseBody c' of
226-
Nothing -> genericDocError =<< text "not allowed: absurd clause for superclass"
226+
Nothing -> agda2hsError "not allowed: absurd clause for superclass"
227227
Just b -> return b
228228
addContext (clauseTel c) $ do
229229
liftTCM $ setModuleCheckpoint curModule
@@ -257,7 +257,7 @@ compileInstanceClause' curModule ty (p:ps) c
257257
-- same (minimal) dictionary as the primitive fields.
258258
| Clause {namedClausePats = [], clauseBody = Just (Def n es)} <- c'
259259
, n .~ q -> do
260-
let err = genericDocError =<< text "illegal instance declaration: instances using default methods should use a named definition or an anonymous `λ where`."
260+
let err = agda2hsError $ "illegal instance declaration: instances using default methods should use a named definition or an anonymous `λ where`."
261261
filterArgs :: Type -> [Term] -> C [Term]
262262
filterArgs ty [] = return []
263263
filterArgs ty (v:vs) = do
@@ -308,7 +308,7 @@ fieldArgInfo f = do
308308
df : _ -> return $ getArgInfo df
309309
[] -> badness
310310
where
311-
badness = genericDocError =<< text "Not a record field:" <+> prettyTCM f
311+
badness = agda2hsErrorM $ text "Not a record field:" <+> prettyTCM f
312312

313313

314314
findDefinitions :: (QName -> Definition -> C Bool) -> ModuleName -> C [Definition]
@@ -326,7 +326,7 @@ resolveStringName s = do
326326
rname <- liftTCM $ resolveName cqname
327327
case rname of
328328
DefinedName _ aname _ -> return $ anameName aname
329-
_ -> genericDocError =<< text ("Couldn't find " ++ s)
329+
_ -> agda2hsStringError $ "Couldn't find " ++ s
330330

331331

332332
lookupDefaultImplementations :: QName -> [Hs.Name ()] -> C [Definition]
@@ -339,4 +339,4 @@ classMemberNames :: Definition -> C [Hs.Name ()]
339339
classMemberNames def =
340340
case theDef def of
341341
Record{recFields = fs} -> fmap unQual <$> traverse compileQName (map unDom fs)
342-
_ -> genericDocError =<< text "Not a record:" <+> prettyTCM (defName def)
342+
_ -> agda2hsErrorM $ text "Not a record:" <+> prettyTCM (defName def)

src/Agda2Hs/Compile/Data.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ compileData newtyp ds def = do
5151
allIndicesErased t = reduce (unEl t) >>= \case
5252
Pi dom t -> compileDomType (absName t) dom >>= \case
5353
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"
54+
DomType{} -> agda2hsError "Not supported: indexed datatypes"
55+
DomConstraint{} -> agda2hsError "Not supported: constraints in types"
56+
DomForall{} -> agda2hsError "Not supported: indexed datatypes"
5757
_ -> return ()
5858

5959
compileConstructor :: [Arg Term] -> QName -> C (Hs.QualConDecl ())
@@ -74,6 +74,6 @@ compileConstructorArgs (ExtendTel a tel) = compileDomType (absName tel) a >>= \c
7474
DomType s hsA -> do
7575
ty <- addTyBang s hsA
7676
(ty :) <$> underAbstraction a tel compileConstructorArgs
77-
DomConstraint hsA -> genericDocError =<< text "Not supported: constructors with class constraints"
77+
DomConstraint hsA -> agda2hsError "Not supported: constructors with class constraints"
7878
DomDropped -> underAbstraction a tel compileConstructorArgs
7979
DomForall{} -> __IMPOSSIBLE__

src/Agda2Hs/Compile/Function.hs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ isSpecialCon qn = case prettyShow qn of
6464
]
6565

6666
itsBad :: Type -> NAPs -> C (Hs.Pat ())
67-
itsBad _ _ = genericDocError =<< "constructor `" <> prettyTCM qn <> "` not supported in patterns"
67+
itsBad _ _ = agda2hsErrorM $ "constructor `" <> prettyTCM qn <> "` not supported in patterns"
6868

6969
-- | Translate Int.pos pattern.
7070
posIntPat :: Type -> NAPs -> C (Hs.Pat ())
@@ -91,7 +91,7 @@ compileLitNatPat = \case
9191
| prettyShow (conName ch) == "Agda.Builtin.Nat.Nat.zero" -> return 0
9292
| prettyShow (conName ch) == "Agda.Builtin.Nat.Nat.suc"
9393
, [p] <- ps -> (1+) <$> compileLitNatPat (namedArg p)
94-
p -> genericDocError =<< "not a literal natural number pattern:" <?> prettyTCM p
94+
p -> agda2hsErrorM $ "not a literal natural number pattern:" <?> prettyTCM p
9595

9696

9797
compileFun, compileFun'
@@ -110,7 +110,7 @@ compileFun' withSig def@Defn{..} = inTopContext $ withCurrentModule m $ do
110110
reportSDoc "agda2hs.compile" 6 $ "Compiling function: " <+> prettyTCM defName
111111

112112
whenM ((withSig &&) <$> inRecordMod defName) $
113-
genericDocError =<< text "not supported by agda2hs: functions inside a record module"
113+
agda2hsError "not supported: functions inside a record module"
114114

115115
ifM (endsInSort defType)
116116
-- if the function type ends in Sort, it's a type alias!
@@ -154,8 +154,8 @@ compileFun' withSig def@Defn{..} = inTopContext $ withCurrentModule m $ do
154154
cs <- map (addPats paramPats) <$>
155155
mapMaybeM (compileClause m (Just defName) x typ) clauses
156156

157-
when (null cs) $ genericDocError
158-
=<< text "Functions defined with absurd patterns exclusively are not supported."
157+
when (null cs) $ agda2hsErrorM $
158+
text "Functions defined with absurd patterns exclusively are not supported."
159159
<+> text "Use function `error` from the Haskell.Prelude instead."
160160

161161
return $ sig ++ [Hs.FunBind () cs]
@@ -259,7 +259,7 @@ compilePats _ [] = pure []
259259
compilePats ty ((namedArg -> ProjP po pn):ps) = do
260260
reportSDoc "agda2hs.compile" 10 $ "compiling copattern: " <+> text (prettyShow pn)
261261
unlessM (asks copatternsEnabled `or2M` (isJust <$> isUnboxProjection pn)) $
262-
genericDocError =<< "not supported in Haskell: copatterns"
262+
agda2hsError "not supported in Haskell: copatterns"
263263

264264
ty <- fromMaybe __IMPOSSIBLE__ <$> getDefType pn ty
265265
(a, b) <- mustBePi ty
@@ -278,7 +278,7 @@ compilePats ty ((namedArg -> pat):ps) = do
278278
DOTerm -> do
279279
checkNoAsPatterns pat
280280
(:) <$> compilePat (unDom a) pat <*> rest
281-
where checkForced = when (isForcedPat pat) $ genericDocError =<< "not supported by agda2hs: forced (dot) patterns in non-erased positions"
281+
where checkForced = when (isForcedPat pat) $ agda2hsError "not supported: forced (dot) patterns in non-erased positions"
282282

283283

284284
compilePat :: Type -> DeBruijnPattern -> C (Hs.Pat ())
@@ -308,7 +308,7 @@ compilePat ty (LitP _ l) = compileLitPat l
308308

309309

310310
-- nothing else is supported
311-
compilePat _ p = genericDocError =<< "bad pattern:" <?> prettyTCM p
311+
compilePat _ p = agda2hsErrorM $ "bad pattern:" <?> prettyTCM p
312312

313313

314314
compileErasedConP :: Type -> Strictness -> NAPs -> C (Hs.Pat ())
@@ -319,7 +319,7 @@ compileErasedConP ty s ps = compilePats ty ps >>= \case
319319
compileLitPat :: Literal -> C (Hs.Pat ())
320320
compileLitPat = \case
321321
LitChar c -> return $ Hs.charP c
322-
l -> genericDocError =<< "bad literal pattern:" <?> prettyTCM l
322+
l -> agda2hsErrorM $ "bad literal pattern:" <?> prettyTCM l
323323

324324
compileTupleConP :: Type -> Hs.Boxed -> NAPs -> C (Hs.Pat ())
325325
compileTupleConP ty b ps = do
@@ -388,7 +388,7 @@ checkTransparentPragma def = compileFun False def >>= \case
388388
checkTransparentTypeDef (Hs.DHApp _ _ (Hs.UnkindedVar _ x)) (Hs.TyVar _ y) | x == y = return ()
389389
checkTransparentTypeDef _ _ = errNotTransparent
390390

391-
errNotTransparent = genericDocError =<<
391+
errNotTransparent = agda2hsErrorM $
392392
"Cannot make function" <+> prettyTCM (defName def) <+> "transparent." <+>
393393
"A transparent function must have exactly one non-erased argument and return it unchanged."
394394

@@ -399,11 +399,11 @@ checkInlinePragma def@Defn{defName = f} = do
399399
let Function{funClauses = cs} = theDef def
400400
case filter (isJust . clauseBody) cs of
401401
[c] ->
402-
unlessM (allowedPats (namedClausePats c)) $ genericDocError =<<
402+
unlessM (allowedPats (namedClausePats c)) $ agda2hsErrorM $
403403
"Cannot make function" <+> prettyTCM (defName def) <+> "inlinable." <+>
404404
"Inline functions can only use variable patterns or transparent record constructor patterns."
405405
_ ->
406-
genericDocError =<<
406+
agda2hsErrorM $
407407
"Cannot make function" <+> prettyTCM f <+> "inlinable." <+>
408408
"An inline function must have exactly one clause."
409409

src/Agda2Hs/Compile/Imports.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ compileImports top is0 = do
8181
checkClashingImports [] = return ()
8282
checkClashingImports (Import mod as p q _ : is) =
8383
case filter isClashing is of
84-
(i : _) -> genericDocError =<< text (mkErrorMsg i)
84+
(i : _) -> agda2hsStringError $ mkErrorMsg i
8585
[] -> checkClashingImports is
8686
where
8787
isClashing (Import _ as' p' q' _) = (as' == as) && (p' /= p) && (q' == q)

src/Agda2Hs/Compile/Record.hs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ compileMinRecords def sls = do
8686
-- 2. assert that all default implementations are the same (for a certain field)
8787
let getUnique f (x :| xs)
8888
| all (x ==) xs = return x
89-
| otherwise = genericDocError =<< do
89+
| otherwise = agda2hsErrorM $ do
9090
text ("Conflicting default implementations for " ++ pp f ++ ":") $$
9191
vcat [ text "-" <+> multilineText (pp d) | d <- nub (x : xs) ]
9292
decls <- Map.traverseWithKey getUnique
@@ -129,10 +129,10 @@ compileRecord target def = do
129129
-- record module has been opened.
130130
checkFieldInScope f = isInScopeUnqualified f >>= \case
131131
True -> return ()
132-
False -> setCurrentRangeQ f $ genericError $
133-
"Record projections (`" ++ prettyShow (qnameName f) ++
134-
"` in this case) must be brought into scope when compiling to Haskell record types. " ++
135-
"Add `open " ++ Hs.prettyPrint rName ++ " public` after the record declaration to fix this."
132+
False -> setCurrentRangeQ f $ agda2hsErrorM $
133+
("Record projections (`" <> prettyTCM (qnameName f) <>"` in this case)") <+>
134+
"must be brought into scope when compiling to Haskell record types." <+>
135+
"Add `open" <+> text (Hs.prettyPrint rName) <+> "public` after the record declaration to fix this."
136136

137137
Record{..} = theDef def
138138

@@ -157,7 +157,7 @@ compileRecord target def = do
157157
return (hsAssts, decl fieldName fieldType : hsFields)
158158
DomConstraint hsA -> case target of
159159
ToClass{} -> return (hsA : hsAssts , hsFields)
160-
ToRecord{} -> genericError $
160+
ToRecord{} -> agda2hsError $
161161
"Not supported: record/class with constraint fields"
162162
DomDropped -> return (hsAssts , hsFields)
163163
DomForall{} -> __IMPOSSIBLE__
@@ -182,24 +182,24 @@ checkUnboxPragma def = do
182182

183183
-- recRecursive can be used again after agda 2.6.4.2 is released
184184
-- see agda/agda#7042
185-
unless (all null recMutual) $ genericDocError
186-
=<< text "Unboxed record" <+> prettyTCM (defName def)
185+
unless (all null recMutual) $ agda2hsErrorM $
186+
text "Unboxed record" <+> prettyTCM (defName def)
187187
<+> text "cannot be recursive"
188188

189189
TelV tel _ <- telViewUpTo recPars (defType def)
190190
addContext tel $ do
191191
pars <- getContextArgs
192192
let fieldTel = recTel `apply` pars
193193
fields <- nonErasedFields fieldTel
194-
unless (length fields == 1) $ genericDocError
195-
=<< text "Unboxed record" <+> prettyTCM (defName def)
194+
unless (length fields == 1) $ agda2hsErrorM $
195+
text "Unboxed record" <+> prettyTCM (defName def)
196196
<+> text "should have exactly one non-erased field"
197197

198198
where
199199
nonErasedFields :: Telescope -> C [String]
200200
nonErasedFields EmptyTel = return []
201201
nonErasedFields (ExtendTel a tel) = compileDom a >>= \case
202202
DODropped -> underAbstraction a tel nonErasedFields
203-
DOType -> genericDocError =<< text "Type field in unboxed record not supported"
204-
DOInstance -> genericDocError =<< text "Instance field in unboxed record not supported"
203+
DOType -> agda2hsError "Type field in unboxed record not supported"
204+
DOInstance -> agda2hsError "Instance field in unboxed record not supported"
205205
DOTerm -> (absName tel:) <$> underAbstraction a tel nonErasedFields

0 commit comments

Comments
 (0)