@@ -1939,9 +1939,9 @@ aiFn topname inpat expat qq imp_meths ist fc f ffc ds as
19391939 case ns' of
19401940 [(f',ns)] -> Right $ mkPApp fc (length ns) (PRef ffc [ffc] (isImpName f f'))
19411941 (insertImpl ns as)
1942− [] -> if f `elem` (map fst (idris_metavars ist))
1943− then Right $ PApp fc (PRef ffc [ffc] f) as
1944− else Right $ mkPApp fc (length as) (PRef ffc [ffc] f) as
1942+ [] -> case metaVar f (map fst (idris_metavars ist)) of
1943+ Just f' -> Right $ PApp fc (PRef ffc [ffc] f') as
1944+ Nothing -> Right $ mkPApp fc (length as) (PRef ffc [ffc] f) as
19451945 alts -> Right $
19461946 PAlternative [] (ExactlyOne True) $
19471947 map (\(f', ns) -> mkPApp fc (length ns) (PRef ffc [ffc] (isImpName f f'))
@@ -1952,6 +1952,12 @@ aiFn topname inpat expat qq imp_meths ist fc f ffc ds as
19521952 isImpName f f' | f `elem` imp_meths = f
19531953 | otherwise = f'
19541954
1955+ -- If it's a metavariable name, try to qualify it from the list of
1956+ -- unsolved metavariables
1957+ metaVar f (mvn : ns) | f == nsroot mvn = Just mvn
1958+ metaVar f (_ : ns) = metaVar f ns
1959+ metaVar f [] = Nothing
1960+
19551961 trimAlts [] alts = alts
19561962 trimAlts ns alts
19571963 = filter (\(x, _) -> any (\d -> d `isPrefixOf` nspace x) ns) alts