9
9
import qualified Data.Map as Map
11
11
import Data.Function
12
import qualified System.IO.UTF8 as UTF8
12
import qualified Agda.Utils.IO.Locale as LocIO
14
14
import Agda.Syntax.Abstract.Name
15
15
import Agda.Syntax.Common
204
204
oldDef { conPars = np - size ts, conData = copyName d }
205
205
Datatype{ dataPars = np, dataCons = cs } ->
206
206
oldDef { dataPars = np - size ts, dataClause = Just cl, dataCons = map copyName cs }
207
Record{ recPars = np, recTel = tel } ->
208
oldDef { recPars = np - size ts, recClause = Just cl, recTel = apply tel ts }
207
Record{ recPars = np, recConType = t, recTel = tel } ->
208
oldDef { recPars = np - size ts, recClause = Just cl
209
, recConType = apply t ts, recTel = apply tel ts
210
212
Function { funClauses = [cl]
211
213
, funDelayed = NotDelayed
266
268
Constructor{ conInd = i } -> return i
267
269
_ -> __IMPOSSIBLE__
271
-- | Does the given constructor come from a single-constructor type?
273
-- Precondition: The name has to refer to a constructor.
274
singleConstructorType :: QName -> TCM Bool
275
singleConstructorType q = do
276
d <- theDef <$> getConstInfo q
278
Record {} -> return True
279
Constructor { conData = d } -> do
280
di <- theDef <$> getConstInfo d
283
Datatype { dataCons = cs } -> length cs == 1
269
287
-- | Lookup the definition of a name. The result is a closed thing, all free
270
288
-- variables have been abstracted over.
271
289
getConstInfo :: MonadTCM tcm => QName -> tcm Definition
272
getConstInfo q = liftTCM $ do
273
ab <- treatAbstractly q
274
defs <- sigDefinitions <$> getSignature
275
idefs <- sigDefinitions <$> getImportedSignature
276
let smash = (++) `on` maybe [] (:[])
277
case smash (Map.lookup q defs) (Map.lookup q idefs) of
278
[] -> fail $ show (getRange q) ++ ": no such name " ++ show q
290
getConstInfo q = liftTCM $ join $ pureTCM $ \st env ->
291
let defs = sigDefinitions $ stSignature st
292
idefs = sigDefinitions $ stImports st
293
ab = treatAbstractly' q env
294
smash = (++) `on` maybe [] (:[])
295
in case smash (Map.lookup q defs) (Map.lookup q idefs) of
296
[] -> fail $ "Unbound name: " ++ show q
279
297
[d] -> mkAbs ab d
280
ds -> fail $ show (getRange q) ++ ": ambiguous name " ++ show q
298
ds -> fail $ "Ambiguous name: " ++ show q
283
301
case makeAbstract d of
346
364
-- number of parameters if we're currently inside the section and 0 otherwise.
347
365
getSecFreeVars :: MonadTCM tcm => ModuleName -> tcm Nat
348
366
getSecFreeVars m = do
349
sig <- sigSections <$> getSignature
367
sig <- sigSections <$> getSignature
368
isig <- sigSections <$> getImportedSignature
350
369
top <- currentModule
351
370
case top `isSubModuleOf` m || top == m of
352
True -> return $ maybe 0 secFreeVars $ Map.lookup m sig
371
True -> return $ maybe 0 secFreeVars $ Map.lookup m (Map.union sig isig)
353
372
False -> return 0
355
374
-- | Compute the number of free variables of a module. This is the sum of
376
395
verboseS "tc.sig.inst" 30 $ do
377
396
ctx <- getContext
378
397
m <- currentModule
379
liftIO $ UTF8.putStrLn $ "instDef in " ++ show m ++ ": " ++ show (defName d) ++ " " ++
398
liftIO $ LocIO.putStrLn $ "instDef in " ++ show m ++ ": " ++ show (defName d) ++ " " ++
380
399
unwords (map show . take (size vs) . reverse . map (fst . unArg) $ ctx)
381
400
return $ d `apply` vs