-\subsection{Checking for a decent instance type}
-%* *
-%************************************************************************
-
-@scrutiniseInstanceHead@ checks the type {\em and} its syntactic constraints:
-it must normally look like: @instance Foo (Tycon a b c ...) ...@
-
-The exceptions to this syntactic checking: (1)~if the @GlasgowExts@
-flag is on, or (2)~the instance is imported (they must have been
-compiled elsewhere). In these cases, we let them go through anyway.
-
-We can also have instances for functions: @instance Foo (a -> b) ...@.
-
-\begin{code}
-checkInstValidity dflags theta clas inst_tys
- | null errs = returnTc ()
- | otherwise = addErrsTc errs `thenNF_Tc_` failTc
- where
- errs = checkInstHead dflags theta clas inst_tys ++
- [err | pred <- theta, err <- checkInstConstraint dflags pred]
-
-checkInstConstraint dflags pred
- -- Checks whether a predicate is legal in the
- -- context of an instance declaration
- | ok = []
- | otherwise = [instConstraintErr pred]
- where
- ok = inheritablePred pred &&
- (isTyVarClassPred pred || arbitrary_preds_ok)
-
- arbitrary_preds_ok = dopt Opt_AllowUndecidableInstances dflags
-
-
-checkInstHead dflags theta clas inst_taus
- | -- CCALL CHECK
- -- A user declaration of a CCallable/CReturnable instance
- -- must be for a "boxed primitive" type.
- (clas `hasKey` cCallableClassKey
- && not (ccallable_type dflags first_inst_tau))
- ||
- (clas `hasKey` cReturnableClassKey
- && not (creturnable_type first_inst_tau))
- = [nonBoxedPrimCCallErr clas first_inst_tau]
-
- -- If GlasgowExts then check at least one isn't a type variable
- | dopt Opt_GlasgowExts dflags
- = -- GlasgowExts case
- check_tyvars dflags clas inst_taus ++ check_fundeps dflags theta clas inst_taus
-
- -- WITH HASKELL 1.4, MUST HAVE C (T a b c)
- | not (length inst_taus == 1 &&
- maybeToBool maybe_tycon_app && -- Yes, there's a type constuctor
- not (isSynTyCon tycon) && -- ...but not a synonym
- all isTyVarTy arg_tys && -- Applied to type variables
- length (varSetElems (tyVarsOfTypes arg_tys)) == length arg_tys
- -- This last condition checks that all the type variables are distinct
- )
- = [instTypeErr clas inst_taus
- (text "the instance type must be of form (T a b c)" $$
- text "where T is not a synonym, and a,b,c are distinct type variables")]
-
- | otherwise
- = []
-
- where
- (first_inst_tau : _) = inst_taus
-
- -- Stuff for algebraic or -> type
- maybe_tycon_app = splitTyConApp_maybe first_inst_tau
- Just (tycon, arg_tys) = maybe_tycon_app
-
- ccallable_type dflags ty = isFFIArgumentTy dflags False {- Not safe call -} ty
- creturnable_type ty = isFFIImportResultTy dflags ty
-
-check_tyvars dflags clas inst_taus
- -- Check that at least one isn't a type variable
- -- unless -fallow-undecideable-instances
- | dopt Opt_AllowUndecidableInstances dflags = []
- | not (all isTyVarTy inst_taus) = []
- | otherwise = [the_err]
- where
- the_err = instTypeErr clas inst_taus msg
- msg = ptext SLIT("There must be at least one non-type-variable in the instance head")
- $$ ptext SLIT("Use -fallow-undecidable-instances to lift this restriction")
-
-check_fundeps dflags theta clas inst_taus
- | checkInstFDs theta clas inst_taus = []
- | otherwise = [the_err]
- where
- the_err = instTypeErr clas inst_taus msg
- msg = ptext SLIT("the instance types do not agree with the functional dependencies of the class")
-\end{code}
-
-
-%************************************************************************
-%* *