Skip to content

Commit ed302b4

Browse files
committed
Refactor to remove usage of catchError
1 parent 582d11a commit ed302b4

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

app/Agda/Core/ToCore.hs

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Agda.Core.ToCore
1010

1111
import Control.Monad (when)
1212
import Control.Monad.Reader (ReaderT, runReaderT, MonadReader, asks)
13-
import Control.Monad.Except (MonadError(throwError, catchError), withError)
13+
import Control.Monad.Except (MonadError(throwError), withError)
1414
import Data.Functor ((<&>))
1515
import Data.Map.Strict (Map)
1616
import Numeric.Natural (Natural)
@@ -84,22 +84,16 @@ asksCon :: (Map QName (Index, Index) -> a) -> ToCoreM a
8484
asksCon = asks . (. \ToCoreGlobal{globalCons} -> globalCons)
8585

8686
-- | Lookup a definition name in the current module.
87-
-- Fails if the definition cannot be found.
88-
lookupDef :: QName -> ToCoreM Index
89-
lookupDef qn = fromMaybeM complain $ asksDef (Map.!? qn)
90-
where complain = throwError $ "Trying to access an unknown definition: " <+> pretty qn
87+
lookupDef :: QName -> ToCoreM (Maybe Index)
88+
lookupDef qn = asksDef (Map.!? qn)
9189

9290
-- | Lookup a datatype name in the current module.
93-
-- Fails if the datatype cannot be found.
94-
lookupData :: QName -> ToCoreM (Index, (Nat, Nat))
95-
lookupData qn = fromMaybeM complain $ asksData (Map.!? qn)
96-
where complain = throwError $ "Trying to access an unknown datatype: " <+> pretty qn
91+
lookupData :: QName -> ToCoreM (Maybe (Index, (Nat, Nat)))
92+
lookupData qn = asksData (Map.!? qn)
9793

9894
-- | Lookup a constructor name in the current module.
99-
-- Fails if the constructor cannot be found.
100-
lookupCon :: QName -> ToCoreM (Index, Index)
101-
lookupCon qn = fromMaybeM complain $ asksCon (Map.!? qn)
102-
where complain = throwError $ "Trying to access an unknown constructor: " <+> pretty qn
95+
lookupCon :: QName -> ToCoreM (Maybe (Index, Index))
96+
lookupCon qn = asksCon (Map.!? qn)
10397

10498

10599
-- | Class for things that can be converted to core syntax
@@ -138,52 +132,53 @@ instance ToCore I.Term where
138132
toCore (I.Def qn es)
139133
| Just args <- allApplyElims es
140134
= do
141-
-- Try looking up as definition first
142-
catchError
143-
(do
144-
idx <- lookupDef qn
145-
let def = TDef idx
146-
coreEs <- toCore es
147-
return (tApp def coreEs)
148-
)
149-
--Otherwise, try looking up as datatype
150-
(\_ -> do
151-
(idx, (amountOfParams, amountOfIndices)) <- lookupData qn
152-
153-
--always take all parameters
154-
paramTermS <- toTermS <$> toCore (take amountOfParams args)
155-
156-
-- @m@ is the amount of arguments to the index list which are missing
157-
let indexListGiven = drop amountOfParams args
158-
let m = amountOfIndices - (length indexListGiven)
159-
160-
-- Construct @m@ additional deBruijn indices
161-
-- so we get [TVar 2, TVar 1, TVar 0, ...] of length m
162-
let additionalVars = reverse $ take m $ TVar <$> iterate Scope.inThere Scope.inHere
163-
164-
indexTermS <- toTermS . (++ additionalVars) <$> toCore (raise m indexListGiven)
165-
let tdata = TData idx paramTermS indexTermS
166-
167-
-- in the end, we have (TLam (TLam (TLam ...))) of depth m
168-
return (iterate TLam tdata !! m)
169-
)
135+
-- Try looking up as definition first
136+
maybe_idx <- lookupDef qn
137+
case maybe_idx of
138+
Just idx -> do
139+
let def = TDef idx
140+
coreEs <- toCore es
141+
return (tApp def coreEs)
142+
--Otherwise, try looking up as datatype (must succeed, else fail with error message)
143+
Nothing -> do
144+
lookupData qn >>= \case
145+
Nothing -> throwError $ "Trying to access an unknown definition: " <+> pretty qn
146+
147+
Just (idx, (amountOfParams, amountOfIndices)) -> do
148+
--always take all parameters
149+
paramTermS <- toTermS <$> toCore (take amountOfParams args)
150+
151+
-- @m@ is the amount of arguments to the index list which are missing
152+
let indexListGiven = drop amountOfParams args
153+
let m = amountOfIndices - length indexListGiven
154+
155+
-- Construct @m@ additional deBruijn indices
156+
-- so we get [TVar 2, TVar 1, TVar 0, ...] of length m
157+
let additionalVars = reverse $ take m $ TVar <$> iterate Scope.inThere Scope.inHere
158+
159+
indexTermS <- toTermS . (++ additionalVars) <$> toCore (raise m indexListGiven)
160+
let tdata = TData idx paramTermS indexTermS
161+
162+
-- in the end, we have (TLam (TLam (TLam ...))) of depth m
163+
return (iterate TLam tdata !! m)
170164

