@@ -1,6 +1,6 @@
1{-# LANGUAGE PatternGuards #-}
1{-# LANGUAGE PatternGuards #-}
3−module Idris.CaseSplit(split, splitOnLine) where
3+module Idris.CaseSplit(split, splitOnLine, replaceSplits) where
5-- splitting a variable in a pattern clause
5-- splitting a variable in a pattern clause
@@ -16,6 +16,7 @@ import Core.Typecheck
16import Core.Evaluate
16import Core.Evaluate
18import Data.Maybe
18import Data.Maybe
19import Control.Monad
20import Control.Monad
20import Control.Monad.State.Strict
21import Control.Monad.State.Strict
@@ -25,6 +26,8 @@ import Text.Trifecta(Result(..), parseString)
25import Text.Trifecta.Delta
26import Text.Trifecta.Delta
26import qualified Data.ByteString.UTF8 as UTF8
27import qualified Data.ByteString.UTF8 as UTF8
30Given a pattern clause and a variable 'n', elaborate the cluse and find the
33Given a pattern clause and a variable 'n', elaborate the cluse and find the
@@ -71,7 +74,11 @@ data MergeState = MS { namemap :: [(Name, Name)],
71 updates :: [(Name, PTerm)] }
74 updates :: [(Name, PTerm)] }
73addUpdate n tm = do ms <- get
76addUpdate n tm = do ms <- get
74− put (ms { updates = ((n, tm) : updates ms) } )
77+ put (ms { updates = ((n, stripNS tm) : updates ms) } )
79+stripNS tm = mapPT dens tm where
80+ dens (PRef fc n) = PRef fc (nsroot n)
76mergeAllPats :: Context -> PTerm -> [PTerm] -> [(PTerm, [(Name, PTerm)])]
83mergeAllPats :: Context -> PTerm -> [PTerm] -> [(PTerm, [(Name, PTerm)])]
77mergeAllPats ctxt t [] = []
84mergeAllPats ctxt t [] = []
@@ -169,9 +176,8 @@ replaceVar ctxt n t pat = pat
169splitOnLine :: Int -- ^ line number
176splitOnLine :: Int -- ^ line number
170 -> Name -- ^ variable
177 -> Name -- ^ variable
171 -> FilePath -- ^ name of file
178 -> FilePath -- ^ name of file
172− -> String -- ^ file contents
173 -> Idris [[(Name, PTerm)]]
179 -> Idris [[(Name, PTerm)]]
174−splitOnLine l n fn inp = do
180+splitOnLine l n fn = do
175-- let (before, later) = splitAt (l-1) (lines inp)
181-- let (before, later) = splitAt (l-1) (lines inp)
176-- i <- getIState
182-- i <- getIState
177 cl <- getInternalApp fn l
183 cl <- getInternalApp fn l
@@ -180,7 +186,43 @@ splitOnLine l n fn inp = do
180-- iputStrLn (showSep "\n" (map show tms))
186-- iputStrLn (showSep "\n" (map show tms))
181 return tms -- "" -- not yet done...
187 return tms -- "" -- not yet done...
183−getFnName (PClause _ n _ _ _ _) = n
189+replaceSplits :: String -> [[(Name, PTerm)]] -> [String]
184−getFnName (PWith _ n _ _ _ _) = n
190+replaceSplits l ups = map (rep (expandBraces l)) ups
193+ rep str ((n, tm) : ups) = rep (updatePat False (show n) (nshow False tm) str) ups
195+ -- TMP HACK: If there are Nats, we don't want to show as numerals since
196+ -- this isn't supported in a pattern, so special case here
197+ nshow brack (PRef _ (UN "Z")) = "Z"
198+ nshow brack (PApp _ (PRef _ (UN "S")) [x]) =
199+ if brack then "(S " else "S " ++ nshow True (getTm x) ++
200+ if brack then ")" else ""
203+ -- if there's any {n} replace with {n=n}
204+ expandBraces ('{' : xs)
205+ = let (brace, (_:rest)) = span (/= '}') xs in
206+ if (not ('=' `elem` brace))
207+ then ('{' : brace ++ " = " ++ brace ++ "}") ++
209+ else ('{' : brace ++ "}") ++ expandBraces rest
210+ expandBraces (x : xs) = x : expandBraces xs
211+ expandBraces [] = []
213+ updatePat start n tm [] = []
214+ updatePat start n tm ('{':rest) =
215+ let (space, rest') = span isSpace rest in
216+ '{' : space ++ updatePat False n tm rest'
217+ updatePat True n tm xs@(c:rest) | length xs > length n
218+ = let (before, after@(next:_)) = splitAt (length n) xs in
219+ if (before == n && not (isAlpha next))
220+ then addBrackets tm ++ updatePat False n tm after
221+ else c : updatePat (not (isAlpha c)) n tm rest
222+ updatePat start n tm (c:rest) = c : updatePat (not (isAlpha c)) n tm rest
224+ addBrackets tm | ' ' `elem` tm = "(" ++ tm ++ ")"