[commit: ghc] master: Better documentation of -XConstrainedClassMethods (c4dd4ae)

git at git.haskell.org git at git.haskell.org
Thu Apr 28 16:32:37 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/c4dd4ae71549d9275b8b827af0bfaac85ef7ed4a/ghc

>---------------------------------------------------------------

commit c4dd4ae71549d9275b8b827af0bfaac85ef7ed4a
Author: Simon Peyton Jones <simonpj at microsoft.com>
Date:   Thu Apr 28 15:56:09 2016 +0100

    Better documentation of -XConstrainedClassMethods


>---------------------------------------------------------------

c4dd4ae71549d9275b8b827af0bfaac85ef7ed4a
 docs/users_guide/glasgow_exts.rst | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/docs/users_guide/glasgow_exts.rst b/docs/users_guide/glasgow_exts.rst
index ef66140..fe12568 100644
--- a/docs/users_guide/glasgow_exts.rst
+++ b/docs/users_guide/glasgow_exts.rst
@@ -4551,23 +4551,31 @@ context.
 
 .. _class-method-types:
 
-Class method types
-~~~~~~~~~~~~~~~~~~
+Constrained class method types
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 .. ghc-flag:: -XConstrainedClassMethods
 
-    Allows the definition of further constraints on individual class methods.
-
-Haskell 98 prohibits class method types to mention constraints on the
-class type variable, thus: ::
-
-      class Seq s a where
-        fromList :: [a] -> s a
-        elem     :: Eq a => a -> s a -> Bool
+@ -4562,7 +4562,20 @@ class type variable, thus: ::
 
 The type of ``elem`` is illegal in Haskell 98, because it contains the
 constraint ``Eq a``, which constrains only the class type variable (in
 this case ``a``).
+this case ``a``).  More precisely, a constraint in a class method signature is rejected if
+
+- The constraint mentions at least one type variable.  So this is allowed: ::
+
+     class C a where
+       op1 :: HasCallStack => a -> a
+       op2 :: (?x::Int) => Int -> a
+
+- All of the type variables mentioned are bound by the class declaration, and none is locally quantified.  Examples: ::
+
+     class C a where
+       op3 :: Eq a => a -> a    -- Rejected: constrains class variable only
+       op4 :: D b => a -> b     -- Accepted: constrains a locally-quantified varible `b`
+       op5 :: D (a,b) => a -> b -- Accepted: constrains a locally-quantified varible `b`
+
 
 GHC lifts this restriction with language extension
 :ghc-flag:`-XConstrainedClassMethods`. The restriction is a pretty stupid one in



More information about the ghc-commits mailing list