2 Replies - 766 Views - Last Post: 18 March 2010 - 12:47 PM

#1 s243a   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 17-March 10

Creating Subclasses

Posted 17 March 2010 - 09:24 PM

I am trying to understand subclasses. I created this tinny piece of code:

class (Show a)=>Word a where
   show (Word a) = show a


The hope is to define a default implementation of the class Show. I get the following error:

'show' is not a (visible) method to the class word. Also note, that I could possibly try to have word derive show but I want to understand this error better.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating Subclasses

#2 s243a   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 17-March 10

Re: Creating Subclasses

Posted 18 March 2010 - 11:55 AM

I think that the implicit type system might be preventing me from pattern matching against type classes. Consequently I've decided to build some explicit types into my program. The following is considerably more complex. Types are made explicit by creating functions of the forum IsType.

class IsContext a where
   isContext      :: a -> Bool

class IsWordTypeInstance a where
   isWordTypeInstance    :: a -> Bool

class IsWordType a where
   isWordType    :: a -> Bool

-----------------------------------------------------------


data ContextList1 = Context_Word
                  |Context_Instance
                  |Context_Virtual
                  |Context_Physical
                  |Context_historical

instance Show(ContextList1) where
   show Context_Word="Context_Word"
   show Context_Instance="Context_Instance"
   show Context_Virtual="Context_Virtual"
   show Context_Physical="Context_Physical"
   show Context_historical="Context_historical"

instance IsContext ContextList1 where
   isContext a=True

instance IsWordTypeInstance ContextList1 where
   isWordTypeInstance a=False

instance IsWordType ContextList1 where
   isWordType a=False
-----------------------------------------------------------


data AnimalList1=Animal_Bird
                |Animal_Animal
                |Animal_Jay

instance Show(AnimalList1) where
   show Animal_Bird = "Animal Bird"
   show Animal_Animal = "Animal_Animal"
   show Animal_Jay = "Animal_Jay"

instance IsContext AnimalList1 where
   isContext a=False

instance IsWordTypeInstance AnimalList1 where
   isWordTypeInstance a=True

instance IsWordType AnimalList1 where
   isWordType a=False
-----------------------------------------------------------


data WordTypeList1=WortType_Noun
                  |WordType_Verb
                  |WordType_Adjitive
                  |WordType_Pronoun
                  |WordType_WordType

instance Show(WordTypeList1) where
   show WortType_Noun="WortType_Noun"
   show WordType_Verb="WordType_Verb"
   show WordType_Adjitive="WordType_Adjitive"
   show WordType_Pronoun="WordType_Pronoun"

instance IsContext WordTypeList1 where
   isContext a=False

instance IsWordTypeInstance WordTypeList1 where
   isWordTypeInstance a=False

instance IsWordType WordTypeList1 where
   isWordType a=True


and it works so far.
Was This Post Helpful? 0
  • +
  • -

#3 s243a   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 17-March 10

Re: Creating Subclasses

Posted 18 March 2010 - 12:47 PM

expanded on this a bit: So, I am able to reuse code in a way where I didn't have to Match a pattern to each contructor:

data ContextList1 = Context_Word
                  |Context_Instance
                  |Context_Virtual
                  |Context_Physical
                  |Context_historical

instance ShowWord(ContextList1) where
   showWord Context_Word="Word"
   showWord Context_Instance="Instance"
   showWord Context_Virtual="Virtual"
   showWord Context_Physical="Physical"
   showWord Context_historical="historical"



instance Show(ContextList1) where
   show a= "Context_" ++ showWord a



That is the function Show is able to use the function Show word so it doesn't need to do it's own pattern matching. May seem simple for experience people, but this kind of stuff is tricky for new people.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1