| ... |
... |
@@ -29,7 +29,7 @@ module Haddock.Convert |
|
29
|
29
|
|
|
30
|
30
|
import Control.DeepSeq (force)
|
|
31
|
31
|
import Data.Either (lefts, partitionEithers, rights)
|
|
32
|
|
-import Data.Maybe (catMaybes, mapMaybe, maybeToList)
|
|
|
32
|
+import Data.Maybe (catMaybes, mapMaybe)
|
|
33
|
33
|
import GHC.Builtin.Names
|
|
34
|
34
|
( boxedRepDataConKey
|
|
35
|
35
|
, eqTyConKey
|
| ... |
... |
@@ -140,7 +140,7 @@ tyThingToLHsDecl prr t = case t of |
|
140
|
140
|
hsq_explicit $
|
|
141
|
141
|
fdTyVars fd
|
|
142
|
142
|
, feqn_fixity = fdFixity fd
|
|
143
|
|
- , feqn_rhs = synifyType WithinType [] rhs
|
|
|
143
|
+ , feqn_rhs = synifyType WithinType emptyVarSet rhs
|
|
144
|
144
|
}
|
|
145
|
145
|
|
|
146
|
146
|
extractAtItem
|
| ... |
... |
@@ -179,7 +179,7 @@ tyThingToLHsDecl prr t = case t of |
|
179
|
179
|
noLocA (MinimalSig (noAnn, NoSourceText) . noLocA $ classMinimalDef cl)
|
|
180
|
180
|
: [ noLocA tcdSig
|
|
181
|
181
|
| clsOp <- classOpItems cl
|
|
182
|
|
- , tcdSig <- synifyTcIdSig vs clsOp
|
|
|
182
|
+ , tcdSig <- synifyTcIdSig (mkVarSet vs) clsOp
|
|
183
|
183
|
]
|
|
184
|
184
|
, tcdMeths = [] -- ignore default method definitions, they don't affect signature
|
|
185
|
185
|
-- class associated-types are a subset of TyCon:
|
| ... |
... |
@@ -213,9 +213,9 @@ synifyAxBranch :: TyCon -> CoAxBranch -> TyFamInstEqn GhcRn |
|
213
|
213
|
synifyAxBranch tc (CoAxBranch{cab_tvs = tkvs, cab_lhs = args, cab_rhs = rhs}) =
|
|
214
|
214
|
let name = synifyNameN tc
|
|
215
|
215
|
args_types_only = filterOutInvisibleTypes tc args
|
|
216
|
|
- typats = map (synifyType WithinType []) args_types_only
|
|
|
216
|
+ typats = map (synifyType WithinType emptyVarSet) args_types_only
|
|
217
|
217
|
annot_typats = zipWith3 annotHsType args_poly args_types_only typats
|
|
218
|
|
- hs_rhs = synifyType WithinType [] rhs
|
|
|
218
|
+ hs_rhs = synifyType WithinType emptyVarSet rhs
|
|
219
|
219
|
outer_bndrs = HsOuterImplicit{hso_ximplicit = map tyVarName tkvs}
|
|
220
|
220
|
in -- TODO: this must change eventually
|
|
221
|
221
|
FamEqn
|
| ... |
... |
@@ -344,7 +344,7 @@ synifyTyCon _prr coax tc |
|
344
|
344
|
, tcdLName = synifyNameN tc
|
|
345
|
345
|
, tcdTyVars = synifyTyVars (tyConVisibleTyVars tc)
|
|
346
|
346
|
, tcdFixity = synifyFixity tc
|
|
347
|
|
- , tcdRhs = synifyType WithinType [] ty
|
|
|
347
|
+ , tcdRhs = synifyType WithinType emptyVarSet ty
|
|
348
|
348
|
}
|
|
349
|
349
|
-- (closed) newtype and data
|
|
350
|
350
|
| otherwise = do
|
| ... |
... |
@@ -578,8 +578,8 @@ synifyDataCon use_gadt_syntax dc = |
|
578
|
578
|
linear_tys =
|
|
579
|
579
|
zipWith
|
|
580
|
580
|
( \(Scaled mult ty) (HsSrcBang st unp str) ->
|
|
581
|
|
- let tySyn = synifyType WithinType [] ty
|
|
582
|
|
- multSyn = synifyMultRec [] mult
|
|
|
581
|
+ let tySyn = synifyType WithinType emptyVarSet ty
|
|
|
582
|
+ multSyn = synifyMultRec emptyVarSet mult
|
|
583
|
583
|
in CDF (noAnn, st) unp str multSyn tySyn Nothing
|
|
584
|
584
|
)
|
|
585
|
585
|
arg_tys
|
| ... |
... |
@@ -620,7 +620,7 @@ synifyDataCon use_gadt_syntax dc = |
|
620
|
620
|
, con_inner_bndrs = inner_bndrs
|
|
621
|
621
|
, con_mb_cxt = ctx
|
|
622
|
622
|
, con_g_args = hat
|
|
623
|
|
- , con_res_ty = synifyType WithinType [] res_ty
|
|
|
623
|
+ , con_res_ty = synifyType WithinType emptyVarSet res_ty
|
|
624
|
624
|
, con_doc = Nothing
|
|
625
|
625
|
}
|
|
626
|
626
|
else do
|
| ... |
... |
@@ -657,11 +657,11 @@ synifyIdSig |
|
657
|
657
|
-> SynifyTypeState
|
|
658
|
658
|
-- ^ what to do with a 'forall'
|
|
659
|
659
|
-> [TyVar]
|
|
660
|
|
- -- ^ free variables in the type to convert
|
|
|
660
|
+ -- ^ type variables bound from an outer scope
|
|
661
|
661
|
-> Id
|
|
662
|
662
|
-- ^ the 'Id' from which to get the type signature
|
|
663
|
663
|
-> Sig GhcRn
|
|
664
|
|
-synifyIdSig prr s vs i = TypeSig noAnn [n] (synifySigWcType s vs t)
|
|
|
664
|
+synifyIdSig prr s boundTvs i = TypeSig noAnn [n] (synifySigWcType s boundTvs t)
|
|
665
|
665
|
where
|
|
666
|
666
|
!n = force $ synifyNameN i
|
|
667
|
667
|
t = defaultType prr (varType i)
|
| ... |
... |
@@ -669,18 +669,18 @@ synifyIdSig prr s vs i = TypeSig noAnn [n] (synifySigWcType s vs t) |
|
669
|
669
|
-- | Turn a 'ClassOpItem' into a list of signatures. The list returned is going
|
|
670
|
670
|
-- to contain the synified 'ClassOpSig' as well (when appropriate) a default
|
|
671
|
671
|
-- 'ClassOpSig'.
|
|
672
|
|
-synifyTcIdSig :: [TyVar] -> ClassOpItem -> [Sig GhcRn]
|
|
673
|
|
-synifyTcIdSig vs (i, dm) =
|
|
|
672
|
+synifyTcIdSig :: TyVarSet -> ClassOpItem -> [Sig GhcRn]
|
|
|
673
|
+synifyTcIdSig boundTvs (i, dm) =
|
|
674
|
674
|
[ClassOpSig noAnn False [synifyNameN i] (mainSig (varType i))]
|
|
675
|
675
|
++ [ ClassOpSig noAnn True [noLocA dn] (defSig dt)
|
|
676
|
676
|
| Just (dn, GenericDM dt) <- [dm]
|
|
677
|
677
|
]
|
|
678
|
678
|
where
|
|
679
|
|
- mainSig t = synifySigType DeleteTopLevelQuantification vs t
|
|
680
|
|
- defSig t = synifySigType ImplicitizeForAll vs t
|
|
|
679
|
+ mainSig t = synifySigType DeleteTopLevelQuantification boundTvs t
|
|
|
680
|
+ defSig t = synifySigType ImplicitizeForAll boundTvs t
|
|
681
|
681
|
|
|
682
|
682
|
synifyCtx :: [PredType] -> LHsContext GhcRn
|
|
683
|
|
-synifyCtx ts = noLocA (map (synifyType WithinType []) ts)
|
|
|
683
|
+synifyCtx ts = noLocA (map (synifyType WithinType emptyVarSet) ts)
|
|
684
|
684
|
|
|
685
|
685
|
synifyTyVars :: [TyVar] -> LHsQTyVars GhcRn
|
|
686
|
686
|
synifyTyVars ktvs =
|
| ... |
... |
@@ -699,7 +699,7 @@ synifyTyVarBndr' :: VarSet -> VarBndr TyVar flag -> LHsTyVarBndr flag GhcRn |
|
699
|
699
|
synifyTyVarBndr' no_kinds (Bndr tv spec) = synify_ty_var no_kinds spec tv
|
|
700
|
700
|
|
|
701
|
701
|
-- | Like 'synifyTyVarBndr', but accepts a set of variables for which to omit kind
|
|
702
|
|
--- signatures (even if they don't have the lifted type kind).
|
|
|
702
|
+-- signatures (even if they don't have kind 'Type').
|
|
703
|
703
|
synify_ty_var :: VarSet -> flag -> TyVar -> LHsTyVarBndr flag GhcRn
|
|
704
|
704
|
synify_ty_var no_kinds flag tv =
|
|
705
|
705
|
noLocA (HsTvb noAnn flag bndr_var bndr_kind)
|
| ... |
... |
@@ -726,7 +726,7 @@ annotHsType _ _ hs_ty@(L _ (HsKindSig{})) = hs_ty |
|
726
|
726
|
annotHsType True ty hs_ty
|
|
727
|
727
|
| not $ isEmptyVarSet $ filterVarSet isTyVar $ tyCoVarsOfType ty =
|
|
728
|
728
|
let ki = typeKind ty
|
|
729
|
|
- hs_ki = synifyType WithinType [] ki
|
|
|
729
|
+ hs_ki = synifyType WithinType emptyVarSet ki
|
|
730
|
730
|
in noLocA (HsKindSig noAnn hs_ty hs_ki)
|
|
731
|
731
|
annotHsType _ _ hs_ty = hs_ty
|
|
732
|
732
|
|
| ... |
... |
@@ -768,14 +768,15 @@ data SynifyTypeState |
|
768
|
768
|
-- the defining class gets to quantify all its functions for free!
|
|
769
|
769
|
DeleteTopLevelQuantification
|
|
770
|
770
|
|
|
771
|
|
-synifySigType :: SynifyTypeState -> [TyVar] -> Type -> LHsSigType GhcRn
|
|
|
771
|
+synifySigType :: SynifyTypeState -> TyVarSet -> Type -> LHsSigType GhcRn
|
|
772
|
772
|
-- The use of mkEmptySigType (which uses empty binders in OuterImplicit)
|
|
773
|
773
|
-- is a bit suspicious; what if the type has free variables?
|
|
774
|
|
-synifySigType s vs ty = mkEmptySigType (synifyType s vs ty)
|
|
|
774
|
+synifySigType s boundTvs ty = mkEmptySigType (synifyType s boundTvs ty)
|
|
775
|
775
|
|
|
776
|
776
|
synifySigWcType :: SynifyTypeState -> [TyVar] -> Type -> LHsSigWcType GhcRn
|
|
777
|
777
|
-- Ditto (see synifySigType)
|
|
778
|
|
-synifySigWcType s vs ty = mkEmptyWildCardBndrs (mkEmptySigType (rename (map getName vs) $ synifyType s vs ty))
|
|
|
778
|
+synifySigWcType s vs ty =
|
|
|
779
|
+ mkEmptyWildCardBndrs (mkEmptySigType (rename (map getName vs) $ synifyType s (mkVarSet vs) ty))
|
|
779
|
780
|
|
|
780
|
781
|
synifyPatSynSigType :: PatSyn -> LHsSigType GhcRn
|
|
781
|
782
|
-- Ditto (see synifySigType)
|
| ... |
... |
@@ -791,13 +792,13 @@ defaultType HideRuntimeRep = defaultRuntimeRepVars |
|
791
|
792
|
synifyType
|
|
792
|
793
|
:: SynifyTypeState
|
|
793
|
794
|
-- ^ what to do with a 'forall'
|
|
794
|
|
- -> [TyVar]
|
|
795
|
|
- -- ^ free variables in the type to convert
|
|
|
795
|
+ -> TyVarSet
|
|
|
796
|
+ -- ^ bound type variables
|
|
796
|
797
|
-> Type
|
|
797
|
798
|
-- ^ the type to convert
|
|
798
|
799
|
-> LHsType GhcRn
|
|
799
|
800
|
synifyType _ _ (TyVarTy tv) = noLocA $ HsTyVar noAnn NotPromoted $ noLocA (noUserRdr $ getName tv)
|
|
800
|
|
-synifyType _ vs (TyConApp tc tys) =
|
|
|
801
|
+synifyType _ boundTvs (TyConApp tc tys) =
|
|
801
|
802
|
maybe_sig res_ty
|
|
802
|
803
|
where
|
|
803
|
804
|
res_ty :: LHsType GhcRn
|
| ... |
... |
@@ -819,24 +820,24 @@ synifyType _ vs (TyConApp tc tys) = |
|
819
|
820
|
ConstraintTuple -> HsBoxedOrConstraintTuple
|
|
820
|
821
|
UnboxedTuple -> HsUnboxedTuple
|
|
821
|
822
|
)
|
|
822
|
|
- (map (synifyType WithinType vs) vis_tys)
|
|
|
823
|
+ (map (synifyType WithinType boundTvs) vis_tys)
|
|
823
|
824
|
| isUnboxedSumTyCon tc =
|
|
824
|
|
- noLocA $ HsSumTy noAnn (map (synifyType WithinType vs) vis_tys)
|
|
|
825
|
+ noLocA $ HsSumTy noAnn (map (synifyType WithinType boundTvs) vis_tys)
|
|
825
|
826
|
| Just dc <- isPromotedDataCon_maybe tc
|
|
826
|
827
|
, isTupleDataCon dc
|
|
827
|
828
|
, dataConSourceArity dc == length vis_tys =
|
|
828
|
|
- noLocA $ HsExplicitTupleTy noExtField IsPromoted (map (synifyType WithinType vs) vis_tys)
|
|
|
829
|
+ noLocA $ HsExplicitTupleTy noExtField IsPromoted (map (synifyType WithinType boundTvs) vis_tys)
|
|
829
|
830
|
-- ditto for lists
|
|
830
|
831
|
| getName tc == listTyConName
|
|
831
|
832
|
, [ty] <- vis_tys =
|
|
832
|
|
- noLocA $ HsListTy noAnn (synifyType WithinType vs ty)
|
|
|
833
|
+ noLocA $ HsListTy noAnn (synifyType WithinType boundTvs ty)
|
|
833
|
834
|
| tc == promotedNilDataCon
|
|
834
|
835
|
, [] <- vis_tys =
|
|
835
|
836
|
noLocA $ HsExplicitListTy noExtField IsPromoted []
|
|
836
|
837
|
| tc == promotedConsDataCon
|
|
837
|
838
|
, [ty1, ty2] <- vis_tys =
|
|
838
|
|
- let hTy = synifyType WithinType vs ty1
|
|
839
|
|
- in case synifyType WithinType vs ty2 of
|
|
|
839
|
+ let hTy = synifyType WithinType boundTvs ty1
|
|
|
840
|
+ in case synifyType WithinType boundTvs ty2 of
|
|
840
|
841
|
tTy
|
|
841
|
842
|
| L _ (HsExplicitListTy _ IsPromoted tTy') <- stripKindSig tTy ->
|
|
842
|
843
|
noLocA $ HsExplicitListTy noExtField IsPromoted (hTy : tTy')
|
| ... |
... |
@@ -846,7 +847,7 @@ synifyType _ vs (TyConApp tc tys) = |
|
846
|
847
|
| tc `hasKey` ipClassKey
|
|
847
|
848
|
, [name, ty] <- tys
|
|
848
|
849
|
, Just x <- isStrLitTy name =
|
|
849
|
|
- noLocA $ HsIParamTy noAnn (noLocA $ HsIPName x) (synifyType WithinType vs ty)
|
|
|
850
|
+ noLocA $ HsIParamTy noAnn (noLocA $ HsIPName x) (synifyType WithinType boundTvs ty)
|
|
850
|
851
|
-- and equalities
|
|
851
|
852
|
| tc `hasKey` eqTyConKey
|
|
852
|
853
|
, [ty1, ty2] <- tys =
|
| ... |
... |
@@ -854,9 +855,9 @@ synifyType _ vs (TyConApp tc tys) = |
|
854
|
855
|
HsOpTy
|
|
855
|
856
|
noExtField
|
|
856
|
857
|
NotPromoted
|
|
857
|
|
- (synifyType WithinType vs ty1)
|
|
|
858
|
+ (synifyType WithinType boundTvs ty1)
|
|
858
|
859
|
(noLocA $ noUserRdr eqTyConName)
|
|
859
|
|
- (synifyType WithinType vs ty2)
|
|
|
860
|
+ (synifyType WithinType boundTvs ty2)
|
|
860
|
861
|
-- and infix type operators
|
|
861
|
862
|
| isSymOcc (nameOccName (getName tc))
|
|
862
|
863
|
, ty1 : ty2 : tys_rest <- vis_tys =
|
| ... |
... |
@@ -864,9 +865,9 @@ synifyType _ vs (TyConApp tc tys) = |
|
864
|
865
|
( HsOpTy
|
|
865
|
866
|
noExtField
|
|
866
|
867
|
prom
|
|
867
|
|
- (synifyType WithinType vs ty1)
|
|
|
868
|
+ (synifyType WithinType boundTvs ty1)
|
|
868
|
869
|
(noLocA $ noUserRdr $ getName tc)
|
|
869
|
|
- (synifyType WithinType vs ty2)
|
|
|
870
|
+ (synifyType WithinType boundTvs ty2)
|
|
870
|
871
|
)
|
|
871
|
872
|
tys_rest
|
|
872
|
873
|
-- Most TyCons:
|
| ... |
... |
@@ -880,7 +881,7 @@ synifyType _ vs (TyConApp tc tys) = |
|
880
|
881
|
foldl
|
|
881
|
882
|
(\t1 t2 -> noLocA $ HsAppTy noExtField t1 t2)
|
|
882
|
883
|
(noLocA ty_app)
|
|
883
|
|
- ( map (synifyType WithinType vs) $
|
|
|
884
|
+ ( map (synifyType WithinType boundTvs) $
|
|
884
|
885
|
filterOut isCoercionTy ty_args
|
|
885
|
886
|
)
|
|
886
|
887
|
|
| ... |
... |
@@ -891,56 +892,57 @@ synifyType _ vs (TyConApp tc tys) = |
|
891
|
892
|
maybe_sig ty'
|
|
892
|
893
|
| tyConAppNeedsKindSig False tc tys_len =
|
|
893
|
894
|
let full_kind = typeKind (mkTyConApp tc tys)
|
|
894
|
|
- full_kind' = synifyType WithinType vs full_kind
|
|
|
895
|
+ full_kind' = synifyType WithinType boundTvs full_kind
|
|
895
|
896
|
in noLocA $ HsKindSig noAnn ty' full_kind'
|
|
896
|
897
|
| otherwise = ty'
|
|
897
|
|
-synifyType _ vs ty@(AppTy{}) =
|
|
|
898
|
+synifyType _ boundTvs ty@(AppTy{}) =
|
|
898
|
899
|
let
|
|
899
|
900
|
(ty_head, ty_args) = splitAppTys ty
|
|
900
|
|
- ty_head' = synifyType WithinType vs ty_head
|
|
|
901
|
+ ty_head' = synifyType WithinType boundTvs ty_head
|
|
901
|
902
|
ty_args' =
|
|
902
|
|
- map (synifyType WithinType vs) $
|
|
|
903
|
+ map (synifyType WithinType boundTvs) $
|
|
903
|
904
|
filterOut isCoercionTy $
|
|
904
|
905
|
filterByList
|
|
905
|
906
|
(map isVisibleForAllTyFlag $ appTyForAllTyFlags ty_head ty_args)
|
|
906
|
907
|
ty_args
|
|
907
|
908
|
in
|
|
908
|
909
|
foldl (\t1 t2 -> noLocA $ HsAppTy noExtField t1 t2) ty_head' ty_args'
|
|
909
|
|
-synifyType s vs funty@(FunTy af w t1 t2)
|
|
910
|
|
- | isInvisibleFunArg af = synifySigmaType s vs funty
|
|
|
910
|
+synifyType s boundTvs funty@(FunTy af w t1 t2)
|
|
|
911
|
+ | isInvisibleFunArg af = synifySigmaType s boundTvs funty
|
|
911
|
912
|
| otherwise = noLocA $ HsFunTy noExtField w' s1 s2
|
|
912
|
913
|
where
|
|
913
|
|
- s1 = synifyType WithinType vs t1
|
|
914
|
|
- s2 = synifyType WithinType vs t2
|
|
915
|
|
- w' = synifyMultArrow vs w
|
|
916
|
|
-synifyType s vs forallty@(ForAllTy (Bndr _ argf) _ty) =
|
|
|
914
|
+ s1 = synifyType WithinType boundTvs t1
|
|
|
915
|
+ s2 = synifyType WithinType boundTvs t2
|
|
|
916
|
+ w' = synifyMultArrow boundTvs w
|
|
|
917
|
+synifyType s boundTvs forallty@(ForAllTy (Bndr _ argf) _ty) =
|
|
917
|
918
|
case argf of
|
|
918
|
|
- Required -> synifyVisForAllType vs forallty
|
|
919
|
|
- Invisible _ -> synifySigmaType s vs forallty
|
|
|
919
|
+ Required -> synifyVisForAllType boundTvs forallty
|
|
|
920
|
+ Invisible _ -> synifySigmaType s boundTvs forallty
|
|
920
|
921
|
synifyType _ _ (LitTy t) = noLocA $ HsTyLit noExtField $ synifyTyLit t
|
|
921
|
|
-synifyType s vs (CastTy t _) = synifyType s vs t
|
|
|
922
|
+synifyType s boundTvs (CastTy t _) = synifyType s boundTvs t
|
|
922
|
923
|
synifyType _ _ (CoercionTy{}) = error "synifyType:Coercion"
|
|
923
|
924
|
|
|
924
|
925
|
-- | Process a 'Type' which starts with a visible @forall@ into an 'HsType'
|
|
925
|
926
|
synifyVisForAllType
|
|
926
|
|
- :: [TyVar]
|
|
927
|
|
- -- ^ free variables in the type to convert
|
|
|
927
|
+ :: TyVarSet
|
|
|
928
|
+ -- ^ bound type variables
|
|
928
|
929
|
-> Type
|
|
929
|
930
|
-- ^ the forall type to convert
|
|
930
|
931
|
-> LHsType GhcRn
|
|
931
|
|
-synifyVisForAllType vs ty =
|
|
|
932
|
+synifyVisForAllType boundTvs ty =
|
|
932
|
933
|
let (tvs, rho) = tcSplitForAllTysReqPreserveSynonyms ty
|
|
933
|
934
|
|
|
934
|
|
- sTvs = map synifyTyVarBndr tvs
|
|
|
935
|
+ sTvs = map (synifyTyVarBndr' noKindSigTvs) tvs
|
|
|
936
|
+ noKindSigTvs = noKindSigTyVars ty
|
|
935
|
937
|
|
|
936
|
938
|
-- Figure out what the type variable order would be inferred in the
|
|
937
|
939
|
-- absence of an explicit forall
|
|
938
|
|
- tvs' = orderedFVs (mkVarSet vs) [rho]
|
|
|
940
|
+ tvs' = orderedFVs boundTvs [rho]
|
|
939
|
941
|
in noLocA $
|
|
940
|
942
|
HsForAllTy
|
|
941
|
943
|
{ hst_tele = mkHsForAllVisTele noAnn sTvs
|
|
942
|
944
|
, hst_xforall = noExtField
|
|
943
|
|
- , hst_body = synifyType WithinType (tvs' ++ vs) rho
|
|
|
945
|
+ , hst_body = synifyType WithinType (extendVarSetList boundTvs tvs') rho
|
|
944
|
946
|
}
|
|
945
|
947
|
|
|
946
|
948
|
-- | Process a 'Type' which starts with an invisible @forall@ or a constraint
|
| ... |
... |
@@ -948,18 +950,18 @@ synifyVisForAllType vs ty = |
|
948
|
950
|
synifySigmaType
|
|
949
|
951
|
:: SynifyTypeState
|
|
950
|
952
|
-- ^ what to do with the 'forall'
|
|
951
|
|
- -> [TyVar]
|
|
952
|
|
- -- ^ free variables in the type to convert
|
|
|
953
|
+ -> TyVarSet
|
|
|
954
|
+ -- ^ bound type variables
|
|
953
|
955
|
-> Type
|
|
954
|
956
|
-- ^ the forall type to convert
|
|
955
|
957
|
-> LHsType GhcRn
|
|
956
|
|
-synifySigmaType s vs ty =
|
|
|
958
|
+synifySigmaType s boundTvs ty =
|
|
957
|
959
|
let (tvs, ctx, tau) = tcSplitSigmaTyPreserveSynonyms ty
|
|
958
|
960
|
sPhi =
|
|
959
|
961
|
HsQualTy
|
|
960
|
962
|
{ hst_ctxt = synifyCtx ctx
|
|
961
|
963
|
, hst_xqual = noExtField
|
|
962
|
|
- , hst_body = synifyType WithinType (tvs' ++ vs) tau
|
|
|
964
|
+ , hst_body = synifyType WithinType (extendVarSetList boundTvs tvs' ) tau
|
|
963
|
965
|
}
|
|
964
|
966
|
|
|
965
|
967
|
sTy =
|
| ... |
... |
@@ -969,49 +971,56 @@ synifySigmaType s vs ty = |
|
969
|
971
|
, hst_body = noLocA sPhi
|
|
970
|
972
|
}
|
|
971
|
973
|
|
|
972
|
|
- sTvs = map synifyTyVarBndr tvs
|
|
|
974
|
+ sTvs = map (synifyTyVarBndr' noKindSigTvs) tvs
|
|
|
975
|
+
|
|
|
976
|
+ noKindSigTvs = noKindSigTyVars ty
|
|
973
|
977
|
|
|
974
|
978
|
-- Figure out what the type variable order would be inferred in the
|
|
975
|
979
|
-- absence of an explicit forall
|
|
976
|
|
- tvs' = orderedFVs (mkVarSet vs) (ctx ++ [tau])
|
|
|
980
|
+ tvs' = orderedFVs boundTvs (ctx ++ [tau])
|
|
977
|
981
|
in case s of
|
|
978
|
|
- DeleteTopLevelQuantification -> synifyType ImplicitizeForAll (tvs' ++ vs) tau
|
|
|
982
|
+ DeleteTopLevelQuantification -> synifyType ImplicitizeForAll (extendVarSetList boundTvs tvs') tau
|
|
979
|
983
|
-- Put a forall in if there are any type variables
|
|
980
|
984
|
WithinType
|
|
981
|
985
|
| not (null tvs) -> noLocA sTy
|
|
982
|
986
|
| otherwise -> noLocA sPhi
|
|
983
|
|
- ImplicitizeForAll -> implicitForAll [] vs tvs ctx (synifyType WithinType) tau
|
|
|
987
|
+ ImplicitizeForAll -> implicitForAll boundTvs tvs ctx (synifyType WithinType) tau
|
|
984
|
988
|
|
|
985
|
|
--- | Put a forall in if there are any type variables which require
|
|
986
|
|
--- explicit kind annotations or if the inferred type variable order
|
|
987
|
|
--- would be different.
|
|
|
989
|
+-- | Use an explicit forall if there are any type variables which require
|
|
|
990
|
+-- explicit kind annotations or if the inferred type variable quantification
|
|
|
991
|
+-- order would be different.
|
|
988
|
992
|
implicitForAll
|
|
989
|
|
- :: [TyCon]
|
|
990
|
|
- -- ^ type constructors that determine their args kinds
|
|
991
|
|
- -> [TyVar]
|
|
992
|
|
- -- ^ free variables in the type to convert
|
|
|
993
|
+ :: TyVarSet
|
|
|
994
|
+ -- ^ bound type variables (e.g. bound from an outer scope)
|
|
993
|
995
|
-> [InvisTVBinder]
|
|
994
|
996
|
-- ^ type variable binders in the forall
|
|
995
|
997
|
-> ThetaType
|
|
996
|
998
|
-- ^ constraints right after the forall
|
|
997
|
|
- -> ([TyVar] -> Type -> LHsType GhcRn)
|
|
|
999
|
+ -> (TyVarSet -> Type -> LHsType GhcRn)
|
|
998
|
1000
|
-- ^ how to convert the inner type
|
|
999
|
1001
|
-> Type
|
|
1000
|
1002
|
-- ^ inner type
|
|
1001
|
1003
|
-> LHsType GhcRn
|
|
1002
|
|
-implicitForAll tycons vs tvs ctx synInner tau
|
|
1003
|
|
- | any (isHsKindedTyVar . unLoc) sTvs = noLocA sTy
|
|
1004
|
|
- | tvs' /= (binderVars tvs) = noLocA sTy
|
|
1005
|
|
- | otherwise = noLocA sPhi
|
|
|
1004
|
+implicitForAll boundTvs tvbs ctx synInner tau
|
|
|
1005
|
+ | any (isHsKindedTyVar . unLoc) sTvs
|
|
|
1006
|
+ -- Explicit forall: some type variable needs an explicit kind annotation.
|
|
|
1007
|
+ = noLocA sTy
|
|
|
1008
|
+ | tvs /= inferredFreeTvs
|
|
|
1009
|
+ -- Explicit forall: the inferred quantification order would be different.
|
|
|
1010
|
+ = noLocA sTy
|
|
|
1011
|
+ | otherwise
|
|
|
1012
|
+ -- Implicit forall.
|
|
|
1013
|
+ = noLocA sPhi
|
|
1006
|
1014
|
where
|
|
1007
|
|
- sRho = synInner (tvs' ++ vs) tau
|
|
|
1015
|
+ tvs = binderVars tvbs
|
|
|
1016
|
+ sRho = synInner (extendVarSetList boundTvs inferredFreeTvs) tau
|
|
1008
|
1017
|
sPhi
|
|
1009
|
1018
|
| null ctx = unLoc sRho
|
|
1010
|
1019
|
| otherwise =
|
|
1011
|
1020
|
HsQualTy
|
|
1012
|
1021
|
{ hst_ctxt = synifyCtx ctx
|
|
1013
|
1022
|
, hst_xqual = noExtField
|
|
1014
|
|
- , hst_body = synInner (tvs' ++ vs) tau
|
|
|
1023
|
+ , hst_body = sRho
|
|
1015
|
1024
|
}
|
|
1016
|
1025
|
sTy =
|
|
1017
|
1026
|
HsForAllTy
|
| ... |
... |
@@ -1020,84 +1029,129 @@ implicitForAll tycons vs tvs ctx synInner tau |
|
1020
|
1029
|
, hst_body = noLocA sPhi
|
|
1021
|
1030
|
}
|
|
1022
|
1031
|
|
|
1023
|
|
- no_kinds_needed = noKindTyVars tycons tau
|
|
1024
|
|
- sTvs = map (synifyTyVarBndr' no_kinds_needed) tvs
|
|
|
1032
|
+ no_kinds_needed = noKindSigTyVars tau
|
|
|
1033
|
+ sTvs = map (synifyTyVarBndr' no_kinds_needed) tvbs
|
|
1025
|
1034
|
|
|
1026
|
1035
|
-- Figure out what the type variable order would be inferred in the
|
|
1027
|
1036
|
-- absence of an explicit forall
|
|
1028
|
|
- tvs' = orderedFVs (mkVarSet vs) (ctx ++ [tau])
|
|
|
1037
|
+ inferredFreeTvs = orderedFVs boundTvs (ctx ++ [tau])
|
|
1029
|
1038
|
|
|
1030
|
|
--- | Find the set of type variables whose kind signatures can be properly
|
|
1031
|
|
--- inferred just from their uses in the type signature. This means the type
|
|
1032
|
|
--- variable to has at least one fully applied use @f x1 x2 ... xn@ where:
|
|
|
1039
|
+-- | Returns a subset of the free type variables of the given type whose kinds
|
|
|
1040
|
+-- can definitely be inferred from their occurrences in the type.
|
|
1033
|
1041
|
--
|
|
1034
|
|
--- * @f@ has a function kind where the arguments have the same kinds
|
|
1035
|
|
--- as @x1 x2 ... xn@.
|
|
|
1042
|
+-- This function is only a simple heuristic, which is used in order to avoid
|
|
|
1043
|
+-- needlessly cluttering Haddocks with explicit foralls that are not needed.
|
|
|
1044
|
+-- This function may return some type variables for which we aren't sure
|
|
|
1045
|
+-- (which will cause us to display the type with an explicit forall, just in
|
|
|
1046
|
+-- case).
|
|
1036
|
1047
|
--
|
|
1037
|
|
--- * @f@ has a function kind whose final return has lifted type kind
|
|
1038
|
|
-noKindTyVars
|
|
1039
|
|
- :: [TyCon]
|
|
1040
|
|
- -- ^ type constructors that determine their args kinds
|
|
1041
|
|
- -> Type
|
|
|
1048
|
+-- In the future, we hope to address the issue of whether to print a type with
|
|
|
1049
|
+-- an explicit forall by storing whether the user wrote the type with an
|
|
|
1050
|
+-- explicit forall in the first place (see GHC ticket #26271).
|
|
|
1051
|
+noKindSigTyVars
|
|
|
1052
|
+ :: Type
|
|
1042
|
1053
|
-- ^ type to inspect
|
|
1043
|
1054
|
-> VarSet
|
|
1044
|
|
- -- ^ set of variables whose kinds can be inferred from uses in the type
|
|
1045
|
|
-noKindTyVars _ (TyVarTy var)
|
|
1046
|
|
- | isLiftedTypeKind (tyVarKind var) = unitVarSet var
|
|
1047
|
|
-noKindTyVars ts ty
|
|
1048
|
|
- | (f, xs) <- splitAppTys ty
|
|
1049
|
|
- , not (null xs) =
|
|
1050
|
|
- let args = map (noKindTyVars ts) xs
|
|
1051
|
|
- func = case f of
|
|
1052
|
|
- TyVarTy var
|
|
1053
|
|
- | (xsKinds, outKind) <- splitFunTys (tyVarKind var)
|
|
1054
|
|
- , map scaledThing xsKinds `eqTypes` map typeKind xs
|
|
1055
|
|
- , isLiftedTypeKind outKind ->
|
|
1056
|
|
- unitVarSet var
|
|
1057
|
|
- TyConApp t ks
|
|
1058
|
|
- | t `elem` ts
|
|
1059
|
|
- , all noFreeVarsOfType ks ->
|
|
1060
|
|
- mkVarSet [v | TyVarTy v <- xs]
|
|
1061
|
|
- _ -> noKindTyVars ts f
|
|
1062
|
|
- in unionVarSets (func : args)
|
|
1063
|
|
-noKindTyVars ts (ForAllTy _ t) = noKindTyVars ts t
|
|
1064
|
|
-noKindTyVars ts (FunTy _ w t1 t2) =
|
|
1065
|
|
- noKindTyVars ts w
|
|
1066
|
|
- `unionVarSet` noKindTyVars ts t1
|
|
1067
|
|
- `unionVarSet` noKindTyVars ts t2
|
|
1068
|
|
-noKindTyVars ts (CastTy t _) = noKindTyVars ts t
|
|
1069
|
|
-noKindTyVars _ _ = emptyVarSet
|
|
1070
|
|
-
|
|
1071
|
|
-synifyMultArrow :: [TyVar] -> Mult -> HsMultAnn GhcRn
|
|
1072
|
|
-synifyMultArrow vs t = case t of
|
|
|
1055
|
+ -- ^ set of variables whose kinds can definitely be inferred from occurrences in the type
|
|
|
1056
|
+noKindSigTyVars ty
|
|
|
1057
|
+ | Just ty' <- coreView ty
|
|
|
1058
|
+ = noKindSigTyVars ty'
|
|
|
1059
|
+ -- In a TyConApp 'T ty_1 ... ty_n', if 'ty_i = tv' is a type variable and the
|
|
|
1060
|
+ -- i-th argument of the kind of 'T' is monomorphic, then the kind of 'tv'
|
|
|
1061
|
+ -- is fully determined by its occurrence in the TyConApp.
|
|
|
1062
|
+ | Just (tc, args) <- splitTyConApp_maybe ty
|
|
|
1063
|
+ , let (tcArgBndrs, _tcResKi) = splitPiTys (tyConKind tc)
|
|
|
1064
|
+ tcArgKis = map (\case { Named (Bndr b _) -> tyVarKind b; Anon (Scaled _ t) _ -> t}) tcArgBndrs
|
|
|
1065
|
+ = mono_tvs tcArgKis args `unionVarSet` (mapUnionVarSet noKindSigTyVars args)
|
|
|
1066
|
+ -- If we have 'f ty_1 ... ty_n' where 'f :: ki_1 -> ... -> ki_n -> Type'
|
|
|
1067
|
+ -- then we can infer the kind of 'f' from the kinds of its arguments.
|
|
|
1068
|
+ --
|
|
|
1069
|
+ -- This special case handles common examples involving functors, monads...
|
|
|
1070
|
+ -- with type signatures such as '(a -> b) -> (f a -> f b)'.
|
|
|
1071
|
+ | (TyVarTy fun, args) <- splitAppTys ty
|
|
|
1072
|
+ , not (null args)
|
|
|
1073
|
+ , (funArgKinds, funResKind) <- splitFunTys (tyVarKind fun)
|
|
|
1074
|
+ , map scaledThing funArgKinds `eqTypes` map typeKind args
|
|
|
1075
|
+ , isLiftedTypeKind funResKind
|
|
|
1076
|
+ = ( `extendVarSet` fun ) $ mapUnionVarSet noKindSigTyVars args
|
|
|
1077
|
+ where
|
|
|
1078
|
+ mono_tvs :: [Type] -> [Type] -> VarSet
|
|
|
1079
|
+ mono_tvs (tcArgKi:tcArgKis) (arg:args)
|
|
|
1080
|
+ | TyVarTy arg_tv <- arg
|
|
|
1081
|
+ , noFreeVarsOfType tcArgKi
|
|
|
1082
|
+ = ( `extendVarSet` arg_tv ) $ mono_tvs tcArgKis args
|
|
|
1083
|
+ | otherwise
|
|
|
1084
|
+ = mono_tvs tcArgKis args
|
|
|
1085
|
+ mono_tvs _ _ = emptyVarSet
|
|
|
1086
|
+noKindSigTyVars (ForAllTy _ t) = noKindSigTyVars t
|
|
|
1087
|
+noKindSigTyVars (CastTy t _) = noKindSigTyVars t
|
|
|
1088
|
+noKindSigTyVars _ = emptyVarSet
|
|
|
1089
|
+
|
|
|
1090
|
+synifyMultArrow :: TyVarSet -> Mult -> HsMultAnn GhcRn
|
|
|
1091
|
+synifyMultArrow boundTvs t = case t of
|
|
1073
|
1092
|
OneTy -> HsLinearAnn noExtField
|
|
1074
|
1093
|
ManyTy -> HsUnannotated noExtField
|
|
1075
|
|
- ty -> HsExplicitMult noExtField (synifyType WithinType vs ty)
|
|
|
1094
|
+ ty -> HsExplicitMult noExtField (synifyType WithinType boundTvs ty)
|
|
1076
|
1095
|
|
|
1077
|
|
-synifyMultRec :: [TyVar] -> Mult -> HsMultAnn GhcRn
|
|
1078
|
|
-synifyMultRec vs t = case t of
|
|
|
1096
|
+synifyMultRec :: TyVarSet -> Mult -> HsMultAnn GhcRn
|
|
|
1097
|
+synifyMultRec boundTvs t = case t of
|
|
1079
|
1098
|
OneTy -> HsUnannotated noExtField
|
|
1080
|
|
- ty -> HsExplicitMult noExtField (synifyType WithinType vs ty)
|
|
|
1099
|
+ ty -> HsExplicitMult noExtField (synifyType WithinType boundTvs ty)
|
|
1081
|
1100
|
|
|
1082
|
1101
|
synifyPatSynType :: PatSyn -> LHsType GhcRn
|
|
1083
|
1102
|
synifyPatSynType ps =
|
|
1084
|
|
- let (univ_tvs, req_theta, ex_tvs, prov_theta, arg_tys, res_ty) = patSynSigBndr ps
|
|
1085
|
|
- ts = maybeToList (tyConAppTyCon_maybe res_ty)
|
|
|
1103
|
+ let (univ_tvbs, req_theta, ex_tvbs, prov_theta, arg_tys, res_ty) = patSynSigBndr ps
|
|
|
1104
|
+
|
|
|
1105
|
+{- Recall that pattern synonyms have both "required" and "provided" constraints,
|
|
|
1106
|
+e.g.
|
|
|
1107
|
+
|
|
|
1108
|
+ pattern P :: forall a b c. req => forall e f g => prov => arg_ty1 -> ... -> res_ty
|
|
|
1109
|
+
|
|
|
1110
|
+Here:
|
|
|
1111
|
+
|
|
|
1112
|
+ a, b, c are universal type variables
|
|
|
1113
|
+ req are required constraints
|
|
1086
|
1114
|
|
|
1087
|
|
- -- HACK: a HsQualTy with theta = [unitTy] will be printed as "() =>",
|
|
1088
|
|
- -- i.e., an explicit empty context, which is what we need. This is not
|
|
1089
|
|
- -- possible by taking theta = [], as that will print no context at all
|
|
|
1115
|
+ e, f, g are existential type variables
|
|
|
1116
|
+ prov are provided constraints
|
|
|
1117
|
+
|
|
|
1118
|
+The first pair comes from the outside, while the second pair is obtained upon
|
|
|
1119
|
+a successful match on the pattern.
|
|
|
1120
|
+
|
|
|
1121
|
+Remarks:
|
|
|
1122
|
+
|
|
|
1123
|
+ 1. Both foralls are optional.
|
|
|
1124
|
+
|
|
|
1125
|
+ 2. If there is only one =>, we interpret the constraints as required.
|
|
|
1126
|
+ Thus, if we want an empty set of required constraints and a non-empty set
|
|
|
1127
|
+ of provided constraints, the type signature must be written like
|
|
|
1128
|
+
|
|
|
1129
|
+ () => prov => res_ty
|
|
|
1130
|
+-}
|
|
|
1131
|
+
|
|
|
1132
|
+
|
|
|
1133
|
+ -- Add an explicit "() => ..." when req_theta is empty but there are
|
|
|
1134
|
+ -- existential variables or provided constraints.
|
|
1090
|
1135
|
req_theta'
|
|
1091
|
1136
|
| null req_theta
|
|
1092
|
|
- , not (null prov_theta && null ex_tvs) =
|
|
|
1137
|
+ , not (null prov_theta && null ex_tvbs) =
|
|
1093
|
1138
|
[unitTy]
|
|
1094
|
1139
|
| otherwise = req_theta
|
|
|
1140
|
+ univ_tvs = mkVarSet $ binderVars univ_tvbs
|
|
|
1141
|
+ ex_tvs = mkVarSet $ binderVars ex_tvbs
|
|
|
1142
|
+
|
|
|
1143
|
+
|
|
1095
|
1144
|
in implicitForAll
|
|
1096
|
|
- ts
|
|
1097
|
|
- []
|
|
1098
|
|
- (univ_tvs ++ ex_tvs)
|
|
|
1145
|
+ ex_tvs -- consider the ex_tvs non-free, so that we don't quantify over them here
|
|
|
1146
|
+ univ_tvbs -- quantify only over the universals
|
|
1099
|
1147
|
req_theta'
|
|
1100
|
|
- (\vs -> implicitForAll ts vs [] prov_theta (synifyType WithinType))
|
|
|
1148
|
+ ( \_ ->
|
|
|
1149
|
+ implicitForAll
|
|
|
1150
|
+ univ_tvs -- the univ_tvs are already bound
|
|
|
1151
|
+ ex_tvbs -- quantify only over the existentials
|
|
|
1152
|
+ prov_theta
|
|
|
1153
|
+ (synifyType WithinType)
|
|
|
1154
|
+ )
|
|
1101
|
1155
|
(mkScaledFunTys arg_tys res_ty)
|
|
1102
|
1156
|
|
|
1103
|
1157
|
synifyTyLit :: TyLit -> HsTyLit GhcRn
|
| ... |
... |
@@ -1106,7 +1160,7 @@ synifyTyLit (StrTyLit s) = HsStrTy NoSourceText s |
|
1106
|
1160
|
synifyTyLit (CharTyLit c) = HsCharTy NoSourceText c
|
|
1107
|
1161
|
|
|
1108
|
1162
|
synifyKindSig :: Kind -> LHsKind GhcRn
|
|
1109
|
|
-synifyKindSig k = synifyType WithinType [] k
|
|
|
1163
|
+synifyKindSig k = synifyType WithinType emptyVarSet k
|
|
1110
|
1164
|
|
|
1111
|
1165
|
stripKindSig :: LHsType GhcRn -> LHsType GhcRn
|
|
1112
|
1166
|
stripKindSig (L _ (HsKindSig _ t _)) = t
|
| ... |
... |
@@ -1119,7 +1173,7 @@ synifyInstHead (vs, preds, cls, types) associated_families = |
|
1119
|
1173
|
, ihdTypes = map unLoc annot_ts
|
|
1120
|
1174
|
, ihdInstType =
|
|
1121
|
1175
|
ClassInst
|
|
1122
|
|
- { clsiCtx = map (unLoc . synifyType WithinType []) preds
|
|
|
1176
|
+ { clsiCtx = map (unLoc . synifyType WithinType emptyVarSet) preds
|
|
1123
|
1177
|
, clsiTyVars = synifyTyVars (tyConVisibleTyVars cls_tycon)
|
|
1124
|
1178
|
, clsiSigs = map synifyClsIdSig $ specialized_class_methods
|
|
1125
|
1179
|
, clsiAssocTys =
|
| ... |
... |
@@ -1132,7 +1186,7 @@ synifyInstHead (vs, preds, cls, types) associated_families = |
|
1132
|
1186
|
where
|
|
1133
|
1187
|
cls_tycon = classTyCon cls
|
|
1134
|
1188
|
ts = filterOutInvisibleTypes cls_tycon types
|
|
1135
|
|
- ts' = map (synifyType WithinType vs) ts
|
|
|
1189
|
+ ts' = map (synifyType WithinType $ mkVarSet vs) ts
|
|
1136
|
1190
|
annot_ts = zipWith3 annotHsType args_poly ts ts'
|
|
1137
|
1191
|
args_poly = tyConArgsPolyKinded cls_tycon
|
|
1138
|
1192
|
synifyClsIdSig = synifyIdSig ShowRuntimeRep DeleteTopLevelQuantification vs
|
| ... |
... |
@@ -1151,7 +1205,7 @@ synifyFamInst fi opaque = do |
|
1151
|
1205
|
where
|
|
1152
|
1206
|
ityp SynFamilyInst | opaque = return $ TypeInst Nothing
|
|
1153
|
1207
|
ityp SynFamilyInst =
|
|
1154
|
|
- return . TypeInst . Just . unLoc $ synifyType WithinType [] fam_rhs
|
|
|
1208
|
+ return . TypeInst . Just . unLoc $ synifyType WithinType emptyVarSet fam_rhs
|
|
1155
|
1209
|
ityp (DataFamilyInst c) =
|
|
1156
|
1210
|
DataInst <$> synifyTyCon HideRuntimeRep (Just $ famInstAxiom fi) c
|
|
1157
|
1211
|
fam_tc = famInstTyCon fi
|
| ... |
... |
@@ -1173,7 +1227,7 @@ synifyFamInst fi opaque = do |
|
1173
|
1227
|
fam_lhs
|
|
1174
|
1228
|
|
|
1175
|
1229
|
ts = filterOutInvisibleTypes fam_tc eta_expanded_lhs
|
|
1176
|
|
- synifyTypes = map (synifyType WithinType [])
|
|
|
1230
|
+ synifyTypes = map (synifyType WithinType emptyVarSet)
|
|
1177
|
1231
|
ts' = synifyTypes ts
|
|
1178
|
1232
|
annot_ts = zipWith3 annotHsType args_poly ts ts'
|
|
1179
|
1233
|
args_poly = tyConArgsPolyKinded fam_tc
|