2 % (c) The GRASP/AQUA Project, Glasgow University, 2000
4 \section[FunDeps]{FunDeps - functional dependencies}
6 It's better to read it as: "if we know these, then we're going to know these"
11 instantiateFdClassTys,
16 #include "HsVersions.h"
19 import Class ( Class, FunDep, classTvsFds )
20 import Type ( Type, tyVarsOfTypes )
21 import Outputable ( Outputable, SDoc, interppSP, ptext, empty, hsep, punctuate, comma )
25 import Unique ( Uniquable )
26 import Util ( zipEqual )
31 oclose :: Uniquable a => [FunDep a] -> UniqSet a -> UniqSet a
32 -- (oclose fds tvs) closes the set of type variables tvs,
33 -- wrt the functional dependencies fds. The result is a superset
34 -- of the argument set.
37 -- oclose [a -> b] {a} = {a,b}
38 -- oclose [a b -> c] {a} = {a}
39 -- oclose [a b -> c] {a,b} = {a,b,c}
40 -- If all of the things on the left of an arrow are in the set, add
41 -- the things on the right of that arrow.
44 case oclose1 fds vs of
46 (vs', True) -> oclose fds vs'
48 oclose1 [] vs = (vs, False)
49 oclose1 (fd@(ls, rs):fds) vs =
55 vs'b1@(vs', b1) = oclose1 fds vs
56 (vs'', b2) = ounion rs vs'
59 osubset (u:us) vs = if u `elementOfUniqSet` vs then osubset us vs else False
61 ounion [] ys = (ys, False)
63 | x `elementOfUniqSet` ys = (ys', b)
64 | otherwise = (addOneToUniqSet ys' x, True)
66 (ys', b) = ounion xs ys
68 instantiateFdClassTys :: Class -> [Type] -> [FunDep Type]
69 -- Get the FDs of the class, and instantiate them
70 instantiateFdClassTys clas tys
71 = [(map lookup us, map lookup vs) | (us,vs) <- fundeps]
73 (tyvars, fundeps) = classTvsFds clas
74 env = mkVarEnv (zipEqual "instantiateFdClassTys" tyvars tys)
75 lookup tv = lookupVarEnv_NF env tv
77 tyVarFunDep :: [FunDep Type] -> [FunDep TyVar]
79 = [(getTyvars xs, getTyvars ys) | (xs, ys) <- fdtys]
81 getTyvars = varSetElems . tyVarsOfTypes
83 pprFundeps :: Outputable a => [FunDep a] -> SDoc
85 pprFundeps fds = hsep (ptext SLIT("|") : punctuate comma (map ppr_fd fds))
87 ppr_fd (us, vs) = hsep [interppSP us, ptext SLIT("->"), interppSP vs]