"class []" proposal Re: [Haskell-cafe] One thought: Num to 0as ? to list?

Brian Hulley brianh at metamilk.com
Tue Aug 22 16:30:54 EDT 2006

Bulat Ziganshin wrote:
> Hello Brian,
> Tuesday, August 22, 2006, 9:35:21 PM, you wrote:
>> I feel "if it ain't broken don't fix it", and not only is the
>> existing syntax not broken, it's already (imho) absolutely perfect
>> in it's clarity and consistency
> it's because you not programmed a lot with type classes. if you start,
> you will soon realize that type signatures with classes are just
> unreadable. just look at sources of my streams library

    copyStream :: (BlockStream h1, BlockStream h2, Integral size)
                   => h1 -> h2 -> size -> IO ()

Here is another possible syntax, which wouldn't conflict with the existing 
syntax but could be used as a sugar:

    copyStream :: {BlockStream} h1 -> {BlockStream} h2 -> {Integral} size -> 
IO ()

The type variables could be optional if they are distinct (or not needed in 
the body) so you could write:

    copyStream :: {BlockStream} -> {BlockStream} -> {Integral} -> IO ()

The {} is needed to distinguish the use of classes from that of types, and 
also allows more than one constraint eg:

    foo :: (Num a, Bar a) => a -> a
    foo :: {Num, Bar} a -> a

(the constraint(s) just get written before the first occurrence of a given 
type variable), and it's even possible to represent constraints between type 
variables eg:

    foo :: Collection c a => a -> c
    foo :: {Collection c} a -> c

It's slightly more difficult to know how to represent the following:

    foo :: Collection c a => c -> c


    foo :: {Collection * a} c -> c

where the * represents the location of the variable in the constraint.

Even a rank 2 type like:

    forall a. Num a => (forall b. (Coll b a, Ord b) => b a -> ()) -> ()

    (forall b. {Coll * a, Ord}b    {Num}a   -> ())    ->   ()
    forall a. (forall b. ({Coll * a, Ord} b) ({Num} a) -> ()) -> ()

(It doesn't matter how nested the location of the constraints are since we'd 
just gather them up and use them to restrict the quantification for that 

Anyway that's as far as I've got trying to think up alternative 
representations - the above may have some bug or problem with it but I'm 
half thinking of adding the above sugar to my editor (when I eventally get 
past all the low-level gui stuff I'm doing at the moment).

Best regards, Brian.
Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.


More information about the Haskell-Cafe mailing list