diff options
| author | Simon Peyton Jones <simonpj@microsoft.com> | 2014-10-29 15:07:24 +0000 |
|---|---|---|
| committer | Simon Peyton Jones <simonpj@microsoft.com> | 2014-10-29 15:07:24 +0000 |
| commit | 3c17388f3a23dbed0a69c6e385d57bac1a91f83e (patch) | |
| tree | a7052be8ac08433afc12e6268716e62fa68455f7 | |
| parent | dbb36b5210c20f81351d941e9cc2641732c45e9a (diff) | |
| download | haskell-3c17388f3a23dbed0a69c6e385d57bac1a91f83e.tar.gz | |
Final wibbles
| -rw-r--r-- | compiler/stranal/DmdAnal.lhs | 3 | ||||
| -rw-r--r-- | compiler/typecheck/TcSimplify.lhs | 21 | ||||
| -rw-r--r-- | compiler/types/Coercion.lhs | 36 | ||||
| -rw-r--r-- | compiler/types/FamInstEnv.lhs | 30 | ||||
| -rw-r--r-- | testsuite/tests/indexed-types/should_compile/PushedInAsGivens.hs | 9 | ||||
| -rw-r--r-- | testsuite/tests/indexed-types/should_compile/all.T | 2 | ||||
| -rw-r--r-- | testsuite/tests/indexed-types/should_fail/ExtraTcsUntch.hs | 14 | ||||
| -rw-r--r-- | testsuite/tests/indexed-types/should_fail/ExtraTcsUntch.stderr | 22 | ||||
| -rw-r--r-- | testsuite/tests/indexed-types/should_fail/T1897b.stderr | 8 | ||||
| -rw-r--r-- | testsuite/tests/indexed-types/should_fail/T4093a.hs | 4 | ||||
| -rw-r--r-- | testsuite/tests/indexed-types/should_fail/T4093a.stderr | 17 | ||||
| -rw-r--r-- | testsuite/tests/simplCore/should_compile/T3772.stdout | 24 |
12 files changed, 118 insertions, 72 deletions
diff --git a/compiler/stranal/DmdAnal.lhs b/compiler/stranal/DmdAnal.lhs index 5cb2655afd..244aa5b0ee 100644 --- a/compiler/stranal/DmdAnal.lhs +++ b/compiler/stranal/DmdAnal.lhs @@ -1089,7 +1089,8 @@ extendSigsWithLam env id findBndrDmd :: AnalEnv -> Bool -> DmdType -> Id -> (DmdType, Demand) -- See Note [Trimming a demand to a type] in Demand.lhs findBndrDmd env arg_of_dfun dmd_ty id - = (dmd_ty', dmd') + = -- pprTrace "findBndrDmd" (vcat [ ppr id, ppr id_ty, ppr starting_dmd, ppr (findTypeShape fam_envs id_ty), ppr dmd' ]) + (dmd_ty', dmd') where dmd' = zapDemand (ae_dflags env) $ strictify $ diff --git a/compiler/typecheck/TcSimplify.lhs b/compiler/typecheck/TcSimplify.lhs index 197ed975c7..6eae445b97 100644 --- a/compiler/typecheck/TcSimplify.lhs +++ b/compiler/typecheck/TcSimplify.lhs @@ -21,7 +21,8 @@ import TcSMonad as TcS import TcInteract import Kind ( isKind, isSubKind, defaultKind_maybe ) import Inst -import Type ( classifyPredType, PredTree(..), getClassPredTys_maybe ) +import Type ( classifyPredType, isIPClass, PredTree(..), getClassPredTys_maybe ) +import TyCon ( isSynFamilyTyCon ) import Class ( Class ) import Id ( idType ) import Var @@ -431,8 +432,22 @@ growThetaTyVars theta tvs quantifyPred :: TyVarSet -- Quantifying over these -> PredType -> Bool -- True <=> quantify over this wanted quantifyPred qtvs pred - | isIPPred pred = True -- See note [Inheriting implicit parameters] - | otherwise = tyVarsOfType pred `intersectsVarSet` qtvs + = case classifyPredType pred of + ClassPred cls tys + | isIPClass cls -> True -- See note [Inheriting implicit parameters] + | otherwise -> tyVarsOfTypes tys `intersectsVarSet` qtvs + EqPred ty1 ty2 -> quant_fun ty1 || quant_fun ty2 + IrredPred ty -> tyVarsOfType ty `intersectsVarSet` qtvs + TuplePred {} -> False + where + -- Only quantify over (F tys ~ ty) if tys mentions a quantiifed variable + -- In particular, quanitifying over (F Int ~ ty) is a bit like quantifying + -- over (Eq Int); the instance should kick in right here + quant_fun ty + = case tcSplitTyConApp_maybe ty of + Just (tc, tys) | isSynFamilyTyCon tc + -> tyVarsOfTypes tys `intersectsVarSet` qtvs + _ -> False \end{code} Note [Inheriting implicit parameters] diff --git a/compiler/types/Coercion.lhs b/compiler/types/Coercion.lhs index 36eb711bdc..dc0a7d0d45 100644 --- a/compiler/types/Coercion.lhs +++ b/compiler/types/Coercion.lhs @@ -844,7 +844,7 @@ mkSubCo: Requires a nominal input coercion and always produces a representational output. This is used when you (the programmer) are sure you know exactly that role you have and what you want. -setRole_maybe: This function takes both the input role and the output role +downgradeRole_maybe: This function takes both the input role and the output role as parameters. (The *output* role comes first!) It can only *downgrade* a role -- that is, change it from N to R or P, or from R to P. This one-way behavior is why there is the "_maybe". If an upgrade is requested, this @@ -853,10 +853,10 @@ coercion, but you're not sure (as you're writing the code) of which roles are involved. This function could have been written using coercionRole to ascertain the role -of the input. But, that function is recursive, and the caller of setRole_maybe +of the input. But, that function is recursive, and the caller of downgradeRole_maybe often knows the input role. So, this is more efficient. -downgradeRole: This is just like setRole_maybe, but it panics if the conversion +downgradeRole: This is just like downgradeRole_maybe, but it panics if the conversion isn't a downgrade. setNominalRole_maybe: This is the only function that can *upgrade* a coercion. The result @@ -880,7 +880,7 @@ API, as he was decomposing Core casts. The Core casts use representational coerc as they must, but his use case required nominal coercions (he was building a GADT). So, that's why this function is exported from this module. -One might ask: shouldn't setRole_maybe just use setNominalRole_maybe as appropriate? +One might ask: shouldn't downgradeRole_maybe just use setNominalRole_maybe as appropriate? I (Richard E.) have decided not to do this, because upgrading a role is bizarre and a caller should have to ask for this behavior explicitly. @@ -1081,15 +1081,15 @@ mkSubCo co = ASSERT2( coercionRole co == Nominal, ppr co <+> ppr (coercionRole c SubCo co -- only *downgrades* a role. See Note [Role twiddling functions] -setRole_maybe :: Role -- desired role - -> Role -- current role - -> Coercion -> Maybe Coercion -setRole_maybe Representational Nominal = Just . mkSubCo -setRole_maybe Nominal Representational = const Nothing -setRole_maybe Phantom Phantom = Just -setRole_maybe Phantom _ = Just . mkPhantomCo -setRole_maybe _ Phantom = const Nothing -setRole_maybe _ _ = Just +downgradeRole_maybe :: Role -- desired role + -> Role -- current role + -> Coercion -> Maybe Coercion +downgradeRole_maybe Representational Nominal co = Just (mkSubCo co) +downgradeRole_maybe Nominal Representational _ = Nothing +downgradeRole_maybe Phantom Phantom co = Just co +downgradeRole_maybe Phantom _ co = Just (mkPhantomCo co) +downgradeRole_maybe _ Phantom _ = Nothing +downgradeRole_maybe _ _ co = Just co -- panics if the requested conversion is not a downgrade. -- See also Note [Role twiddling functions] @@ -1097,7 +1097,7 @@ downgradeRole :: Role -- desired role -> Role -- current role -> Coercion -> Coercion downgradeRole r1 r2 co - = case setRole_maybe r1 r2 co of + = case downgradeRole_maybe r1 r2 co of Just co' -> co' Nothing -> pprPanic "downgradeRole" (ppr co) @@ -1158,8 +1158,9 @@ nthRole Phantom _ _ = Phantom nthRole Representational tc n = (tyConRolesX Representational tc) !! n --- is one role "less" than another? ltRole :: Role -> Role -> Bool +-- Is one role "less" than another? +-- Nominal < Representational < Phantom ltRole Phantom _ = False ltRole Representational Phantom = True ltRole Representational _ = False @@ -1619,17 +1620,16 @@ failing for reason 2) is fine. matchAxiom is trying to find a set of coercions that match, but it may fail, and this is healthy behavior. Bottom line: if you find that liftCoSubst is doing weird things (like leaving out-of-scope variables lying around), disable coercion optimization (bypassing matchAxiom) -and use downgradeRole instead of setRole_maybe. The panic will then happen, +and use downgradeRole instead of downgradeRole_maybe. The panic will then happen, and you may learn something useful. \begin{code} - liftCoSubstTyVar :: LiftCoSubst -> Role -> TyVar -> Maybe Coercion liftCoSubstTyVar (LCS _ cenv) r tv = do { co <- lookupVarEnv cenv tv ; let co_role = coercionRole co -- could theoretically take this as -- a parameter, but painful - ; setRole_maybe r co_role co } -- see Note [liftCoSubstTyVar] + ; downgradeRole_maybe r co_role co } -- see Note [liftCoSubstTyVar] liftCoSubstTyVarBndr :: LiftCoSubst -> TyVar -> (LiftCoSubst, TyVar) liftCoSubstTyVarBndr subst@(LCS in_scope cenv) old_var diff --git a/compiler/types/FamInstEnv.lhs b/compiler/types/FamInstEnv.lhs index 894b94993d..bc21e2e1d7 100644 --- a/compiler/types/FamInstEnv.lhs +++ b/compiler/types/FamInstEnv.lhs @@ -790,19 +790,33 @@ The lookupFamInstEnv function does a nice job for *open* type families, but we also need to handle closed ones when normalising a type: \begin{code} -reduceTyFamApp_maybe :: FamInstEnvs -> Role -> TyCon -> [Type] -> Maybe (Coercion, Type) --- Attempt to do a *one-step* reduction of a type-synonym-family application --- (i.e. is a no-op on data families) +reduceTyFamApp_maybe :: FamInstEnvs + -> Role -- Desired role of result coercion + -> TyCon -> [Type] + -> Maybe (Coercion, Type) +-- Attempt to do a *one-step* reduction of a type-family application +-- but *not* newtypes +-- Works on type-synonym families always; data-families only if +-- the role we seek is representational -- It first normalises the type arguments, wrt functions but *not* newtypes, --- to be sure that nested calls like --- F (G Int) --- are correctly reduced +-- to be sure that nested calls like +-- F (G Int) +-- are correctly reduced -- -- The TyCon can be oversaturated. -- Works on both open and closed families reduceTyFamApp_maybe envs role tc tys - | isOpenSynFamilyTyCon tc + | Phantom <- role + = Nothing + + | case role of + Representational -> isOpenFamilyTyCon tc + _ -> isOpenSynFamilyTyCon tc + -- If we seek a representational coercion + -- (e.g. the call in topNormaliseType_maybe) then we can + -- unwrap data families as well as type-synonym families; + -- otherwise only type-synonym families , [FamInstMatch { fim_instance = fam_inst , fim_tys = inst_tys }] <- lookupFamInstEnv envs tc ntys = let ax = famInstAxiom fam_inst @@ -929,6 +943,7 @@ topNormaliseType_maybe env ty --------------- normaliseTcApp :: FamInstEnvs -> Role -> TyCon -> [Type] -> (Coercion, Type) +-- See comments on normaliseType for the arguments of this function normaliseTcApp env role tc tys | isTypeSynonymTyCon tc , (co1, ntys) <- normaliseTcArgs env role tc tys @@ -965,6 +980,7 @@ normaliseType :: FamInstEnvs -- environment with family instances -> (Coercion, Type) -- (coercion,new type), where -- co :: old-type ~ new_type -- Normalise the input type, by eliminating *all* type-function redexes +-- but *not* newtypes (which are visible to the programmer) -- Returns with Refl if nothing happens -- Try to not to disturb type syonyms if possible diff --git a/testsuite/tests/indexed-types/should_compile/PushedInAsGivens.hs b/testsuite/tests/indexed-types/should_compile/PushedInAsGivens.hs index 0117b81d47..063f7021cf 100644 --- a/testsuite/tests/indexed-types/should_compile/PushedInAsGivens.hs +++ b/testsuite/tests/indexed-types/should_compile/PushedInAsGivens.hs @@ -8,7 +8,7 @@ type family F a bar y = let foo :: (F Int ~ [a]) => a -> Int
foo x = length [x,y]
- in (y,foo y)
+ in (y,foo y)
-- This example demonstrates why we need to push in
@@ -20,5 +20,12 @@ bar y = let foo :: (F Int ~ [a]) => a -> Int -- Given/Solved, it will be discarded when we meet the given (F Int ~ [a]) and
-- we will not be able to solve the implication constraint.
+-- Oct 14: actually this example is _really_ strange, and doesn't illustrate
+-- the real issue in Trac #4935, for which there is a separate test
+--
+-- The example here requires us to infer a type
+-- bar :: F Int ~ [a] => ...
+-- which is a strange type to quantify over; better to complain about
+-- having no instance for F Int.
diff --git a/testsuite/tests/indexed-types/should_compile/all.T b/testsuite/tests/indexed-types/should_compile/all.T index ff45df2c83..58fc964879 100644 --- a/testsuite/tests/indexed-types/should_compile/all.T +++ b/testsuite/tests/indexed-types/should_compile/all.T @@ -170,7 +170,7 @@ test('T4981-V2', normal, compile, ['']) test('T4981-V3', normal, compile, ['']) test('T5002', normal, compile, ['']) -test('PushedInAsGivens', normal, compile, ['']) +test('PushedInAsGivens', normal, compile_fail, ['']) # Superclass equalities test('T4338', normal, compile, ['']) diff --git a/testsuite/tests/indexed-types/should_fail/ExtraTcsUntch.hs b/testsuite/tests/indexed-types/should_fail/ExtraTcsUntch.hs index cce5aa3cd1..62798fad39 100644 --- a/testsuite/tests/indexed-types/should_fail/ExtraTcsUntch.hs +++ b/testsuite/tests/indexed-types/should_fail/ExtraTcsUntch.hs @@ -48,14 +48,14 @@ f x = From g2 we get [W] (forall c. 0 => F Int ~ [[alpha]] /\ C beta [c]) (g2 is not generalised; the forall comes from the TEx pattern) -Floating we get - F Int ~ [beta], F Int ~ [[alpha]], alpha ~ alpha', forall c. C beta [c] +approximateWC then gives the candidate constraints to quantify + F Int ~ [beta], F Int ~ [[alpha']] -= { alpha := alpha' } - beta ~ [alpha'], F Int ~ [[alpha']], forall c. C beta [c] +(alpha' is the promoted version of alpha) -= { beta := [alpha'] - F Int ~ [[alpha']], forall c. C [alpha'] [c] +Now decide inferred sig for f :: F Int ~ [beta] => beta -> blah +since beta is mentioned in tau-type for f but alpha' is not -= F Int ~ [[alpha']], forall c. (C [alpha'] [c], alpha' ~ c) +Perhaps this is a stupid constraint to generalise over (we don't +generalise over (C Int). -}
\ No newline at end of file diff --git a/testsuite/tests/indexed-types/should_fail/ExtraTcsUntch.stderr b/testsuite/tests/indexed-types/should_fail/ExtraTcsUntch.stderr index 3e34058def..13f9911e6c 100644 --- a/testsuite/tests/indexed-types/should_fail/ExtraTcsUntch.stderr +++ b/testsuite/tests/indexed-types/should_fail/ExtraTcsUntch.stderr @@ -1,10 +1,16 @@ -ExtraTcsUntch.hs:24:53: - Could not deduce (C [t] [a]) arising from a use of ‘op’ - from the context (F Int ~ [[t]]) - bound by the inferred type of - f :: (F Int ~ [[t]]) => [t] -> ((), ((), ())) - at ExtraTcsUntch.hs:(21,1)-(25,29) - In the expression: op x [y] +ExtraTcsUntch.hs:23:18: + Couldn't match expected type ‘F Int’ with actual type ‘[t]’ + Relevant bindings include + x :: t (bound at ExtraTcsUntch.hs:21:3) + f :: t -> ((), ((), ())) (bound at ExtraTcsUntch.hs:21:1) + In the first argument of ‘h’, namely ‘[x]’ + In the expression: h [x] + In an equation for ‘g1’: g1 _ = h [x] + +ExtraTcsUntch.hs:25:38: + Couldn't match expected type ‘F Int’ with actual type ‘[[t0]]’ + The type variable ‘t0’ is ambiguous + In the first argument of ‘h’, namely ‘[[undefined]]’ + In the expression: h [[undefined]] In the expression: (h [[undefined]], op x [y]) - In a case alternative: TEx y -> (h [[undefined]], op x [y]) diff --git a/testsuite/tests/indexed-types/should_fail/T1897b.stderr b/testsuite/tests/indexed-types/should_fail/T1897b.stderr index cb2b99cddf..936aa26924 100644 --- a/testsuite/tests/indexed-types/should_fail/T1897b.stderr +++ b/testsuite/tests/indexed-types/should_fail/T1897b.stderr @@ -3,8 +3,10 @@ T1897b.hs:16:1: Couldn't match type ‘Depend a’ with ‘Depend a0’ NB: ‘Depend’ is a type function, and may not be injective The type variable ‘a0’ is ambiguous - Expected type: [Depend a] -> Bool - Actual type: [Depend a0] -> Bool + Expected type: t (Depend a) -> Bool + Actual type: t (Depend a0) -> Bool When checking that ‘isValid’ has the inferred type - isValid :: forall a. Bug a => [Depend a] -> Bool + isValid :: forall a (t :: * -> *). + (Foldable t, Bug a) => + t (Depend a) -> Bool Probable cause: the inferred type is ambiguous diff --git a/testsuite/tests/indexed-types/should_fail/T4093a.hs b/testsuite/tests/indexed-types/should_fail/T4093a.hs index 21d16b3113..746f996d57 100644 --- a/testsuite/tests/indexed-types/should_fail/T4093a.hs +++ b/testsuite/tests/indexed-types/should_fail/T4093a.hs @@ -32,4 +32,8 @@ hang = Just () [W] fmv2 ~ Maybe e
[W] fmv2 ~ Maybe ee
+Now the question is whether we get a derived equality e ~ ee. Currently
+we don't, but we easily could. But then we'd need to be careful not to
+report insoluble Int ~ Bool if we had
+ F a ~ Int, F a ~ Bool
-}
\ No newline at end of file diff --git a/testsuite/tests/indexed-types/should_fail/T4093a.stderr b/testsuite/tests/indexed-types/should_fail/T4093a.stderr index 90b8a592f6..076041be32 100644 --- a/testsuite/tests/indexed-types/should_fail/T4093a.stderr +++ b/testsuite/tests/indexed-types/should_fail/T4093a.stderr @@ -1,14 +1,11 @@ -T4093a.hs:8:8: - Could not deduce (e ~ ()) +T4093a.hs:7:9: + Could not deduce (Foo e0 ~ Maybe e0) from the context (Foo e ~ Maybe e) bound by the type signature for hang :: (Foo e ~ Maybe e) => Foo e at T4093a.hs:7:9-34 - ‘e’ is a rigid type variable bound by - the type signature for hang :: (Foo e ~ Maybe e) => Foo e - at T4093a.hs:7:9 - Expected type: Foo e - Actual type: Maybe () - Relevant bindings include hang :: Foo e (bound at T4093a.hs:8:1) - In the expression: Just () - In an equation for ‘hang’: hang = Just () + The type variable ‘e0’ is ambiguous + In the ambiguity check for: forall e. (Foo e ~ Maybe e) => Foo e + To defer the ambiguity check to use sites, enable AllowAmbiguousTypes + In the type signature for ‘hang’: + hang :: (Foo e ~ Maybe e) => Foo e diff --git a/testsuite/tests/simplCore/should_compile/T3772.stdout b/testsuite/tests/simplCore/should_compile/T3772.stdout index e2ca0851b9..6609024183 100644 --- a/testsuite/tests/simplCore/should_compile/T3772.stdout +++ b/testsuite/tests/simplCore/should_compile/T3772.stdout @@ -3,14 +3,13 @@ Result size of Tidy Core = {terms: 26, types: 11, coercions: 0} Rec { -xs :: GHC.Prim.Int# -> () -[GblId, Arity=1, Caf=NoCafRefs, Str=DmdType <L,U>] -xs = - \ (m :: GHC.Prim.Int#) -> - case GHC.Prim.tagToEnum# @ Bool (GHC.Prim.<=# m 1) - of _ [Occ=Dead] { - False -> xs (GHC.Prim.-# m 1); - True -> GHC.Tuple.() +$wxs :: GHC.Prim.Int# -> () +[GblId, Arity=1, Caf=NoCafRefs, Str=DmdType <S,1*U>] +$wxs = + \ (ww :: GHC.Prim.Int#) -> + case ww of ds1 { + __DEFAULT -> $wxs (GHC.Prim.-# ds1 1); + 1 -> GHC.Tuple.() } end Rec } @@ -18,11 +17,10 @@ foo [InlPrag=NOINLINE] :: Int -> () [GblId, Arity=1, Caf=NoCafRefs, Str=DmdType <S,1*U(U)>] foo = \ (n :: Int) -> - case n of _ [Occ=Dead] { GHC.Types.I# n# -> - case GHC.Prim.tagToEnum# @ Bool (GHC.Prim.<=# n# 0) - of _ [Occ=Dead] { - False -> xs n#; - True -> GHC.Tuple.() + case n of _ [Occ=Dead] { GHC.Types.I# y -> + case GHC.Prim.tagToEnum# @ Bool (GHC.Prim.<# 0 y) of _ [Occ=Dead] { + False -> GHC.Tuple.(); + True -> $wxs y } } |
