7 Replies - 4326 Views - Last Post: 01 May 2010 - 05:22 PM

#1 s243a  Icon User is offline

  • D.I.C Head

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

Haskell - Not it Scope type contructor or class 'TopObj'

Posted 08 April 2010 - 09:55 PM

The type system in Haskell totally confuses me:

data ObjType = MkObj String ObjType
             | TopObj String

newtype Animal = Animal TopObj String



gives the error:

Not it Scope type constructor or class 'TopObj'

on line 4. What could possibly be wrong? I defined the type constructor TopObj on line 2.
Is This A Good Question/Topic? 0
  • +

Replies To: Haskell - Not it Scope type contructor or class 'TopObj'

#2 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Haskell - Not it Scope type contructor or class 'TopObj'

Posted 08 April 2010 - 10:10 PM

Well, there are two things wrong here. Do you know what a newtype is?

Newtypes simply give new identities to existing types. They give a type a new identity, but it's still essentially the same type. The first problem is that you're trying to supply a value constructor where a type constructor.

Read the first section of this: http://book.realworl...-functions.html and "How to give a type a new identity" in this: http://book.realworl...ypeclasses.html

That should clarify some stuff.

Anyways, you can do this:

data ObjType = MkObj String ObjType
             | TopObj String

newtype Animal = Animal ObjType



Which obviously isn't what you want. Are you deliberately trying to use Haskell as an Object Oriented language or something? :o
Was This Post Helpful? 0
  • +
  • -

#3 s243a  Icon User is offline

  • D.I.C Head

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

Re: Haskell - Not it Scope type contructor or class 'TopObj'

Posted 14 April 2010 - 01:01 PM

View PostRaynes, on 08 April 2010 - 09:10 PM, said:

Well, there are two things wrong here. Do you know what a newtype is?

Newtypes simply give new identities to existing types. They give a type a new identity, but it's still essentially the same type. The first problem is that you're trying to supply a value constructor where a type constructor.

The data structure is the same but you can define a new behavior with how this new type is treated with regards to functions. (At least that's what I read.)

Quote

data ObjType = MkObj String ObjType
             | TopObj String

newtype Animal = Animal ObjType



Which obviously isn't what you want.

Not really. It still could work but but it is more awkward.

Quote

Are you deliberately trying to use Haskell as an Object Oriented language or something? :o


I am trying to learn what functionality the type system of Haskell gives us. I am finding rather limited and believe that their could be more functionality in the type system while preserving static type checking. As a consequence I am deciding to put more information into the data and less into the type system. The consequence being that less of any programing I do can be checked at compile time.
Was This Post Helpful? 0
  • +
  • -

#4 s243a  Icon User is offline

  • D.I.C Head

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

Re: Haskell - Not it Scope type contructor or class 'TopObj'

Posted 14 April 2010 - 03:10 PM

I think the Haskell extension I'm looking for is called Generalized Algebraic Data Types:
http://www.haskell.o...sions.html#gadt
Was This Post Helpful? 0
  • +
  • -

#5 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Haskell - Not it Scope type contructor or class 'TopObj'

Posted 14 April 2010 - 03:39 PM

Yeah, GADTs are pretty great. I'm wondering, and this is just a completely friendly question, because I'm curious, if you hate Haskell so much, why do you continue to use it? I mean, I'm not sure what your expectations really are. Haskell's type system is pretty expressive and powerful.
Was This Post Helpful? 0
  • +
  • -

#6 s243a  Icon User is offline

  • D.I.C Head

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

Re: Haskell - Not it Scope type contructor or class 'TopObj'

Posted 14 April 2010 - 04:42 PM

View PostRaynes, on 14 April 2010 - 02:39 PM, said:

Yeah, GADTs are pretty great. I'm wondering, and this is just a completely friendly question, because I'm curious, if you hate Haskell so much, why do you continue to use it? I mean, I'm not sure what your expectations really are. Haskell's type system is pretty expressive and powerful.


My reasons for wanting to learn haskell are:
1) It is a pure functional language. (Seems like the best starting point to learn functional languages even if not the most practical for actual coding)

