1
votes

I try to understand more deeply the Global Variables paradigm in Haskell, I took to work on https://kodu.ut.ee/~nestra/eng/splst11.pdf, as an exercise. First thing I did is rewrite some of the code in 2.1 - The library module : changed this

data family Var a :: *    -- family declaration
class (Show l, Ord l) => Variable l where    -- Class w/o method

to

class (Show l, Ord l, Eq l) => Variable l where
    data Var l :: *

This should have the same meaning (my guess..) with the original code GHC complains further in compiling. Nevertheless the curious thing (for me) was that GHC complains if a class instance is not declared with an explicit deriving clause (my first attempt, it looks redundant:

instance Variable Integer where 
    data Var Integer = X | Y deriving (Show, Eq, Ord)

Questions:

  1. Isn't the derivation part of the inherited class "property" - I expected not to repeat this in the class instances.
  2. Is it possible to write in the same style as the paper w/o GHC complaining of a Illegal instance declaration for `Variable (Var Int)' on the line with instance Variable (Var Int) may be it's old style haskell (GHC98)? To avoid this I had to declare the class differently.
1
Have you looked at the full text of the "Illegal instance declaration" error? What is it?Fyodor Soikin
Yes you're right I didn't read to the end GHC gives a hint "Use FlexibleInstances if you want to disable this"... my fault. Question 1 remains - in my first attempt I declare the instances as it, without derivation and GHC wasn't happy.user3680029
Once again: how is GHC "not happy"? What did it tell you?Fyodor Soikin
It says all instances types must be of the form (T a1 .... an) etc... etc.. and each type variable appears at most once in the instance head. Use FlexibleInstances if you want to disble this I will just add that this pragma wasn't in the original code...user3680029
That's now what I meant. When you say "in my first attempt I declare the instances as it, without derivation and GHC wasn't happy", what does "not happy" mean here? And in general: when asking questions on SO, it's a good idea to include error messages and other relevant info. People who answer questions can't help you if they don't know what's happening.Fyodor Soikin

1 Answers

0
votes

Ok folks, the issue wasn't really an issue, first I missed the GHC' hint (GHC is always for me a bit cryptic in its error message, so I'm not used to spend lot of time reading them) - I think that with this missing language pragma the code run smoothly. For the ones who will try to implement the paper - add {-# LANGUAGE FlexibleInstances #-} then probably no message will be posted - if like me you go on to define the data type and method inside the class add a constraint on Eq a at the class level (the paper implements it the instances but not at the class level)