9
9
import Agda.TypeChecking.Monad
10
10
import Agda.TypeChecking.Substitute hiding (Substitution)
11
import qualified Agda.TypeChecking.Substitute as S (Substitution)
11
12
import Agda.TypeChecking.Free
12
13
import Agda.TypeChecking.Pretty
13
14
import Agda.TypeChecking.Reduce
14
15
import Agda.TypeChecking.Telescope
16
17
import Agda.TypeChecking.Rules.LHS.Problem
18
-- import Agda.TypeChecking.Rules.LHS.ProblemRest
17
19
import Agda.TypeChecking.Rules.LHS.Split ( asView )
21
import Agda.Utils.List
19
22
import Agda.Utils.Permutation
20
23
import Agda.Utils.Size
25
28
-- | Instantiate a telescope with a substitution. Might reorder the telescope.
26
29
-- @instantiateTel (Γ : Tel)(σ : Γ --> Γ) = Γσ~@
27
30
-- Monadic only for debugging purposes.
28
instantiateTel :: Substitution -> Telescope -> TCM (Telescope, Permutation, [Term], [Type])
31
instantiateTel :: Substitution -> Telescope -> TCM (Telescope, Permutation, S.Substitution, [Dom Type])
29
32
instantiateTel s tel = liftTCM $ do
31
34
tel <- normalise tel
40
43
-- Shrinking permutation (removing Justs) (and its complement, and reverse)
41
let ps = Perm (size s) [ i | (i, Nothing) <- zip [0..] $ reverse s ]
43
psC = Perm (size s) [ i | (i, Just _) <- zip [0..] $ reverse s ]
45
{- OLD CODE, leave as documentation
46
ps = Perm n [ i | (i, Nothing) <- zip [0..] $ reverse s ]
48
psC = Perm n [ i | (i, Just _) <- zip [0..] $ reverse s ]
50
deal (i, Nothing) = Left i
51
deal (i, Just _ ) = Right i
52
(is, isC) = mapEither deal $ zip [0..] $ reverse s
45
57
reportSDoc "tc.lhs.inst" 10 $ vcat
46
58
[ nest 2 $ text $ "ps = " ++ show ps
83
98
p <- case reorderTel tel3 of
84
99
Nothing -> inContext [] $ do
85
100
xs <- mapM freshName_ names3
86
addCtxs xs (Arg NotHidden Relevant prop) $ do
101
addCtxs xs (Dom NotHidden Relevant prop) $ do
87
102
err <- sep [ text "Recursive telescope in left hand side:"
88
103
, fsep [ parens (prettyTCM x <+> text ":" <+> prettyTCM t)
89
104
| (x, t) <- zip xs tel3 ]
97
112
let rho' = renaming (reverseP p)
99
114
-- tel4 : [Type Γσ~]Γσ~
100
let tel4 = substs rho' (permute p tel3)
115
let tel4 = applySubst rho' (permute p tel3)
101
116
names4 = permute p names3
103
118
reportSDoc "tc.lhs.inst" 15 $ nest 2 $
112
127
-- remember the types of the instantiations
113
128
-- itypes : [Type Γσ~]Γ*
114
let itypes = substs rho' $ permute psC $ map unArg tel2
129
let itypes = applySubst rho' $ permute psC tel2
116
return (tel5, composeP p ps, substs rho' rho, itypes)
131
return (tel5, composeP p ps, applySubst rho' rho, itypes)
119
-- Turn a Substitution ([Maybe Term]) into a substitution ([Term])
120
-- (The result is an infinite list)
121
mkSubst :: [Maybe Term] -> [Term]
134
-- Turn a Substitution ([Maybe Term]) into a substitution (S.Substitution)
135
mkSubst :: [Maybe Term] -> S.Substitution
122
136
mkSubst s = rho 0 s'
123
where s' = s ++ repeat Nothing
124
rho i (Nothing : s) = Var i [] : rho (i + 1) s
125
rho i (Just u : s) = u : rho i s
126
rho _ [] = __IMPOSSIBLE__
138
rho i (Nothing : s) = var i :# rho (i + 1) s
139
rho i (Just u : s) = u :# rho i s
128
142
-- | Produce a nice error message when splitting failed
129
143
nothingToSplitError :: Problem -> TCM a
130
nothingToSplitError (Problem ps _ tel) = splitError ps tel
144
nothingToSplitError (Problem ps _ tel pr) = splitError ps tel
132
146
splitError [] EmptyTel = __IMPOSSIBLE__
133
147
splitError (_:_) EmptyTel = __IMPOSSIBLE__
134
148
splitError [] ExtendTel{} = __IMPOSSIBLE__
135
149
splitError (p : ps) (ExtendTel a tel)
136
| isBad p = traceCall (CheckPattern (strip p) EmptyTel (unArg a)) $ case strip p of
150
| isBad p = traceCall (CheckPattern (strip p) EmptyTel (unDom a)) $ case strip p of
137
151
A.DotP _ e -> typeError $ UninstantiatedDotPattern e
138
p -> typeError $ IlltypedPattern p (unArg a)
152
p -> typeError $ IlltypedPattern p (unDom a)
139
153
| otherwise = underAbstraction a tel $ \tel -> splitError ps tel
141
strip = snd . asView . namedThing . unArg
155
strip = snd . asView . namedArg
142
156
isBad p = case strip p of
143
157
A.DotP _ _ -> True
144
158
A.ConP _ _ _ -> True