2 module TcImprove ( tcImprove ) where
4 #include "HsVersions.h"
7 import Class ( Class, FunDep, className, classInstEnv, classExtraBigSig )
8 import Unify ( unifyTyListsX, matchTys )
9 import Subst ( mkSubst, substTy )
11 import TcType ( TcType, TcTyVar, TcTyVarSet, zonkTcType, zonkTcTypes )
12 import TcUnify ( unifyTauTyLists )
13 import Inst ( LIE, Inst, LookupInstResult(..),
14 lookupInst, getFunDepsOfLIE, getIPsOfLIE,
15 zonkLIE, zonkFunDeps {- for debugging -} )
16 import InstEnv ( InstEnv ) -- Reqd for 4.02; InstEnv is a synonym, and
17 -- 4.02 doesn't "see" it soon enough
18 import VarSet ( VarSet, emptyVarSet, unionVarSet )
19 import VarEnv ( emptyVarEnv )
20 import FunDeps ( instantiateFdClassTys )
22 import List ( elemIndex, nub )
26 tcImprove :: LIE -> TcM s ()
27 -- Do unifications based on functional dependencies in the LIE
29 | null nfdss = returnTc ()
30 | otherwise = iterImprove nfdss
32 nfdss, clas_nfdss, inst_nfdss, ip_nfdss :: [(TcTyVarSet, Name, [FunDep TcType])]
33 nfdss = ip_nfdss ++ clas_nfdss ++ inst_nfdss
35 cfdss :: [(Class, [FunDep TcType])]
36 cfdss = getFunDepsOfLIE lie
37 clas_nfdss = map (\(c, fds) -> (emptyVarSet, className c, fds)) cfdss
39 classes = nub (map fst cfdss)
40 inst_nfdss = concatMap getInstNfdssOf classes
43 ip_nfdss = map (\(n, ty) -> (emptyVarSet, n, [([], [ty])])) ips
46 class C a b c | a->b where ...
49 Given the LIE FD C (Int->t)
50 we get clas_nfdss = [({}, C, [Int->t, t->Int])
51 inst_nfdss = [({c}, C, [Int->Bool, Bool->Int])]
53 Another way would be to flatten a bit
54 we get clas_nfdss = [({}, C, Int->t), ({}, C, t->Int)]
55 inst_nfdss = [({c}, C, Int->Bool), ({c}, C, Bool->Int)]
57 iterImprove then matches up the C and Int, and unifies t <-> Bool
60 getInstNfdssOf :: Class -> [(TcTyVarSet, Name, [FunDep TcType])]
62 = [ (free, nm, instantiateFdClassTys clas ts)
63 | (free, ts, i) <- classInstEnv clas
68 iterImprove :: [(VarSet, Name, [FunDep TcType])] -> TcM s ()
69 iterImprove [] = returnTc ()
71 = selfImprove pairImprove cfdss `thenTc` \ change2 ->
72 if {- change1 || -} change2 then
77 -- ZZ this will do a lot of redundant checking wrt instances
78 -- it would do to make this operate over two lists, the first
79 -- with only clas_nfds and ip_nfds, and the second with everything
80 -- control would otherwise mimic the current loop, so that the
81 -- caller could control whether the redundant inst improvements
83 -- you could then also use this to check for consistency of new instances
85 -- selfImprove is really just doing a cartesian product of all the fds
86 selfImprove f [] = returnTc False
87 selfImprove f (nfds : nfdss)
88 = mapTc (f nfds) nfdss `thenTc` \ changes ->
89 selfImprove f nfdss `thenTc` \ rest_changed ->
90 returnTc (or changes || rest_changed)
92 pairImprove (free1, n1, fds1) (free2, n2, fds2)
94 checkFds (free1 `unionVarSet` free2) fds1 fds2
98 checkFds free [] [] = returnTc False
99 checkFds free (fd1 : fd1s) (fd2 : fd2s) =
100 checkFd free fd1 fd2 `thenTc` \ change ->
101 checkFds free fd1s fd2s `thenTc` \ changes ->
102 returnTc (change || changes)
103 --checkFds _ _ = returnTc False
105 checkFd free (t_x, t_y) (s_x, s_y)
106 -- we need to zonk each time because unification
107 -- may happen at any time
108 = zonkUnifyTys free t_x s_x `thenTc` \ msubst ->
111 let t_y' = map (substTy (mkSubst emptyVarEnv subst)) t_y
112 s_y' = map (substTy (mkSubst emptyVarEnv subst)) s_y
114 zonkEqTys t_y' s_y' `thenTc` \ eq ->
116 -- they're the same, nothing changes...
119 -- ZZ what happens if two instance vars unify?
120 unifyTauTyLists t_y' s_y' `thenTc_`
121 -- if we get here, something must have unified
127 = mapTc zonkTcType ts1 `thenTc` \ ts1' ->
128 mapTc zonkTcType ts2 `thenTc` \ ts2' ->
129 returnTc (ts1' == ts2')
131 zonkMatchTys ts1 free ts2
132 = mapTc zonkTcType ts1 `thenTc` \ ts1' ->
133 mapTc zonkTcType ts2 `thenTc` \ ts2' ->
134 -- pprTrace "zMT" (ppr (ts1', free, ts2')) $
135 case matchTys free ts2' ts1' of
136 Just (subst, []) -> -- pprTrace "zMT match!" empty $
137 returnTc (Just subst)
138 Nothing -> returnTc Nothing
140 zonkUnifyTys free ts1 ts2
141 = mapTc zonkTcType ts1 `thenTc` \ ts1' ->
142 mapTc zonkTcType ts2 `thenTc` \ ts2' ->
143 -- pprTrace "zMT" (ppr (ts1', free, ts2')) $
144 case unifyTyListsX free ts2' ts1' of
145 Just subst -> returnTc (Just subst)
146 Nothing -> returnTc Nothing