2) It is lazy (Lazy programing seems to have interesting potential for optimizations and allows for neat algebraic expressions).

3) It produces very readable Parsers. Comparing the output of Happy to Yacc (The C equivalent) the generated code looks much more readable in Haskell.

4) The code can be more concise in Hakell.

5) With regards to types, when I first looked at Haskell I had very high expectations with regards to generic programing and an expressive type system. Since then other languages (Java and C#) have incorporated generics and when I look further into Haskell, I see that without extensions the more explicitly we try to type things the less generic the code becomes. The reason this is important is that, a selling aspect of functional programming is the ability for the programmer to write generic code. Additionally, a selling aspect of Haskell is the ability of the strong type system to catch errors at compile time.

You can in Haskell write generic code if you keep the number of types small but then you must rely on comparison operations instead of polymorphism and you lose much of the advantages of the advertised advantages about Haskell being strongly typed. Alternatively, you can use extensions like generalized algebraic types to overcome the limitations of Haskell 98. This is the approach I strongly prefer because GHC makes it easy to use Haskell extensions.

Now with regards to generics and Haskell, it is my understanding that in Haskell, you can't even create a list that has different data types in the list. Fortunately, I believe there is an extension to overcome this.

6) Anyway, as much as I complain about Haskell's type system, my real complaint is that the compiler does not give very helpful error messages and without trial and error it is not obvious what one can and cannot do. That said, I believe with extensions Haskell may have a powerful type system and I still believe that there is much utility in the language.

7) The object oriented paradigm is too hierarchical. Not all concepts fall neatly into a hierarchy and therefore I question why we should structure our code this way. Of course C++ allows multiple inheritance and Java allows interfaces so perhaps there is some flexibility but I question how elegant it is. Moreover, in set theory when we subclass we restrict properties, but in object oriented programming when we subclass we add properties. Is this necessarily the most logical way to organize our code?

I don't know what better alternatives Haskell has to offer but I think it is important to explore different paradigms.
Was This Post Helpful? 0
  • +
  • -

#7 s243a  Icon User is offline

  • D.I.C Head

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

Re: Haskell - Not it Scope type contructor or class 'TopObj'

Posted 21 April 2010 - 06:18 PM

Anyway, I found a solution. Check this stuff out:

{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}

data Animal=Animal
newtype Cat = Cat Animal
type CatSyn = Animal->Cat
data Parrent a b = Parrent a b

type family GetParrentTyp a
type instance GetParrentTyp (a->B)/> = a

instance Show (Animal) where
   show a = "Prit Animal"

instance Show (Animal->Cat) where
   show a = "Print Cat"

class GetParrent a where
   getParrent :: a->GetParrentTyp a

instance GetParrent (CatSyn) where
   getParrent a = Animal


now if I type getParrent Cat on the prompt, it will print "Print Animal"

Edit: As a side note if instead of specifying an instance of Show (Animal->Cat) I use the deriving statment then for some reason show cat doesn't work:

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}

data Animal=Animal deriving (Show)
newtype Cat = Cat Animal
   deriving (Show)
type CatSyn = Animal->Cat
data Parrent a b = Parrent a b

type family GetParrentTyp a
type instance GetParrentTyp (a->B)/> = a

class GetParrent a where
   getParrent :: a->GetParrentTyp a

instance GetParrent (CatSyn) where
   getParrent a = Animal

This post has been edited by s243a: 21 April 2010 - 06:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 s243a  Icon User is offline

  • D.I.C Head

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

Re: Haskell - Not it Scope type contructor or class 'TopObj'

Posted 01 May 2010 - 05:22 PM

Another approach I cam up with using classes and functional dependencies can be found here:

http://groups.google...f5eb88e7a?hl=en

and someone else suggested a slightly different approach:

http://hpaste.org/fa...i/view?id=25265

and told me that their liberies:

Parrent.lhs
ExceptionHieracy.lhs
http://patch-tag.com...d/PureException

also give examples of type hierarchies in Haskell.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1