13
13
import Agda.Syntax.Common
14
14
import Agda.Syntax.Position
15
import qualified Agda.Syntax.Concrete as C
15
16
import qualified Agda.Syntax.Abstract as A
16
17
import qualified Agda.Syntax.Info as A
17
18
import Agda.Syntax.Internal
19
import Agda.Syntax.Scope.Monad (resolveName, ResolvedName(..))
20
import Agda.Syntax.Translation.ConcreteToAbstract
18
21
import Agda.Syntax.Translation.InternalToAbstract
20
23
import Agda.TypeChecking.Monad
28
31
import Agda.Interaction.BasicOps
33
import Agda.Utils.Functor
30
34
import Agda.Utils.List
31
35
import Agda.Utils.Monad
32
36
import qualified Agda.Utils.Pretty as P
33
37
import Agda.Utils.Size
34
38
import qualified Agda.Utils.HashMap as HMap
36
#include "../undefined.h"
40
#include "undefined.h"
37
41
import Agda.Utils.Impossible
39
data CaseContext = FunctionDef | ExtendedLambda Int Int
45
| ExtendedLambda Int Int
41
48
-- | Find the clause whose right hand side is the given meta
42
49
-- BY SEARCHING THE WHOLE SIGNATURE. Returns
43
50
-- the original clause, before record patterns have been translated
82
89
MetaV m' _ -> m == m'
85
93
-- | Parse variables (visible or hidden), returning their de Bruijn indices.
86
94
-- Used in 'makeCase'.
87
96
parseVariables :: InteractionId -> Range -> [String] -> TCM [Int]
88
97
parseVariables ii rng ss = do
99
-- Get into the context of the meta.
89
100
mId <- lookupInteractionId ii
90
101
updateMetaVarRange mId rng
91
102
mi <- getMetaInfo <$> lookupMeta mId
92
enterClosure mi $ \ _r -> do
94
xs <- forM (downFrom n) $ \ i -> do
95
(,i) . P.render <$> prettyTCM (var i)
98
Nothing -> typeError $ GenericError $ "Unbound variable " ++ s
103
enterClosure mi $ \ r -> do
105
-- Get printed representation of variables in context.
107
xs <- forM (downFrom n) $ \ i -> do
108
(,i) . P.render <$> prettyTCM (var i)
110
-- Get number of module parameters. These cannot be split on.
111
fv <- getModuleFreeVars =<< currentModule
112
let numSplittableVars = n - fv
114
-- Resolve each string to a variable.
116
let failNotVar = typeError $ GenericError $ "Not a (splittable) variable: " ++ s
118
| i < numSplittableVars = return i
119
| otherwise = failNotVar
121
-- Note: the range in the concrete name is only approximate.
122
resName <- resolveName $ C.QName $ C.Name r $ C.stringNameParts s
125
-- Fail if s is a name, but not of a variable.
126
DefinedName{} -> failNotVar
127
FieldName{} -> failNotVar
128
ConstructorName{} -> failNotVar
129
PatternSynResName{} -> failNotVar
131
-- If s is a variable name in scope, get its de Bruijn index
132
-- via the type checker.
134
(v, _) <- getVarInfo x
135
case ignoreSharing v of
139
-- If s is not a name, compare it to the printed variable representation.
140
-- This fallback is to enable splitting on hidden variables.
142
case filter ((s ==) . fst) xs of
143
[] -> typeError $ GenericError $ "Unbound variable " ++ s
145
-- Issue 1325: Variable names in context can be ambiguous.
146
_ -> typeError $ GenericError $ "Ambiguous variable " ++ s
101
149
-- | Entry point for case splitting tactic.
102
150
makeCase :: InteractionId -> Range -> String -> TCM (CaseContext , [A.Clause])
116
164
let vars = words s
117
165
if null vars then do
119
res <- splitResult f =<< fixTarget (clauseToSplitClause clause)
121
Nothing -> typeError $ GenericError $ "Cannot split on result here"
122
Just cov -> (casectxt,) <$> do mapM (makeAbstractClause f) $ splitClauses cov
167
(newPats, sc) <- fixTarget (clauseToSplitClause clause)
168
res <- splitResult f sc
170
Nothing -> if newPats then return [sc] else
171
typeError $ GenericError $ "Cannot split on result here"
172
Just cov -> mapM (snd <.> fixTarget) $ splitClauses cov
173
(casectxt,) <$> mapM (makeAbstractClause f) scs
124
175
-- split on variables
125
176
vars <- parseVariables hole rng vars