[Haskell-cafe] GADT and instance deriving

Richard Eisenberg eir at cis.upenn.edu
Sat May 25 10:52:00 CEST 2013


Hi TP,

Thankfully, the problem you have is fixed in HEAD -- the most recent version of GHC that we are actively working on. I am able, using the HEAD build of GHC, to use a `deriving Typeable` annotation to get a Typeable instance for a type that has non-*-kinded parameters. To get the HEAD compiler working, see here: http://hackage.haskell.org/trac/ghc/wiki/Building

However, I'm worried that other aspects of your design may be suboptimal. The `Box` type you mentioned a few posts ago is called an existential type. Existential types have constructors who have type parameters that are *not* mentioned in the conclusion. As an example, your `Box` constructor involved a type parameter `a`, but the `Box` type itself has no parameters. This existential nature of the type is why your comparison didn't work.

A Tensor, however, doesn't seem like it would need to be an existential type. The order of the tensor should probably (to my thinking) appear in the type, making it not existential anymore.

In general, I (personally -- others will differ here) don't love using Typeable. By using Typeable, you are essentially making a part of your program dynamically typed (i.e., checked at runtime). The beauty of Haskell (well, one of its beauties) is how it can check your code thoroughly at compile time using its rich type language. This prevents the possibility of certain bugs at runtime. Using Typeable circumvents some of that, so I would recommend thinking carefully about your design to see if its use can be avoided.

Just to diffuse any flames I get for the above paragraph: I fully support the role of Typeable within Haskell. Indeed, sometimes it is unavoidable. In fact, I have a small update to the Typeable interface on my to-do list (adding functionality, not changing existing). I am just arguing that its use should be judicious.

I hope this helps!
Richard

On May 24, 2013, at 11:45 PM, TP wrote:

> Alexander Solla wrote:
> 
>>> (Do you confirm that tilde in s~s1 means "s has the same type as s1"? I
>>> have
>>> not found this information explicitly in the Haskell stuff I have read).
>>> 
>> 
>> Yes.
>> 
>> http://www.haskell.org/ghc/docs/7.4.1/html/users_guide/equality-constraints.html
>> 
>> Is this (Typeable) the right way to go? Is there any other solution?
>>> 
>> 
>> Using typeable is a perfectly reasonable way to go.
> 
> Thanks for your help.
> Unfortunately, I am in the following case (in my real code, not the dummy 
> example of my initial post):
> 
> http://haskell.1045720.n5.nabble.com/Can-t-make-a-derived-instance-of-Typeable-A-B-A-has-arguments-of-kind-other-than-td3121994.html
> 
> Indeed, I obtain at compilation:
> 
> Can't make a derived instance of `Typeable (Tensor ($a))':
>      `Tensor' must only have arguments of kind `*'
> 
> "Tensor" is a type constructor which takes a type-level integer as argument 
> to make a concrete type "Tensor order" (so its kind is Nat -> *).
> Thus in my real code, I cannot derive the typeable instance automatically. I 
> am compelled to write an instance of typeable for my GADT. Are there some 
> tutorial around here? Because the documentation page is a bit terse for my 
> level of knowledge:
> 
> http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Typeable.html
> 
> In the first link above, someone writes:
> 
> """
> You'll have to manually
> write a Typeable instance if you want one.  The process is somewhat
> trickier than you might expect, due to the fact that Typeable does
> some unsafe stuff.  But there are plenty of examples for how to do it
> safely. 
> """
> 
> Where are these examples that can help me to write my instance?
> I have tried to read the source of the implemented instances in 
> data.typeable, not so easy for me.
> 
> Thanks,
> 
> TP
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe




More information about the Haskell-Cafe mailing list