import HsSyn -- lots of things
import CoreSyn -- lots of things
import CoreUtils
+import CoreFVs
import TcHsSyn ( mkArbitraryType ) -- Mis-placed?
import TcType
import Module
import Id
import Var ( TyVar )
+import VarSet
import Rules
import VarEnv
import Type
-- inlined and specialised
--
-- Given SpecPrag (/\as.\ds. f es) t, we have
--- the defn f_spec as ds = f es
--- and the RULE f es = f_spec as ds
+-- the defn f_spec as ds = let-nonrec f = /\fas\fds. let f_mono = <f-rhs> in f_mono
+-- in f es
+-- and the RULE forall as, ds. f es = f_spec as ds
--
-- It is *possible* that 'es' does not mention all of the dictionaries 'ds'
-- (a bit silly, because then the
= return Nothing
dsSpec all_tvs dicts tvs poly_id mono_id mono_bind
- (L loc (SpecPrag spec_expr spec_ty _const_dicts inl))
- -- See Note [Const rule dicts]
+ (L loc (SpecPrag spec_expr spec_ty inl))
= putSrcSpanDs loc $
do { let poly_name = idName poly_id
; spec_name <- newLocalName poly_name
spec_rhs = Let (NonRec local_poly poly_f_body) ds_spec_expr
poly_f_body = mkLams (tvs ++ dicts) f_body
+ extra_dict_bndrs = filter isDictId (varSetElems (exprFreeVars ds_spec_expr))
+ -- Note [Const rule dicts]
+
rule = mkLocalRule (mkFastString ("SPEC " ++ showSDoc (ppr poly_name)))
AlwaysActive poly_name
- bndrs args
+ (extra_dict_bndrs ++ bndrs) args
(mkVarApps (Var spec_id) bndrs)
; return (Just (addInlineInfo inl spec_id spec_rhs, rule))
} } }
Note [Const rule dicts]
~~~~~~~~~~~~~~~~~~~~~~~
-A SpecPrag has a field for "constant dicts" in the RULE, but I think
-it's pretty useless. See the place where it's generated in TcBinds.
-TcSimplify will discharge a constraint by binding it to, say,
-GHC.Base.$f2 :: Eq Int, withour putting anything in the LIE, so this
-dict won't show up in the const-dicts field. It probably doesn't matter,
-because the rule will end up being something like
- f Int GHC.Base.$f2 = ...
-rather than
- forall d. f Int d = ...
-The latter is more general, but in practice I think it won't make any
-difference.
+When the LHS of a specialisation rule, (/\as\ds. f es) has a free dict,
+which is presumably in scope at the function definition site, we can quantify
+over it too. *Any* dict with that type will do.
+
+So for example when you have
+ f :: Eq a => a -> a
+ f = <rhs>
+ {-# SPECIALISE f :: Int -> Int #-}
+
+Then we get the SpecPrag
+ SpecPrag (f Int dInt) Int
+
+And from that we want the rule
+
+ RULE forall dInt. f Int dInt = f_spec
+ f_spec = let f = <rhs> in f Int dInt
+
%************************************************************************
| SpecPrag
(HsExpr Id) -- An expression, of the given specialised type, which
PostTcType -- specialises the polymorphic function
- [Id] -- Dicts mentioned free in the expression
- -- Apr07: I think this is pretty useless
- -- see Note [Const rule dicts] in DsBinds
InlineSpec -- Inlining spec for the specialised function
isInlinePrag (InlinePrag _) = True
pprSpec var ty inl = sep [ptext SLIT("SPECIALIZE") <+> ppr inl <+> pprVarSig var ty]
pprPrag :: Outputable id => id -> LPrag -> SDoc
-pprPrag var (L _ (InlinePrag inl)) = ppr inl <+> ppr var
-pprPrag var (L _ (SpecPrag expr ty _ inl)) = pprSpec var ty inl
+pprPrag var (L _ (InlinePrag inl)) = ppr inl <+> ppr var
+pprPrag var (L _ (SpecPrag expr ty inl)) = pprSpec var ty inl
\end{code}
tcSpecPrag poly_id hs_ty inl
= do { let name = idName poly_id
; spec_ty <- tcHsSigType (FunSigCtxt name) hs_ty
- ; (co_fn, lie) <- getLIE (tcSubExp (SpecPragOrigin name) (idType poly_id) spec_ty)
- ; extendLIEs lie
- ; let const_dicts = map instToId lie
- ; return (SpecPrag (mkHsWrap co_fn (HsVar poly_id)) spec_ty const_dicts inl) }
+ ; co_fn <- tcSubExp (SpecPragOrigin name) (idType poly_id) spec_ty
+ ; return (SpecPrag (mkHsWrap co_fn (HsVar poly_id)) spec_ty inl) }
-- Most of the work of specialisation is done by
-- the desugarer, guided by the SpecPrag
mapM zonk_prag prags `thenM` \ new_prags ->
returnM (tyvars, new_global, zonkIdOcc env local, new_prags)
zonk_prag prag@(L _ (InlinePrag {})) = return prag
- zonk_prag (L loc (SpecPrag expr ty ds inl))
+ zonk_prag (L loc (SpecPrag expr ty inl))
= do { expr' <- zonkExpr env expr
; ty' <- zonkTcTypeToType env ty
- ; let ds' = zonkIdOccs env ds
- ; return (L loc (SpecPrag expr' ty' ds' inl)) }
+ ; return (L loc (SpecPrag expr' ty' inl)) }
\end{code}
%************************************************************************