46
46
import Agda.TypeChecking.CompiledClause.Compile
48
48
import Agda.TypeChecking.Rules.Term ( checkExpr, inferExpr, inferExprForWith, checkDontExpandLast, checkTelescope_, ConvColor(..) )
49
import Agda.TypeChecking.Rules.LHS ( checkLeftHandSide )
49
import Agda.TypeChecking.Rules.LHS ( checkLeftHandSide, LHSResult(..) )
50
50
import {-# SOURCE #-} Agda.TypeChecking.Rules.Decl ( checkDecls )
52
import Agda.Utils.Except ( MonadError(catchError, throwError) )
53
import Agda.Utils.Lens
52
54
import Agda.Utils.Size
53
55
import Agda.Utils.Permutation
54
56
import Agda.Utils.Monad
56
#include "../../undefined.h"
58
#include "undefined.h"
57
59
import Agda.Utils.Impossible
59
61
---------------------------------------------------------------------------
266
272
npats = size . clausePats
268
{- BEGIN RETIRING implicit argument insertion
270
{- | Ensure that all clauses have the same number of trailing implicits.
274
test : Bool → {A B : Set} → Set
276
test false {B = B} = B
279
@trailingImplicits@ patches these clauses to
282
test : Bool → {A B : Set} → Set
283
test true {A} {_} = A
284
test false {_} {B = B} = B
287
such that the arity of the clauses of @test@ is uniform.
289
trailingImplicits :: Type -> [A.SpineClause] -> TCM [A.SpineClause]
290
trailingImplicits t [] = __IMPOSSIBLE__
291
trailingImplicits t cs | hasProjP cs = return cs
293
hasProjP = any (any (isJust . A.isProjP) . A.spLhsPats . A.clauseLHS)
294
trailingImplicits t cs@(c:_) = do
295
pps@((ps,ips):_) <- mapM splitTrailingImplicits cs
296
-- compute the trailing implicits from type t
297
TelV tel t0 <- telView t
298
let -- number of non-hidden patterns
299
nh = genericLength $ filter ((NotHidden ==) . getHiding) ps
300
-- drop nh non-hidden domains from t
301
l = dropNonHidden nh $ telToList tel
302
-- take the hidden domains immediately after the dropped stuff
303
is = takeWhile ((NotHidden /=) . getHiding) l
304
itel = telFromList is
305
-- get the trailing implicit patterns
307
-- complete the implicit pattern lists
308
ipss <- mapM (\ ps -> insertImplicitPatterns DontExpandLast ps itel) ipss
309
let longest = head $ sortBy (compare `on` ((0-) . length)) ipss
310
pps' = zip (map fst pps) ipss
311
return $ zipWith (patchUpTrailingImplicits longest) pps' cs
313
dropNonHidden :: Nat -> [I.Dom (String, Type)] -> [I.Dom (String, Type)]
314
dropNonHidden 0 l = l
315
dropNonHidden n l = case dropWhile ((NotHidden /=) . getHiding) l of
316
[] -> [] -- or raise a type checking error "too many arguments in lhs"
317
(_:l) -> dropNonHidden (n-1) l
319
splitTrailingImplicits :: A.SpineClause -> TCM (A.Patterns, A.Patterns)
320
splitTrailingImplicits (A.Clause (A.SpineLHS _ _ _ wps@(_ : _)) _ _) =
321
typeError $ UnexpectedWithPatterns wps
322
splitTrailingImplicits (A.Clause (A.SpineLHS _ _ aps []) _ _) = do
323
let (ips, ps) = span isHidden $ reverse aps
324
return (reverse ps, reverse ips)
326
patchUpTrailingImplicits :: A.Patterns -> (A.Patterns, A.Patterns) -> A.SpineClause -> A.SpineClause
327
patchUpTrailingImplicits should (ps, is) c | length is >= length should = c
328
patchUpTrailingImplicits should (ps, is) (A.Clause (A.SpineLHS i x aps []) rhs0 wh) =
329
let imp = hide $ defaultArg $ Named Nothing $ A.ImplicitP $ Info.patNoRange
330
imps = replicate (length should - length is) imp
331
in A.Clause (A.SpineLHS i x (ps ++ is ++ imps) []) rhs0 wh
332
patchUpTrailingImplicits _ _ _ = __IMPOSSIBLE__
274
-- | Set 'funTerminates' according to termination info in 'TCEnv',
275
-- which comes from a possible termination pragma.
276
useTerPragma :: Definition -> TCM Definition
277
useTerPragma def@Defn{ defName = name, theDef = fun@Function{}} = do
278
tc <- asks envTerminationCheck
279
let terminates = case tc of
280
NonTerminating -> Just False
281
Terminating -> Just True
283
reportSLn "tc.fundef" 30 $ unlines $
284
[ "funTerminates of " ++ show name ++ " set to " ++ show terminates
285
, " tc = " ++ show tc
287
return $ def { theDef = fun { funTerminates = terminates }}
288
useTerPragma def = return def
335
291
-- | Insert some patterns in the in with-clauses LHS of the given RHS
336
292
insertPatterns :: [A.Pattern] -> A.RHS -> A.RHS
337
293
insertPatterns pats (A.WithRHS aux es cs) = A.WithRHS aux es (map insertToClause cs)
338
294
where insertToClause (A.Clause (A.LHS i lhscore ps) rhs ds)
339
295
= A.Clause (A.LHS i lhscore (pats ++ ps)) (insertPatterns pats rhs) ds
340
296
insertPatterns pats (A.RewriteRHS qs eqs rhs wh) = A.RewriteRHS qs eqs (insertPatterns pats rhs) wh
341
297
insertPatterns pats rhs = rhs
378
318
-- | Type check a function clause.
380
checkClause :: Type -> A.Clause -> TCM Clause
381
checkClause t c@(A.Clause (A.LHS i (A.LHSProj{}) []) rhs0 wh) =
382
typeError $ NotImplemented "type checking definitions by copatterns"
383
checkClause t c@(A.Clause (A.LHS i (A.LHSHead x aps) []) rhs0 wh) =
385
319
checkClause :: Type -> A.SpineClause -> TCM Clause
387
checkClause t c@(A.Clause lhs rhs0 wh) = do
388
let A.SpineLHS i x aps withPats = A.lhsToSpine lhs
390
320
checkClause t c@(A.Clause (A.SpineLHS i x aps withPats) rhs0 wh) = do
391
321
unless (null withPats) $
392
322
typeError $ UnexpectedWithPatterns withPats
393
323
traceCall (CheckClause t c) $ do
394
324
aps <- (traverse . traverse . traverse) expandPatternSynonyms aps
395
checkLeftHandSide (CheckPatternShadowing c) (Just x) aps t $ \ mgamma delta sub xs ps trhs perm -> do
325
checkLeftHandSide (CheckPatternShadowing c) (Just x) aps t $ \ (LHSResult mgamma delta sub xs ps trhs perm) -> do
396
326
-- Note that we might now be in irrelevant context,
397
327
-- in case checkLeftHandSide walked over an irrelevant projection pattern.
398
328
let mkBody v = foldr (\x t -> Bind $ Abs x t) (Body $ applySubst sub v) xs
446
376
equality <- primEqualityName
447
377
Con reflCon [] <- ignoreSharing <$> primRefl
449
-- Andreas, 2014-05-17 Issue 1110:
450
-- Rewriting with REFL has no effect, but gives an
451
-- incomprehensible error message about the generated
452
-- with clause. Thus, we rather do simply nothing if
453
-- rewriting with REFL is attempted.
456
-- let isRefl v = isRefl' . ignoreSharing =<< reduce v
457
-- isRefl' (Con c _) | c == reflCon = return True
458
-- isRefl' (Lam h t) = isRefl $ unAbs t
459
-- isRefl' (DontCare t) = isRefl t
460
-- isRefl' _ = return False
462
-- ifM (isRefl proof) recurse $ {- else -} do
464
379
-- Check that the type is actually an equality (lhs ≡ rhs)
465
380
-- and extract lhs, rhs, and their type.
529
444
reportSDoc "tc.with.top" 30 $
446
reportSDoc "tc.with.top" 20 $ do
448
nfv <- getModuleFreeVars m
449
sep [ text "with function module:" <+>
450
prettyList (map prettyTCM $ mnameToList m)
451
, text $ "free variables: " ++ show nfv
531
454
-- Infer the types of the with expressions
532
vas <- mapM inferExprForWith es
533
(vs0, as) <- instantiateFull (unzip vas)
455
(vs0, as) <- unzip <$> mapM inferExprForWith es
534
456
(vs, as) <- normalise (vs0, as)
536
-- Invent a clever name for the with function
538
reportSDoc "tc.with.top" 20 $ text "with function module:" <+> prettyList (map prettyTCM $ mnameToList m)
540
458
-- Split the telescope into the part needed to type the with arguments
541
459
-- and all the other stuff
542
460
let fv = allVars $ freeVars (vs, as)
584
496
-- and Δ₁ ⊢ vs : as
586
498
let -- We know that as does not depend on Δ₂
587
rho = parallelS (replicate (size delta2) __IMPOSSIBLE__)
499
rho = compactS __IMPOSSIBLE__ (replicate (size delta2) Nothing)
588
500
return $ applySubst rho $ renameP (reverseP perm') (vs, as)
591
503
-- Andreas, 2013-02-26 add with-name to signature for printing purposes
592
addConstant aux (Defn defaultArgInfo aux typeDontCare [] [] [] 0 noCompiledRep emptyFunction)
504
addConstant aux =<< do
505
useTerPragma $ Defn defaultArgInfo aux typeDontCare [] [] [] 0 noCompiledRep [] Nothing emptyFunction
594
507
-- Andreas, 2013-02-26 separate msgs to see which goes wrong
595
508
reportSDoc "tc.with.top" 20 $
607
520
reportSDoc "tc.with.top" 20 $
608
521
text " body" <+> (addCtxTel delta $ prettyTCM $ mkBody v)
611
reportSDoc "tc.with.top" 20 $ vcat
612
[ text " with arguments" <+> do escapeContext (size delta2) $ prettyList (map prettyTCM vs)
613
, text " types" <+> do escapeContext (size delta2) $ prettyList (map prettyTCM as)
614
, text "with function call" <+> prettyTCM v
615
, text " context" <+> (prettyTCM =<< getContextTelescope)
616
, text " delta" <+> do escapeContext (size delta) $ prettyTCM delta
617
, text " fv" <+> text (show fv)
618
, text " body" <+> (addCtxTel delta $ prettyTCM $ mkBody v)
621
523
gamma <- maybe (typeError $ NotImplemented "with clauses for functions with unfolding arity") return mgamma
622
524
return (mkBody v, WithFunction x aux gamma delta1 delta2 vs as t' ps perm' perm finalPerm cs)
623
525
in handleRHS rhs0
773
678
A.ConP _ _ ps -> any (containsAbsurdPattern . namedArg) ps
774
679
A.DefP _ _ _ -> False -- projection pattern
775
680
A.PatternSynP _ _ _ -> __IMPOSSIBLE__ -- False
778
actualConstructor :: QName -> TCM QName
779
actualConstructor c = do
780
v <- constructorForm =<< getConTerm c
781
case ignoreSharing v of
782
Con c _ -> return $ conName c
783
_ -> actualConstructor =<< stripLambdas v
785
stripLambdas v = case ignoreSharing v of
788
x <- freshName_ $ absName b
789
addCtx x (Dom info $ sort Prop) $
790
stripLambdas (absBody b)
791
_ -> typeError $ GenericError $ "Not a constructor: " ++ show c