Add debugPprType
[ghc.git] / compiler / types / InstEnv.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5 \section[InstEnv]{Utilities for typechecking instance declarations}
6
7 The bits common to TcInstDcls and TcDeriv.
8 -}
9
10 {-# LANGUAGE CPP, DeriveDataTypeable #-}
11
12 module InstEnv (
13 DFunId, InstMatch, ClsInstLookupResult,
14 OverlapFlag(..), OverlapMode(..), setOverlapModeMaybe,
15 ClsInst(..), DFunInstType, pprInstance, pprInstanceHdr, pprInstances,
16 instanceHead, instanceSig, mkLocalInstance, mkImportedInstance,
17 instanceDFunId, tidyClsInstDFun, instanceRoughTcs,
18 fuzzyClsInstCmp, orphNamesOfClsInst,
19
20 InstEnvs(..), VisibleOrphanModules, InstEnv,
21 emptyInstEnv, extendInstEnv, deleteFromInstEnv, identicalClsInstHead,
22 extendInstEnvList, lookupUniqueInstEnv, lookupInstEnv, instEnvElts,
23 memberInstEnv, instIsVisible,
24 classInstances, instanceBindFun,
25 instanceCantMatch, roughMatchTcs,
26 isOverlappable, isOverlapping, isIncoherent
27 ) where
28
29 #include "HsVersions.h"
30
31 import TcType -- InstEnv is really part of the type checker,
32 -- and depends on TcType in many ways
33 import CoreSyn ( IsOrphan(..), isOrphan, chooseOrphanAnchor )
34 import Module
35 import Class
36 import Var
37 import VarSet
38 import Name
39 import NameSet
40 import Unify
41 import Outputable
42 import ErrUtils
43 import BasicTypes
44 import UniqDFM
45 import Util
46 import Id
47 import Data.Data ( Data )
48 import Data.Maybe ( isJust, isNothing )
49
50 {-
51 ************************************************************************
52 * *
53 ClsInst: the data type for type-class instances
54 * *
55 ************************************************************************
56 -}
57
58 -- | A type-class instance. Note that there is some tricky laziness at work
59 -- here. See Note [ClsInst laziness and the rough-match fields] for more
60 -- details.
61 data ClsInst
62 = ClsInst { -- Used for "rough matching"; see
63 -- Note [ClsInst laziness and the rough-match fields]
64 -- INVARIANT: is_tcs = roughMatchTcs is_tys
65 is_cls_nm :: Name -- ^ Class name
66 , is_tcs :: [Maybe Name] -- ^ Top of type args
67
68 -- | @is_dfun_name = idName . is_dfun@.
69 --
70 -- We use 'is_dfun_name' for the visibility check,
71 -- 'instIsVisible', which needs to know the 'Module' which the
72 -- dictionary is defined in. However, we cannot use the 'Module'
73 -- attached to 'is_dfun' since doing so would mean we would
74 -- potentially pull in an entire interface file unnecessarily.
75 -- This was the cause of #12367.
76 , is_dfun_name :: Name
77
78 -- Used for "proper matching"; see Note [Proper-match fields]
79 , is_tvs :: [TyVar] -- Fresh template tyvars for full match
80 -- See Note [Template tyvars are fresh]
81 , is_cls :: Class -- The real class
82 , is_tys :: [Type] -- Full arg types (mentioning is_tvs)
83 -- INVARIANT: is_dfun Id has type
84 -- forall is_tvs. (...) => is_cls is_tys
85 -- (modulo alpha conversion)
86
87 , is_dfun :: DFunId -- See Note [Haddock assumptions]
88
89 , is_flag :: OverlapFlag -- See detailed comments with
90 -- the decl of BasicTypes.OverlapFlag
91 , is_orphan :: IsOrphan
92 }
93 deriving Data
94
95 -- | A fuzzy comparison function for class instances, intended for sorting
96 -- instances before displaying them to the user.
97 fuzzyClsInstCmp :: ClsInst -> ClsInst -> Ordering
98 fuzzyClsInstCmp x y =
99 stableNameCmp (is_cls_nm x) (is_cls_nm y) `mappend`
100 mconcat (map cmp (zip (is_tcs x) (is_tcs y)))
101 where
102 cmp (Nothing, Nothing) = EQ
103 cmp (Nothing, Just _) = LT
104 cmp (Just _, Nothing) = GT
105 cmp (Just x, Just y) = stableNameCmp x y
106
107 isOverlappable, isOverlapping, isIncoherent :: ClsInst -> Bool
108 isOverlappable i = hasOverlappableFlag (overlapMode (is_flag i))
109 isOverlapping i = hasOverlappingFlag (overlapMode (is_flag i))
110 isIncoherent i = hasIncoherentFlag (overlapMode (is_flag i))
111
112 {-
113 Note [ClsInst laziness and the rough-match fields]
114 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115 Suppose we load 'instance A.C B.T' from A.hi, but suppose that the type B.T is
116 otherwise unused in the program. Then it's stupid to load B.hi, the data type
117 declaration for B.T -- and perhaps further instance declarations!
118
119 We avoid this as follows:
120
121 * is_cls_nm, is_tcs, is_dfun_name are all Names. We can poke them to our heart's
122 content.
123
124 * Proper-match fields. is_dfun, and its related fields is_tvs, is_cls, is_tys
125 contain TyVars, Class, Type, Class etc, and so are all lazy thunks. When we
126 poke any of these fields we'll typecheck the DFunId declaration, and hence
127 pull in interfaces that it refers to. See Note [Proper-match fields].
128
129 * Rough-match fields. During instance lookup, we use the is_cls_nm :: Name and
130 is_tcs :: [Maybe Name] fields to perform a "rough match", *without* poking
131 inside the DFunId. The rough-match fields allow us to say "definitely does not
132 match", based only on Names.
133
134 This laziness is very important; see Trac #12367. Try hard to avoid pulling on
135 the structured fields unless you really need the instance.
136
137 * Another place to watch is InstEnv.instIsVisible, which needs the module to
138 which the ClsInst belongs. We can get this from is_dfun_name.
139
140 * In is_tcs,
141 Nothing means that this type arg is a type variable
142
143 (Just n) means that this type arg is a
144 TyConApp with a type constructor of n.
145 This is always a real tycon, never a synonym!
146 (Two different synonyms might match, but two
147 different real tycons can't.)
148 NB: newtypes are not transparent, though!
149 -}
150
151 {-
152 Note [Template tyvars are fresh]
153 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154 The is_tvs field of a ClsInst has *completely fresh* tyvars.
155 That is, they are
156 * distinct from any other ClsInst
157 * distinct from any tyvars free in predicates that may
158 be looked up in the class instance environment
159 Reason for freshness: we use unification when checking for overlap
160 etc, and that requires the tyvars to be distinct.
161
162 The invariant is checked by the ASSERT in lookupInstEnv'.
163
164 Note [Proper-match fields]
165 ~~~~~~~~~~~~~~~~~~~~~~~~~
166 The is_tvs, is_cls, is_tys fields are simply cached values, pulled
167 out (lazily) from the dfun id. They are cached here simply so
168 that we don't need to decompose the DFunId each time we want
169 to match it. The hope is that the rough-match fields mean
170 that we often never poke the proper-match fields.
171
172 However, note that:
173 * is_tvs must be a superset of the free vars of is_tys
174
175 * is_tvs, is_tys may be alpha-renamed compared to the ones in
176 the dfun Id
177
178 Note [Haddock assumptions]
179 ~~~~~~~~~~~~~~~~~~~~~~~~~~
180 For normal user-written instances, Haddock relies on
181
182 * the SrcSpan of
183 * the Name of
184 * the is_dfun of
185 * an Instance
186
187 being equal to
188
189 * the SrcSpan of
190 * the instance head type of
191 * the InstDecl used to construct the Instance.
192 -}
193
194 instanceDFunId :: ClsInst -> DFunId
195 instanceDFunId = is_dfun
196
197 tidyClsInstDFun :: (DFunId -> DFunId) -> ClsInst -> ClsInst
198 tidyClsInstDFun tidy_dfun ispec
199 = ispec { is_dfun = tidy_dfun (is_dfun ispec) }
200
201 instanceRoughTcs :: ClsInst -> [Maybe Name]
202 instanceRoughTcs = is_tcs
203
204
205 instance NamedThing ClsInst where
206 getName ispec = getName (is_dfun ispec)
207
208 instance Outputable ClsInst where
209 ppr = pprInstance
210
211 pprInstance :: ClsInst -> SDoc
212 -- Prints the ClsInst as an instance declaration
213 pprInstance ispec
214 = hang (pprInstanceHdr ispec)
215 2 (vcat [ text "--" <+> pprDefinedAt (getName ispec)
216 , whenPprDebug (ppr (is_dfun ispec)) ])
217
218 -- * pprInstanceHdr is used in VStudio to populate the ClassView tree
219 pprInstanceHdr :: ClsInst -> SDoc
220 -- Prints the ClsInst as an instance declaration
221 pprInstanceHdr (ClsInst { is_flag = flag, is_dfun = dfun })
222 = text "instance" <+> ppr flag <+> pprSigmaType (idType dfun)
223
224 pprInstances :: [ClsInst] -> SDoc
225 pprInstances ispecs = vcat (map pprInstance ispecs)
226
227 instanceHead :: ClsInst -> ([TyVar], Class, [Type])
228 -- Returns the head, using the fresh tyavs from the ClsInst
229 instanceHead (ClsInst { is_tvs = tvs, is_tys = tys, is_dfun = dfun })
230 = (tvs, cls, tys)
231 where
232 (_, _, cls, _) = tcSplitDFunTy (idType dfun)
233
234 -- | Collects the names of concrete types and type constructors that make
235 -- up the head of a class instance. For instance, given `class Foo a b`:
236 --
237 -- `instance Foo (Either (Maybe Int) a) Bool` would yield
238 -- [Either, Maybe, Int, Bool]
239 --
240 -- Used in the implementation of ":info" in GHCi.
241 --
242 -- The 'tcSplitSigmaTy' is because of
243 -- instance Foo a => Baz T where ...
244 -- The decl is an orphan if Baz and T are both not locally defined,
245 -- even if Foo *is* locally defined
246 orphNamesOfClsInst :: ClsInst -> NameSet
247 orphNamesOfClsInst (ClsInst { is_cls_nm = cls_nm, is_tys = tys })
248 = orphNamesOfTypes tys `unionNameSet` unitNameSet cls_nm
249
250 instanceSig :: ClsInst -> ([TyVar], [Type], Class, [Type])
251 -- Decomposes the DFunId
252 instanceSig ispec = tcSplitDFunTy (idType (is_dfun ispec))
253
254 mkLocalInstance :: DFunId -> OverlapFlag
255 -> [TyVar] -> Class -> [Type]
256 -> ClsInst
257 -- Used for local instances, where we can safely pull on the DFunId.
258 -- Consider using newClsInst instead; this will also warn if
259 -- the instance is an orphan.
260 mkLocalInstance dfun oflag tvs cls tys
261 = ClsInst { is_flag = oflag, is_dfun = dfun
262 , is_tvs = tvs
263 , is_dfun_name = dfun_name
264 , is_cls = cls, is_cls_nm = cls_name
265 , is_tys = tys, is_tcs = roughMatchTcs tys
266 , is_orphan = orph
267 }
268 where
269 cls_name = className cls
270 dfun_name = idName dfun
271 this_mod = ASSERT( isExternalName dfun_name ) nameModule dfun_name
272 is_local name = nameIsLocalOrFrom this_mod name
273
274 -- Compute orphanhood. See Note [Orphans] in InstEnv
275 (cls_tvs, fds) = classTvsFds cls
276 arg_names = [filterNameSet is_local (orphNamesOfType ty) | ty <- tys]
277
278 -- See Note [When exactly is an instance decl an orphan?]
279 orph | is_local cls_name = NotOrphan (nameOccName cls_name)
280 | all notOrphan mb_ns = ASSERT( not (null mb_ns) ) head mb_ns
281 | otherwise = IsOrphan
282
283 notOrphan NotOrphan{} = True
284 notOrphan _ = False
285
286 mb_ns :: [IsOrphan] -- One for each fundep; a locally-defined name
287 -- that is not in the "determined" arguments
288 mb_ns | null fds = [choose_one arg_names]
289 | otherwise = map do_one fds
290 do_one (_ltvs, rtvs) = choose_one [ns | (tv,ns) <- cls_tvs `zip` arg_names
291 , not (tv `elem` rtvs)]
292
293 choose_one nss = chooseOrphanAnchor (unionNameSets nss)
294
295 mkImportedInstance :: Name -- ^ the name of the class
296 -> [Maybe Name] -- ^ the types which the class was applied to
297 -> Name -- ^ the 'Name' of the dictionary binding
298 -> DFunId -- ^ the 'Id' of the dictionary.
299 -> OverlapFlag -- ^ may this instance overlap?
300 -> IsOrphan -- ^ is this instance an orphan?
301 -> ClsInst
302 -- Used for imported instances, where we get the rough-match stuff
303 -- from the interface file
304 -- The bound tyvars of the dfun are guaranteed fresh, because
305 -- the dfun has been typechecked out of the same interface file
306 mkImportedInstance cls_nm mb_tcs dfun_name dfun oflag orphan
307 = ClsInst { is_flag = oflag, is_dfun = dfun
308 , is_tvs = tvs, is_tys = tys
309 , is_dfun_name = dfun_name
310 , is_cls_nm = cls_nm, is_cls = cls, is_tcs = mb_tcs
311 , is_orphan = orphan }
312 where
313 (tvs, _, cls, tys) = tcSplitDFunTy (idType dfun)
314
315 {-
316 Note [When exactly is an instance decl an orphan?]
317 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
318 (see MkIface.instanceToIfaceInst, which implements this)
319 Roughly speaking, an instance is an orphan if its head (after the =>)
320 mentions nothing defined in this module.
321
322 Functional dependencies complicate the situation though. Consider
323
324 module M where { class C a b | a -> b }
325
326 and suppose we are compiling module X:
327
328 module X where
329 import M
330 data T = ...
331 instance C Int T where ...
332
333 This instance is an orphan, because when compiling a third module Y we
334 might get a constraint (C Int v), and we'd want to improve v to T. So
335 we must make sure X's instances are loaded, even if we do not directly
336 use anything from X.
337
338 More precisely, an instance is an orphan iff
339
340 If there are no fundeps, then at least of the names in
341 the instance head is locally defined.
342
343 If there are fundeps, then for every fundep, at least one of the
344 names free in a *non-determined* part of the instance head is
345 defined in this module.
346
347 (Note that these conditions hold trivially if the class is locally
348 defined.)
349
350
351 ************************************************************************
352 * *
353 InstEnv, ClsInstEnv
354 * *
355 ************************************************************************
356
357 A @ClsInstEnv@ all the instances of that class. The @Id@ inside a
358 ClsInstEnv mapping is the dfun for that instance.
359
360 If class C maps to a list containing the item ([a,b], [t1,t2,t3], dfun), then
361
362 forall a b, C t1 t2 t3 can be constructed by dfun
363
364 or, to put it another way, we have
365
366 instance (...) => C t1 t2 t3, witnessed by dfun
367 -}
368
369 ---------------------------------------------------
370 {-
371 Note [InstEnv determinism]
372 ~~~~~~~~~~~~~~~~~~~~~~~~~~
373 We turn InstEnvs into a list in some places that don't directly affect
374 the ABI. That happens when we create output for `:info`.
375 Unfortunately that nondeterminism is nonlocal and it's hard to tell what it
376 affects without following a chain of functions. It's also easy to accidentally
377 make that nondeterminism affect the ABI. Furthermore the envs should be
378 relatively small, so it should be free to use deterministic maps here.
379 Testing with nofib and validate detected no difference between UniqFM and
380 UniqDFM. See also Note [Deterministic UniqFM]
381 -}
382
383 type InstEnv = UniqDFM ClsInstEnv -- Maps Class to instances for that class
384 -- See Note [InstEnv determinism]
385
386 -- | 'InstEnvs' represents the combination of the global type class instance
387 -- environment, the local type class instance environment, and the set of
388 -- transitively reachable orphan modules (according to what modules have been
389 -- directly imported) used to test orphan instance visibility.
390 data InstEnvs = InstEnvs {
391 ie_global :: InstEnv, -- External-package instances
392 ie_local :: InstEnv, -- Home-package instances
393 ie_visible :: VisibleOrphanModules -- Set of all orphan modules transitively
394 -- reachable from the module being compiled
395 -- See Note [Instance lookup and orphan instances]
396 }
397
398 -- | Set of visible orphan modules, according to what modules have been directly
399 -- imported. This is based off of the dep_orphs field, which records
400 -- transitively reachable orphan modules (modules that define orphan instances).
401 type VisibleOrphanModules = ModuleSet
402
403 newtype ClsInstEnv
404 = ClsIE [ClsInst] -- The instances for a particular class, in any order
405
406 instance Outputable ClsInstEnv where
407 ppr (ClsIE is) = pprInstances is
408
409 -- INVARIANTS:
410 -- * The is_tvs are distinct in each ClsInst
411 -- of a ClsInstEnv (so we can safely unify them)
412
413 -- Thus, the @ClassInstEnv@ for @Eq@ might contain the following entry:
414 -- [a] ===> dfun_Eq_List :: forall a. Eq a => Eq [a]
415 -- The "a" in the pattern must be one of the forall'd variables in
416 -- the dfun type.
417
418 emptyInstEnv :: InstEnv
419 emptyInstEnv = emptyUDFM
420
421 instEnvElts :: InstEnv -> [ClsInst]
422 instEnvElts ie = [elt | ClsIE elts <- eltsUDFM ie, elt <- elts]
423 -- See Note [InstEnv determinism]
424
425 -- | Test if an instance is visible, by checking that its origin module
426 -- is in 'VisibleOrphanModules'.
427 -- See Note [Instance lookup and orphan instances]
428 instIsVisible :: VisibleOrphanModules -> ClsInst -> Bool
429 instIsVisible vis_mods ispec
430 -- NB: Instances from the interactive package always are visible. We can't
431 -- add interactive modules to the set since we keep creating new ones
432 -- as a GHCi session progresses.
433 | isInteractiveModule mod = True
434 | IsOrphan <- is_orphan ispec = mod `elemModuleSet` vis_mods
435 | otherwise = True
436 where
437 mod = nameModule $ is_dfun_name ispec
438
439 classInstances :: InstEnvs -> Class -> [ClsInst]
440 classInstances (InstEnvs { ie_global = pkg_ie, ie_local = home_ie, ie_visible = vis_mods }) cls
441 = get home_ie ++ get pkg_ie
442 where
443 get env = case lookupUDFM env cls of
444 Just (ClsIE insts) -> filter (instIsVisible vis_mods) insts
445 Nothing -> []
446
447 -- | Checks for an exact match of ClsInst in the instance environment.
448 -- We use this when we do signature checking in TcRnDriver
449 memberInstEnv :: InstEnv -> ClsInst -> Bool
450 memberInstEnv inst_env ins_item@(ClsInst { is_cls_nm = cls_nm } ) =
451 maybe False (\(ClsIE items) -> any (identicalDFunType ins_item) items)
452 (lookupUDFM inst_env cls_nm)
453 where
454 identicalDFunType cls1 cls2 =
455 eqType (varType (is_dfun cls1)) (varType (is_dfun cls2))
456
457 extendInstEnvList :: InstEnv -> [ClsInst] -> InstEnv
458 extendInstEnvList inst_env ispecs = foldl extendInstEnv inst_env ispecs
459
460 extendInstEnv :: InstEnv -> ClsInst -> InstEnv
461 extendInstEnv inst_env ins_item@(ClsInst { is_cls_nm = cls_nm })
462 = addToUDFM_C add inst_env cls_nm (ClsIE [ins_item])
463 where
464 add (ClsIE cur_insts) _ = ClsIE (ins_item : cur_insts)
465
466 deleteFromInstEnv :: InstEnv -> ClsInst -> InstEnv
467 deleteFromInstEnv inst_env ins_item@(ClsInst { is_cls_nm = cls_nm })
468 = adjustUDFM adjust inst_env cls_nm
469 where
470 adjust (ClsIE items) = ClsIE (filterOut (identicalClsInstHead ins_item) items)
471
472 identicalClsInstHead :: ClsInst -> ClsInst -> Bool
473 -- ^ True when when the instance heads are the same
474 -- e.g. both are Eq [(a,b)]
475 -- Used for overriding in GHCi
476 -- Obviously should be insenstive to alpha-renaming
477 identicalClsInstHead (ClsInst { is_cls_nm = cls_nm1, is_tcs = rough1, is_tys = tys1 })
478 (ClsInst { is_cls_nm = cls_nm2, is_tcs = rough2, is_tys = tys2 })
479 = cls_nm1 == cls_nm2
480 && not (instanceCantMatch rough1 rough2) -- Fast check for no match, uses the "rough match" fields
481 && isJust (tcMatchTys tys1 tys2)
482 && isJust (tcMatchTys tys2 tys1)
483
484 {-
485 ************************************************************************
486 * *
487 Looking up an instance
488 * *
489 ************************************************************************
490
491 @lookupInstEnv@ looks up in a @InstEnv@, using a one-way match. Since
492 the env is kept ordered, the first match must be the only one. The
493 thing we are looking up can have an arbitrary "flexi" part.
494
495 Note [Instance lookup and orphan instances]
496 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
497 Suppose we are compiling a module M, and we have a zillion packages
498 loaded, and we are looking up an instance for C (T W). If we find a
499 match in module 'X' from package 'p', should be "in scope"; that is,
500
501 is p:X in the transitive closure of modules imported from M?
502
503 The difficulty is that the "zillion packages" might include ones loaded
504 through earlier invocations of the GHC API, or earlier module loads in GHCi.
505 They might not be in the dependencies of M itself; and if not, the instances
506 in them should not be visible. Trac #2182, #8427.
507
508 There are two cases:
509 * If the instance is *not an orphan*, then module X defines C, T, or W.
510 And in order for those types to be involved in typechecking M, it
511 must be that X is in the transitive closure of M's imports. So we
512 can use the instance.
513
514 * If the instance *is an orphan*, the above reasoning does not apply.
515 So we keep track of the set of orphan modules transitively below M;
516 this is the ie_visible field of InstEnvs, of type VisibleOrphanModules.
517
518 If module p:X is in this set, then we can use the instance, otherwise
519 we can't.
520
521 Note [Rules for instance lookup]
522 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
523 These functions implement the carefully-written rules in the user
524 manual section on "overlapping instances". At risk of duplication,
525 here are the rules. If the rules change, change this text and the
526 user manual simultaneously. The link may be this:
527 http://www.haskell.org/ghc/docs/latest/html/users_guide/glasgow_exts.html#instance-overlap
528
529 The willingness to be overlapped or incoherent is a property of the
530 instance declaration itself, controlled as follows:
531
532 * An instance is "incoherent"
533 if it has an INCOHERENT pragma, or
534 if it appears in a module compiled with -XIncoherentInstances.
535
536 * An instance is "overlappable"
537 if it has an OVERLAPPABLE or OVERLAPS pragma, or
538 if it appears in a module compiled with -XOverlappingInstances, or
539 if the instance is incoherent.
540
541 * An instance is "overlapping"
542 if it has an OVERLAPPING or OVERLAPS pragma, or
543 if it appears in a module compiled with -XOverlappingInstances, or
544 if the instance is incoherent.
545 compiled with -XOverlappingInstances.
546
547 Now suppose that, in some client module, we are searching for an instance
548 of the target constraint (C ty1 .. tyn). The search works like this.
549
550 * Find all instances I that match the target constraint; that is, the
551 target constraint is a substitution instance of I. These instance
552 declarations are the candidates.
553
554 * Find all non-candidate instances that unify with the target
555 constraint. Such non-candidates instances might match when the
556 target constraint is further instantiated. If all of them are
557 incoherent, proceed; if not, the search fails.
558
559 * Eliminate any candidate IX for which both of the following hold:
560 * There is another candidate IY that is strictly more specific;
561 that is, IY is a substitution instance of IX but not vice versa.
562
563 * Either IX is overlappable or IY is overlapping.
564
565 * If only one candidate remains, pick it. Otherwise if all remaining
566 candidates are incoherent, pick an arbitrary candidate. Otherwise fail.
567
568 Note [Overlapping instances] (NB: these notes are quite old)
569 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
570 Overlap is permitted, but only in such a way that one can make
571 a unique choice when looking up. That is, overlap is only permitted if
572 one template matches the other, or vice versa. So this is ok:
573
574 [a] [Int]
575
576 but this is not
577
578 (Int,a) (b,Int)
579
580 If overlap is permitted, the list is kept most specific first, so that
581 the first lookup is the right choice.
582
583
584 For now we just use association lists.
585
586 \subsection{Avoiding a problem with overlapping}
587
588 Consider this little program:
589
590 \begin{pseudocode}
591 class C a where c :: a
592 class C a => D a where d :: a
593
594 instance C Int where c = 17
595 instance D Int where d = 13
596
597 instance C a => C [a] where c = [c]
598 instance ({- C [a], -} D a) => D [a] where d = c
599
600 instance C [Int] where c = [37]
601
602 main = print (d :: [Int])
603 \end{pseudocode}
604
605 What do you think `main' prints (assuming we have overlapping instances, and
606 all that turned on)? Well, the instance for `D' at type `[a]' is defined to
607 be `c' at the same type, and we've got an instance of `C' at `[Int]', so the
608 answer is `[37]', right? (the generic `C [a]' instance shouldn't apply because
609 the `C [Int]' instance is more specific).
610
611 Ghc-4.04 gives `[37]', while ghc-4.06 gives `[17]', so 4.06 is wrong. That
612 was easy ;-) Let's just consult hugs for good measure. Wait - if I use old
613 hugs (pre-September99), I get `[17]', and stranger yet, if I use hugs98, it
614 doesn't even compile! What's going on!?
615
616 What hugs complains about is the `D [a]' instance decl.
617
618 \begin{pseudocode}
619 ERROR "mj.hs" (line 10): Cannot build superclass instance
620 *** Instance : D [a]
621 *** Context supplied : D a
622 *** Required superclass : C [a]
623 \end{pseudocode}
624
625 You might wonder what hugs is complaining about. It's saying that you
626 need to add `C [a]' to the context of the `D [a]' instance (as appears
627 in comments). But there's that `C [a]' instance decl one line above
628 that says that I can reduce the need for a `C [a]' instance to the
629 need for a `C a' instance, and in this case, I already have the
630 necessary `C a' instance (since we have `D a' explicitly in the
631 context, and `C' is a superclass of `D').
632
633 Unfortunately, the above reasoning indicates a premature commitment to the
634 generic `C [a]' instance. I.e., it prematurely rules out the more specific
635 instance `C [Int]'. This is the mistake that ghc-4.06 makes. The fix is to
636 add the context that hugs suggests (uncomment the `C [a]'), effectively
637 deferring the decision about which instance to use.
638
639 Now, interestingly enough, 4.04 has this same bug, but it's covered up
640 in this case by a little known `optimization' that was disabled in
641 4.06. Ghc-4.04 silently inserts any missing superclass context into
642 an instance declaration. In this case, it silently inserts the `C
643 [a]', and everything happens to work out.
644
645 (See `basicTypes/MkId:mkDictFunId' for the code in question. Search for
646 `Mark Jones', although Mark claims no credit for the `optimization' in
647 question, and would rather it stopped being called the `Mark Jones
648 optimization' ;-)
649
650 So, what's the fix? I think hugs has it right. Here's why. Let's try
651 something else out with ghc-4.04. Let's add the following line:
652
653 d' :: D a => [a]
654 d' = c
655
656 Everyone raise their hand who thinks that `d :: [Int]' should give a
657 different answer from `d' :: [Int]'. Well, in ghc-4.04, it does. The
658 `optimization' only applies to instance decls, not to regular
659 bindings, giving inconsistent behavior.
660
661 Old hugs had this same bug. Here's how we fixed it: like GHC, the
662 list of instances for a given class is ordered, so that more specific
663 instances come before more generic ones. For example, the instance
664 list for C might contain:
665 ..., C Int, ..., C a, ...
666 When we go to look for a `C Int' instance we'll get that one first.
667 But what if we go looking for a `C b' (`b' is unconstrained)? We'll
668 pass the `C Int' instance, and keep going. But if `b' is
669 unconstrained, then we don't know yet if the more specific instance
670 will eventually apply. GHC keeps going, and matches on the generic `C
671 a'. The fix is to, at each step, check to see if there's a reverse
672 match, and if so, abort the search. This prevents hugs from
673 prematurely chosing a generic instance when a more specific one
674 exists.
675
676 --Jeff
677 v
678 BUT NOTE [Nov 2001]: we must actually *unify* not reverse-match in
679 this test. Suppose the instance envt had
680 ..., forall a b. C a a b, ..., forall a b c. C a b c, ...
681 (still most specific first)
682 Now suppose we are looking for (C x y Int), where x and y are unconstrained.
683 C x y Int doesn't match the template {a,b} C a a b
684 but neither does
685 C a a b match the template {x,y} C x y Int
686 But still x and y might subsequently be unified so they *do* match.
687
688 Simple story: unify, don't match.
689 -}
690
691 type DFunInstType = Maybe Type
692 -- Just ty => Instantiate with this type
693 -- Nothing => Instantiate with any type of this tyvar's kind
694 -- See Note [DFunInstType: instantiating types]
695
696 type InstMatch = (ClsInst, [DFunInstType])
697
698 type ClsInstLookupResult
699 = ( [InstMatch] -- Successful matches
700 , [ClsInst] -- These don't match but do unify
701 , [InstMatch] ) -- Unsafe overlapped instances under Safe Haskell
702 -- (see Note [Safe Haskell Overlapping Instances] in
703 -- TcSimplify).
704
705 {-
706 Note [DFunInstType: instantiating types]
707 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
708 A successful match is a ClsInst, together with the types at which
709 the dfun_id in the ClsInst should be instantiated
710 The instantiating types are (Either TyVar Type)s because the dfun
711 might have some tyvars that *only* appear in arguments
712 dfun :: forall a b. C a b, Ord b => D [a]
713 When we match this against D [ty], we return the instantiating types
714 [Just ty, Nothing]
715 where the 'Nothing' indicates that 'b' can be freely instantiated.
716 (The caller instantiates it to a flexi type variable, which will
717 presumably later become fixed via functional dependencies.)
718 -}
719
720 -- |Look up an instance in the given instance environment. The given class application must match exactly
721 -- one instance and the match may not contain any flexi type variables. If the lookup is unsuccessful,
722 -- yield 'Left errorMessage'.
723 lookupUniqueInstEnv :: InstEnvs
724 -> Class -> [Type]
725 -> Either MsgDoc (ClsInst, [Type])
726 lookupUniqueInstEnv instEnv cls tys
727 = case lookupInstEnv False instEnv cls tys of
728 ([(inst, inst_tys)], _, _)
729 | noFlexiVar -> Right (inst, inst_tys')
730 | otherwise -> Left $ text "flexible type variable:" <+>
731 (ppr $ mkTyConApp (classTyCon cls) tys)
732 where
733 inst_tys' = [ty | Just ty <- inst_tys]
734 noFlexiVar = all isJust inst_tys
735 _other -> Left $ text "instance not found" <+>
736 (ppr $ mkTyConApp (classTyCon cls) tys)
737
738 lookupInstEnv' :: InstEnv -- InstEnv to look in
739 -> VisibleOrphanModules -- But filter against this
740 -> Class -> [Type] -- What we are looking for
741 -> ([InstMatch], -- Successful matches
742 [ClsInst]) -- These don't match but do unify
743 -- The second component of the result pair happens when we look up
744 -- Foo [a]
745 -- in an InstEnv that has entries for
746 -- Foo [Int]
747 -- Foo [b]
748 -- Then which we choose would depend on the way in which 'a'
749 -- is instantiated. So we report that Foo [b] is a match (mapping b->a)
750 -- but Foo [Int] is a unifier. This gives the caller a better chance of
751 -- giving a suitable error message
752
753 lookupInstEnv' ie vis_mods cls tys
754 = lookup ie
755 where
756 rough_tcs = roughMatchTcs tys
757 all_tvs = all isNothing rough_tcs
758
759 --------------
760 lookup env = case lookupUDFM env cls of
761 Nothing -> ([],[]) -- No instances for this class
762 Just (ClsIE insts) -> find [] [] insts
763
764 --------------
765 find ms us [] = (ms, us)
766 find ms us (item@(ClsInst { is_tcs = mb_tcs, is_tvs = tpl_tvs
767 , is_tys = tpl_tys }) : rest)
768 | not (instIsVisible vis_mods item)
769 = find ms us rest -- See Note [Instance lookup and orphan instances]
770
771 -- Fast check for no match, uses the "rough match" fields
772 | instanceCantMatch rough_tcs mb_tcs
773 = find ms us rest
774
775 | Just subst <- tcMatchTys tpl_tys tys
776 = find ((item, map (lookupTyVar subst) tpl_tvs) : ms) us rest
777
778 -- Does not match, so next check whether the things unify
779 -- See Note [Overlapping instances] and Note [Incoherent instances]
780 | isIncoherent item
781 = find ms us rest
782
783 | otherwise
784 = ASSERT2( tyCoVarsOfTypes tys `disjointVarSet` tpl_tv_set,
785 (ppr cls <+> ppr tys <+> ppr all_tvs) $$
786 (ppr tpl_tvs <+> ppr tpl_tys)
787 )
788 -- Unification will break badly if the variables overlap
789 -- They shouldn't because we allocate separate uniques for them
790 -- See Note [Template tyvars are fresh]
791 case tcUnifyTys instanceBindFun tpl_tys tys of
792 Just _ -> find ms (item:us) rest
793 Nothing -> find ms us rest
794 where
795 tpl_tv_set = mkVarSet tpl_tvs
796
797 ---------------
798 -- This is the common way to call this function.
799 lookupInstEnv :: Bool -- Check Safe Haskell overlap restrictions
800 -> InstEnvs -- External and home package inst-env
801 -> Class -> [Type] -- What we are looking for
802 -> ClsInstLookupResult
803 -- ^ See Note [Rules for instance lookup]
804 -- ^ See Note [Safe Haskell Overlapping Instances] in TcSimplify
805 -- ^ See Note [Safe Haskell Overlapping Instances Implementation] in TcSimplify
806 lookupInstEnv check_overlap_safe
807 (InstEnvs { ie_global = pkg_ie
808 , ie_local = home_ie
809 , ie_visible = vis_mods })
810 cls
811 tys
812 = -- pprTrace "lookupInstEnv" (ppr cls <+> ppr tys $$ ppr home_ie) $
813 (final_matches, final_unifs, unsafe_overlapped)
814 where
815 (home_matches, home_unifs) = lookupInstEnv' home_ie vis_mods cls tys
816 (pkg_matches, pkg_unifs) = lookupInstEnv' pkg_ie vis_mods cls tys
817 all_matches = home_matches ++ pkg_matches
818 all_unifs = home_unifs ++ pkg_unifs
819 final_matches = foldr insert_overlapping [] all_matches
820 -- Even if the unifs is non-empty (an error situation)
821 -- we still prune the matches, so that the error message isn't
822 -- misleading (complaining of multiple matches when some should be
823 -- overlapped away)
824
825 unsafe_overlapped
826 = case final_matches of
827 [match] -> check_safe match
828 _ -> []
829
830 -- If the selected match is incoherent, discard all unifiers
831 final_unifs = case final_matches of
832 (m:_) | isIncoherent (fst m) -> []
833 _ -> all_unifs
834
835 -- NOTE [Safe Haskell isSafeOverlap]
836 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
837 -- We restrict code compiled in 'Safe' mode from overriding code
838 -- compiled in any other mode. The rationale is that code compiled
839 -- in 'Safe' mode is code that is untrusted by the ghc user. So
840 -- we shouldn't let that code change the behaviour of code the
841 -- user didn't compile in 'Safe' mode since that's the code they
842 -- trust. So 'Safe' instances can only overlap instances from the
843 -- same module. A same instance origin policy for safe compiled
844 -- instances.
845 check_safe (inst,_)
846 = case check_overlap_safe && unsafeTopInstance inst of
847 -- make sure it only overlaps instances from the same module
848 True -> go [] all_matches
849 -- most specific is from a trusted location.
850 False -> []
851 where
852 go bad [] = bad
853 go bad (i@(x,_):unchecked) =
854 if inSameMod x || isOverlappable x
855 then go bad unchecked
856 else go (i:bad) unchecked
857
858 inSameMod b =
859 let na = getName $ getName inst
860 la = isInternalName na
861 nb = getName $ getName b
862 lb = isInternalName nb
863 in (la && lb) || (nameModule na == nameModule nb)
864
865 -- We consider the most specific instance unsafe when it both:
866 -- (1) Comes from a module compiled as `Safe`
867 -- (2) Is an orphan instance, OR, an instance for a MPTC
868 unsafeTopInstance inst = isSafeOverlap (is_flag inst) &&
869 (isOrphan (is_orphan inst) || classArity (is_cls inst) > 1)
870
871 ---------------
872 insert_overlapping :: InstMatch -> [InstMatch] -> [InstMatch]
873 -- ^ Add a new solution, knocking out strictly less specific ones
874 -- See Note [Rules for instance lookup]
875 insert_overlapping new_item [] = [new_item]
876 insert_overlapping new_item@(new_inst,_) (old_item@(old_inst,_) : old_items)
877 | new_beats_old -- New strictly overrides old
878 , not old_beats_new
879 , new_inst `can_override` old_inst
880 = insert_overlapping new_item old_items
881
882 | old_beats_new -- Old strictly overrides new
883 , not new_beats_old
884 , old_inst `can_override` new_inst
885 = old_item : old_items
886
887 -- Discard incoherent instances; see Note [Incoherent instances]
888 | isIncoherent old_inst -- Old is incoherent; discard it
889 = insert_overlapping new_item old_items
890 | isIncoherent new_inst -- New is incoherent; discard it
891 = old_item : old_items
892
893 -- Equal or incomparable, and neither is incoherent; keep both
894 | otherwise
895 = old_item : insert_overlapping new_item old_items
896 where
897
898 new_beats_old = new_inst `more_specific_than` old_inst
899 old_beats_new = old_inst `more_specific_than` new_inst
900
901 -- `instB` can be instantiated to match `instA`
902 -- or the two are equal
903 instA `more_specific_than` instB
904 = isJust (tcMatchTys (is_tys instB) (is_tys instA))
905
906 instA `can_override` instB
907 = isOverlapping instA || isOverlappable instB
908 -- Overlap permitted if either the more specific instance
909 -- is marked as overlapping, or the more general one is
910 -- marked as overlappable.
911 -- Latest change described in: Trac #9242.
912 -- Previous change: Trac #3877, Dec 10.
913
914 {-
915 Note [Incoherent instances]
916 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
917 For some classes, the choice of a particular instance does not matter, any one
918 is good. E.g. consider
919
920 class D a b where { opD :: a -> b -> String }
921 instance D Int b where ...
922 instance D a Int where ...
923
924 g (x::Int) = opD x x -- Wanted: D Int Int
925
926 For such classes this should work (without having to add an "instance D Int
927 Int", and using -XOverlappingInstances, which would then work). This is what
928 -XIncoherentInstances is for: Telling GHC "I don't care which instance you use;
929 if you can use one, use it."
930
931 Should this logic only work when *all* candidates have the incoherent flag, or
932 even when all but one have it? The right choice is the latter, which can be
933 justified by comparing the behaviour with how -XIncoherentInstances worked when
934 it was only about the unify-check (note [Overlapping instances]):
935
936 Example:
937 class C a b c where foo :: (a,b,c)
938 instance C [a] b Int
939 instance [incoherent] [Int] b c
940 instance [incoherent] C a Int c
941 Thanks to the incoherent flags,
942 [Wanted] C [a] b Int
943 works: Only instance one matches, the others just unify, but are marked
944 incoherent.
945
946 So I can write
947 (foo :: ([a],b,Int)) :: ([Int], Int, Int).
948 but if that works then I really want to be able to write
949 foo :: ([Int], Int, Int)
950 as well. Now all three instances from above match. None is more specific than
951 another, so none is ruled out by the normal overlapping rules. One of them is
952 not incoherent, but we still want this to compile. Hence the
953 "all-but-one-logic".
954
955 The implementation is in insert_overlapping, where we remove matching
956 incoherent instances as long as there are others.
957
958
959
960 ************************************************************************
961 * *
962 Binding decisions
963 * *
964 ************************************************************************
965 -}
966
967 instanceBindFun :: TyCoVar -> BindFlag
968 instanceBindFun tv | isOverlappableTyVar tv = Skolem
969 | otherwise = BindMe
970 -- Note [Binding when looking up instances]
971
972 {-
973 Note [Binding when looking up instances]
974 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
975 When looking up in the instance environment, or family-instance environment,
976 we are careful about multiple matches, as described above in
977 Note [Overlapping instances]
978
979 The key_tys can contain skolem constants, and we can guarantee that those
980 are never going to be instantiated to anything, so we should not involve
981 them in the unification test. Example:
982 class Foo a where { op :: a -> Int }
983 instance Foo a => Foo [a] -- NB overlap
984 instance Foo [Int] -- NB overlap
985 data T = forall a. Foo a => MkT a
986 f :: T -> Int
987 f (MkT x) = op [x,x]
988 The op [x,x] means we need (Foo [a]). Without the filterVarSet we'd
989 complain, saying that the choice of instance depended on the instantiation
990 of 'a'; but of course it isn't *going* to be instantiated.
991
992 We do this only for isOverlappableTyVar skolems. For example we reject
993 g :: forall a => [a] -> Int
994 g x = op x
995 on the grounds that the correct instance depends on the instantiation of 'a'
996 -}