{-
 I am trying to understand and provide a *simplified* explanation of instance contexts and their relationship with class hierarchies.
 I use the example from [1]. Are the following two sentences and annotated code a reasonable explanation?
 
" When instantiating an instance I of C, its context must be at the same level or lower than the context of any instance of any super-class of C.
  The purpose of this rule is to guarantee that the required super-class methods exist."

 [1] 4.3.2  Instance Declarations
 http://www.haskell.org/onlinereport/haskell2010/haskellch4.html

Class hierarchy
  Eq1   Show1           Foo  
     \     /                        |
     \   /                          |
    Num1                   Bar
       |
       |
    Num2
-}

class Foo a where
class Show1 a where
class Foo a => Bar a where
class Eq1 a where


-- Eq1 and Show1 are super-classes of Num1 and Num2.
class (Eq1 a, Show1 a) => Num1 a
class Num1 a => Num2 a

-- We must make an instance of Foo [a], before we can have instance Bar [a]
instance (Num1 a) => Foo [a] where
-- But that instance of Foo [a] depends on a being a member of Num1
-- Hence Bar[a] can only be defined if their exists Num1 a
-- But Eq1 & Show1 are super classes of Num1
-- The following context causes an *error*, because the context is weaker than required
-- instance (Eq1 a, Show1 a) => Bar [a] where
-- But this is OK
instance (Num1 a) => Bar [a] where
-- Also, this would be OK
-- instance (Num2 a) => Bar [a] where




Tá an teachtaireacht seo scanta ó thaobh ábhar agus víreas ag Seirbhís Scanta Ríomhphost de chuid Seirbhísí Faisnéise, ITBÁC agus meastar í a bheith slán. http://www.dit.ie
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to