@@ -1,6 +1,6 @@
1{-# LANGUAGE PatternGuards #-}
1{-# LANGUAGE PatternGuards #-}
3−module Idris.CaseSplit(split, splitOnLine, replaceSplits,
3+module Idris.CaseSplit(split, splitOnLine, replaceSplits,
5 mkWith) where
5 mkWith) where
@@ -30,9 +30,9 @@ import qualified Data.ByteString.UTF8 as UTF8
31import Debug.Trace
31import Debug.Trace
35−Given a pattern clause and a variable 'n', elaborate the cluse and find the
35+Given a pattern clause and a variable 'n', elaborate the clause and find the
36type of 'n'.
36type of 'n'.
38Make new pattern clauses by replacing 'n' with all the possibly constructors
38Make new pattern clauses by replacing 'n' with all the possibly constructors
@@ -47,7 +47,7 @@ names over '_', patterns over names, etc.
47-- Given a variable to split, and a term application, return a list of
47-- Given a variable to split, and a term application, return a list of
48-- variable updates
48-- variable updates
49split :: Name -> PTerm -> Idris [[(Name, PTerm)]]
49split :: Name -> PTerm -> Idris [[(Name, PTerm)]]
50−split n t'
50+split n t'
51 = do (tm, ty, pats) <- elabValBind toplevel True t'
51 = do (tm, ty, pats) <- elabValBind toplevel True t'
52 logLvl 4 ("Elaborated:\n" ++ show tm ++ " : " ++ show ty ++ "\n" ++ show pats)
52 logLvl 4 ("Elaborated:\n" ++ show tm ++ " : " ++ show ty ++ "\n" ++ show pats)
53 ist <- getIState
53 ist <- getIState
@@ -57,7 +57,7 @@ split n t'
57 let ctxt = tt_ctxt ist
57 let ctxt = tt_ctxt ist
58 case lookup n pats of
58 case lookup n pats of
59 Nothing -> fail $ show n ++ " is not a pattern variable"
59 Nothing -> fail $ show n ++ " is not a pattern variable"
60− Just ty ->
60+ Just ty ->
61 do let splits = findPats ist ty
61 do let splits = findPats ist ty
62 iLOG ("New patterns " ++ show splits)
62 iLOG ("New patterns " ++ show splits)
63 let newPats_in = zipWith (replaceVar ctxt n) splits (repeat t)
63 let newPats_in = zipWith (replaceVar ctxt n) splits (repeat t)
@@ -65,7 +65,7 @@ split n t'
65 logLvl 4 ("Trying " ++ showSep "\n" (map show newPats_in))
65 logLvl 4 ("Trying " ++ showSep "\n" (map show newPats_in))
66 newPats <- mapM elabNewPat newPats_in
66 newPats <- mapM elabNewPat newPats_in
67 logLvl 3 ("Original:\n" ++ show t)
67 logLvl 3 ("Original:\n" ++ show t)
68− logLvl 3 ("Split:\n" ++
68+ logLvl 3 ("Split:\n" ++
69 (showSep "\n" (map show (mapMaybe id newPats))))
69 (showSep "\n" (map show (mapMaybe id newPats))))
70 logLvl 3 "----"
70 logLvl 3 "----"
71 let newPats' = mergeAllPats ctxt t (mapMaybe id newPats)
71 let newPats' = mergeAllPats ctxt t (mapMaybe id newPats)
@@ -75,17 +75,17 @@ split n t'
76data MergeState = MS { namemap :: [(Name, Name)],
76data MergeState = MS { namemap :: [(Name, Name)],
77 updates :: [(Name, PTerm)] }
77 updates :: [(Name, PTerm)] }
79addUpdate n tm = do ms <- get
79addUpdate n tm = do ms <- get
80 put (ms { updates = ((n, stripNS tm) : updates ms) } )
80 put (ms { updates = ((n, stripNS tm) : updates ms) } )
82−stripNS tm = mapPT dens tm where
82+stripNS tm = mapPT dens tm where
83 dens (PRef fc n) = PRef fc (nsroot n)
83 dens (PRef fc n) = PRef fc (nsroot n)
84 dens t = t
84 dens t = t
86mergeAllPats :: Context -> PTerm -> [PTerm] -> [(PTerm, [(Name, PTerm)])]
86mergeAllPats :: Context -> PTerm -> [PTerm] -> [(PTerm, [(Name, PTerm)])]
87mergeAllPats ctxt t [] = []
87mergeAllPats ctxt t [] = []
88−mergeAllPats ctxt t (p : ps)
88+mergeAllPats ctxt t (p : ps)
89 = let (p', MS _ u) = runState (mergePat ctxt t p) (MS [] [])
89 = let (p', MS _ u) = runState (mergePat ctxt t p) (MS [] [])
90 ps' = mergeAllPats ctxt t ps in
90 ps' = mergeAllPats ctxt t ps in
91 ((p, u) : ps')
91 ((p, u) : ps')
@@ -97,7 +97,7 @@ mergePat ctxt (PPatvar fc n) new
97 = mergePat ctxt (PRef fc n) new
97 = mergePat ctxt (PRef fc n) new
98mergePat ctxt old (PPatvar fc n)
98mergePat ctxt old (PPatvar fc n)
99 = mergePat ctxt old (PRef fc n)
99 = mergePat ctxt old (PRef fc n)
100−mergePat ctxt orig@(PRef fc n) new@(PRef _ n')
100+mergePat ctxt orig@(PRef fc n) new@(PRef _ n')
101 | isDConName n' ctxt = do addUpdate n new;
101 | isDConName n' ctxt = do addUpdate n new;
102 return new
102 return new
103 | otherwise
103 | otherwise
@@ -107,7 +107,7 @@ mergePat ctxt orig@(PRef fc n) new@(PRef _ n')
107 return (PRef fc x)
107 return (PRef fc x)
108 Nothing -> do put (ms { namemap = ((n', n) : namemap ms) })
108 Nothing -> do put (ms { namemap = ((n', n) : namemap ms) })
109 return (PRef fc n)
109 return (PRef fc n)
110−mergePat ctxt (PApp _ _ args) (PApp fc f args')
110+mergePat ctxt (PApp _ _ args) (PApp fc f args')
111 = do newArgs <- zipWithM mergeArg args args'
111 = do newArgs <- zipWithM mergeArg args args'
112 return (PApp fc f newArgs)
112 return (PApp fc f newArgs)
113 where mergeArg x y = do tm' <- mergePat ctxt (getTm x) (getTm y)
113 where mergeArg x y = do tm' <- mergePat ctxt (getTm x) (getTm y)
@@ -124,7 +124,7 @@ mergePat ctxt x y = return y
124mergeUserImpl :: PTerm -> PTerm -> PTerm
124mergeUserImpl :: PTerm -> PTerm -> PTerm
125mergeUserImpl x y = x
125mergeUserImpl x y = x
127−tidy orig@(PRef fc n)
127+tidy orig@(PRef fc n)
128 = do ms <- get
128 = do ms <- get
129 case lookup n (namemap ms) of
129 case lookup n (namemap ms) of
130 Just x -> return (PRef fc x)
130 Just x -> return (PRef fc x)
@@ -181,10 +181,10 @@ splitOnLine :: Int -- ^ line number
181 -> Name -- ^ variable
181 -> Name -- ^ variable
182 -> FilePath -- ^ name of file
182 -> FilePath -- ^ name of file
183 -> Idris [[(Name, PTerm)]]
183 -> Idris [[(Name, PTerm)]]
184−splitOnLine l n fn = do
184+splitOnLine l n fn = do
185-- let (before, later) = splitAt (l-1) (lines inp)
185-- let (before, later) = splitAt (l-1) (lines inp)
186-- i <- getIState
186-- i <- getIState
187− cl <- getInternalApp fn l
187+ cl <- getInternalApp fn l
188 logLvl 3 ("Working with " ++ showImp Nothing True False cl)
188 logLvl 3 ("Working with " ++ showImp Nothing True False cl)
189 tms <- split n cl
189 tms <- split n cl
190-- iputStrLn (showSep "\n" (map show tms))
190-- iputStrLn (showSep "\n" (map show tms))
@@ -210,7 +210,7 @@ replaceSplits l ups = updateRHSs 1 (map (rep (expandBraces l)) ups)
210 return (x : xs', i')
210 return (x : xs', i')
211 updateRHS i [] = return ("", i)
211 updateRHS i [] = return ("", i)
213− getUniq nm i
213+ getUniq nm i
214 = do ist <- getIState
214 = do ist <- getIState
215 let n = nameRoot [] nm ++ "_" ++ show i
215 let n = nameRoot [] nm ++ "_" ++ show i
216 case lookupTy (UN n) (tt_ctxt ist) of
216 case lookupTy (UN n) (tt_ctxt ist) of
@@ -218,15 +218,15 @@ replaceSplits l ups = updateRHSs 1 (map (rep (expandBraces l)) ups)
218 _ -> getUniq nm (i+1)
218 _ -> getUniq nm (i+1)
220 nameRoot acc nm | all isDigit nm = showSep "_" acc
220 nameRoot acc nm | all isDigit nm = showSep "_" acc
221− nameRoot acc nm =
221+ nameRoot acc nm =
222 case span (/='_') nm of
222 case span (/='_') nm of
223 (before, ('_' : after)) -> nameRoot (acc ++ [before]) after
223 (before, ('_' : after)) -> nameRoot (acc ++ [before]) after
224 _ -> showSep "_" (acc ++ [nm])
224 _ -> showSep "_" (acc ++ [nm])
226− -- TMP HACK: If there are Nats, we don't want to show as numerals since
226+ -- TMP HACK: If there are Nats, we don't want to show as numerals since
227 -- this isn't supported in a pattern, so special case here
227 -- this isn't supported in a pattern, so special case here
228 nshow brack (PRef _ (UN "Z")) = "Z"
228 nshow brack (PRef _ (UN "Z")) = "Z"
229− nshow brack (PApp _ (PRef _ (UN "S")) [x]) =
229+ nshow brack (PApp _ (PRef _ (UN "S")) [x]) =
230 if brack then "(S " else "S " ++ nshow True (getTm x) ++
230 if brack then "(S " else "S " ++ nshow True (getTm x) ++
231 if brack then ")" else ""
231 if brack then ")" else ""
232 nshow _ t = show t
232 nshow _ t = show t
@@ -235,14 +235,14 @@ replaceSplits l ups = updateRHSs 1 (map (rep (expandBraces l)) ups)
235 expandBraces ('{' : xs)
235 expandBraces ('{' : xs)
236 = let (brace, (_:rest)) = span (/= '}') xs in
236 = let (brace, (_:rest)) = span (/= '}') xs in
237 if (not ('=' `elem` brace))
237 if (not ('=' `elem` brace))
238− then ('{' : brace ++ " = " ++ brace ++ "}") ++
238+ then ('{' : brace ++ " = " ++ brace ++ "}") ++
239 expandBraces rest
239 expandBraces rest
240 else ('{' : brace ++ "}") ++ expandBraces rest
240 else ('{' : brace ++ "}") ++ expandBraces rest
241 expandBraces (x : xs) = x : expandBraces xs
241 expandBraces (x : xs) = x : expandBraces xs
242 expandBraces [] = []
242 expandBraces [] = []
244 updatePat start n tm [] = []
244 updatePat start n tm [] = []
245− updatePat start n tm ('{':rest) =
245+ updatePat start n tm ('{':rest) =
246 let (space, rest') = span isSpace rest in
246 let (space, rest') = span isSpace rest in
247 '{' : space ++ updatePat False n tm rest'
247 '{' : space ++ updatePat False n tm rest'
248 updatePat True n tm xs@(c:rest) | length xs > length n
248 updatePat True n tm xs@(c:rest) | length xs > length n
@@ -261,7 +261,7 @@ getClause :: Int -> -- ^ Line type is declared on
261 Idris String
261 Idris String
262getClause l fn fp = do ty <- getInternalApp fp l
262getClause l fn fp = do ty <- getInternalApp fp l
263 let ap = mkApp ty [1..]
263 let ap = mkApp ty [1..]
264− return (show fn ++ " " ++ ap ++
264+ return (show fn ++ " " ++ ap ++
265 "= ?" ++ show fn ++ "_rhs")
265 "= ?" ++ show fn ++ "_rhs")
266 where mkApp (PPi (Exp _ _ _ False) (MN _ _) _ sc) (n : ns)
266 where mkApp (PPi (Exp _ _ _ False) (MN _ _) _ sc) (n : ns)
267 = "x" ++ show n ++ " " ++ mkApp sc ns
267 = "x" ++ show n ++ " " ++ mkApp sc ns
@@ -275,17 +275,15 @@ getClause l fn fp = do ty <- getInternalApp fp l
276mkWith :: String -> Name -> String
276mkWith :: String -> Name -> String
277mkWith str n = let ind = getIndent str
277mkWith str n = let ind = getIndent str
278− str' = replicate ind ' ' ++
278+ str' = replicate ind ' ' ++
279 replaceRHS str "with (_)"
279 replaceRHS str "with (_)"
280− newpat = replicate (ind + 2) ' ' ++
280+ newpat = replicate (ind + 2) ' ' ++
281 replaceRHS str "| with_pat = ?" ++ show n ++ "_rhs" in
281 replaceRHS str "| with_pat = ?" ++ show n ++ "_rhs" in
282 str' ++ "\n" ++ newpat
282 str' ++ "\n" ++ newpat
284 where getIndent s = length (takeWhile isSpace s)
284 where getIndent s = length (takeWhile isSpace s)
286 replaceRHS [] str = str
286 replaceRHS [] str = str
287 replaceRHS ('?':'=': rest) str = str
287 replaceRHS ('?':'=': rest) str = str
288 replaceRHS ('=': rest) str = str
288 replaceRHS ('=': rest) str = str
289 replaceRHS (x : rest) str = x : replaceRHS rest str
289 replaceRHS (x : rest) str = x : replaceRHS rest str