summaryrefslogtreecommitdiff
path: root/compiler
diff options
context:
space:
mode:
authorThomas Winant <thomas.winant@cs.kuleuven.be>2015-08-03 14:57:40 +0200
committerBen Gamari <ben@smart-cactus.org>2015-08-03 14:58:21 +0200
commitd9d2102ea7f6da1bc3a69fa469b89ea843cb8b02 (patch)
tree82b0177bdb0f2696015b225177ba54eac322fd16 /compiler
parent697079f118197931e7a8c0768e99bf60be4150fd (diff)
downloadhaskell-d9d2102ea7f6da1bc3a69fa469b89ea843cb8b02.tar.gz
Support wild cards in data/type family instances
Handle anonymous wild cards in type or data family instance declarations like unnamed type variables. For instance (pun intented): type family F (a :: *) (b :: *) :: * type instance F Int _ = Int Is now the same as: type family F (a :: *) (b :: *) :: * type instance F Int x = Int Note that unlike wild cards in partial type signatures, no errors (or warnings with -XPartialTypeSignatures) are generated for these wild cards, as there is nothing interesting to report to the user, i.e. the inferred kind. Only anonymous wild cards are supported here, named and extra-constraints wild card are not. Test Plan: pass new tests Reviewers: goldfire, austin, simonpj, bgamari Reviewed By: simonpj, bgamari Subscribers: goldfire, thomie Differential Revision: https://phabricator.haskell.org/D1092 GHC Trac Issues: #3699, #10586
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rename/RnSource.hs9
-rw-r--r--compiler/typecheck/TcTyClsDecls.hs35
2 files changed, 41 insertions, 3 deletions
diff --git a/compiler/rename/RnSource.hs b/compiler/rename/RnSource.hs
index 8396e84b25..f6a300763f 100644
--- a/compiler/rename/RnSource.hs
+++ b/compiler/rename/RnSource.hs
@@ -550,12 +550,19 @@ rnFamInstDecl doc mb_cls tycon pats payload rnPayload
; let all_fvs = fvs `addOneFV` unLoc tycon'
+ awcs = concatMap collectAnonymousWildCardNames pats'
; return (tycon',
HsWB { hswb_cts = pats', hswb_kvs = kv_names,
- hswb_tvs = tv_names, hswb_wcs = [] },
+ hswb_tvs = tv_names, hswb_wcs = awcs },
payload',
all_fvs) }
-- type instance => use, hence addOneFV
+ where
+ collectAnonymousWildCardNames ty
+ = [ wildCardName wc
+ | L _ wc <- snd (collectWildCards ty)
+ , isAnonWildCard wc ]
+
rnTyFamInstDecl :: Maybe (Name, [Name])
-> TyFamInstDecl RdrName
diff --git a/compiler/typecheck/TcTyClsDecls.hs b/compiler/typecheck/TcTyClsDecls.hs
index 2c18d5e701..3750be8d6b 100644
--- a/compiler/typecheck/TcTyClsDecls.hs
+++ b/compiler/typecheck/TcTyClsDecls.hs
@@ -1009,7 +1009,8 @@ tc_fam_ty_pats :: FamTyConShape
-- (and, if C is poly-kinded, so will its kind parameter).
tc_fam_ty_pats (name, arity, kind)
- (HsWB { hswb_cts = arg_pats, hswb_kvs = kvars, hswb_tvs = tvars })
+ (HsWB { hswb_cts = arg_pats, hswb_kvs = kvars
+ , hswb_tvs = tvars, hswb_wcs = wcs })
kind_checker
= do { let (fam_kvs, fam_body) = splitForAllTys kind
@@ -1029,8 +1030,11 @@ tc_fam_ty_pats (name, arity, kind)
; let (arg_kinds, res_kind)
= splitKindFunTysN (length arg_pats) $
substKiWith fam_kvs fam_arg_kinds fam_body
+ -- Treat (anonymous) wild cards as type variables without a name.
+ -- See Note [Wild cards in family instances]
+ anon_tvs = [L (nameSrcSpan wc) (UserTyVar wc) | wc <- wcs]
hs_tvs = HsQTvs { hsq_kvs = kvars
- , hsq_tvs = userHsTyVarBndrs loc tvars }
+ , hsq_tvs = anon_tvs ++ userHsTyVarBndrs loc tvars }
-- Kind-check and quantify
-- See Note [Quantifying over family patterns]
@@ -1114,6 +1118,33 @@ none. The role of the kind signature (a :: Maybe k) is to add a constraint
that 'a' must have that kind, and to bring 'k' into scope.
+Note [Wild cards in family instances]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Wild cards can be used in type/data family instance declarations to indicate
+that the name of a type variable doesn't matter. Each wild card will be
+replaced with a new unique type variable. For instance:
+
+ type family F a b :: *
+ type instance F Int _ = Int
+
+is the same as
+
+ type family F a b :: *
+ type instance F Int b = Int
+
+This is implemented as follows: during renaming anonymous wild cards are given
+freshly generated names. These names are collected after renaming
+(rnFamInstDecl) and used to make new type variables during type checking
+(tc_fam_ty_pats). One should not confuse these wild cards with the ones from
+partial type signatures. The latter generate fresh meta-variables whereas the
+former generate fresh skolems.
+
+Named and extra-constraints wild cards are not supported in type/data family
+instance declarations.
+
+Relevant tickets: #3699 and #10586.
+
************************************************************************
* *
Data types