@@ -1,6 +1,8 @@
11{-# LANGUAGE PatternGuards #-}
22
3−module Idris.CaseSplit(split, splitOnLine, replaceSplits, getClause) where
3+module Idris.CaseSplit(split, splitOnLine, replaceSplits,
4+ getClause,
5+ mkWith) where
46
57-- splitting a variable in a pattern clause
68
@@ -59,6 +61,7 @@ split n t'
5961 do let splits = findPats ist ty
6062 iLOG ("New patterns " ++ show splits)
6163 let newPats_in = zipWith (replaceVar ctxt n) splits (repeat t)
64+ logLvl 4 ("Working from " ++ show t)
6265 logLvl 4 ("Trying " ++ showSep "\n" (map show newPats_in))
6366 newPats <- mapM elabNewPat newPats_in
6467 logLvl 3 ("Original:\n" ++ show t)
@@ -95,8 +98,8 @@ mergePat ctxt (PPatvar fc n) new
9598mergePat ctxt old (PPatvar fc n)
9699 = mergePat ctxt old (PRef fc n)
97100mergePat ctxt orig@(PRef fc n) new@(PRef _ n')
98− | isConName n' ctxt = do addUpdate n new;
99− return new
101+ | isDConName n' ctxt = do addUpdate n new;
102+ return new
100103 | otherwise
101104 = do ms <- get
102105 case lookup n' (namemap ms) of
@@ -164,9 +167,10 @@ replaceVar ctxt n t (PApp fc f pats) = PApp fc f (map substArg pats)
164167 subst orig@(PPatvar _ v) | v == n = t
165168 | otherwise = Placeholder
166169 subst orig@(PRef _ v) | v == n = t
167− | isConName v ctxt = orig
170+ | isDConName v ctxt = orig
168171 subst (PRef _ _) = Placeholder
169172 subst (PApp fc f pats) = PApp fc f (map substArg pats)
173+ subst (PEq fc l r) = PEq fc (subst l) (subst r)
170174 subst x = x
171175
172176 substArg arg = arg { getTm = subst (getTm arg) }
@@ -239,7 +243,22 @@ getClause l fn fp = do ty <- getInternalApp fp l
239243 mkApp (PPi _ _ _ sc) ns = mkApp sc ns
240244 mkApp _ _ = ""
241245
242−
243−
246+-- Purely syntactic - turn a pattern match clause into a with and a new
247+-- match clause
248+
249+mkWith :: String -> Name -> String
250+mkWith str n = let ind = getIndent str
251+ str' = replicate ind ' ' ++
252+ replaceRHS str "with (_)"
253+ newpat = replicate (ind + 2) ' ' ++
254+ replaceRHS str "| with_pat = ?" ++ show n ++ "_rhs" in
255+ str' ++ "\n" ++ newpat
256+
257+ where getIndent s = length (takeWhile isSpace s)
258+
259+ replaceRHS [] str = str
260+ replaceRHS ('?':'=': rest) str = str
261+ replaceRHS ('=': rest) str = str
262+ replaceRHS (x : rest) str = x : replaceRHS rest str
244263
245264