@@ -12,6 +12,7 @@ import System.Console.Haskeline
1212import Control.Monad.State
1313import Data.List
1414import Data.Char
15+import Data.Either
1516import Debug.Trace
1617
1718import qualified Epic.Epic as E
@@ -1154,50 +1155,75 @@ dumpDecl _ = "..."
11541155-- dumpDecl (PImport i) = "import " ++ i
11551156
11561157-- syntactic match of a against b, returning pair of variables in a
1157−-- and what they match. Returns 'Nothing' if not a match.
1158+-- and what they match. Returns the pair that failed if not a match.
11581159
1159−matchClause :: PTerm -> PTerm -> Maybe [(Name, PTerm)]
1160−matchClause x y = match x y where
1160+matchClause :: PTerm -> PTerm -> Either (PTerm, PTerm) [(Name, PTerm)]
1161+matchClause x y = checkRpts $ match (fullApp x) (fullApp y) where
11611162 matchArg x y = match (fullApp (getTm x)) (fullApp (getTm y))
11621163
11631164 fullApp (PApp _ (PApp fc f args) xs) = fullApp (PApp fc f (args ++ xs))
11641165 fullApp x = x
11651166
1167+ match' x y = match (fullApp x) (fullApp y)
11661168 match (PApp _ f args) (PApp _ f' args')
11671169 | length args == length args'
1168− = do mf <- match f f'
1170+ = do mf <- match' f f'
11691171 ms <- zipWithM matchArg args args'
11701172 return (mf ++ concat ms)
11711173-- match (PRef _ n) (PRef _ n') | n == n' = return []
11721174-- | otherwise = Nothing
11731175 match (PRef _ n) tm = return [(n, tm)]
1174− match (PEq _ l r) (PEq _ l' r') = do ml <- match l l'
1175− mr <- match r r'
1176+ match (PEq _ l r) (PEq _ l' r') = do ml <- match' l l'
1177+ mr <- match' r r'
11761178 return (ml ++ mr)
1177− match (PPair _ l r) (PPair _ l' r') = do ml <- match l l'
1178− mr <- match r r'
1179+ match (PPair _ l r) (PPair _ l' r') = do ml <- match' l l'
1180+ mr <- match' r r'
11791181 return (ml ++ mr)
1180− match (PDPair _ l t r) (PDPair _ l' t' r') = do ml <- match l l'
1181− mt <- match t t'
1182− mr <- match r r'
1182+ match (PDPair _ l t r) (PDPair _ l' t' r') = do ml <- match' l l'
1183+ mt <- match' t t'
1184+ mr <- match' r r'
11831185 return (ml ++ mt ++ mr)
11841186 match (PAlternative as) (PAlternative as')
1185− = do ms <- zipWithM match as as'
1187+ = do ms <- zipWithM match' as as'
11861188 return (concat ms)
1189+ match a@(PAlternative as) b
1190+ = do let ms = zipWith match' as (repeat b)
1191+ case (rights ms) of
1192+ (x: _) -> return x
1193+ _ -> Left (a, b)
1194+ match (PCase _ _ _) _ = return [] -- lifted out
1195+ match (PMetavar _) _ = return [] -- modified
1196+ match (PQuote _) _ = return []
1197+ match (PProof _) _ = return []
1198+ match (PTactics _) _ = return []
11871199 match (PRefl _) (PRefl _) = return []
11881200 match (PResolveTC _) (PResolveTC _) = return []
11891201 match (PTrue _) (PTrue _) = return []
11901202 match (PFalse _) (PFalse _) = return []
11911203 match (PReturn _) (PReturn _) = return []
1192− match (PPi _ _ t s) (PPi _ _ t' s') = do mt <- match t t'
1193− ms <- match s s'
1204+ match (PPi _ _ t s) (PPi _ _ t' s') = do mt <- match' t t'
1205+ ms <- match' s s'
11941206 return (mt ++ ms)
1195− match (PLam _ t s) (PLam _ t' s') = do mt <- match t t'
1196− ms <- match s s'
1207+ match (PLam _ t s) (PLam _ t' s') = do mt <- match' t t'
1208+ ms <- match' s s'
11971209 return (mt ++ ms)
1198− match (PHidden x) (PHidden y) = match x y
1210+ match (PLet _ t ty s) (PLet _ t' ty' s') = do mt <- match' t t'
1211+ mty <- match' ty ty'
1212+ ms <- match' s s'
1213+ return (mt ++ mty ++ ms)
1214+ match (PHidden x) (PHidden y) = match' x y
1215+ match Placeholder _ = return []
1216+ match (PResolveTC _) _ = return []
11991217 match a b | a == b = return []
1200− | otherwise = Nothing
1218+ | otherwise = Left (a, b)
1219+
1220+ checkRpts (Right ms) = check ms where
1221+ check ((n,t):xs)
1222+ | Just t' <- lookup n xs = if t/=t' then Left (t, t')
1223+ else check xs
1224+ check (_:xs) = check xs
1225+ check [] = Right ms
1226+ checkRpts x = x
12011227
12021228substMatches :: [(Name, PTerm)] -> PTerm -> PTerm
12031229substMatches [] t = t