@@ -1195,7 +1195,8 @@ addImpl = addImpl' False [] []
11951195-- and *not* inside a PHidden
11961196
11971197addImpl' :: 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)
11991200 where
12001201 ai env ds (PRef fc f)
12011202 | f `elem` infns = PInferRef fc f
@@ -1624,9 +1625,11 @@ substMatchShadow n shs tm t = sm shs t where
16241625shadow :: Name -> Name -> PTerm -> PTerm
16251626shadow n n' t = sm t where
16261627 sm (PRef fc x) | n == x = PRef fc n'
1627− sm (PLam x t sc) = PLam x (sm t) (sm sc)
1628− sm (PPi p x t sc) = PPi p x (sm t) (sm sc)
1628+ sm (PLam x t sc) | n /= x = PLam 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)
16291631 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)
16301633 sm (PCase f x as) = PCase f (sm x) (map (pmap sm) as)
16311634 sm (PEq f x y) = PEq f (sm x) (sm y)
16321635 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
16341637 sm (PPair f x y) = PPair f (sm x) (sm y)
16351638 sm (PDPair f x t y) = PDPair f (sm x) (sm t) (sm y)
16361639 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)
16371642 sm (PHidden x) = PHidden (sm x)
16381643 sm (PUnifyLog x) = PUnifyLog (sm x)
16391644 sm (PNoImplicits x) = PNoImplicits (sm x)
16401645 sm x = x
16411646
1647+-- Rename any binders which are repeated (so that we don't have to mess
1648+-- about with shadowing anywhere else).
1649+
1650+mkUniqueNames :: [Name] -> PTerm -> PTerm
1651+mkUniqueNames env tm = evalState (mkUniq tm) env where
1652+ inScope = boundNamesIn tm
1653+
1654+ mkUniqA arg = do t' <- mkUniq (getTm arg)
1655+ return (arg { getTm = t' })
1656+
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
1660+
1661+ mkUniq :: PTerm -> State [Name] PTerm
1662+ mkUniq (PLam n ty sc)
1663+ = do env <- get
1664+ (n', sc') <-
1665+ if n `elem` env
1666+ then do let n' = uniqueName n (env ++ inScope)
1667+ return (n', shadow n n' sc)
1668+ else return (n, sc)
1669+ put (n' : env)
1670+ ty' <- mkUniq ty
1671+ sc'' <- mkUniq sc'
1672+ return $! PLam n' ty' sc''
1673+ mkUniq (PPi p n ty sc)
1674+ = do env <- get
1675+ (n', sc') <-
1676+ if n `elem` env
1677+ then do let n' = uniqueName n (env ++ inScope)
1678+ return (n', shadow n n' sc)
1679+ else return (n, sc)
1680+ put (n' : env)
1681+ ty' <- mkUniq ty
1682+ sc'' <- mkUniq sc'
1683+ return $! PPi p n' ty' sc''
1684+ mkUniq (PLet n ty val sc)
1685+ = do env <- get
1686+ (n', sc') <-
1687+ if n `elem` env
1688+ then do let n' = uniqueName n (env ++ inScope)
1689+ return (n', shadow n n' sc)
1690+ else return (n, sc)
1691+ put (n' : env)
1692+ ty' <- mkUniq ty; val' <- mkUniq val
1693+ sc'' <- mkUniq sc'
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
16421723