-1

how can i group getX and putX in a class instance ? the code below is an answer for this post Class set method in Haskell using State-Monad


import Control.Monad.State

data Point = Point { x :: Int, y :: Int } deriving Show

getX :: State Point Int
getX = get >>= return . x

putX :: Int -> State Point ()
putX newVal = do
    pt - get
    put (pt { x = newVal })

increaseX :: State Point ()
increaseX = do
    x - getX
    putX (x + 1)

Later I hope I will implement setters and getters for a hierarchy of 2 classes, but for now i just wanna do something like this:


class A a where

   putX :: Int -> State Point ()

instance A (State Point) where

    putX newVal = do
        pt - get
            put (pt { x = newVal })
 
Community
  • 1
  • 1
Daniel
  • 1
  • 1
  • 1
    Classes are for overloading an operation for multiple data types. What do you want to overload over in this case? Can you give examples of several data types that should be class instances? Or, can you describe the intended generalization of getX and putX? – Heatsink Jun 02 '11 at 14:04

1 Answers1

1

You seem to be conflating multiple concepts here, to the point that I'm not sure exactly what you're aiming to accomplish. A few thoughts on what you might be after:

  • Field access, i.e., a way to inspect or replace a piece of a larger data structure. This doesn't really lend itself to a type class, because for many combinations of "inner field" and "data structure" there will be more than one accessor possible. Abstractions along these lines are often called "lenses".

  • Stateful references in some generic fashion. For the most part in a State monad this amounts to combining something like the aforementioned lenses with the standard get and put. In this case you could have a type class for the combination of a particular monad and the accessor data type, but it wouldn't really do that much.

  • Overloading access to a particular field, so that functions can work on any data type that contains an "x" field. In this case I assume you'd also want "y", as some sort of type class for 2D points. This is entirely separate from the above issues, however.

Perhaps you could clarify your goal?

C. A. McCann
  • 76,893
  • 19
  • 209
  • 302
  • i want to have two classes: A {methods:setXA(xa),getXA() }and B:A{methods:setXB(b),getXB()}(A the superclass of B) and be able to do b = new B(),b.setXB(),b.getXA(), Thanks for reply – Daniel Jun 02 '11 at 16:23
  • 1
    @Daniel: I think you're misunderstanding how type classes work. They're not at all the same thing as a "class" in an OOP language, and what you're suggesting doesn't make any sense in Haskell. – C. A. McCann Jun 02 '11 at 16:26
  • this is a homework and my teacher suggested to use monads like State, IO and Transformers, I lost a lot of time trying to figure it out. – Daniel Jun 02 '11 at 16:45
  • 1
    @Daniel: My point stands. I suspect you've taken a wrong turn in how you're approaching this, so you might get better information if you explain the rough outline of the homework assignment. My guess is that you don't actually want to use type classes at all. – C. A. McCann Jun 02 '11 at 17:13