@@ -71,7 +71,7 @@ split n t'
71 logLvl 3 ("Split:\n" ++
71 logLvl 3 ("Split:\n" ++
72 (showSep "\n" (map show (mapMaybe id newPats))))
72 (showSep "\n" (map show (mapMaybe id newPats))))
73 logLvl 3 "----"
73 logLvl 3 "----"
74− let newPats' = mergeAllPats ctxt t (mapMaybe id newPats)
74+ let newPats' = mergeAllPats ist n t (mapMaybe id newPats)
75 iLOG ("Name updates " ++ showSep "\n"
75 iLOG ("Name updates " ++ showSep "\n"
76 (map (\ (p, u) -> show u ++ " " ++ show p) newPats'))
76 (map (\ (p, u) -> show u ++ " " ++ show p) newPats'))
77 return (map snd newPats')
77 return (map snd newPats')
@@ -84,47 +84,55 @@ data MergeState = MS { namemap :: [(Name, Name)],
84addUpdate n tm = do ms <- get
84addUpdate n tm = do ms <- get
85 put (ms { updates = ((n, stripNS tm) : updates ms) } )
85 put (ms { updates = ((n, stripNS tm) : updates ms) } )
87−inventName n =
87+inventName ist ty n =
88 do ms <- get
88 do ms <- get
89+ let supp = case ty of
91+ Just t -> getNameHints ist t
92+ let nsupp = case n of
93+ MN i ('_':_) -> mkSupply (supp ++ varlist)
94+ MN i n -> mkSupply (UN n : supp ++ varlist)
95+ x -> mkSupply (x : supp)
89 let badnames = map snd (namemap ms) ++ map snd (invented ms) ++
96 let badnames = map snd (namemap ms) ++ map snd (invented ms) ++
90 explicit ms
97 explicit ms
91 case lookup n (invented ms) of
98 case lookup n (invented ms) of
92 Just n' -> return n'
99 Just n' -> return n'
93 Nothing ->
100 Nothing ->
94− do let n' = case n of
101+ do let n' = uniqueNameFrom nsupp badnames
96− uniqueName (UN "var") badnames
97− MN i n -> uniqueName (UN n) badnames
99 put (ms { invented = (n, n') : invented ms })
102 put (ms { invented = (n, n') : invented ms })
100 return n'
103 return n'
105+mkSupply ns = mkSupply' ns (map nextName ns)
106+ where mkSupply' xs ns' = xs ++ mkSupply ns'
108+varlist = map (UN . (:[])) "xyzwstuv" -- EB's personal preference :)
102stripNS tm = mapPT dens tm where
110stripNS tm = mapPT dens tm where
103 dens (PRef fc n) = PRef fc (nsroot n)
111 dens (PRef fc n) = PRef fc (nsroot n)
104 dens t = t
112 dens t = t
106−mergeAllPats :: Context -> PTerm -> [PTerm] -> [(PTerm, [(Name, PTerm)])]
114+mergeAllPats :: IState -> Name -> PTerm -> [PTerm] -> [(PTerm, [(Name, PTerm)])]
107−mergeAllPats ctxt t [] = []
115+mergeAllPats ist cv t [] = []
108−mergeAllPats ctxt t (p : ps)
116+mergeAllPats ist cv t (p : ps)
109− = let (p', MS _ _ _ u) = runState (mergePat ctxt t p)
117+ = let (p', MS _ _ _ u) = runState (mergePat ist t p Nothing)
110− (MS [] [] (patvars t) [])
118+ (MS [] [] (filter (/=cv) (patvars t)) [])
111− ps' = mergeAllPats ctxt t ps in
119+ ps' = mergeAllPats ist cv t ps in
112 ((p, u) : ps')
120 ((p, u) : ps')
113 where patvars (PRef _ n) = [n]
121 where patvars (PRef _ n) = [n]
114 patvars (PApp _ _ as) = concatMap (patvars . getTm) as
122 patvars (PApp _ _ as) = concatMap (patvars . getTm) as
115 patvars (PPatvar _ n) = [n]
123 patvars (PPatvar _ n) = [n]
116 patvars _ = []
124 patvars _ = []
118−mergePat :: Context -> PTerm -> PTerm -> State MergeState PTerm
126+mergePat :: IState -> PTerm -> PTerm -> Maybe Name -> State MergeState PTerm
119-- If any names are unified, make sure they stay unified. Always prefer
127-- If any names are unified, make sure they stay unified. Always prefer
120-- user provided name (first pattern)
128-- user provided name (first pattern)
121−mergePat ctxt (PPatvar fc n) new
129+mergePat ist (PPatvar fc n) new t
122− = mergePat ctxt (PRef fc n) new
130+ = mergePat ist (PRef fc n) new t
123−mergePat ctxt old (PPatvar fc n)
131+mergePat ist old (PPatvar fc n) t
124− = mergePat ctxt old (PRef fc n)
132+ = mergePat ist old (PRef fc n) t
125−mergePat ctxt orig@(PRef fc n) new@(PRef _ n')
133+mergePat ist orig@(PRef fc n) new@(PRef _ n') t
126− | isDConName n' ctxt = do addUpdate n new;
134+ | isDConName n' (tt_ctxt ist) = do addUpdate n new
127− return new
135+ return new
128 | otherwise
136 | otherwise
129 = do ms <- get
137 = do ms <- get
130 case lookup n' (namemap ms) of
138 case lookup n' (namemap ms) of
@@ -132,37 +140,49 @@ mergePat ctxt orig@(PRef fc n) new@(PRef _ n')
132 return (PRef fc x)
140 return (PRef fc x)
133 Nothing -> do put (ms { namemap = ((n', n) : namemap ms) })
141 Nothing -> do put (ms { namemap = ((n', n) : namemap ms) })
134 return (PRef fc n)
142 return (PRef fc n)
135−mergePat ctxt (PApp _ _ args) (PApp fc f args')
143+mergePat ist (PApp _ _ args) (PApp fc f args') t
136− = do newArgs <- zipWithM mergeArg args args'
144+ = do newArgs <- zipWithM mergeArg args (zip args' (argTys ist f))
137 return (PApp fc f newArgs)
145 return (PApp fc f newArgs)
146+ where mergeArg x (y, t)
138− where mergeArg x y = do tm' <- mergePat ctxt (getTm x) (getTm y)
147+ = do tm' <- mergePat ist (getTm x) (getTm y) t
139− case x of
148+ case x of
140− (PImp _ _ _ _ _ _) ->
149+ (PImp _ _ _ _ _ _) ->
141− return (y { machine_inf = machine_inf x,
150+ return (y { machine_inf = machine_inf x,
142− getTm = tm' })
151+ getTm = tm' })
143− _ -> return (y { getTm = tm' })
152+ _ -> return (y { getTm = tm' })
144−mergePat ctxt (PRef fc n) t = do tm <- tidy t
153+mergePat ist (PRef fc n) tm ty = do tm <- tidy ist tm ty
145− addUpdate n tm
154+ addUpdate n tm
146− return tm
155+ return tm
147−mergePat ctxt x y = return y
156+mergePat ist x y t = return y
149mergeUserImpl :: PTerm -> PTerm -> PTerm
158mergeUserImpl :: PTerm -> PTerm -> PTerm
150mergeUserImpl x y = x
159mergeUserImpl x y = x
161+argTys :: IState -> PTerm -> [Maybe Name]
162+argTys ist (PRef fc n)
163+ = case lookupTy n (tt_ctxt ist) of
164+ [ty] -> map (tyName . snd) (getArgTys ty) ++ repeat Nothing
166+ where tyName (Bind _ (Pi _) _) = Just (UN "->")
167+ tyName t | (P _ n _, _) <- unApply t = Just n
168+ | otherwise = Nothing
169+argTys _ _ = repeat Nothing
171+tidy :: IState -> PTerm -> Maybe Name -> State MergeState PTerm
152−tidy orig@(PRef fc n)
172+tidy ist orig@(PRef fc n) ty
153 = do ms <- get
173 = do ms <- get
154 case lookup n (namemap ms) of
174 case lookup n (namemap ms) of
155 Just x -> return (PRef fc x)
175 Just x -> return (PRef fc x)
156 Nothing -> case n of
176 Nothing -> case n of
157 (UN _) -> return orig
177 (UN _) -> return orig
158− _ -> do n' <- inventName n
178+ _ -> do n' <- inventName ist ty n
159 return (PRef fc n')
179 return (PRef fc n')
160−tidy (PApp fc f args)
180+tidy ist (PApp fc f args) ty
161− = do args' <- mapM tidyArg args
181+ = do args' <- zipWithM tidyArg args (argTys ist f)
162 return (PApp fc f args')
182 return (PApp fc f args')
163− where tidyArg x = do tm' <- tidy (getTm x)
183+ where tidyArg x ty' = do tm' <- tidy ist (getTm x) ty'
164− return (x { getTm = tm' })
184+ return (x { getTm = tm' })
165−tidy tm = return tm
185+tidy ist tm ty = return tm
168-- mapPT tidyVar tm
188-- mapPT tidyVar tm
@@ -291,15 +311,26 @@ getClause :: Int -- ^ line number that the type is declared on
291 -> FilePath -- ^ Source file name
311 -> FilePath -- ^ Source file name
292 -> Idris String
312 -> Idris String
293getClause l fn fp = do ty <- getInternalApp fp l
313getClause l fn fp = do ty <- getInternalApp fp l
294− let ap = mkApp ty [1..]
315+ let ap = mkApp ist ty []
295 return (show fn ++ " " ++ ap ++
316 return (show fn ++ " " ++ ap ++
296 "= ?" ++ show fn ++ "_rhs")
317 "= ?" ++ show fn ++ "_rhs")
297− where mkApp (PPi (Exp _ _ _ False) (MN _ _) _ sc) (n : ns)
318+ where mkApp i (PPi (Exp _ _ _ False) (MN _ _) ty sc) used
319+ = let n = getNameFrom i used ty in
298− = "x" ++ show n ++ " " ++ mkApp sc ns
320+ show n ++ " " ++ mkApp i sc (n : used)
299− mkApp (PPi (Exp _ _ _ False) n _ sc) ns
321+ mkApp i (PPi (Exp _ _ _ False) n _ sc) used
300− = show n ++ " " ++ mkApp sc ns
322+ = show n ++ " " ++ mkApp i sc (n : used)
301− mkApp (PPi _ _ _ sc) ns = mkApp sc ns
323+ mkApp i (PPi _ _ _ sc) used = mkApp i sc used
302− mkApp _ _ = ""
324+ mkApp i _ _ = ""
326+ getNameFrom i used (PPi _ _ _ _)
327+ = uniqueNameFrom (mkSupply [UN "f", UN "g"]) used
328+ getNameFrom i used (PApp fc f as) = getNameFrom i used f
329+ getNameFrom i used (PRef fc f)
330+ = case getNameHints i f of
331+ [] -> uniqueName (UN "x") used
332+ ns -> uniqueNameFrom (mkSupply ns) used
333+ getNameFrom i used _ = uniqueName (UN "x") used
304getProofClause :: Int -- ^ line number that the type is declared
335getProofClause :: Int -- ^ line number that the type is declared
305 -> Name -- ^ Function name
336 -> Name -- ^ Function name
@@ -326,4 +357,3 @@ mkWith str n = let str' = replaceRHS str "with (_)"
326 | not ('=' `elem` rest) = str
357 | not ('=' `elem` rest) = str
327 replaceRHS (x : rest) str = x : replaceRHS rest str
358 replaceRHS (x : rest) str = x : replaceRHS rest str