171165
toCore I.Def{} = throwError "cubical endpoint application to definitions/datatypes not supported"
172166

173167
toCore (I.Con ch _ es)
174168
| Just args <- allApplyElims es
175-
= do
176-
-- @l@ is the amount of arguments missing from the application.
177-
-- we need to eta-expand manually @l@ times to fully-apply the constructor.
178-
let l = length (I.conFields ch) - length es
179-
-- Construct @l@ additional deBruijn indices
180-
let additionalVars = reverse $ take l $ TVar <$> iterate Scope.inThere Scope.inHere
181-
(dt , con) <- lookupCon (I.conName ch)
169+
= lookupCon (I.conName ch) >>= \case
170+
Nothing -> throwError $ "Trying to access an unknown constructor: " <+> pretty (I.conName ch)
171+
Just (dt , con) -> do
172+
-- @l@ is the amount of arguments missing from the application.
173+
-- we need to eta-expand manually @l@ times to fully-apply the constructor.
174+
let l = length (I.conFields ch) - length es
175+
-- Construct @l@ additional deBruijn indices
176+
let additionalVars = reverse $ take l $ TVar <$> iterate Scope.inThere Scope.inHere
182177

183-
t <- TCon dt con . toTermS . (++ additionalVars) <$> toCore (raise l args)
178+
t <- TCon dt con . toTermS . (++ additionalVars) <$> toCore (raise l args)
184179

185-
-- in the end, we bind @l@ fresh deBruijn indices
186-
pure (iterate TLam t !! l)
180+
-- in the end, we bind @l@ fresh deBruijn indices
181+
pure (iterate TLam t !! l)
187182

188183
toCore I.Con{} = throwError "cubical endpoint application to constructors not supported"
189184

@@ -295,7 +290,10 @@ toCoreDefn (I.DatatypeDefn dt) ty =
295290
let I.TelV{theTel = internalIxsTel} = I.telView'UpTo ixs ty1
296291
parsTel <- toCore internalParsTel
297292
ixsTel <- toCore internalIxsTel
298-
cons_dt_indexes <- traverse lookupCon cons
293+
cons_dt_indexes <- traverse (\qn -> lookupCon qn >>= \case
294+
Nothing -> throwError $ "Trying to access an unknown constructor: " <+> pretty qn
295+
Just result -> pure result
296+
) cons
299297
let cons_indexes = map snd cons_dt_indexes
300298
let d = Core.Datatype{ dataSort = sort',
301299
dataParTel = parsTel,

0 commit comments

Comments
 (0)