2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section[TcType]{Types used in the typechecker}
7 This module provides the Type interface for front-end parts of the
10 * treat "source types" as opaque:
11 newtypes, and predicates are meaningful.
12 * look through usage types
14 The "tc" prefix is for "TypeChecker", because the type checker
15 is the principal client.
19 --------------------------------
21 TcType, TcSigmaType, TcRhoType, TcTauType, TcPredType, TcThetaType,
22 TcTyVar, TcTyVarSet, TcKind,
24 BoxyTyVar, BoxySigmaType, BoxyRhoType, BoxyThetaType, BoxyType,
26 --------------------------------
28 UserTypeCtxt(..), pprUserTypeCtxt,
29 TcTyVarDetails(..), BoxInfo(..), pprTcTyVarDetails,
30 MetaDetails(Flexi, Indirect), SkolemInfo(..), pprSkolTvBinding, pprSkolInfo,
31 isImmutableTyVar, isSkolemTyVar, isMetaTyVar, isBoxyTyVar,
32 isSigTyVar, isExistentialTyVar, isTyConableTyVar,
34 isFlexi, isIndirect, isRuntimeUnk, isUnk,
36 --------------------------------
40 --------------------------------
42 -- These are important because they do not look through newtypes
44 tcSplitForAllTys, tcSplitPhiTy, tcSplitPredFunTy_maybe,
45 tcSplitFunTy_maybe, tcSplitFunTys, tcFunArgTy, tcFunResultTy, tcSplitFunTysN,
46 tcSplitTyConApp, tcSplitTyConApp_maybe, tcTyConAppTyCon, tcTyConAppArgs,
47 tcSplitAppTy_maybe, tcSplitAppTy, tcSplitAppTys, repSplitAppTy_maybe,
48 tcInstHeadTyNotSynonym, tcInstHeadTyAppAllTyVars,
49 tcGetTyVar_maybe, tcGetTyVar,
50 tcSplitSigmaTy, tcMultiSplitSigmaTy,
52 ---------------------------------
54 -- Again, newtypes are opaque
55 tcEqType, tcEqTypes, tcEqPred, tcCmpType, tcCmpTypes, tcCmpPred, tcEqTypeX,
57 isSigmaTy, isOverloadedTy, isRigidTy, isBoxyTy,
58 isDoubleTy, isFloatTy, isIntTy, isWordTy, isStringTy,
59 isIntegerTy, isBoolTy, isUnitTy, isCharTy,
60 isTauTy, isTauTyCon, tcIsTyVarTy, tcIsForAllTy,
63 ---------------------------------
64 -- Misc type manipulators
66 tyClsNamesOfType, tyClsNamesOfDFunHead,
69 ---------------------------------
71 getClassPredTys_maybe, getClassPredTys,
72 isClassPred, isTyVarClassPred, isEqPred,
73 mkDictTy, tcSplitPredTy_maybe,
74 isPredTy, isDictTy, isDictLikeTy,
75 tcSplitDFunTy, tcSplitDFunHead, predTyUnique,
76 mkClassPred, isInheritablePred, isIPPred,
77 isRefineableTy, isRefineablePred,
79 ---------------------------------
80 -- Foreign import and export
81 isFFIArgumentTy, -- :: DynFlags -> Safety -> Type -> Bool
82 isFFIImportResultTy, -- :: DynFlags -> Type -> Bool
83 isFFIExportResultTy, -- :: Type -> Bool
84 isFFIExternalTy, -- :: Type -> Bool
85 isFFIDynArgumentTy, -- :: Type -> Bool
86 isFFIDynResultTy, -- :: Type -> Bool
87 isFFILabelTy, -- :: Type -> Bool
88 isFFIDotnetTy, -- :: DynFlags -> Type -> Bool
89 isFFIDotnetObjTy, -- :: Type -> Bool
90 isFFITy, -- :: Type -> Bool
91 isFunPtrTy, -- :: Type -> Bool
92 tcSplitIOType_maybe, -- :: Type -> Maybe Type
93 toDNType, -- :: Type -> DNType
95 --------------------------------
96 -- Rexported from Type
97 Kind, -- Stuff to do with kinds is insensitive to pre/post Tc
98 unliftedTypeKind, liftedTypeKind, argTypeKind,
99 openTypeKind, mkArrowKind, mkArrowKinds,
100 isLiftedTypeKind, isUnliftedTypeKind, isSubOpenTypeKind,
101 isSubArgTypeKind, isSubKind, splitKindFunTys, defaultKind,
102 kindVarRef, mkKindVar,
104 Type, PredType(..), ThetaType,
105 mkForAllTy, mkForAllTys,
106 mkFunTy, mkFunTys, zipFunTys,
107 mkTyConApp, mkAppTy, mkAppTys, applyTy, applyTys,
108 mkTyVarTy, mkTyVarTys, mkTyConTy, mkPredTy, mkPredTys,
110 -- Type substitutions
111 TvSubst(..), -- Representation visible to a few friends
112 TvSubstEnv, emptyTvSubst, substEqSpec,
113 mkOpenTvSubst, zipOpenTvSubst, zipTopTvSubst, mkTopTvSubst, notElemTvSubst,
114 getTvSubstEnv, setTvSubstEnv, getTvInScope, extendTvInScope, lookupTyVar,
115 extendTvSubst, extendTvSubstList, isInScope, mkTvSubst, zipTyEnv,
116 substTy, substTys, substTyWith, substTheta, substTyVar, substTyVars, substTyVarBndr,
118 isUnLiftedType, -- Source types are always lifted
119 isUnboxedTupleType, -- Ditto
122 tidyTopType, tidyType, tidyPred, tidyTypes, tidyFreeTyVars, tidyOpenType, tidyOpenTypes,
123 tidyTyVarBndr, tidyOpenTyVar, tidyOpenTyVars, tidySkolemTyVar,
126 tyVarsOfType, tyVarsOfTypes, tyVarsOfPred, tyVarsOfTheta,
127 tcTyVarsOfType, tcTyVarsOfTypes, exactTyVarsOfType, exactTyVarsOfTypes,
129 pprKind, pprParendKind,
130 pprType, pprParendType, pprTypeApp, pprTyThingCategory,
131 pprPred, pprTheta, pprThetaArrow, pprClassPred
135 #include "HsVersions.h"
167 %************************************************************************
171 %************************************************************************
173 The type checker divides the generic Type world into the
174 following more structured beasts:
176 sigma ::= forall tyvars. phi
177 -- A sigma type is a qualified type
179 -- Note that even if 'tyvars' is empty, theta
180 -- may not be: e.g. (?x::Int) => Int
182 -- Note that 'sigma' is in prenex form:
183 -- all the foralls are at the front.
184 -- A 'phi' type has no foralls to the right of
192 -- A 'tau' type has no quantification anywhere
193 -- Note that the args of a type constructor must be taus
195 | tycon tau_1 .. tau_n
199 -- In all cases, a (saturated) type synonym application is legal,
200 -- provided it expands to the required form.
203 type TcTyVar = TyVar -- Used only during type inference
204 type TcType = Type -- A TcType can have mutable type variables
205 -- Invariant on ForAllTy in TcTypes:
207 -- a cannot occur inside a MutTyVar in T; that is,
208 -- T is "flattened" before quantifying over a
210 -- These types do not have boxy type variables in them
211 type TcPredType = PredType
212 type TcThetaType = ThetaType
213 type TcSigmaType = TcType
214 type TcRhoType = TcType
215 type TcTauType = TcType
217 type TcTyVarSet = TyVarSet
219 -- These types may have boxy type variables in them
220 type BoxyTyVar = TcTyVar
221 type BoxyRhoType = TcType
222 type BoxyThetaType = TcThetaType
223 type BoxySigmaType = TcType
224 type BoxyType = TcType
228 %************************************************************************
230 \subsection{TyVarDetails}
232 %************************************************************************
234 TyVarDetails gives extra info about type variables, used during type
235 checking. It's attached to mutable type variables only.
236 It's knot-tied back to Var.lhs. There is no reason in principle
237 why Var.lhs shouldn't actually have the definition, but it "belongs" here.
240 Note [Signature skolems]
241 ~~~~~~~~~~~~~~~~~~~~~~~~
246 (x,y,z) = ([y,z], z, head x)
248 Here, x and y have type sigs, which go into the environment. We used to
249 instantiate their types with skolem constants, and push those types into
250 the RHS, so we'd typecheck the RHS with type
252 where a*, b* are skolem constants, and c is an ordinary meta type varible.
254 The trouble is that the occurrences of z in the RHS force a* and b* to
255 be the *same*, so we can't make them into skolem constants that don't unify
256 with each other. Alas.
258 One solution would be insist that in the above defn the programmer uses
259 the same type variable in both type signatures. But that takes explanation.
261 The alternative (currently implemented) is to have a special kind of skolem
262 constant, SigTv, which can unify with other SigTvs. These are *not* treated
263 as righd for the purposes of GADTs. And they are used *only* for pattern
264 bindings and mutually recursive function bindings. See the function
265 TcBinds.tcInstSig, and its use_skols parameter.
269 -- A TyVarDetails is inside a TyVar
271 = SkolemTv SkolemInfo -- A skolem constant
273 | MetaTv BoxInfo (IORef MetaDetails)
276 = BoxTv -- The contents is a (non-boxy) sigma-type
277 -- That is, this MetaTv is a "box"
279 | TauTv -- The contents is a (non-boxy) tau-type
280 -- That is, this MetaTv is an ordinary unification variable
282 | SigTv SkolemInfo -- A variant of TauTv, except that it should not be
283 -- unified with a type, only with a type variable
284 -- SigTvs are only distinguished to improve error messages
285 -- see Note [Signature skolems]
286 -- The MetaDetails, if filled in, will
287 -- always be another SigTv or a SkolemTv
290 -- A TauTv is always filled in with a tau-type, which
291 -- never contains any BoxTvs, nor any ForAlls
293 -- However, a BoxTv can contain a type that contains further BoxTvs
294 -- Notably, when typechecking an explicit list, say [e1,e2], with
295 -- expected type being a box b1, we fill in b1 with (List b2), where
296 -- b2 is another (currently empty) box.
299 = Flexi -- Flexi type variables unify to become
302 | Indirect TcType -- INVARIANT:
303 -- For a BoxTv, this type must be non-boxy
304 -- For a TauTv, this type must be a tau-type
306 -- Generally speaking, SkolemInfo should not contain location info
307 -- that is contained in the Name of the tyvar with this SkolemInfo
309 = SigSkol UserTypeCtxt -- A skolem that is created by instantiating
310 -- a programmer-supplied type signature
311 -- Location of the binding site is on the TyVar
313 -- The rest are for non-scoped skolems
314 | ClsSkol Class -- Bound at a class decl
315 | InstSkol -- Bound at an instance decl
316 | FamInstSkol -- Bound at a family instance decl
317 | PatSkol DataCon -- An existential type variable bound by a pattern for
318 -- a data constructor with an existential type. E.g.
319 -- data T = forall a. Eq a => MkT a
321 -- The pattern MkT x will allocate an existential type
323 | ArrowSkol -- An arrow form (see TcArrows)
325 | RuleSkol RuleName -- The LHS of a RULE
326 | GenSkol [TcTyVar] -- Bound when doing a subsumption check for
327 TcType -- (forall tvs. ty)
329 | RuntimeUnkSkol -- a type variable used to represent an unknown
330 -- runtime type (used in the GHCi debugger)
332 | UnkSkol -- Unhelpful info (until I improve it)
334 -------------------------------------
335 -- UserTypeCtxt describes the places where a
336 -- programmer-written type signature can occur
337 -- Like SkolemInfo, no location info
339 = FunSigCtxt Name -- Function type signature
340 -- Also used for types in SPECIALISE pragmas
341 | ExprSigCtxt -- Expression type signature
342 | ConArgCtxt Name -- Data constructor argument
343 | TySynCtxt Name -- RHS of a type synonym decl
344 | GenPatCtxt -- Pattern in generic decl
345 -- f{| a+b |} (Inl x) = ...
346 | LamPatSigCtxt -- Type sig in lambda pattern
348 | BindPatSigCtxt -- Type sig in pattern binding pattern
350 | ResSigCtxt -- Result type sig
352 | ForSigCtxt Name -- Foreign inport or export signature
353 | DefaultDeclCtxt -- Types in a default declaration
354 | SpecInstCtxt -- SPECIALISE instance pragma
356 -- Notes re TySynCtxt
357 -- We allow type synonyms that aren't types; e.g. type List = []
359 -- If the RHS mentions tyvars that aren't in scope, we'll
360 -- quantify over them:
361 -- e.g. type T = a->a
362 -- will become type T = forall a. a->a
364 -- With gla-exts that's right, but for H98 we should complain.
366 ---------------------------------
369 mkKindName :: Unique -> Name
370 mkKindName unique = mkSystemName unique kind_var_occ
372 kindVarRef :: KindVar -> IORef MetaDetails
374 ASSERT ( isTcTyVar tc )
375 case tcTyVarDetails tc of
376 MetaTv TauTv ref -> ref
377 _ -> pprPanic "kindVarRef" (ppr tc)
379 mkKindVar :: Unique -> IORef MetaDetails -> KindVar
381 = mkTcTyVar (mkKindName u)
382 tySuperKind -- not sure this is right,
383 -- do we need kind vars for
387 kind_var_occ :: OccName -- Just one for all KindVars
388 -- They may be jiggled by tidying
389 kind_var_occ = mkOccName tvName "k"
392 %************************************************************************
396 %************************************************************************
399 pprTcTyVarDetails :: TcTyVarDetails -> SDoc
401 pprTcTyVarDetails (SkolemTv _) = ptext (sLit "sk")
402 pprTcTyVarDetails (MetaTv BoxTv _) = ptext (sLit "box")
403 pprTcTyVarDetails (MetaTv TauTv _) = ptext (sLit "tau")
404 pprTcTyVarDetails (MetaTv (SigTv _) _) = ptext (sLit "sig")
406 pprUserTypeCtxt :: UserTypeCtxt -> SDoc
407 pprUserTypeCtxt (FunSigCtxt n) = ptext (sLit "the type signature for") <+> quotes (ppr n)
408 pprUserTypeCtxt ExprSigCtxt = ptext (sLit "an expression type signature")
409 pprUserTypeCtxt (ConArgCtxt c) = ptext (sLit "the type of the constructor") <+> quotes (ppr c)
410 pprUserTypeCtxt (TySynCtxt c) = ptext (sLit "the RHS of the type synonym") <+> quotes (ppr c)
411 pprUserTypeCtxt GenPatCtxt = ptext (sLit "the type pattern of a generic definition")
412 pprUserTypeCtxt LamPatSigCtxt = ptext (sLit "a pattern type signature")
413 pprUserTypeCtxt BindPatSigCtxt = ptext (sLit "a pattern type signature")
414 pprUserTypeCtxt ResSigCtxt = ptext (sLit "a result type signature")
415 pprUserTypeCtxt (ForSigCtxt n) = ptext (sLit "the foreign declaration for") <+> quotes (ppr n)
416 pprUserTypeCtxt DefaultDeclCtxt = ptext (sLit "a type in a `default' declaration")
417 pprUserTypeCtxt SpecInstCtxt = ptext (sLit "a SPECIALISE instance pragma")
420 --------------------------------
421 tidySkolemTyVar :: TidyEnv -> TcTyVar -> (TidyEnv, TcTyVar)
422 -- Tidy the type inside a GenSkol, preparatory to printing it
423 tidySkolemTyVar env tv
424 = ASSERT( isTcTyVar tv && (isSkolemTyVar tv || isSigTyVar tv ) )
425 (env1, mkTcTyVar (tyVarName tv) (tyVarKind tv) info1)
427 (env1, info1) = case tcTyVarDetails tv of
428 SkolemTv info -> (env1, SkolemTv info')
430 (env1, info') = tidy_skol_info env info
431 MetaTv (SigTv info) box -> (env1, MetaTv (SigTv info') box)
433 (env1, info') = tidy_skol_info env info
436 tidy_skol_info env (GenSkol tvs ty) = (env2, GenSkol tvs1 ty1)
438 (env1, tvs1) = tidyOpenTyVars env tvs
439 (env2, ty1) = tidyOpenType env1 ty
440 tidy_skol_info env info = (env, info)
442 pprSkolTvBinding :: TcTyVar -> SDoc
443 -- Print info about the binding of a skolem tyvar,
444 -- or nothing if we don't have anything useful to say
446 = ASSERT ( isTcTyVar tv )
447 quotes (ppr tv) <+> ppr_details (tcTyVarDetails tv)
449 ppr_details (MetaTv TauTv _) = ptext (sLit "is a meta type variable")
450 ppr_details (MetaTv BoxTv _) = ptext (sLit "is a boxy type variable")
451 ppr_details (MetaTv (SigTv info) _) = ppr_skol info
452 ppr_details (SkolemTv info) = ppr_skol info
454 ppr_skol UnkSkol = ptext (sLit "is an unknown type variable") -- Unhelpful
455 ppr_skol RuntimeUnkSkol = ptext (sLit "is an unknown runtime type")
456 ppr_skol info = sep [ptext (sLit "is a rigid type variable bound by"),
457 sep [pprSkolInfo info,
458 nest 2 (ptext (sLit "at") <+> ppr (getSrcLoc tv))]]
460 pprSkolInfo :: SkolemInfo -> SDoc
461 pprSkolInfo (SigSkol ctxt) = pprUserTypeCtxt ctxt
462 pprSkolInfo (ClsSkol cls) = ptext (sLit "the class declaration for") <+> quotes (ppr cls)
463 pprSkolInfo InstSkol = ptext (sLit "the instance declaration")
464 pprSkolInfo FamInstSkol = ptext (sLit "the family instance declaration")
465 pprSkolInfo (RuleSkol name) = ptext (sLit "the RULE") <+> doubleQuotes (ftext name)
466 pprSkolInfo ArrowSkol = ptext (sLit "the arrow form")
467 pprSkolInfo (PatSkol dc) = sep [ptext (sLit "the constructor") <+> quotes (ppr dc)]
468 pprSkolInfo (GenSkol tvs ty) = sep [ptext (sLit "the polymorphic type"),
469 nest 2 (quotes (ppr (mkForAllTys tvs ty)))]
472 -- For type variables the others are dealt with by pprSkolTvBinding.
473 -- For Insts, these cases should not happen
474 pprSkolInfo UnkSkol = panic "UnkSkol"
475 pprSkolInfo RuntimeUnkSkol = panic "RuntimeUnkSkol"
477 instance Outputable MetaDetails where
478 ppr Flexi = ptext (sLit "Flexi")
479 ppr (Indirect ty) = ptext (sLit "Indirect") <+> ppr ty
483 %************************************************************************
487 %************************************************************************
490 isImmutableTyVar :: TyVar -> Bool
493 | isTcTyVar tv = isSkolemTyVar tv
496 isTyConableTyVar, isSkolemTyVar, isExistentialTyVar,
497 isBoxyTyVar, isMetaTyVar :: TcTyVar -> Bool
500 -- True of a meta-type variable that can be filled in
501 -- with a type constructor application; in particular,
503 = ASSERT( isTcTyVar tv)
504 case tcTyVarDetails tv of
505 MetaTv BoxTv _ -> True
506 MetaTv TauTv _ -> True
507 MetaTv (SigTv {}) _ -> False
511 = ASSERT2( isTcTyVar tv, ppr tv )
512 case tcTyVarDetails tv of
516 isExistentialTyVar tv -- Existential type variable, bound by a pattern
517 = ASSERT( isTcTyVar tv )
518 case tcTyVarDetails tv of
519 SkolemTv (PatSkol {}) -> True
523 = ASSERT2( isTcTyVar tv, ppr tv )
524 case tcTyVarDetails tv of
529 = ASSERT( isTcTyVar tv )
530 case tcTyVarDetails tv of
531 MetaTv BoxTv _ -> True
534 isSigTyVar :: Var -> Bool
536 = ASSERT( isTcTyVar tv )
537 case tcTyVarDetails tv of
538 MetaTv (SigTv _) _ -> True
541 metaTvRef :: TyVar -> IORef MetaDetails
543 = ASSERT2( isTcTyVar tv, ppr tv )
544 case tcTyVarDetails tv of
546 _ -> pprPanic "metaTvRef" (ppr tv)
548 isFlexi, isIndirect :: MetaDetails -> Bool
552 isIndirect (Indirect _) = True
555 isRuntimeUnk :: TyVar -> Bool
556 isRuntimeUnk x | isTcTyVar x
557 , SkolemTv RuntimeUnkSkol <- tcTyVarDetails x = True
560 isUnk :: TyVar -> Bool
561 isUnk x | isTcTyVar x
562 , SkolemTv UnkSkol <- tcTyVarDetails x = True
567 %************************************************************************
569 \subsection{Tau, sigma and rho}
571 %************************************************************************
574 mkSigmaTy :: [TyVar] -> [PredType] -> Type -> Type
575 mkSigmaTy tyvars theta tau = mkForAllTys tyvars (mkPhiTy theta tau)
577 mkPhiTy :: [PredType] -> Type -> Type
578 mkPhiTy theta ty = foldr (\p r -> mkFunTy (mkPredTy p) r) ty theta
581 @isTauTy@ tests for nested for-alls. It should not be called on a boxy type.
584 isTauTy :: Type -> Bool
585 isTauTy ty | Just ty' <- tcView ty = isTauTy ty'
586 isTauTy (TyVarTy tv) = ASSERT( not (isTcTyVar tv && isBoxyTyVar tv) )
588 isTauTy (TyConApp tc tys) = all isTauTy tys && isTauTyCon tc
589 isTauTy (AppTy a b) = isTauTy a && isTauTy b
590 isTauTy (FunTy a b) = isTauTy a && isTauTy b
591 isTauTy (PredTy _) = True -- Don't look through source types
595 isTauTyCon :: TyCon -> Bool
596 -- Returns False for type synonyms whose expansion is a polytype
598 | isClosedSynTyCon tc = isTauTy (snd (synTyConDefn tc))
602 isBoxyTy :: TcType -> Bool
603 isBoxyTy ty = any isBoxyTyVar (varSetElems (tcTyVarsOfType ty))
605 isRigidTy :: TcType -> Bool
606 -- A type is rigid if it has no meta type variables in it
607 isRigidTy ty = all isImmutableTyVar (varSetElems (tcTyVarsOfType ty))
609 isRefineableTy :: TcType -> (Bool,Bool)
610 -- A type should have type refinements applied to it if it has
611 -- free type variables, and they are all rigid
612 isRefineableTy ty = (null tc_tvs, all isImmutableTyVar tc_tvs)
614 tc_tvs = varSetElems (tcTyVarsOfType ty)
616 isRefineablePred :: TcPredType -> Bool
617 isRefineablePred pred = not (null tc_tvs) && all isImmutableTyVar tc_tvs
619 tc_tvs = varSetElems (tcTyVarsOfPred pred)
622 getDFunTyKey :: Type -> OccName -- Get some string from a type, to be used to
623 -- construct a dictionary function name
624 getDFunTyKey ty | Just ty' <- tcView ty = getDFunTyKey ty'
625 getDFunTyKey (TyVarTy tv) = getOccName tv
626 getDFunTyKey (TyConApp tc _) = getOccName tc
627 getDFunTyKey (AppTy fun _) = getDFunTyKey fun
628 getDFunTyKey (FunTy _ _) = getOccName funTyCon
629 getDFunTyKey (ForAllTy _ t) = getDFunTyKey t
630 getDFunTyKey ty = pprPanic "getDFunTyKey" (pprType ty)
631 -- PredTy shouldn't happen
635 %************************************************************************
637 \subsection{Expanding and splitting}
639 %************************************************************************
641 These tcSplit functions are like their non-Tc analogues, but
642 a) they do not look through newtypes
643 b) they do not look through PredTys
644 c) [future] they ignore usage-type annotations
646 However, they are non-monadic and do not follow through mutable type
647 variables. It's up to you to make sure this doesn't matter.
650 tcSplitForAllTys :: Type -> ([TyVar], Type)
651 tcSplitForAllTys ty = split ty ty []
653 split orig_ty ty tvs | Just ty' <- tcView ty = split orig_ty ty' tvs
654 split _ (ForAllTy tv ty) tvs
655 | not (isCoVar tv) = split ty ty (tv:tvs)
656 split orig_ty _ tvs = (reverse tvs, orig_ty)
658 tcIsForAllTy :: Type -> Bool
659 tcIsForAllTy ty | Just ty' <- tcView ty = tcIsForAllTy ty'
660 tcIsForAllTy (ForAllTy tv _) = not (isCoVar tv)
661 tcIsForAllTy _ = False
663 tcSplitPredFunTy_maybe :: Type -> Maybe (PredType, Type)
664 -- Split off the first predicate argument from a type
665 tcSplitPredFunTy_maybe ty | Just ty' <- tcView ty = tcSplitPredFunTy_maybe ty'
666 tcSplitPredFunTy_maybe (ForAllTy tv ty)
667 | isCoVar tv = Just (coVarPred tv, ty)
668 tcSplitPredFunTy_maybe (FunTy arg res)
669 | Just p <- tcSplitPredTy_maybe arg = Just (p, res)
670 tcSplitPredFunTy_maybe _
673 tcSplitPhiTy :: Type -> (ThetaType, Type)
678 = case tcSplitPredFunTy_maybe ty of
679 Just (pred, ty) -> split ty (pred:ts)
680 Nothing -> (reverse ts, ty)
682 tcSplitSigmaTy :: Type -> ([TyVar], ThetaType, Type)
683 tcSplitSigmaTy ty = case tcSplitForAllTys ty of
684 (tvs, rho) -> case tcSplitPhiTy rho of
685 (theta, tau) -> (tvs, theta, tau)
687 -----------------------
690 -> ( [([TyVar], ThetaType)], -- forall as.C => forall bs.D
691 TcSigmaType) -- The rest of the type
693 -- We need a loop here because we are now prepared to entertain
695 -- f:: forall a. Eq a => forall b. Baz b => tau
696 -- We want to instantiate this to
697 -- f2::tau {f2 = f1 b (Baz b), f1 = f a (Eq a)}
699 tcMultiSplitSigmaTy sigma
700 = case (tcSplitSigmaTy sigma) of
701 ([], [], _) -> ([], sigma)
702 (tvs, theta, ty) -> case tcMultiSplitSigmaTy ty of
703 (pairs, rest) -> ((tvs,theta):pairs, rest)
705 -----------------------
706 tcTyConAppTyCon :: Type -> TyCon
707 tcTyConAppTyCon ty = case tcSplitTyConApp_maybe ty of
709 Nothing -> pprPanic "tcTyConAppTyCon" (pprType ty)
711 tcTyConAppArgs :: Type -> [Type]
712 tcTyConAppArgs ty = case tcSplitTyConApp_maybe ty of
713 Just (_, args) -> args
714 Nothing -> pprPanic "tcTyConAppArgs" (pprType ty)
716 tcSplitTyConApp :: Type -> (TyCon, [Type])
717 tcSplitTyConApp ty = case tcSplitTyConApp_maybe ty of
719 Nothing -> pprPanic "tcSplitTyConApp" (pprType ty)
721 tcSplitTyConApp_maybe :: Type -> Maybe (TyCon, [Type])
722 tcSplitTyConApp_maybe ty | Just ty' <- tcView ty = tcSplitTyConApp_maybe ty'
723 tcSplitTyConApp_maybe (TyConApp tc tys) = Just (tc, tys)
724 tcSplitTyConApp_maybe (FunTy arg res) = Just (funTyCon, [arg,res])
725 -- Newtypes are opaque, so they may be split
726 -- However, predicates are not treated
727 -- as tycon applications by the type checker
728 tcSplitTyConApp_maybe _ = Nothing
730 -----------------------
731 tcSplitFunTys :: Type -> ([Type], Type)
732 tcSplitFunTys ty = case tcSplitFunTy_maybe ty of
734 Just (arg,res) -> (arg:args, res')
736 (args,res') = tcSplitFunTys res
738 tcSplitFunTy_maybe :: Type -> Maybe (Type, Type)
739 tcSplitFunTy_maybe ty | Just ty' <- tcView ty = tcSplitFunTy_maybe ty'
740 tcSplitFunTy_maybe (FunTy arg res) | not (isPredTy arg) = Just (arg, res)
741 tcSplitFunTy_maybe _ = Nothing
742 -- Note the (not (isPredTy arg)) guard
743 -- Consider (?x::Int) => Bool
744 -- We don't want to treat this as a function type!
745 -- A concrete example is test tc230:
746 -- f :: () -> (?p :: ()) => () -> ()
752 -> Arity -- N: Number of desired args
753 -> ([TcSigmaType], -- Arg types (N or fewer)
754 TcSigmaType) -- The rest of the type
756 tcSplitFunTysN ty n_args
759 | Just (arg,res) <- tcSplitFunTy_maybe ty
760 = case tcSplitFunTysN res (n_args - 1) of
761 (args, res) -> (arg:args, res)
765 tcSplitFunTy :: Type -> (Type, Type)
766 tcSplitFunTy ty = expectJust "tcSplitFunTy" (tcSplitFunTy_maybe ty)
768 tcFunArgTy :: Type -> Type
769 tcFunArgTy ty = fst (tcSplitFunTy ty)
771 tcFunResultTy :: Type -> Type
772 tcFunResultTy ty = snd (tcSplitFunTy ty)
774 -----------------------
775 tcSplitAppTy_maybe :: Type -> Maybe (Type, Type)
776 tcSplitAppTy_maybe ty | Just ty' <- tcView ty = tcSplitAppTy_maybe ty'
777 tcSplitAppTy_maybe ty = repSplitAppTy_maybe ty
779 tcSplitAppTy :: Type -> (Type, Type)
780 tcSplitAppTy ty = case tcSplitAppTy_maybe ty of
782 Nothing -> pprPanic "tcSplitAppTy" (pprType ty)
784 tcSplitAppTys :: Type -> (Type, [Type])
788 go ty args = case tcSplitAppTy_maybe ty of
789 Just (ty', arg) -> go ty' (arg:args)
792 -----------------------
793 tcGetTyVar_maybe :: Type -> Maybe TyVar
794 tcGetTyVar_maybe ty | Just ty' <- tcView ty = tcGetTyVar_maybe ty'
795 tcGetTyVar_maybe (TyVarTy tv) = Just tv
796 tcGetTyVar_maybe _ = Nothing
798 tcGetTyVar :: String -> Type -> TyVar
799 tcGetTyVar msg ty = expectJust msg (tcGetTyVar_maybe ty)
801 tcIsTyVarTy :: Type -> Bool
802 tcIsTyVarTy ty = maybeToBool (tcGetTyVar_maybe ty)
804 -----------------------
805 tcSplitDFunTy :: Type -> ([TyVar], [PredType], Class, [Type])
806 -- Split the type of a dictionary function
808 = case tcSplitSigmaTy ty of { (tvs, theta, tau) ->
809 case tcSplitDFunHead tau of { (clas, tys) ->
810 (tvs, theta, clas, tys) }}
812 tcSplitDFunHead :: Type -> (Class, [Type])
814 = case tcSplitPredTy_maybe tau of
815 Just (ClassP clas tys) -> (clas, tys)
816 _ -> panic "tcSplitDFunHead"
818 tcInstHeadTyNotSynonym :: Type -> Bool
819 -- Used in Haskell-98 mode, for the argument types of an instance head
820 -- These must not be type synonyms, but everywhere else type synonyms
821 -- are transparent, so we need a special function here
822 tcInstHeadTyNotSynonym ty
824 TyConApp tc _ -> not (isSynTyCon tc)
827 tcInstHeadTyAppAllTyVars :: Type -> Bool
828 -- Used in Haskell-98 mode, for the argument types of an instance head
829 -- These must be a constructor applied to type variable arguments
830 tcInstHeadTyAppAllTyVars ty
832 TyConApp _ tys -> ok tys
833 FunTy arg res -> ok [arg, res]
836 -- Check that all the types are type variables,
837 -- and that each is distinct
838 ok tys = equalLength tvs tys && hasNoDups tvs
840 tvs = mapCatMaybes get_tv tys
842 get_tv (TyVarTy tv) = Just tv -- through synonyms
848 %************************************************************************
850 \subsection{Predicate types}
852 %************************************************************************
855 tcSplitPredTy_maybe :: Type -> Maybe PredType
856 -- Returns Just for predicates only
857 tcSplitPredTy_maybe ty | Just ty' <- tcView ty = tcSplitPredTy_maybe ty'
858 tcSplitPredTy_maybe (PredTy p) = Just p
859 tcSplitPredTy_maybe _ = Nothing
861 predTyUnique :: PredType -> Unique
862 predTyUnique (IParam n _) = getUnique (ipNameName n)
863 predTyUnique (ClassP clas _) = getUnique clas
864 predTyUnique (EqPred a b) = pprPanic "predTyUnique" (ppr (EqPred a b))
868 --------------------- Dictionary types ---------------------------------
871 mkClassPred :: Class -> [Type] -> PredType
872 mkClassPred clas tys = ClassP clas tys
874 isClassPred :: PredType -> Bool
875 isClassPred (ClassP _ _) = True
876 isClassPred _ = False
878 isTyVarClassPred :: PredType -> Bool
879 isTyVarClassPred (ClassP _ tys) = all tcIsTyVarTy tys
880 isTyVarClassPred _ = False
882 getClassPredTys_maybe :: PredType -> Maybe (Class, [Type])
883 getClassPredTys_maybe (ClassP clas tys) = Just (clas, tys)
884 getClassPredTys_maybe _ = Nothing
886 getClassPredTys :: PredType -> (Class, [Type])
887 getClassPredTys (ClassP clas tys) = (clas, tys)
888 getClassPredTys _ = panic "getClassPredTys"
890 mkDictTy :: Class -> [Type] -> Type
891 mkDictTy clas tys = mkPredTy (ClassP clas tys)
893 isDictTy :: Type -> Bool
894 isDictTy ty | Just ty' <- tcView ty = isDictTy ty'
895 isDictTy (PredTy p) = isClassPred p
898 isDictLikeTy :: Type -> Bool
899 -- Note [Dictionary-like types]
900 isDictLikeTy ty | Just ty' <- tcView ty = isDictTy ty'
901 isDictLikeTy (PredTy p) = isClassPred p
902 isDictLikeTy (TyConApp tc tys)
903 | isTupleTyCon tc = all isDictLikeTy tys
904 isDictLikeTy _ = False
907 Note [Dictionary-like types]
908 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
909 Being "dictionary-like" means either a dictionary type or a tuple thereof.
910 In GHC 6.10 we build implication constraints which construct such tuples,
911 and if we land up with a binding
914 then we want to treat t as cheap under "-fdicts-cheap" for example.
915 (Implication constraints are normally inlined, but sadly not if the
916 occurrence is itself inside an INLINE function! Until we revise the
917 handling of implication constraints, that is.) This turned out to
918 be important in getting good arities in DPH code. Example:
921 class D a where { foo :: a -> a }
922 instance C a => D (Maybe a) where { foo x = x }
924 bar :: (C a, C b) => a -> b -> (Maybe a, Maybe b)
926 bar x y = (foo (Just x), foo (Just y))
928 Then 'bar' should jolly well have arity 4 (two dicts, two args), but
929 we ended up with something like
930 bar = __inline_me__ (\d1,d2. let t :: (D (Maybe a), D (Maybe b)) = ...
933 This is all a bit ad-hoc; eg it relies on knowing that implication
934 constraints build tuples.
936 --------------------- Implicit parameters ---------------------------------
939 isIPPred :: PredType -> Bool
940 isIPPred (IParam _ _) = True
943 isInheritablePred :: PredType -> Bool
944 -- Can be inherited by a context. For example, consider
945 -- f x = let g y = (?v, y+x)
946 -- in (g 3 with ?v = 8,
948 -- The point is that g's type must be quantifed over ?v:
949 -- g :: (?v :: a) => a -> a
950 -- but it doesn't need to be quantified over the Num a dictionary
951 -- which can be free in g's rhs, and shared by both calls to g
952 isInheritablePred (ClassP _ _) = True
953 isInheritablePred (EqPred _ _) = True
954 isInheritablePred _ = False
957 --------------------- Equality predicates ---------------------------------
959 substEqSpec :: TvSubst -> [(TyVar,Type)] -> [(TcType,TcType)]
960 substEqSpec subst eq_spec = [ (substTyVar subst tv, substTy subst ty)
961 | (tv,ty) <- eq_spec]
965 %************************************************************************
967 \subsection{Predicates}
969 %************************************************************************
971 isSigmaTy returns true of any qualified type. It doesn't *necessarily* have
973 f :: (?x::Int) => Int -> Int
976 isSigmaTy :: Type -> Bool
977 isSigmaTy ty | Just ty' <- tcView ty = isSigmaTy ty'
978 isSigmaTy (ForAllTy _ _) = True
979 isSigmaTy (FunTy a _) = isPredTy a
982 isOverloadedTy :: Type -> Bool
983 -- Yes for a type of a function that might require evidence-passing
984 -- Used only by bindInstsOfLocalFuns/Pats
985 -- NB: be sure to check for type with an equality predicate; hence isCoVar
986 isOverloadedTy ty | Just ty' <- tcView ty = isOverloadedTy ty'
987 isOverloadedTy (ForAllTy tv ty) = isCoVar tv || isOverloadedTy ty
988 isOverloadedTy (FunTy a _) = isPredTy a
989 isOverloadedTy _ = False
991 isPredTy :: Type -> Bool -- Belongs in TcType because it does
992 -- not look through newtypes, or predtypes (of course)
993 isPredTy ty | Just ty' <- tcView ty = isPredTy ty'
994 isPredTy (PredTy _) = True
999 isFloatTy, isDoubleTy, isIntegerTy, isIntTy, isWordTy, isBoolTy,
1000 isUnitTy, isCharTy :: Type -> Bool
1001 isFloatTy = is_tc floatTyConKey
1002 isDoubleTy = is_tc doubleTyConKey
1003 isIntegerTy = is_tc integerTyConKey
1004 isIntTy = is_tc intTyConKey
1005 isWordTy = is_tc wordTyConKey
1006 isBoolTy = is_tc boolTyConKey
1007 isUnitTy = is_tc unitTyConKey
1008 isCharTy = is_tc charTyConKey
1010 isStringTy :: Type -> Bool
1012 = case tcSplitTyConApp_maybe ty of
1013 Just (tc, [arg_ty]) -> tc == listTyCon && isCharTy arg_ty
1016 is_tc :: Unique -> Type -> Bool
1017 -- Newtypes are opaque to this
1018 is_tc uniq ty = case tcSplitTyConApp_maybe ty of
1019 Just (tc, _) -> uniq == getUnique tc
1024 -- NB: Currently used in places where we have already expanded type synonyms;
1025 -- hence no 'coreView'. This could, however, be changed without breaking
1027 isOpenSynTyConApp :: TcTauType -> Bool
1028 isOpenSynTyConApp (TyConApp tc tys) = isOpenSynTyCon tc &&
1029 length tys == tyConArity tc
1030 isOpenSynTyConApp _other = False
1034 %************************************************************************
1038 %************************************************************************
1041 deNoteType :: Type -> Type
1042 -- Remove all *outermost* type synonyms and other notes
1043 deNoteType ty | Just ty' <- tcView ty = deNoteType ty'
1048 tcTyVarsOfType :: Type -> TcTyVarSet
1049 -- Just the *TcTyVars* free in the type
1050 -- (Types.tyVarsOfTypes finds all free TyVars)
1051 tcTyVarsOfType (TyVarTy tv) = if isTcTyVar tv then unitVarSet tv
1053 tcTyVarsOfType (TyConApp _ tys) = tcTyVarsOfTypes tys
1054 tcTyVarsOfType (PredTy sty) = tcTyVarsOfPred sty
1055 tcTyVarsOfType (FunTy arg res) = tcTyVarsOfType arg `unionVarSet` tcTyVarsOfType res
1056 tcTyVarsOfType (AppTy fun arg) = tcTyVarsOfType fun `unionVarSet` tcTyVarsOfType arg
1057 tcTyVarsOfType (ForAllTy tyvar ty) = (tcTyVarsOfType ty `delVarSet` tyvar)
1058 `unionVarSet` tcTyVarsOfTyVar tyvar
1059 -- We do sometimes quantify over skolem TcTyVars
1061 tcTyVarsOfTyVar :: TcTyVar -> TyVarSet
1062 tcTyVarsOfTyVar tv | isCoVar tv = tcTyVarsOfType (tyVarKind tv)
1063 | otherwise = emptyVarSet
1065 tcTyVarsOfTypes :: [Type] -> TyVarSet
1066 tcTyVarsOfTypes tys = foldr (unionVarSet.tcTyVarsOfType) emptyVarSet tys
1068 tcTyVarsOfPred :: PredType -> TyVarSet
1069 tcTyVarsOfPred (IParam _ ty) = tcTyVarsOfType ty
1070 tcTyVarsOfPred (ClassP _ tys) = tcTyVarsOfTypes tys
1071 tcTyVarsOfPred (EqPred ty1 ty2) = tcTyVarsOfType ty1 `unionVarSet` tcTyVarsOfType ty2
1074 Note [Silly type synonym]
1075 ~~~~~~~~~~~~~~~~~~~~~~~~~
1078 What are the free tyvars of (T x)? Empty, of course!
1079 Here's the example that Ralf Laemmel showed me:
1080 foo :: (forall a. C u a -> C u a) -> u
1081 mappend :: Monoid u => u -> u -> u
1083 bar :: Monoid u => u
1084 bar = foo (\t -> t `mappend` t)
1085 We have to generalise at the arg to f, and we don't
1086 want to capture the constraint (Monad (C u a)) because
1087 it appears to mention a. Pretty silly, but it was useful to him.
1089 exactTyVarsOfType is used by the type checker to figure out exactly
1090 which type variables are mentioned in a type. It's also used in the
1091 smart-app checking code --- see TcExpr.tcIdApp
1093 On the other hand, consider a *top-level* definition
1094 f = (\x -> x) :: T a -> T a
1095 If we don't abstract over 'a' it'll get fixed to GHC.Prim.Any, and then
1096 if we have an application like (f "x") we get a confusing error message
1097 involving Any. So the conclusion is this: when generalising
1098 - at top level use tyVarsOfType
1099 - in nested bindings use exactTyVarsOfType
1100 See Trac #1813 for example.
1103 exactTyVarsOfType :: TcType -> TyVarSet
1104 -- Find the free type variables (of any kind)
1105 -- but *expand* type synonyms. See Note [Silly type synonym] above.
1106 exactTyVarsOfType ty
1109 go ty | Just ty' <- tcView ty = go ty' -- This is the key line
1110 go (TyVarTy tv) = unitVarSet tv
1111 go (TyConApp _ tys) = exactTyVarsOfTypes tys
1112 go (PredTy ty) = go_pred ty
1113 go (FunTy arg res) = go arg `unionVarSet` go res
1114 go (AppTy fun arg) = go fun `unionVarSet` go arg
1115 go (ForAllTy tyvar ty) = delVarSet (go ty) tyvar
1116 `unionVarSet` go_tv tyvar
1118 go_pred (IParam _ ty) = go ty
1119 go_pred (ClassP _ tys) = exactTyVarsOfTypes tys
1120 go_pred (EqPred ty1 ty2) = go ty1 `unionVarSet` go ty2
1122 go_tv tyvar | isCoVar tyvar = go (tyVarKind tyvar)
1123 | otherwise = emptyVarSet
1125 exactTyVarsOfTypes :: [TcType] -> TyVarSet
1126 exactTyVarsOfTypes tys = foldr (unionVarSet . exactTyVarsOfType) emptyVarSet tys
1129 Find the free tycons and classes of a type. This is used in the front
1130 end of the compiler.
1133 tyClsNamesOfType :: Type -> NameSet
1134 tyClsNamesOfType (TyVarTy _) = emptyNameSet
1135 tyClsNamesOfType (TyConApp tycon tys) = unitNameSet (getName tycon) `unionNameSets` tyClsNamesOfTypes tys
1136 tyClsNamesOfType (PredTy (IParam _ ty)) = tyClsNamesOfType ty
1137 tyClsNamesOfType (PredTy (ClassP cl tys)) = unitNameSet (getName cl) `unionNameSets` tyClsNamesOfTypes tys
1138 tyClsNamesOfType (PredTy (EqPred ty1 ty2)) = tyClsNamesOfType ty1 `unionNameSets` tyClsNamesOfType ty2
1139 tyClsNamesOfType (FunTy arg res) = tyClsNamesOfType arg `unionNameSets` tyClsNamesOfType res
1140 tyClsNamesOfType (AppTy fun arg) = tyClsNamesOfType fun `unionNameSets` tyClsNamesOfType arg
1141 tyClsNamesOfType (ForAllTy _ ty) = tyClsNamesOfType ty
1143 tyClsNamesOfTypes :: [Type] -> NameSet
1144 tyClsNamesOfTypes tys = foldr (unionNameSets . tyClsNamesOfType) emptyNameSet tys
1146 tyClsNamesOfDFunHead :: Type -> NameSet
1147 -- Find the free type constructors and classes
1148 -- of the head of the dfun instance type
1149 -- The 'dfun_head_type' is because of
1150 -- instance Foo a => Baz T where ...
1151 -- The decl is an orphan if Baz and T are both not locally defined,
1152 -- even if Foo *is* locally defined
1153 tyClsNamesOfDFunHead dfun_ty
1154 = case tcSplitSigmaTy dfun_ty of
1155 (_, _, head_ty) -> tyClsNamesOfType head_ty
1159 %************************************************************************
1161 \subsection[TysWiredIn-ext-type]{External types}
1163 %************************************************************************
1165 The compiler's foreign function interface supports the passing of a
1166 restricted set of types as arguments and results (the restricting factor
1170 tcSplitIOType_maybe :: Type -> Maybe (TyCon, Type, CoercionI)
1171 -- (isIOType t) returns Just (IO,t',co)
1172 -- if co : t ~ IO t'
1173 -- returns Nothing otherwise
1174 tcSplitIOType_maybe ty
1175 = case tcSplitTyConApp_maybe ty of
1176 -- This split absolutely has to be a tcSplit, because we must
1177 -- see the IO type; and it's a newtype which is transparent to splitTyConApp.
1179 Just (io_tycon, [io_res_ty])
1180 | io_tycon `hasKey` ioTyConKey
1181 -> Just (io_tycon, io_res_ty, IdCo)
1184 | not (isRecursiveTyCon tc)
1185 , Just (ty, co1) <- instNewTyCon_maybe tc tys
1186 -- Newtypes that require a coercion are ok
1187 -> case tcSplitIOType_maybe ty of
1189 Just (tc, ty', co2) -> Just (tc, ty', co1 `mkTransCoI` co2)
1193 isFFITy :: Type -> Bool
1194 -- True for any TyCon that can possibly be an arg or result of an FFI call
1195 isFFITy ty = checkRepTyCon legalFFITyCon ty
1197 isFFIArgumentTy :: DynFlags -> Safety -> Type -> Bool
1198 -- Checks for valid argument type for a 'foreign import'
1199 isFFIArgumentTy dflags safety ty
1200 = checkRepTyCon (legalOutgoingTyCon dflags safety) ty
1202 isFFIExternalTy :: Type -> Bool
1203 -- Types that are allowed as arguments of a 'foreign export'
1204 isFFIExternalTy ty = checkRepTyCon legalFEArgTyCon ty
1206 isFFIImportResultTy :: DynFlags -> Type -> Bool
1207 isFFIImportResultTy dflags ty
1208 = checkRepTyCon (legalFIResultTyCon dflags) ty
1210 isFFIExportResultTy :: Type -> Bool
1211 isFFIExportResultTy ty = checkRepTyCon legalFEResultTyCon ty
1213 isFFIDynArgumentTy :: Type -> Bool
1214 -- The argument type of a foreign import dynamic must be Ptr, FunPtr, Addr,
1215 -- or a newtype of either.
1216 isFFIDynArgumentTy = checkRepTyConKey [ptrTyConKey, funPtrTyConKey]
1218 isFFIDynResultTy :: Type -> Bool
1219 -- The result type of a foreign export dynamic must be Ptr, FunPtr, Addr,
1220 -- or a newtype of either.
1221 isFFIDynResultTy = checkRepTyConKey [ptrTyConKey, funPtrTyConKey]
1223 isFFILabelTy :: Type -> Bool
1224 -- The type of a foreign label must be Ptr, FunPtr, Addr,
1225 -- or a newtype of either.
1226 isFFILabelTy = checkRepTyConKey [ptrTyConKey, funPtrTyConKey]
1228 isFFIDotnetTy :: DynFlags -> Type -> Bool
1229 isFFIDotnetTy dflags ty
1230 = checkRepTyCon (\ tc -> (legalFIResultTyCon dflags tc ||
1231 isFFIDotnetObjTy ty || isStringTy ty)) ty
1232 -- NB: isStringTy used to look through newtypes, but
1233 -- it no longer does so. May need to adjust isFFIDotNetTy
1234 -- if we do want to look through newtypes.
1236 isFFIDotnetObjTy :: Type -> Bool
1238 = checkRepTyCon check_tc t_ty
1240 (_, t_ty) = tcSplitForAllTys ty
1241 check_tc tc = getName tc == objectTyConName
1243 isFunPtrTy :: Type -> Bool
1244 isFunPtrTy = checkRepTyConKey [funPtrTyConKey]
1246 toDNType :: Type -> DNType
1248 | isStringTy ty = DNString
1249 | isFFIDotnetObjTy ty = DNObject
1250 | Just (tc,argTys) <- tcSplitTyConApp_maybe ty
1251 = case lookup (getUnique tc) dn_assoc of
1254 | tc `hasKey` ioTyConKey -> toDNType (head argTys)
1255 | otherwise -> pprPanic ("toDNType: unsupported .NET type")
1256 (pprType ty <+> parens (hcat (map pprType argTys)) <+> ppr tc)
1257 | otherwise = panic "toDNType" -- Is this right?
1259 dn_assoc :: [ (Unique, DNType) ]
1260 dn_assoc = [ (unitTyConKey, DNUnit)
1261 , (intTyConKey, DNInt)
1262 , (int8TyConKey, DNInt8)
1263 , (int16TyConKey, DNInt16)
1264 , (int32TyConKey, DNInt32)
1265 , (int64TyConKey, DNInt64)
1266 , (wordTyConKey, DNInt)
1267 , (word8TyConKey, DNWord8)
1268 , (word16TyConKey, DNWord16)
1269 , (word32TyConKey, DNWord32)
1270 , (word64TyConKey, DNWord64)
1271 , (floatTyConKey, DNFloat)
1272 , (doubleTyConKey, DNDouble)
1273 , (ptrTyConKey, DNPtr)
1274 , (funPtrTyConKey, DNPtr)
1275 , (charTyConKey, DNChar)
1276 , (boolTyConKey, DNBool)
1279 checkRepTyCon :: (TyCon -> Bool) -> Type -> Bool
1280 -- Look through newtypes, but *not* foralls
1281 -- Should work even for recursive newtypes
1282 -- eg Manuel had: newtype T = MkT (Ptr T)
1283 checkRepTyCon check_tc ty
1287 | Just (tc,tys) <- splitTyConApp_maybe ty
1288 = case carefullySplitNewType_maybe rec_nts tc tys of
1289 Just (rec_nts', ty') -> go rec_nts' ty'
1290 Nothing -> check_tc tc
1294 checkRepTyConKey :: [Unique] -> Type -> Bool
1295 -- Like checkRepTyCon, but just looks at the TyCon key
1296 checkRepTyConKey keys
1297 = checkRepTyCon (\tc -> tyConUnique tc `elem` keys)
1300 ----------------------------------------------
1301 These chaps do the work; they are not exported
1302 ----------------------------------------------
1305 legalFEArgTyCon :: TyCon -> Bool
1307 -- It's illegal to make foreign exports that take unboxed
1308 -- arguments. The RTS API currently can't invoke such things. --SDM 7/2000
1309 = boxedMarshalableTyCon tc
1311 legalFIResultTyCon :: DynFlags -> TyCon -> Bool
1312 legalFIResultTyCon dflags tc
1313 | tc == unitTyCon = True
1314 | otherwise = marshalableTyCon dflags tc
1316 legalFEResultTyCon :: TyCon -> Bool
1317 legalFEResultTyCon tc
1318 | tc == unitTyCon = True
1319 | otherwise = boxedMarshalableTyCon tc
1321 legalOutgoingTyCon :: DynFlags -> Safety -> TyCon -> Bool
1322 -- Checks validity of types going from Haskell -> external world
1323 legalOutgoingTyCon dflags _ tc
1324 = marshalableTyCon dflags tc
1326 legalFFITyCon :: TyCon -> Bool
1327 -- True for any TyCon that can possibly be an arg or result of an FFI call
1329 = isUnLiftedTyCon tc || boxedMarshalableTyCon tc || tc == unitTyCon
1331 marshalableTyCon :: DynFlags -> TyCon -> Bool
1332 marshalableTyCon dflags tc
1333 = (dopt Opt_UnliftedFFITypes dflags
1334 && isUnLiftedTyCon tc
1335 && not (isUnboxedTupleTyCon tc)
1336 && case tyConPrimRep tc of -- Note [Marshalling VoidRep]
1339 || boxedMarshalableTyCon tc
1341 boxedMarshalableTyCon :: TyCon -> Bool
1342 boxedMarshalableTyCon tc
1343 = getUnique tc `elem` [ intTyConKey, int8TyConKey, int16TyConKey
1344 , int32TyConKey, int64TyConKey
1345 , wordTyConKey, word8TyConKey, word16TyConKey
1346 , word32TyConKey, word64TyConKey
1347 , floatTyConKey, doubleTyConKey
1348 , ptrTyConKey, funPtrTyConKey
1355 Note [Marshalling VoidRep]
1356 ~~~~~~~~~~~~~~~~~~~~~~~~~~
1357 We don't treat State# (whose PrimRep is VoidRep) as marshalable.
1358 In turn that means you can't write
1359 foreign import foo :: Int -> State# RealWorld
1361 Reason: the back end falls over with panic "primRepHint:VoidRep";
1362 and there is no compelling reason to permit it