Re: type specs not making it in to functions

2002-01-28 Thread Fergus Henderson

On 25-Jan-2002, Hal Daume III [EMAIL PROTECTED] wrote:
 consider the following definition:
 
  class C a where foo :: a - Int
  instance C Bool where foo _ = 5
 
 I can then say:
 
  bar :: C a = a - Int
  bar (x :: a) = foo (undefined :: a)
 
 But not:
 
  bar :: C a = a - Int
  bar x = foo (undefined :: a)
 
 because it tries to use a new scope for the type variable a and doesn't
 unify it with the one in the type spec.  Why not?

I don't know why it isn't the case in Haskell.
In Mercury we do allow type variables in function type declarations
to scope over explicit type qualifications in clause bodies.

One drawback with allowing this is that the type declaration may be
separated from the clause by an arbitrary amount of text,
e.g.
bar :: C a = a - Int  -- type var `a' introduced here...
foo :: Int
quux :: String

foo = 42
quux = hello world
bar x = foo (undefined :: a)-- ... and used here.

which means that this feature can be used in ways that have the
potential to make programs harder to read.

-- 
Fergus Henderson [EMAIL PROTECTED]  |  I have always known that the pursuit
The University of Melbourne |  of excellence is a lethal habit
WWW: http://www.cs.mu.oz.au/~fjh  | -- the last words of T. S. Garp.

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



RE: type specs not making it in to functions

2002-01-28 Thread Simon Peyton-Jones

|  I can then say:
|  
|   bar :: C a = a - Int
|   bar (x :: a) = foo (undefined :: a)
|  
|  But not:
|  
|   bar :: C a = a - Int
|   bar x = foo (undefined :: a)
|  
|  because it tries to use a new scope for the type variable a and 
|  doesn't unify it with the one in the type spec.  Why not?
| 
| I don't know why it isn't the case in Haskell.
| In Mercury we do allow type variables in function type 
| declarations to scope over explicit type qualifications in 
| clause bodies.

It's a deliberate design choice, but certainly only one of taste.
It seems a bit odd for a type signature (perhaps separated by a long
way from the definition) should bind a type variable that scopes over
the function body.  

Mark Shields and I are writing a paper about the scoped-type-variable
design in GHC; we'll discuss this point.

Simon

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



Re: type specs not making it in to functions

2002-01-25 Thread Ashley Yakeley

At 2002-01-25 14:00, Hal Daume III wrote:

 class D a where constMember :: Int
 instance D Int where constMember = 8

It seems ehre that there's no way to extract constMember for a
/particular/ class, since you can't tell it what a is supposed to
be.  So, instead, I do:

 class D a where constMember :: a - Int -- should be independent of arg
 instance D Int where constMember _ = 8

(which brings me to my use of undefined::a).  But is there a
better/preferred way to do this?

Unfortunately this seems to be the preferred way to do this. For
instance, from the Prelude:

  class  (RealFrac a, Floating a) = RealFloat a  where
floatRadix   :: a - Integer
floatDigits  :: a - Int

It's not the _best_ way to do it, in my opinion. I do this:

  data Type a = Type

  class D a where
constMember :: Type a - Int

  instance D Int where
constMember Type = 8

  intConst = constMember (Type :: Type Int)

See
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/jvm-bridge/sourc
e/Haskell/Type.hs?rev=HEADcontent-type=text/plain.

Why is this better?

1. It's absolutely clear to the user of the class that constMember
depends only on the type.

2. It prevents the user from putting in an inappropriate back channel
in the member for some instance of the class.

3. It avoids use of 'undefined', which is just plain ugly. After all,
intuitively everything is defined.

--
Ashley Yakeley, Seattle WA
Almost empty page: http://semantic.org/


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell