summaryrefslogtreecommitdiff
path: root/compiler/typecheck/TcBinds.lhs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/typecheck/TcBinds.lhs')
-rw-r--r--compiler/typecheck/TcBinds.lhs214
1 files changed, 124 insertions, 90 deletions
diff --git a/compiler/typecheck/TcBinds.lhs b/compiler/typecheck/TcBinds.lhs
index c2af40703d..ec5f9d777a 100644
--- a/compiler/typecheck/TcBinds.lhs
+++ b/compiler/typecheck/TcBinds.lhs
@@ -9,14 +9,14 @@
module TcBinds ( tcLocalBinds, tcTopBinds, tcRecSelBinds,
tcHsBootSigs, tcPolyCheck,
- PragFun, tcSpecPrags, tcVectDecls, mkPragFun,
- TcSigInfo(..), TcSigFun,
+ PragFun, tcSpecPrags, tcVectDecls, mkPragFun,
+ TcSigInfo(..), TcSigFun,
instTcTySig, instTcTySigFromId, findScopedTyVars,
badBootDeclErr ) where
import {-# SOURCE #-} TcMatches ( tcGRHSsPat, tcMatchesFun )
import {-# SOURCE #-} TcExpr ( tcMonoExpr )
-import {-# SOURCE #-} TcPatSyn ( tcPatSynDecl, tcPatSynWorker )
+import {-# SOURCE #-} TcPatSyn ( tcInferPatSynDecl, tcCheckPatSynDecl, tcPatSynWorker )
import DynFlags
import HsSyn
@@ -33,7 +33,7 @@ import PatSyn
import ConLike
import FamInstEnv( normaliseType )
import FamInst( tcGetFamInstEnvs )
-import Type( tidyOpenType )
+import Type( tidyOpenType, splitFunTys )
import TyCon
import TcType
import TysPrim
@@ -61,6 +61,7 @@ import PrelNames(ipClassName)
import TcValidity (checkValidType)
import Control.Monad
+import Data.List (partition)
#include "HsVersions.h"
\end{code}
@@ -99,10 +100,10 @@ dictionaries, which we resolve at the module level.
Note [Polymorphic recursion]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The game plan for polymorphic recursion in the code above is
+The game plan for polymorphic recursion in the code above is
* Bind any variable for which we have a type signature
- to an Id with a polymorphic type. Then when type-checking
+ to an Id with a polymorphic type. Then when type-checking
the RHSs we'll make a full polymorphic call.
This fine, but if you aren't a bit careful you end up with a horrendous
@@ -174,7 +175,7 @@ tcTopBinds (ValBindsOut binds sigs)
, tcg_imp_specs = specs ++ tcg_imp_specs tcg_env } }
; return (tcg_env', tcl_env) }
- -- The top level bindings are flattened into a giant
+ -- The top level bindings are flattened into a giant
-- implicitly-mutually-recursive LHsBinds
tcTopBinds (ValBindsIn {}) = panic "tcTopBinds"
@@ -183,12 +184,12 @@ tcRecSelBinds :: HsValBinds Name -> TcM TcGblEnv
tcRecSelBinds (ValBindsOut binds sigs)
= tcExtendGlobalValEnv [sel_id | L _ (IdSig sel_id) <- sigs] $
do { (rec_sel_binds, tcg_env) <- discardWarnings (tcValBinds TopLevel binds sigs getGblEnv)
- ; let tcg_env'
+ ; let tcg_env'
| isHsBootOrSig (tcg_src tcg_env) = tcg_env
| otherwise = tcg_env { tcg_binds = foldr (unionBags . snd)
(tcg_binds tcg_env)
rec_sel_binds }
- -- Do not add the code for record-selector bindings when
+ -- Do not add the code for record-selector bindings when
-- compiling hs-boot files
; return tcg_env' }
tcRecSelBinds (ValBindsIn {}) = panic "tcRecSelBinds"
@@ -215,7 +216,7 @@ badBootDeclErr = ptext (sLit "Illegal declarations in an hs-boot file")
tcLocalBinds :: HsLocalBinds Name -> TcM thing
-> TcM (HsLocalBinds TcId, thing)
-tcLocalBinds EmptyLocalBinds thing_inside
+tcLocalBinds EmptyLocalBinds thing_inside
= do { thing <- thing_inside
; return (EmptyLocalBinds, thing) }
@@ -229,10 +230,10 @@ tcLocalBinds (HsIPBinds (IPBinds ip_binds _)) thing_inside
; (given_ips, ip_binds') <-
mapAndUnzipM (wrapLocSndM (tc_ip_bind ipClass)) ip_binds
- -- If the binding binds ?x = E, we must now
+ -- If the binding binds ?x = E, we must now
-- discharge any ?x constraints in expr_lie
-- See Note [Implicit parameter untouchables]
- ; (ev_binds, result) <- checkConstraints (IPSkol ips)
+ ; (ev_binds, result) <- checkConstraints (IPSkol ips)
[] given_ips thing_inside
; return (HsIPBinds (IPBinds ip_binds' ev_binds), result) }
@@ -268,11 +269,11 @@ as untouchables, not so much because we really must not unify them,
but rather because we otherwise end up with constraints like this
Num alpha, Implic { wanted = alpha ~ Int }
The constraint solver solves alpha~Int by unification, but then
-doesn't float that solved constraint out (it's not an unsolved
+doesn't float that solved constraint out (it's not an unsolved
wanted). Result disaster: the (Num alpha) is again solved, this
time by defaulting. No no no.
-However [Oct 10] this is all handled automatically by the
+However [Oct 10] this is all handled automatically by the
untouchable-range idea.
Note [Placeholder PatSyn kinds]
@@ -300,10 +301,10 @@ tcTyVar, doesn't look inside the TcTyThing.
\begin{code}
-tcValBinds :: TopLevelFlag
+tcValBinds :: TopLevelFlag
-> [(RecFlag, LHsBinds Name)] -> [LSig Name]
-> TcM thing
- -> TcM ([(RecFlag, LHsBinds TcId)], thing)
+ -> TcM ([(RecFlag, LHsBinds TcId)], thing)
tcValBinds top_lvl binds sigs thing_inside
= do { -- Typecheck the signature
@@ -313,7 +314,7 @@ tcValBinds top_lvl binds sigs thing_inside
; let prag_fn = mkPragFun sigs (foldr (unionBags . snd) emptyBag binds)
- -- Extend the envt right away with all
+ -- Extend the envt right away with all
-- the Ids declared with type signatures
-- Use tcExtendIdEnv2 to avoid extending the TcIdBinder stack
; tcExtendIdEnv2 [(idName id, id) | id <- poly_ids] $ do
@@ -339,7 +340,7 @@ tcBindGroups :: TopLevelFlag -> TcSigFun -> PragFun
-- Typecheck a whole lot of value bindings,
-- one strongly-connected component at a time
-- Here a "strongly connected component" has the strightforward
--- meaning of a group of bindings that mention each other,
+-- meaning of a group of bindings that mention each other,
-- ignoring type signatures (that part comes later)
tcBindGroups _ _ _ [] thing_inside
@@ -348,18 +349,18 @@ tcBindGroups _ _ _ [] thing_inside
tcBindGroups top_lvl sig_fn prag_fn (group : groups) thing_inside
= do { (group', (groups', thing))
- <- tc_group top_lvl sig_fn prag_fn group $
+ <- tc_group top_lvl sig_fn prag_fn group $
tcBindGroups top_lvl sig_fn prag_fn groups thing_inside
; return (group' ++ groups', thing) }
------------------------
-tc_group :: forall thing.
+tc_group :: forall thing.
TopLevelFlag -> TcSigFun -> PragFun
-> (RecFlag, LHsBinds Name) -> TcM thing
-> TcM ([(RecFlag, LHsBinds TcId)], thing)
-- Typecheck one strongly-connected component of the original program.
--- We get a list of groups back, because there may
+-- We get a list of groups back, because there may
-- be specialisations etc as well
tc_group top_lvl sig_fn prag_fn (NonRecursive, binds) thing_inside
@@ -374,8 +375,8 @@ tc_group top_lvl sig_fn prag_fn (NonRecursive, binds) thing_inside
; return ( [(NonRecursive, bind')], thing) }
tc_group top_lvl sig_fn prag_fn (Recursive, binds) thing_inside
- = -- To maximise polymorphism, we do a new
- -- strongly-connected-component analysis, this time omitting
+ = -- To maximise polymorphism, we do a new
+ -- strongly-connected-component analysis, this time omitting
-- any references to variables with type signatures.
-- (This used to be optional, but isn't now.)
do { traceTc "tc_group rec" (pprLHsBinds binds)
@@ -395,7 +396,7 @@ tc_group top_lvl sig_fn prag_fn (Recursive, binds) thing_inside
go :: [SCC (LHsBind Name)] -> TcM (LHsBinds TcId, [TcId], thing)
go (scc:sccs) = do { (binds1, ids1, closed) <- tc_scc scc
- ; (binds2, ids2, thing) <- tcExtendLetEnv top_lvl closed ids1 $
+ ; (binds2, ids2, thing) <- tcExtendLetEnv top_lvl closed ids1 $
go sccs
; return (binds1 `unionBags` binds2, ids1 ++ ids2, thing) }
go [] = do { thing <- thing_inside; return (emptyBag, [], thing) }
@@ -419,9 +420,8 @@ tc_single :: forall thing.
TopLevelFlag -> TcSigFun -> PragFun
-> LHsBind Name -> TcM thing
-> TcM (LHsBinds TcId, thing)
-tc_single _top_lvl _sig_fn _prag_fn (L _ (PatSynBind psb)) thing_inside
- = do { (pat_syn, aux_binds) <- tcPatSynDecl psb
-
+tc_single _top_lvl sig_fn _prag_fn (L _ (PatSynBind psb@PSB{ psb_id = L _ name })) thing_inside
+ = do { (pat_syn, aux_binds) <- tc_pat_syn_decl
; let tything = AConLike (PatSynCon pat_syn)
implicit_ids = (patSynMatcher pat_syn) :
(maybeToList (patSynWorker pat_syn))
@@ -431,13 +431,19 @@ tc_single _top_lvl _sig_fn _prag_fn (L _ (PatSynBind psb)) thing_inside
thing_inside
; return (aux_binds, thing)
}
+ where
+ tc_pat_syn_decl = case sig_fn name of
+ Nothing -> tcInferPatSynDecl psb
+ Just (TcPatSynInfo tpsi) -> tcCheckPatSynDecl psb tpsi
+ Just _ -> panic "tc_single"
+
tc_single top_lvl sig_fn prag_fn lbind thing_inside
= do { (binds1, ids, closed) <- tcPolyBinds top_lvl sig_fn prag_fn
NonRecursive NonRecursive
[lbind]
; thing <- tcExtendLetEnv top_lvl closed ids thing_inside
; return (binds1, thing) }
-
+
------------------------
mkEdges :: TcSigFun -> LHsBinds Name
-> [(LHsBind Name, BKey, [BKey])]
@@ -474,26 +480,26 @@ tcPolyBinds :: TopLevelFlag -> TcSigFun -> PragFun
-> [LHsBind Name]
-> TcM (LHsBinds TcId, [TcId], TopLevelFlag)
--- Typechecks a single bunch of bindings all together,
+-- Typechecks a single bunch of bindings all together,
-- and generalises them. The bunch may be only part of a recursive
-- group, because we use type signatures to maximise polymorphism
--
-- Returns a list because the input may be a single non-recursive binding,
-- in which case the dependency order of the resulting bindings is
--- important.
---
+-- important.
+--
-- Knows nothing about the scope of the bindings
tcPolyBinds top_lvl sig_fn prag_fn rec_group rec_tc bind_list
= setSrcSpan loc $
- recoverM (recoveryCode binder_names sig_fn) $ do
+ recoverM (recoveryCode binder_names sig_fn) $ do
-- Set up main recover; take advantage of any type sigs
{ traceTc "------------------------------------------------" Outputable.empty
; traceTc "Bindings for {" (ppr binder_names)
; dflags <- getDynFlags
; type_env <- getLclTypeEnv
- ; let plan = decideGeneralisationPlan dflags type_env
+ ; let plan = decideGeneralisationPlan dflags type_env
binder_names bind_list sig_fn
; traceTc "Generalisation plan" (ppr plan)
; result@(tc_binds, poly_ids, _) <- case plan of
@@ -513,7 +519,7 @@ tcPolyBinds top_lvl sig_fn prag_fn rec_group rec_tc bind_list
where
binder_names = collectHsBindListBinders bind_list
loc = foldr1 combineSrcSpans (map getLoc bind_list)
- -- The mbinds have been dependency analysed and
+ -- The mbinds have been dependency analysed and
-- may no longer be adjacent; so find the narrowest
-- span that includes them all
@@ -527,7 +533,7 @@ tcPolyNoGen -- No generalisation whatsoever
tcPolyNoGen rec_tc prag_fn tc_sig_fn bind_list
= do { (binds', mono_infos) <- tcMonoBinds rec_tc tc_sig_fn
- (LetGblBndr prag_fn)
+ (LetGblBndr prag_fn)
bind_list
; mono_ids' <- mapM tc_mono_info mono_infos
; return (binds', mono_ids', NotTopLevel) }
@@ -546,22 +552,22 @@ tcPolyNoGen rec_tc prag_fn tc_sig_fn bind_list
------------------
tcPolyCheck :: RecFlag -- Whether it's recursive after breaking
-- dependencies based on type signatures
- -> PragFun -> TcSigInfo
+ -> PragFun -> TcSigInfo
-> LHsBind Name
-> TcM (LHsBinds TcId, [TcId], TopLevelFlag)
--- There is just one binding,
+-- There is just one binding,
-- it binds a single variable,
-- it has a signature,
tcPolyCheck rec_tc prag_fn
- sig@(TcSigInfo { sig_id = poly_id, sig_tvs = tvs_w_scoped
+ sig@(TcSigInfo { sig_id = poly_id, sig_tvs = tvs_w_scoped
, sig_theta = theta, sig_tau = tau, sig_loc = loc })
bind
= do { ev_vars <- newEvVars theta
; let skol_info = SigSkol (FunSigCtxt (idName poly_id)) (mkPhiTy theta tau)
prag_sigs = prag_fn (idName poly_id)
tvs = map snd tvs_w_scoped
- ; (ev_binds, (binds', [mono_info]))
- <- setSrcSpan loc $
+ ; (ev_binds, (binds', [mono_info]))
+ <- setSrcSpan loc $
checkConstraints skol_info tvs ev_vars $
tcExtendTyVarEnv2 [(n,tv) | (Just n, tv) <- tvs_w_scoped] $
tcMonoBinds rec_tc (\_ -> Just sig) LetLclBndr [bind]
@@ -574,7 +580,7 @@ tcPolyCheck rec_tc prag_fn
, abe_poly = poly_id
, abe_mono = mono_id
, abe_prags = SpecPrags spec_prags }
- abs_bind = L loc $ AbsBinds
+ abs_bind = L loc $ AbsBinds
{ abs_tvs = tvs
, abs_ev_vars = ev_vars, abs_ev_binds = ev_binds
, abs_exports = [export], abs_binds = binds' }
@@ -582,11 +588,14 @@ tcPolyCheck rec_tc prag_fn
| otherwise = NotTopLevel
; return (unitBag abs_bind, [poly_id], closed) }
+tcPolyCheck _rec_tc _prag_fn sig _bind
+ = pprPanic "tcPolyCheck" (ppr sig)
+
------------------
-tcPolyInfer
+tcPolyInfer
:: RecFlag -- Whether it's recursive after breaking
-- dependencies based on type signatures
- -> PragFun -> TcSigFun
+ -> PragFun -> TcSigFun
-> Bool -- True <=> apply the monomorphism restriction
-> Bool -- True <=> free vars have closed types
-> [LHsBind Name]
@@ -608,7 +617,7 @@ tcPolyInfer rec_tc prag_fn tc_sig_fn mono closed bind_list
; let poly_ids = map abe_poly exports
final_closed | closed && not mr_bites = TopLevel
| otherwise = NotTopLevel
- abs_bind = L loc $
+ abs_bind = L loc $
AbsBinds { abs_tvs = qtvs
, abs_ev_vars = givens, abs_ev_binds = ev_binds
, abs_exports = exports, abs_binds = binds' }
@@ -640,7 +649,8 @@ mkExport prag_fn qtvs theta (poly_name, mb_sig, mono_id)
= do { mono_ty <- zonkTcType (idType mono_id)
; poly_id <- case mb_sig of
- Just sig -> return (sig_id sig)
+ Just TcSigInfo{ sig_id = id } -> return id
+ Just _ -> panic "mkExport"
Nothing -> mkInferredPolyId poly_name qtvs theta mono_ty
-- NB: poly_id has a zonked type
@@ -715,7 +725,7 @@ mk_bind_msg inferred want_ambig poly_name poly_ty tidy_env
Note [Validity of inferred types]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-We need to check inferred type for validity, in case it uses language
+We need to check inferred type for validity, in case it uses language
extensions that are not turned on. The principle is that if the user
simply adds the inferred type to the program source, it'll compile fine.
See #8883.
@@ -726,7 +736,7 @@ Examples that might fail:
- an inferred type that includes unboxed tuples
However we don't do the ambiguity check (checkValidType omits it for
-InfSigCtxt) because the impedence-matching stage, which follows
+InfSigCtxt) because the impedence-matching stage, which follows
immediately, will do it and we don't want two error messages.
Moreover, because of the impedence matching stage, the ambiguity-check
suggestion of -XAllowAmbiguiousTypes will not work.
@@ -742,8 +752,8 @@ Consider
g _ y = f 9 y
After typechecking we'll get
- f_mono_ty :: a -> Bool -> Bool
- g_mono_ty :: [b] -> Bool -> Bool
+ f_mono_ty :: a -> Bool -> Bool
+ g_mono_ty :: [b] -> Bool -> Bool
with constraints
(Eq a, Num a)
@@ -760,9 +770,9 @@ We can get these by "impedence matching":
g b = case tuple Integer b dEqInteger dNumInteger of (f,g) -> g
Suppose the shared quantified tyvars are qtvs and constraints theta.
-Then we want to check that
+Then we want to check that
f's polytype is more polymorphic than forall qtvs. theta => f_mono_ty
-and the proof is the impedence matcher.
+and the proof is the impedence matcher.
Notice that the impedence matcher may do defaulting. See Trac #7173.
@@ -826,7 +836,7 @@ tcSpecPrags poly_id prag_sigs
--------------
tcSpec :: TcId -> Sig Name -> TcM TcSpecPrag
-tcSpec poly_id prag@(SpecSig fun_name hs_ty inl)
+tcSpec poly_id prag@(SpecSig fun_name hs_ty inl)
-- The Name fun_name in the SpecSig may not be the same as that of the poly_id
-- Example: SPECIALISE for a class method: the Name in the SpecSig is
-- for the selector Id, but the poly_id is something like $cop
@@ -835,7 +845,7 @@ tcSpec poly_id prag@(SpecSig fun_name hs_ty inl)
= addErrCtxt (spec_ctxt prag) $
do { spec_ty <- tcHsSigType sig_ctxt hs_ty
; warnIf (not (isOverloadedTy poly_ty || isInlinePragma inl))
- (ptext (sLit "SPECIALISE pragma for non-overloaded function")
+ (ptext (sLit "SPECIALISE pragma for non-overloaded function")
<+> quotes (ppr fun_name))
-- Note [SPECIALISE pragmas]
; wrap <- tcSubType origin sig_ctxt (idType poly_id) spec_ty
@@ -858,14 +868,14 @@ tcImpPrags prags
; if (not_specialising dflags) then
return []
else
- mapAndRecoverM (wrapLocM tcImpSpec)
+ mapAndRecoverM (wrapLocM tcImpSpec)
[L loc (name,prag) | (L loc prag@(SpecSig (L _ name) _ _)) <- prags
, not (nameIsLocalOrFrom this_mod name) ] }
where
-- Ignore SPECIALISE pragmas for imported things
-- when we aren't specialising, or when we aren't generating
-- code. The latter happens when Haddocking the base library;
- -- we don't wnat complaints about lack of INLINABLE pragmas
+ -- we don't wnat complaints about lack of INLINABLE pragmas
not_specialising dflags
| not (gopt Opt_Specialise dflags) = True
| otherwise = case hscTarget dflags of
@@ -884,7 +894,7 @@ impSpecErr :: Name -> SDoc
impSpecErr name
= hang (ptext (sLit "You cannot SPECIALISE") <+> quotes (ppr name))
2 (vcat [ ptext (sLit "because its definition has no INLINE/INLINABLE pragma")
- , parens $ sep
+ , parens $ sep
[ ptext (sLit "or its defining module") <+> quotes (ppr mod)
, ptext (sLit "was compiled without -O")]])
where
@@ -892,7 +902,7 @@ impSpecErr name
--------------
tcVectDecls :: [LVectDecl Name] -> TcM ([LVectDecl TcId])
-tcVectDecls decls
+tcVectDecls decls
= do { decls' <- mapM (wrapLocM tcVect) decls
; let ids = [lvectDeclName decl | decl <- decls', not $ lvectInstDecl decl]
dups = findDupsEq (==) ids
@@ -901,7 +911,7 @@ tcVectDecls decls
; return decls'
}
where
- reportVectDups (first:_second:_more)
+ reportVectDups (first:_second:_more)
= addErrAt (getSrcSpan first) $
ptext (sLit "Duplicate vectorisation declarations for") <+> ppr first
reportVectDups _ = return ()
@@ -923,25 +933,25 @@ tcVect (HsVect name rhs)
{- OLD CODE:
-- turn the vectorisation declaration into a single non-recursive binding
- ; let bind = L loc $ mkTopFunBind name [mkSimpleMatch [] rhs]
+ ; let bind = L loc $ mkTopFunBind name [mkSimpleMatch [] rhs]
sigFun = const Nothing
pragFun = mkPragFun [] (unitBag bind)
-- perform type inference (including generalisation)
; (binds, [id'], _) <- tcPolyInfer False True sigFun pragFun NonRecursive [bind]
-
+
; traceTc "tcVect inferred type" $ ppr (varType id')
; traceTc "tcVect bindings" $ ppr binds
-
+
-- add all bindings, including the type variable and dictionary bindings produced by type
-- generalisation to the right-hand side of the vectorisation declaration
; let [AbsBinds tvs evs _ evBinds actualBinds] = (map unLoc . bagToList) binds
; let [bind'] = bagToList actualBinds
- MatchGroup
+ MatchGroup
[L _ (Match _ _ (GRHSs [L _ (GRHS _ rhs')] _))]
_ = (fun_matches . unLoc) bind'
rhsWrapped = mkHsLams tvs evs (mkHsDictLet evBinds rhs')
-
+
-- We return the type-checked 'Id', to propagate the inferred signature
-- to the vectoriser - see "Note [Typechecked vectorisation pragmas]" in HsDecls
; return $ HsVect (L loc id') (Just rhsWrapped)
@@ -990,7 +1000,7 @@ scalarTyConMustBeNullary = ptext (sLit "VECTORISE SCALAR type constructor must b
--------------
-- If typechecking the binds fails, then return with each
--- signature-less binder given type (forall a.a), to minimise
+-- signature-less binder given type (forall a.a), to minimise
-- subsequent error messages
recoveryCode :: [Name] -> TcSigFun -> TcM (LHsBinds TcId, [Id], TopLevelFlag)
recoveryCode binder_names sig_fn
@@ -999,7 +1009,7 @@ recoveryCode binder_names sig_fn
; return (emptyBag, poly_ids, if all is_closed poly_ids
then TopLevel else NotTopLevel) }
where
- mk_dummy name
+ mk_dummy name
| isJust (sig_fn name) = tcLookupId name -- Had signature; look it up
| otherwise = return (mkLocalId name forall_a_a) -- No signature
@@ -1021,7 +1031,7 @@ But SPECIALISE INLINE *can* make sense for GADTS:
ArrPair :: !Int -> Arr e1 -> Arr e2 -> Arr (e1, e2)
(!:) :: Arr e -> Int -> e
- {-# SPECIALISE INLINE (!:) :: Arr Int -> Int -> Int #-}
+ {-# SPECIALISE INLINE (!:) :: Arr Int -> Int -> Int #-}
{-# SPECIALISE INLINE (!:) :: Arr (a, b) -> Int -> (a, b) #-}
(ArrInt _ ba) !: (I# i) = I# (indexIntArray# ba i)
(ArrPair _ a1 a2) !: i = (a1 !: i, a2 !: i)
@@ -1046,7 +1056,7 @@ The rule for typing pattern bindings is this:
..sigs..
p = e
-where 'p' binds v1..vn, and 'e' may mention v1..vn,
+where 'p' binds v1..vn, and 'e' may mention v1..vn,
typechecks exactly like
..sigs..
@@ -1055,7 +1065,7 @@ typechecks exactly like
..
vn = case x of p -> vn
-Note that
+Note that
(f :: forall a. a -> a) = id
should not typecheck because
case id of { (f :: forall a. a->a) -> f }
@@ -1065,14 +1075,14 @@ will not typecheck.
tcMonoBinds :: RecFlag -- Whether the binding is recursive for typechecking purposes
-- i.e. the binders are mentioned in their RHSs, and
-- we are not rescued by a type signature
- -> TcSigFun -> LetBndrSpec
+ -> TcSigFun -> LetBndrSpec
-> [LHsBind Name]
-> TcM (LHsBinds TcId, [MonoBindInfo])
tcMonoBinds is_rec sig_fn no_gen
[ L b_loc (FunBind { fun_id = L nm_loc name, fun_infix = inf,
fun_matches = matches, bind_fvs = fvs })]
- -- Single function binding,
+ -- Single function binding,
| NonRecursive <- is_rec -- ...binder isn't mentioned in RHS
, Nothing <- sig_fn name -- ...with no type signature
= -- In this very special case we infer the type of the
@@ -1084,8 +1094,8 @@ tcMonoBinds is_rec sig_fn no_gen
do { rhs_ty <- newFlexiTyVarTy openTypeKind
; mono_id <- newNoSigLetBndr no_gen name rhs_ty
; (co_fn, matches') <- tcExtendIdBndrs [TcIdBndr mono_id NotTopLevel] $
- -- We extend the error context even for a non-recursive
- -- function so that in type error messages we show the
+ -- We extend the error context even for a non-recursive
+ -- function so that in type error messages we show the
-- type of the thing whose rhs we are type checking
tcMatchesFun name inf matches rhs_ty
@@ -1100,12 +1110,12 @@ tcMonoBinds _ sig_fn no_gen binds
-- Bring the monomorphic Ids, into scope for the RHSs
; let mono_info = getMonoBindInfo tc_binds
rhs_id_env = [(name,mono_id) | (name, Nothing, mono_id) <- mono_info]
- -- A monomorphic binding for each term variable that lacks
+ -- A monomorphic binding for each term variable that lacks
-- a type sig. (Ones with a sig are already in scope.)
- ; traceTc "tcMonoBinds" $ vcat [ ppr n <+> ppr id <+> ppr (idType id)
+ ; traceTc "tcMonoBinds" $ vcat [ ppr n <+> ppr id <+> ppr (idType id)
| (n,id) <- rhs_id_env]
- ; binds' <- tcExtendIdEnv2 rhs_id_env $
+ ; binds' <- tcExtendIdEnv2 rhs_id_env $
mapM (wrapLocM tcRhs) tc_binds
; return (listToBag binds', mono_info) }
@@ -1115,7 +1125,7 @@ tcMonoBinds _ sig_fn no_gen binds
-- if there's a signature for it, use the instantiated signature type
-- otherwise invent a type variable
-- You see that quite directly in the FunBind case.
---
+--
-- But there's a complication for pattern bindings:
-- data T = MkT (forall a. a->a)
-- MkT f = e
@@ -1126,7 +1136,7 @@ tcMonoBinds _ sig_fn no_gen binds
-- it; hence the TcMonoBind data type in which the LHS is done but the RHS isn't
data TcMonoBind -- Half completed; LHS done, RHS not done
- = TcFunBind MonoBindInfo SrcSpan Bool (MatchGroup Name (LHsExpr Name))
+ = TcFunBind MonoBindInfo SrcSpan Bool (MatchGroup Name (LHsExpr Name))
| TcPatBind [MonoBindInfo] (LPat TcId) (GRHSs Name (LHsExpr Name)) TcSigmaType
type MonoBindInfo = (Name, Maybe TcSigInfo, TcId)
@@ -1176,11 +1186,11 @@ tcRhs (TcFunBind (_,_,mono_id) loc inf matches)
= tcExtendIdBndrs [TcIdBndr mono_id NotTopLevel] $
-- NotTopLevel: it's a monomorphic binding
do { traceTc "tcRhs: fun bind" (ppr mono_id $$ ppr (idType mono_id))
- ; (co_fn, matches') <- tcMatchesFun (idName mono_id) inf
+ ; (co_fn, matches') <- tcMatchesFun (idName mono_id) inf
matches (idType mono_id)
; return (FunBind { fun_id = L loc mono_id, fun_infix = inf
, fun_matches = matches'
- , fun_co_fn = co_fn
+ , fun_co_fn = co_fn
, bind_fvs = placeHolderNamesTc
, fun_tick = Nothing }) }
@@ -1190,7 +1200,7 @@ tcRhs (TcPatBind infos pat' grhss pat_ty)
do { traceTc "tcRhs: pat bind" (ppr pat' $$ ppr pat_ty)
; grhss' <- addErrCtxt (patMonoBindsCtxt pat' grhss) $
tcGRHSsPat grhss pat_ty
- ; return (PatBind { pat_lhs = pat', pat_rhs = grhss', pat_rhs_ty = pat_ty
+ ; return (PatBind { pat_lhs = pat', pat_rhs = grhss', pat_rhs_ty = pat_ty
, bind_fvs = placeHolderNamesTc
, pat_ticks = (Nothing,[]) }) }
@@ -1231,7 +1241,7 @@ into scope for any explicitly forall-quantified type variables:
f x = e
Then 'a' is in scope inside 'e'.
-However, we do *not* support this
+However, we do *not* support this
- For pattern bindings e.g
f :: forall a. a->a
(f,g) = e
@@ -1244,7 +1254,7 @@ variable is set True when we are typechecking a single function
binding; and False for pattern bindings and a group of several
function bindings.
-Reason: in the latter cases, the "skolems" can be unified together,
+Reason: in the latter cases, the "skolems" can be unified together,
so they aren't properly rigid in the type-refinement sense.
NB: unless we are doing H98, each function with a sig will be done
separately, even if it's mutually recursive, so use_skols will be True
@@ -1267,7 +1277,7 @@ Note [Instantiate sig with fresh variables]
It's vital to instantiate a type signature with fresh variables.
For example:
type T = forall a. [a] -> [a]
- f :: T;
+ f :: T;
f = g where { g :: T; g = <rhs> }
We must not use the same 'a' from the defn of T at both places!!
@@ -1286,7 +1296,7 @@ If a type signaure is wrong, fail immediately:
to the ambiguity error.
ToDo: this means we fall over if any type sig
-is wrong (eg at the top level of the module),
+is wrong (eg at the top level of the module),
which is over-conservative
\begin{code}
@@ -1295,17 +1305,41 @@ tcTySigs hs_sigs
= checkNoErrs $ -- See Note [Fail eagerly on bad signatures]
do { ty_sigs_s<- mapAndRecoverM tcTySig hs_sigs
; let ty_sigs = concat ty_sigs_s
- env = mkNameEnv [(idName (sig_id sig), sig) | sig <- ty_sigs]
- ; return (map sig_id ty_sigs, lookupNameEnv env) }
+ poly_ids = [id | TcSigInfo{ sig_id = id } <- ty_sigs]
+ env = mkNameEnv [(getName sig, sig) | sig <- ty_sigs]
+ ; return (poly_ids, lookupNameEnv env) }
tcTySig :: LSig Name -> TcM [TcSigInfo]
tcTySig (L loc (IdSig id))
= do { sig <- instTcTySigFromId loc id
; return [sig] }
tcTySig (L loc (TypeSig names@(L _ name1 : _) hs_ty))
- = setSrcSpan loc $
+ = setSrcSpan loc $
do { sigma_ty <- tcHsSigType (FunSigCtxt name1) hs_ty
; mapM (instTcTySig hs_ty sigma_ty) (map unLoc names) }
+tcTySig (L loc (PatSynSig (L _ name) (_, qtvs) prov req ty))
+ = setSrcSpan loc $
+ do { traceTc "tcTySig {" $ ppr name $$ ppr qtvs $$ ppr prov $$ ppr req $$ ppr ty
+ ; let ctxt = FunSigCtxt name
+ ; tcHsTyVarBndrs qtvs $ \ qtvs' -> do
+ { ty' <- tcHsSigType ctxt ty
+ ; req' <- tcHsContext req
+ ; prov' <- tcHsContext prov
+
+ ; qtvs' <- mapM zonkQuantifiedTyVar qtvs'
+
+ ; let (_, pat_ty) = splitFunTys ty'
+ univ_set = tyVarsOfType pat_ty
+ (univ_tvs, ex_tvs) = partition (`elemVarSet` univ_set) qtvs'
+
+ ; traceTc "tcTySig }" $ ppr (ex_tvs, prov') $$ ppr (univ_tvs, req') $$ ppr ty'
+ ; let tpsi = TPSI{ patsig_name = name,
+ patsig_tau = ty',
+ patsig_ex = ex_tvs,
+ patsig_univ = univ_tvs,
+ patsig_prov = prov',
+ patsig_req = req' }
+ ; return [TcPatSynInfo tpsi] }}
tcTySig _ = return []
instTcTySigFromId :: SrcSpan -> Id -> TcM TcSigInfo
@@ -1486,12 +1520,12 @@ unliftedMustBeBang binds
polyBindErr :: [LHsBind Name] -> SDoc
polyBindErr binds
= hang (ptext (sLit "You can't mix polymorphic and unlifted bindings"))
- 2 (vcat [vcat (map ppr binds),
+ 2 (vcat [vcat (map ppr binds),
ptext (sLit "Probable fix: use a bang pattern")])
strictBindErr :: String -> Bool -> [LHsBind Name] -> SDoc
strictBindErr flavour unlifted_bndrs binds
- = hang (text flavour <+> msg <+> ptext (sLit "aren't allowed:"))
+ = hang (text flavour <+> msg <+> ptext (sLit "aren't allowed:"))
2 (vcat (map ppr binds))
where
msg | unlifted_bndrs = ptext (sLit "bindings for unlifted types")
@@ -1509,7 +1543,7 @@ Note [Binding scoped type variables]
\begin{code}
--- This one is called on LHS, when pat and grhss are both Name
+-- This one is called on LHS, when pat and grhss are both Name
-- and on RHS, when pat is TcId and grhss is still Name
patMonoBindsCtxt :: (OutputableBndr id, Outputable body) => LPat id -> GRHSs Name body -> SDoc
patMonoBindsCtxt pat grhss