<div style="font-family: 'Times New Roman'; font-size: 16px;">{-<br _moz_dirty="" /> I am trying to understand and provide a *simplified* explanation of instance contexts and their relationship with class hierarchies.<br _moz_dirty="" /> I use the example from [1]. Are the following two sentences and annotated code a reasonable explanation?<br _moz_dirty="" /> <br _moz_dirty="" />" 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.<br _moz_dirty="" /> The purpose of this rule is to guarantee that the required super-class methods exist."<br _moz_dirty="" /><br _moz_dirty="" /> [1] 4.3.2 Instance Declarations<br _moz_dirty="" /> http://www.haskell.org/onlinereport/haskell2010/haskellch4.html<br _moz_dirty="" /><br _moz_dirty="" />Class hierarchy<br _moz_dirty="" />
Eq1 Show1 Foo <br _moz_dirty="" />
\ / |<br _moz_dirty="" />
\ / |<br _moz_dirty="" />
Num1 Bar<br _moz_dirty="" />
|<br _moz_dirty="" />
|<br _moz_dirty="" />
Num2<br _moz_dirty="" />-}<br _moz_dirty="" /><br _moz_dirty="" />class Foo a where<br _moz_dirty="" />class Show1 a where<br _moz_dirty="" />class Foo a => Bar a where<br _moz_dirty="" />class Eq1 a where<br _moz_dirty="" /><br _moz_dirty="" /><br _moz_dirty="" />-- Eq1 and Show1 are super-classes of Num1 and Num2.<br _moz_dirty="" />class (Eq1 a, Show1 a) => Num1 a<br _moz_dirty="" />class Num1 a => Num2 a <br _moz_dirty="" /><br _moz_dirty="" />-- We must make an instance of Foo [a], before we can have instance Bar [a]<br _moz_dirty="" />instance (Num1 a) => Foo [a] where <br _moz_dirty="" />-- But that instance of Foo [a] depends on a being a member of Num1<br _moz_dirty="" />-- Hence Bar[a] can only be defined if their exists Num1 a<br _moz_dirty="" />-- But Eq1 & Show1 are super classes of Num1<br _moz_dirty="" />-- The following context causes an *error*, because the context is weaker than required<br _moz_dirty="" />-- instance (Eq1 a, Show1 a) => Bar [a] where<br _moz_dirty="" />-- But this is OK<br _moz_dirty="" />instance (Num1 a) => Bar [a] where<br _moz_dirty="" />-- Also, this would be OK<br _moz_dirty="" />-- instance (Num2 a) => Bar [a] where<br _moz_dirty="" /><br _moz_dirty="" /><br _moz_dirty="" /><br /></div>
<br clear=all> 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. <a href="http://www.dit.ie">http://www.dit.ie</a><br>
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. <a href="http://www.dit.ie">http://www.dit.ie</a>