@@ -33,7 +33,7 @@ data UResult a = UOK a
3333
3434match_unify :: Context -> Env -> TT Name -> TT Name -> [Name] -> [Name] ->
3535 TC [(Name, TT Name)]
36−match_unify ctxt env topx topy dont holes =
36+match_unify ctxt env topx topy inj holes =
3737 case runStateT (un [] topx topy) (UI 0 []) of
3838 OK (v, UI _ []) -> return (map (renameBinders env) (trimSolutions v))
3939 res ->
@@ -169,7 +169,7 @@ expandLets env (x, tm) = (x, doSubst (reverse env) tm)
169169
170170unify :: Context -> Env -> TT Name -> TT Name -> [Name] -> [Name] ->
171171 TC ([(Name, TT Name)], Fails)
172−unify ctxt env topx topy dont holes =
172+unify ctxt env topx topy inj holes =
173173-- trace ("Unifying " ++ show (topx, topy)) $
174174 -- don't bother if topx and topy are different at the head
175175 case runStateT (un False [] topx topy) (UI 0 []) of
@@ -198,12 +198,20 @@ unify ctxt env topx topy dont holes =
198198 injective (App f a) = injective f -- && injective a
199199 injective _ = False
200200
201+-- injectiveVar (P _ (MN _ _) _) = True -- TMP HACK
202+ injectiveVar (P _ n _) = n `elem` inj
203+ injectiveVar (App f a) = injectiveVar f -- && injective a
204+ injectiveVar _ = False
205+
206+ injectiveApp x = injective x || injectiveVar x
207+
201208 notP (P _ _ _) = False
202209 notP _ = True
203210
204211 sc i = do UI s f <- get
205212 put (UI (s+i) f)
206213
214+ errors :: StateT UInfo TC Bool
207215 errors = do UI s f <- get
208216 return (not (null f))
209217
@@ -302,9 +310,18 @@ unify ctxt env topx topy dont holes =
302310 -- class resolution for type classes over functions.
303311
304312 un' fn bnames (App f x) (Bind n (Pi t) y)
305− | noOccurrence n y && x == y
306− = un' False bnames f (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
307− (Bind n (Pi t) (V 1)))
313+ | noOccurrence n y && injectiveApp f
314+ = do ux <- un' False bnames x y
315+ uf <- un' False bnames f (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
316+ (Bind n (Pi t) (V 1)))
317+ combine bnames ux uf
318+
319+ un' fn bnames (Bind n (Pi t) y) (App f x)
320+ | noOccurrence n y && injectiveApp f
321+ = do ux <- un' False bnames y x
322+ uf <- un' False bnames (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
323+ (Bind n (Pi t) (V 1))) f
324+ combine bnames ux uf
308325
309326 un' fn bnames (Bind x bx sx) (Bind y by sy)
310327 = do h1 <- uB bnames bx by
@@ -321,11 +338,11 @@ unify ctxt env topx topy dont holes =
321338 return [] -- lift $ tfail err
322339
323340 unApp fn bnames appx@(App fx ax) appy@(App fy ay)
324− | (injective fx && injective fy)
325− || (injective fx && rigid appx && metavarApp appy)
326− || (injective fy && rigid appy && metavarApp appx)
327− || (injective fx && metavarApp fy && ax == ay)
328− || (injective fy && metavarApp fx && ax == ay)
341+ | (injectiveApp fx && injectiveApp fy)
342+ || (injectiveApp fx && rigid appx && metavarApp appy)
343+ || (injectiveApp fy && rigid appy && metavarApp appx)
344+ || (injectiveApp fx && metavarApp fy && ax == ay)
345+ || (injectiveApp fy && metavarApp fx && ax == ay)
329346 = do let (headx, _) = unApply fx
330347 let (heady, _) = unApply fy
331348 -- fail quickly if the heads are disjoint
@@ -347,23 +364,24 @@ unify ctxt env topx topy dont holes =
347364 hf <- un' False bnames fx' fy'
348365 sc 1
349366 combine bnames hf ha)
350− | otherwise = -- trace (show (appx, appy, injective fx, metavarApp appy, sameArgStruct appx appy)) $
351− do let (headx, argsx) = unApply appx
352− let (heady, argsy) = unApply appy
353− -- traceWhen (headx == heady) (show (appx, appy)) $
354− uplus (
355− if (length argsx == length argsy &&
356− ((headx == heady && inenv headx) || (argsx == argsy) ||
357− (and (zipWith sameStruct (headx:argsx) (heady:argsy)))))
358− then
359−-- (notFn headx && notFn heady))) then
360− do uf <- un' True bnames headx heady
361− failed <- errors
362− if (not failed) then unArgs uf argsx argsy
363− else return []
364− else -- trace ("TMPFAIL " ++ show (appx, appy, injective appx, injective appy)) $
365− unifyTmpFail appx appy)
366− (unifyTmpFail appx appy) -- whole application fails
367+ | otherwise = -- FIXME: This fudge should be:
368+ unifyTmpFail appx appy
369+-- do let (headx, argsx) = unApply appx
370+-- let (heady, argsy) = unApply appy
371+-- -- traceWhen (headx == heady) (show (appx, appy)) $
372+-- uplus (
373+-- if (length argsx == length argsy &&
374+-- ((headx == heady && inenv headx) || (argsx == argsy) ||
375+-- (and (zipWith sameStruct (headx:argsx) (heady:argsy)))))
376+-- then
377+-- -- (notFn headx && notFn heady))) then
378+-- do uf <- un' True bnames headx heady
379+-- failed <- errors
380+-- if (not failed) then unArgs uf argsx argsy
381+-- else return []
382+-- else -- trace ("TMPFAIL " ++ show (appx, appy, injective appx, injective appy)) $
383+-- unifyTmpFail appx appy)
384+-- (unifyTmpFail appx appy) -- whole application fails
367385 where hnormalise [] _ _ t = t
368386 hnormalise ns ctxt env t = normalise ctxt env t
369387 checkHeads (P (DCon _ _) x _) (P (DCon _ _) y _)
@@ -433,8 +451,7 @@ unify ctxt env topx topy dont holes =
433451 _ -> False
434452
435453 metavar t = case t of
436− P _ x _ -> ((x `elem` holes || holeIn env x) &&
437− not (x `elem` dont))
454+ P _ x _ -> (x `elem` holes || holeIn env x)
438455 || globmetavar t
439456 _ -> False
440457 pat t = case t of
@@ -447,6 +464,7 @@ unify ctxt env topx topy dont holes =
447464 notFn t = injective t || metavar t || inenv t
448465
449466
467+ unifyTmpFail :: Term -> Term -> StateT UInfo TC [(Name, TT Name)]
450468 unifyTmpFail x y
451469 = do UI s f <- get
452470 let r = recoverable x y