@@ -77,11 +77,28 @@ split n t'
77 return (map snd newPats')
77 return (map snd newPats')
79data MergeState = MS { namemap :: [(Name, Name)],
79data MergeState = MS { namemap :: [(Name, Name)],
80+ invented :: [(Name, Name)],
80 updates :: [(Name, PTerm)] }
82 updates :: [(Name, PTerm)] }
82addUpdate n tm = do ms <- get
84addUpdate n tm = do ms <- get
83 put (ms { updates = ((n, stripNS tm) : updates ms) } )
85 put (ms { updates = ((n, stripNS tm) : updates ms) } )
89+ let badnames = map snd (namemap ms) ++ map snd (invented ms) ++
91+ case lookup n (invented ms) of
94+ do let n' = case n of
96+ uniqueName (UN "var") badnames
97+ MN i n -> uniqueName (UN n) badnames
99+ put (ms { invented = (n, n') : invented ms })
85stripNS tm = mapPT dens tm where
102stripNS tm = mapPT dens tm where
86 dens (PRef fc n) = PRef fc (nsroot n)
103 dens (PRef fc n) = PRef fc (nsroot n)
87 dens t = t
104 dens t = t
@@ -89,9 +106,14 @@ stripNS tm = mapPT dens tm where
89mergeAllPats :: Context -> PTerm -> [PTerm] -> [(PTerm, [(Name, PTerm)])]
106mergeAllPats :: Context -> PTerm -> [PTerm] -> [(PTerm, [(Name, PTerm)])]
90mergeAllPats ctxt t [] = []
107mergeAllPats ctxt t [] = []
91mergeAllPats ctxt t (p : ps)
108mergeAllPats ctxt t (p : ps)
92− = let (p', MS _ u) = runState (mergePat ctxt t p) (MS [] [])
109+ = let (p', MS _ _ _ u) = runState (mergePat ctxt t p)
110+ (MS [] [] (patvars t) [])
93 ps' = mergeAllPats ctxt t ps in
111 ps' = mergeAllPats ctxt t ps in
94 ((p, u) : ps')
112 ((p, u) : ps')
113+ where patvars (PRef _ n) = [n]
114+ patvars (PApp _ _ as) = concatMap (patvars . getTm) as
115+ patvars (PPatvar _ n) = [n]
96mergePat :: Context -> PTerm -> PTerm -> State MergeState PTerm
118mergePat :: Context -> PTerm -> PTerm -> State MergeState PTerm
97-- If any names are unified, make sure they stay unified. Always prefer
119-- If any names are unified, make sure they stay unified. Always prefer
@@ -133,7 +155,8 @@ tidy orig@(PRef fc n)
133 Just x -> return (PRef fc x)
155 Just x -> return (PRef fc x)
134 Nothing -> case n of
156 Nothing -> case n of
135 (UN _) -> return orig
157 (UN _) -> return orig
136− _ -> return Placeholder
158+ _ -> do n' <- inventName n
137tidy (PApp fc f args)
160tidy (PApp fc f args)
138 = do args' <- mapM tidyArg args
161 = do args' <- mapM tidyArg args
139 return (PApp fc f args')
162 return (PApp fc f args')
@@ -292,16 +315,12 @@ getProofClause l fn fp
292-- match clause
315-- match clause
294mkWith :: String -> Name -> String
317mkWith :: String -> Name -> String
295−mkWith str n = let ind = getIndent str
296− str' = replicate ind ' ' ++
297− replaceRHS str "with (_)"
318+mkWith str n = let str' = replaceRHS str "with (_)"
298− newpat = replicate (ind + 2) ' ' ++
319+ newpat = " " ++
299 replaceRHS str "| with_pat = ?" ++ show n ++ "_rhs" in
320 replaceRHS str "| with_pat = ?" ++ show n ++ "_rhs" in
300 str' ++ "\n" ++ newpat
321 str' ++ "\n" ++ newpat
302− where getIndent s = length (takeWhile isSpace s)
304− replaceRHS [] str = str
323+ where replaceRHS [] str = str
305 replaceRHS ('?':'=': rest) str = str
324 replaceRHS ('?':'=': rest) str = str
306 replaceRHS ('=': rest) str
325 replaceRHS ('=': rest) str
307 | not ('=' `elem` rest) = str
326 | not ('=' `elem` rest) = str