#include "HsVersions.h"
import {-# SOURCE #-} TcUnify( unifyTauTy )
-import TcEnv -- temp
import HsSyn ( HsBind(..), HsExpr(..), LHsExpr, emptyLHsBinds )
-import TcHsSyn ( TcId, TcDictBinds, mkHsApp, mkHsTyApp, mkHsDictApp )
+import TcHsSyn ( mkHsApp, mkHsTyApp, mkHsDictApp )
import TcRnMonad
import Inst ( lookupInst, LookupInstResult(..),
tyVarsOfInst, fdPredsOfInsts, newDicts,
isDict, isClassDict, isLinearInst, linearInstType,
- isStdClassTyVarDict, isMethodFor, isMethod,
+ isMethodFor, isMethod,
instToId, tyVarsOfInsts, cloneDict,
ipNamesOfInsts, ipNamesOfInst, dictPred,
instBindingRequired, fdPredsOfInst,
newDictsAtLoc, tcInstClassOp,
getDictClassTys, isTyVarDict, instLoc,
zonkInst, tidyInsts, tidyMoreInsts,
- Inst, pprInsts, pprDictsInFull, pprInstInFull, tcGetInstEnvs,
+ pprInsts, pprDictsInFull, pprInstInFull, tcGetInstEnvs,
isInheritableInst, pprDictsTheta
)
-import TcEnv ( tcGetGlobalTyVars, tcLookupId, findGlobals, pprBinders )
+import TcEnv ( tcGetGlobalTyVars, tcLookupId, findGlobals, pprBinders,
+ lclEnvElts, tcMetaTy )
import InstEnv ( lookupInstEnv, classInstances, pprInstances )
import TcMType ( zonkTcTyVarsAndFV, tcInstTyVars, checkAmbiguity )
import TcType ( TcTyVar, TcTyVarSet, ThetaType, TcPredType,
mkClassPred, isOverloadedTy, mkTyConApp, isSkolemTyVar,
mkTyVarTy, tcGetTyVar, isTyVarClassPred, mkTyVarTys,
- tyVarsOfPred, tcEqType, pprPred, mkPredTy )
+ tyVarsOfPred, tcEqType, pprPred, mkPredTy, tcIsTyVarTy )
import TcIface ( checkWiredInTyCon )
import Id ( idType, mkUserLocal )
import Var ( TyVar )
import NameSet ( NameSet, mkNameSet, elemNameSet )
import Class ( classBigSig, classKey )
import FunDeps ( oclose, grow, improve, pprEquationDoc )
-import PrelInfo ( isNumericClass )
+import PrelInfo ( isNumericClass, isStandardClass )
import PrelNames ( splitName, fstName, sndName, integerTyConName,
showClassKey, eqClassKey, ordClassKey )
import Type ( zipTopTvSubst, substTheta, substTy )
data WhatToDo
= ReduceMe WantSCs -- Try to reduce this
-- If there's no instance, behave exactly like
- -- DontReduce: add the inst to
- -- the irreductible ones, but don't
- -- produce an error message of any kind.
+ -- DontReduce: add the inst to the irreductible ones,
+ -- but don't produce an error message of any kind.
-- It might be quite legitimate such as (Eq a)!
| KeepDictWithoutSCs -- Return as irreducible; don't add its superclasses
data WantSCs = NoSCs | AddSCs -- Tells whether we should add the superclasses
-- of a predicate when adding it to the avails
+ -- The reason for this flag is entirely the super-class loop problem
+ -- Note [SUPER-CLASS LOOP 1]
\end{code}
findAllDeps :: IdSet -> Avail -> IdSet
-- Find all the Insts that this one depends on
- -- See Note [SUPERCLASS-LOOP]
+ -- See Note [SUPERCLASS-LOOP 2]
-- Watch out, though. Since the avails may contain loops
-- (see Note [RECURSIVE DICTIONARIES]), so we need to track the ones we've seen so far
findAllDeps so_far (Rhs _ kids) = foldl find_all so_far kids
; (frees, binds, irreds) <- simpleReduceLoop doc try_me wanteds
; let
- -- All the non-std ones are definite errors
- (stds, non_stds) = partition isStdClassTyVarDict irreds
-
- -- Group by type variable
- std_groups = equivClasses cmp_by_tyvar stds
-
- -- Pick the ones which its worth trying to disambiguate
- -- namely, the onese whose type variable isn't bound
- -- up with one of the non-standard classes
- (std_oks, std_bads) = partition worth_a_try std_groups
- worth_a_try group@(d:_) = not (non_std_tyvars `intersectsVarSet` tyVarsOfInst d)
- non_std_tyvars = unionVarSets (map tyVarsOfInst non_stds)
+ -- First get rid of implicit parameters
+ (non_ips, bad_ips) = partition isClassDict irreds
+
+ -- All the non-tv or multi-param ones are definite errors
+ (unary_tv_dicts, non_tvs) = partition is_unary_tyvar_dict non_ips
+ bad_tyvars = unionVarSets (map tyVarsOfInst non_tvs)
+
+ -- Group by type variable
+ tv_groups = equivClasses cmp_by_tyvar unary_tv_dicts
+
+ -- Pick the ones which its worth trying to disambiguate
+ -- namely, the ones whose type variable isn't bound
+ -- up with one of the non-tyvar classes
+ (default_gps, non_default_gps) = partition defaultable_group tv_groups
+ defaultable_group ds
+ = not (bad_tyvars `intersectsVarSet` tyVarsOfInst (head ds))
+ && defaultable_classes (map get_clas ds)
+ defaultable_classes clss
+ | is_interactive = any isInteractiveClass clss
+ | otherwise = all isStandardClass clss && any isNumericClass clss
+
+ isInteractiveClass cls = isNumericClass cls
+ || (classKey cls `elem` [showClassKey, eqClassKey, ordClassKey])
+ -- In interactive mode, we default Show a to Show ()
+ -- to avoid graututious errors on "show []"
+
-- Collect together all the bad guys
- bad_guys = non_stds ++ concat std_bads
- (non_ips, bad_ips) = partition isClassDict bad_guys
- (ambigs, no_insts) = partition isTyVarDict non_ips
+ bad_guys = non_tvs ++ concat non_default_gps
+ (ambigs, no_insts) = partition isTyVarDict bad_guys
-- If the dict has no type constructors involved, it must be ambiguous,
-- except I suppose that another error with fundeps maybe should have
-- constrained those type variables
addTopAmbigErrs ambigs
-- Disambiguate the ones that look feasible
- ; mappM (disambigGroup is_interactive) std_oks }
+ ; mappM disambigGroup default_gps }
; return (binds `unionBags` unionManyBags binds_ambig) }
----------------------------------
d1 `cmp_by_tyvar` d2 = get_tv d1 `compare` get_tv d2
+is_unary_tyvar_dict :: Inst -> Bool -- Dicts of form (C a)
+ -- Invariant: argument is a ClassDict, not IP or method
+is_unary_tyvar_dict d = case getDictClassTys d of
+ (_, [ty]) -> tcIsTyVarTy ty
+ other -> False
+
get_tv d = case getDictClassTys d of
(clas, [ty]) -> tcGetTyVar "tcSimplify" ty
get_clas d = case getDictClassTys d of
- (clas, [ty]) -> clas
+ (clas, _) -> clas
\end{code}
If a dictionary constrains a type variable which is
@void@.
\begin{code}
-disambigGroup :: Bool -- True <=> simplifying at top-level interactive loop
- -> [Inst] -- All standard classes of form (C a)
+disambigGroup :: [Inst] -- All standard classes of form (C a)
-> TcM TcDictBinds
-disambigGroup is_interactive dicts
- | any std_default_class classes -- Guaranteed all standard classes
+disambigGroup dicts
= -- THE DICTS OBEY THE DEFAULTABLE CONSTRAINT
-- SO, TRY DEFAULT TYPES IN ORDER
case mb_ty of
Left _ -> bomb_out
Right chosen_default_ty -> choose_default chosen_default_ty
-
- | otherwise -- No defaults
- = bomb_out
-
where
tyvar = get_tv (head dicts) -- Should be non-empty
classes = map get_clas dicts
- std_default_class cls
- = isNumericClass cls
- || (is_interactive &&
- classKey cls `elem` [showClassKey, eqClassKey, ordClassKey])
- -- In interactive mode, we default Show a to Show ()
- -- to avoid graututious errors on "show []"
-
choose_default default_ty -- Commit to tyvar = default_ty
= -- Bind the type variable
unifyTauTy default_ty (mkTyVarTy tyvar) `thenM_`
addInstLoc :: [Inst] -> Message -> Message
addInstLoc insts msg = msg $$ nest 2 (pprInstLoc (instLoc (head insts)))
-plural [x] = empty
-plural xs = char 's'
-
addTopIPErrs :: [Name] -> [Inst] -> TcM ()
addTopIPErrs bndrs []
= return ()