@@ -1195,7 +1195,8 @@ addImpl = addImpl' False [] []
1195-- and *not* inside a PHidden
1195-- and *not* inside a PHidden
1197addImpl' :: Bool -> [Name] -> [Name] -> IState -> PTerm -> PTerm
1197addImpl' :: Bool -> [Name] -> [Name] -> IState -> PTerm -> PTerm
1198−addImpl' inpat env infns ist ptm = ai (zip env (repeat Nothing)) [] ptm
1198+addImpl' inpat env infns ist ptm
1199+ = mkUniqueNames env (ai (zip env (repeat Nothing)) [] ptm)
1200 ai env ds (PRef fc f)
1201 ai env ds (PRef fc f)
1201 | f `elem` infns = PInferRef fc f
1202 | f `elem` infns = PInferRef fc f
@@ -1624,9 +1625,11 @@ substMatchShadow n shs tm t = sm shs t where
1624shadow :: Name -> Name -> PTerm -> PTerm
1625shadow :: Name -> Name -> PTerm -> PTerm
1625shadow n n' t = sm t where
1626shadow n n' t = sm t where
1626 sm (PRef fc x) | n == x = PRef fc n'
1627 sm (PRef fc x) | n == x = PRef fc n'
1627− sm (PLam x t sc) = PLam x (sm t) (sm sc)
1628+ sm (PLam x t sc) | n /= x = PLam x (sm t) (sm sc)
1628− sm (PPi p x t sc) = PPi p x (sm t) (sm sc)
1629+ sm (PPi p x t sc) | n /=x = PPi p x (sm t) (sm sc)
1630+ sm (PLet x t v sc) | n /= x = PLet x (sm t) (sm v) (sm sc)
1629 sm (PApp f x as) = PApp f (sm x) (map (fmap sm) as)
1631 sm (PApp f x as) = PApp f (sm x) (map (fmap sm) as)
1632+ sm (PAppBind f x as) = PAppBind f (sm x) (map (fmap sm) as)
1630 sm (PCase f x as) = PCase f (sm x) (map (pmap sm) as)
1633 sm (PCase f x as) = PCase f (sm x) (map (pmap sm) as)
1631 sm (PEq f x y) = PEq f (sm x) (sm y)
1634 sm (PEq f x y) = PEq f (sm x) (sm y)
1632 sm (PRewrite f x y tm) = PRewrite f (sm x) (sm y) (fmap sm tm)
1635 sm (PRewrite f x y tm) = PRewrite f (sm x) (sm y) (fmap sm tm)
@@ -1634,9 +1637,87 @@ shadow n n' t = sm t where
1634 sm (PPair f x y) = PPair f (sm x) (sm y)
1637 sm (PPair f x y) = PPair f (sm x) (sm y)
1635 sm (PDPair f x t y) = PDPair f (sm x) (sm t) (sm y)
1638 sm (PDPair f x t y) = PDPair f (sm x) (sm t) (sm y)
1636 sm (PAlternative a as) = PAlternative a (map sm as)
1639 sm (PAlternative a as) = PAlternative a (map sm as)
1640+ sm (PTactics ts) = PTactics (map (fmap sm) ts)
1641+ sm (PProof ts) = PProof (map (fmap sm) ts)
1637 sm (PHidden x) = PHidden (sm x)
1642 sm (PHidden x) = PHidden (sm x)
1638 sm (PUnifyLog x) = PUnifyLog (sm x)
1643 sm (PUnifyLog x) = PUnifyLog (sm x)
1639 sm (PNoImplicits x) = PNoImplicits (sm x)
1644 sm (PNoImplicits x) = PNoImplicits (sm x)
1640 sm x = x
1645 sm x = x
1647+-- Rename any binders which are repeated (so that we don't have to mess
1648+-- about with shadowing anywhere else).
1650+mkUniqueNames :: [Name] -> PTerm -> PTerm
1651+mkUniqueNames env tm = evalState (mkUniq tm) env where
1652+ inScope = boundNamesIn tm
1654+ mkUniqA arg = do t' <- mkUniq (getTm arg)
1655+ return (arg { getTm = t' })
1657+ -- FIXME: Probably ought to do this for completeness! It's fine as
1658+ -- long as there are no bindings inside tactics though.
1659+ mkUniqT tac = return tac
1661+ mkUniq :: PTerm -> State [Name] PTerm
1662+ mkUniq (PLam n ty sc)
1666+ then do let n' = uniqueName n (env ++ inScope)
1667+ return (n', shadow n n' sc)
1668+ else return (n, sc)
1672+ return $! PLam n' ty' sc''
1673+ mkUniq (PPi p n ty sc)
1677+ then do let n' = uniqueName n (env ++ inScope)
1678+ return (n', shadow n n' sc)
1679+ else return (n, sc)
1683+ return $! PPi p n' ty' sc''
1684+ mkUniq (PLet n ty val sc)
1688+ then do let n' = uniqueName n (env ++ inScope)
1689+ return (n', shadow n n' sc)
1690+ else return (n, sc)
1692+ ty' <- mkUniq ty; val' <- mkUniq val
1694+ return $! PLet n' ty' val' sc''
1695+ mkUniq (PApp fc t args)
1696+ = do t' <- mkUniq t
1697+ args' <- mapM mkUniqA args
1698+ return $! PApp fc t' args'
1699+ mkUniq (PAppBind fc t args)
1700+ = do t' <- mkUniq t
1701+ args' <- mapM mkUniqA args
1702+ return $! PAppBind fc t' args'
1703+ mkUniq (PCase fc t alts)
1704+ = do t' <- mkUniq t
1705+ alts' <- mapM (\(x,y)-> do x' <- mkUniq x; y' <- mkUniq y
1706+ return (x', y')) alts
1707+ return $! PCase fc t' alts'
1708+ mkUniq (PPair fc l r)
1709+ = do l' <- mkUniq l; r' <- mkUniq r
1710+ return $! PPair fc l' r'
1711+ mkUniq (PDPair fc l t r)
1712+ = do l' <- mkUniq l; t' <- mkUniq t; r' <- mkUniq r
1713+ return $! PDPair fc l' t' r'
1714+ mkUniq (PAlternative b as)
1715+ = liftM (PAlternative b) (mapM mkUniq as)
1716+ mkUniq (PHidden t) = liftM PHidden (mkUniq t)
1717+ mkUniq (PUnifyLog t) = liftM PUnifyLog (mkUniq t)
1718+ mkUniq (PDisamb n t) = liftM (PDisamb n) (mkUniq t)
1719+ mkUniq (PNoImplicits t) = liftM PNoImplicits (mkUniq t)
1720+ mkUniq (PProof ts) = liftM PProof (mapM mkUniqT ts)
1721+ mkUniq (PTactics ts) = liftM PTactics (mapM mkUniqT ts)
1722+ mkUniq t = return t