[Haskell] Newbie Question about Types

Graham Klyne GK at ninebynine.org
Mon Sep 6 06:02:00 EDT 2004


I maybe don't fully grasp your goals here, but this sounds similar to some 
early problems I ran into with Haskell (coming from comparable background), 
and here are a couple of comments that _might_ just help:

(a) adding a type context to a 'data' declaration seems to be very rarely, 
if ever, of any real benefit.  You still have to declare the context on the 
relevant instance functions of the class, and its presence in the data 
declaration adds constraints that still need to be satisfied 
somehow.  Suggest:  simply drop the context from the data declaration and 
add them as-needed to the functions assigned as component values of that 
datatype.

(b) is what you want *really* a Haskell class?  Have you considered 
creating a data type whose methods are the required functions.  Different 
values of that datatype may have different component functions.  E.g.

data Fooable a =
      { foo1 :: a -> a -> a
      , foo2 :: a -> a -> a
      }

#g
--

At 14:57 27/08/04 -0400, David Greenberg wrote:
>Hi,
>
>I very recently just came to Haskell from the Java and Perl worlds, so
>my understanding of Haskell's type system is still a little vague.
>The tutorial and Google didn't seem to have an answer to my question,
>so I am hoping someone here might be able to help me.  I am writing
>some code comparable to the following:
>
>class Fooable a where
>      foo1 :: a -> a -> a
>      foo2 :: a -> a -> a
>
>type MyType a = String -> a
>
>data (Fooable (MyType a)) => AnotherType a =
>      AnotherType {str :: String, myval :: (MyType a)}
>instance (Fooable (MyType a)) => Fooable (AnotherType a) where
>      (AnotherType s1 m1) `foo1` (AnotherType s2 m2) =
>           AnotherType "dummy string" (m1 `foo1` m2)
>
>The trick here is I want to be able to pass a value of type MyType to
>the AnotherType constructor without making MyType directly an instance
>of Fooable.  In other words, I want some sort of sub-type of MyType to
>be an instance of Fooable.  That way I can use it in the constructor
>of AnotherType.
>
>I understand that type inference might make a lot of this easier, but
>I am trying to make a very strict API out of my module(s), and feel
>that relying on inference at this stage could become buggy.
>
>Thanks in advance for any help that you all can give me.
>
>-David
>_______________________________________________
>Haskell mailing list
>Haskell at haskell.org
>http://www.haskell.org/mailman/listinfo/haskell

------------
Graham Klyne
For email:
http://www.ninebynine.org/#Contact



More information about the Haskell mailing list