@@ -33,7 +33,7 @@ data UResult a = UOK a
34match_unify :: Context -> Env -> TT Name -> TT Name -> [Name] -> [Name] ->
34match_unify :: Context -> Env -> TT Name -> TT Name -> [Name] -> [Name] ->
35 TC [(Name, TT Name)]
35 TC [(Name, TT Name)]
36−match_unify ctxt env topx topy dont holes =
36+match_unify ctxt env topx topy inj holes =
37 case runStateT (un [] topx topy) (UI 0 []) of
37 case runStateT (un [] topx topy) (UI 0 []) of
38 OK (v, UI _ []) -> return (map (renameBinders env) (trimSolutions v))
38 OK (v, UI _ []) -> return (map (renameBinders env) (trimSolutions v))
@@ -169,7 +169,7 @@ expandLets env (x, tm) = (x, doSubst (reverse env) tm)
170unify :: Context -> Env -> TT Name -> TT Name -> [Name] -> [Name] ->
170unify :: Context -> Env -> TT Name -> TT Name -> [Name] -> [Name] ->
171 TC ([(Name, TT Name)], Fails)
171 TC ([(Name, TT Name)], Fails)
172−unify ctxt env topx topy dont holes =
172+unify ctxt env topx topy inj holes =
173-- trace ("Unifying " ++ show (topx, topy)) $
173-- trace ("Unifying " ++ show (topx, topy)) $
174 -- don't bother if topx and topy are different at the head
174 -- don't bother if topx and topy are different at the head
175 case runStateT (un False [] topx topy) (UI 0 []) of
175 case runStateT (un False [] topx topy) (UI 0 []) of
@@ -198,12 +198,20 @@ unify ctxt env topx topy dont holes =
198 injective (App f a) = injective f -- && injective a
198 injective (App f a) = injective f -- && injective a
199 injective _ = False
199 injective _ = False
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
206+ injectiveApp x = injective x || injectiveVar x
201 notP (P _ _ _) = False
208 notP (P _ _ _) = False
202 notP _ = True
209 notP _ = True
204 sc i = do UI s f <- get
211 sc i = do UI s f <- get
205 put (UI (s+i) f)
212 put (UI (s+i) f)
214+ errors :: StateT UInfo TC Bool
207 errors = do UI s f <- get
215 errors = do UI s f <- get
208 return (not (null f))
216 return (not (null f))
@@ -302,9 +310,18 @@ unify ctxt env topx topy dont holes =
302 -- class resolution for type classes over functions.
310 -- class resolution for type classes over functions.
304 un' fn bnames (App f x) (Bind n (Pi t) y)
312 un' fn bnames (App f x) (Bind n (Pi t) y)
305− | noOccurrence n y && x == y
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
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
306− = un' False bnames f (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
322+ uf <- un' False bnames (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
307− (Bind n (Pi t) (V 1)))
323+ (Bind n (Pi t) (V 1))) f
324+ combine bnames ux uf
309 un' fn bnames (Bind x bx sx) (Bind y by sy)
326 un' fn bnames (Bind x bx sx) (Bind y by sy)
310 = do h1 <- uB bnames bx by
327 = do h1 <- uB bnames bx by
@@ -321,11 +338,11 @@ unify ctxt env topx topy dont holes =
321 return [] -- lift $ tfail err
338 return [] -- lift $ tfail err
323 unApp fn bnames appx@(App fx ax) appy@(App fy ay)
340 unApp fn bnames appx@(App fx ax) appy@(App fy ay)
324− | (injective fx && injective fy)
341+ | (injectiveApp fx && injectiveApp fy)
325− || (injective fx && rigid appx && metavarApp appy)
342+ || (injectiveApp fx && rigid appx && metavarApp appy)
326− || (injective fy && rigid appy && metavarApp appx)
343+ || (injectiveApp fy && rigid appy && metavarApp appx)
327− || (injective fx && metavarApp fy && ax == ay)
344+ || (injectiveApp fx && metavarApp fy && ax == ay)
328− || (injective fy && metavarApp fx && ax == ay)
345+ || (injectiveApp fy && metavarApp fx && ax == ay)
329 = do let (headx, _) = unApply fx
346 = do let (headx, _) = unApply fx
330 let (heady, _) = unApply fy
347 let (heady, _) = unApply fy
331 -- fail quickly if the heads are disjoint
348 -- fail quickly if the heads are disjoint
@@ -347,23 +364,24 @@ unify ctxt env topx topy dont holes =
347 hf <- un' False bnames fx' fy'
364 hf <- un' False bnames fx' fy'
349 combine bnames hf ha)
366 combine bnames hf ha)
350− | otherwise = -- trace (show (appx, appy, injective fx, metavarApp appy, sameArgStruct appx appy)) $
367+ | otherwise = -- FIXME: This fudge should be:
368+ unifyTmpFail appx appy
351− do let (headx, argsx) = unApply appx
369+-- do let (headx, argsx) = unApply appx
352− let (heady, argsy) = unApply appy
370+-- let (heady, argsy) = unApply appy
353− -- traceWhen (headx == heady) (show (appx, appy)) $
371+-- -- traceWhen (headx == heady) (show (appx, appy)) $
354− uplus (
372+-- uplus (
355− if (length argsx == length argsy &&
373+-- if (length argsx == length argsy &&
356− ((headx == heady && inenv headx) || (argsx == argsy) ||
374+-- ((headx == heady && inenv headx) || (argsx == argsy) ||
357− (and (zipWith sameStruct (headx:argsx) (heady:argsy)))))
375+-- (and (zipWith sameStruct (headx:argsx) (heady:argsy)))))
359−-- (notFn headx && notFn heady))) then
377+-- -- (notFn headx && notFn heady))) then
360− do uf <- un' True bnames headx heady
378+-- do uf <- un' True bnames headx heady
361− failed <- errors
379+-- failed <- errors
362− if (not failed) then unArgs uf argsx argsy
380+-- if (not failed) then unArgs uf argsx argsy
363− else return []
381+-- else return []
364− else -- trace ("TMPFAIL " ++ show (appx, appy, injective appx, injective appy)) $
382+-- else -- trace ("TMPFAIL " ++ show (appx, appy, injective appx, injective appy)) $
365− unifyTmpFail appx appy)
383+-- unifyTmpFail appx appy)
366− (unifyTmpFail appx appy) -- whole application fails
384+-- (unifyTmpFail appx appy) -- whole application fails
367 where hnormalise [] _ _ t = t
385 where hnormalise [] _ _ t = t
368 hnormalise ns ctxt env t = normalise ctxt env t
386 hnormalise ns ctxt env t = normalise ctxt env t
369 checkHeads (P (DCon _ _) x _) (P (DCon _ _) y _)
387 checkHeads (P (DCon _ _) x _) (P (DCon _ _) y _)
@@ -433,8 +451,7 @@ unify ctxt env topx topy dont holes =
433 _ -> False
451 _ -> False
435 metavar t = case t of
453 metavar t = case t of
436− P _ x _ -> ((x `elem` holes || holeIn env x) &&
454+ P _ x _ -> (x `elem` holes || holeIn env x)
437− not (x `elem` dont))
438 || globmetavar t
455 || globmetavar t
439 _ -> False
456 _ -> False
440 pat t = case t of
457 pat t = case t of
@@ -447,6 +464,7 @@ unify ctxt env topx topy dont holes =
447 notFn t = injective t || metavar t || inenv t
464 notFn t = injective t || metavar t || inenv t
467+ unifyTmpFail :: Term -> Term -> StateT UInfo TC [(Name, TT Name)]
450 unifyTmpFail x y
468 unifyTmpFail x y
451 = do UI s f <- get
469 = do UI s f <- get
452 let r = recoverable x y
470 let r = recoverable x y