[Haskell-cafe] no dynamic binding

2004-09-19 Thread Andrew Harris
Hi -

   I have another question.  I am still working on a soccer server and
thought it would be neat to create command objects that had a
"toString" method.  Then, I was going to keep a list of these command
objects and at the right time stringify them and send them to the
server.  So I created a class with a toString method:

class ServerCommandClass a where
  toString :: a -> String

And then a few instances:

-- dash command
data DashCommand =
 DashCommand { dashpower :: Double }

instance ServerCommandClass DashCommand where
 toString c = "(dash " ++ show (dashpower c) ++ ")\n"

-- move command
data MoveCommand = 
 MoveCommand { x :: Double,
   y :: Double } 

instance ServerCommandClass MoveCommand where
 toString c = "(move " ++ show (x c) ++ " " ++ show (y c) ++ ")\n"

   The problem is, I am not quite sure how to describe a *list* of
command objects where the list could have both DashCommands and
MoveCommands in it.  Ideally the list could contain both, and then for
each item in the list I could call the toString method.

   I was reading Simon Thompson's Haskell: The Craft of Functional
Programming  and I read that Haskell 98 does not support dynamic
binding, which (it seems) is what I'm trying to do.  Does anyone have
a suggestion on an alternative approach?

thanks,
-andrew
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] no dynamic binding

2004-09-19 Thread Abraham Egnor
You can use exisential types to do what you'd like.  From memory, so
there are probably errors:

newtype ServerCommand = forall a. ServerCommandClass a => ServerCommand a

instance ServerCommandClass ServerCommand where
toString (ServerCommand c) = toString c

commands :: [ServerCommand]
commands = [ServerCommand $ DashCommand ..., ServerCommand $ MoveCommand ...]

Also, for a less type-safe approach, see Data.Dynamic.



On Sun, 19 Sep 2004 13:48:53 -0400, Andrew Harris <[EMAIL PROTECTED]> wrote:
> Hi -
>
>I have another question.  I am still working on a soccer server and
> thought it would be neat to create command objects that had a
> "toString" method.  Then, I was going to keep a list of these command
> objects and at the right time stringify them and send them to the
> server.  So I created a class with a toString method:
>
> class ServerCommandClass a where
>   toString :: a -> String
>
> And then a few instances:
>
> -- dash command
> data DashCommand =
>  DashCommand { dashpower :: Double }
>
> instance ServerCommandClass DashCommand where
>  toString c = "(dash " ++ show (dashpower c) ++ ")\n"
>
> -- move command
> data MoveCommand =
>  MoveCommand { x :: Double,
>y :: Double }
>
> instance ServerCommandClass MoveCommand where
>  toString c = "(move " ++ show (x c) ++ " " ++ show (y c) ++ ")\n"
>
>The problem is, I am not quite sure how to describe a *list* of
> command objects where the list could have both DashCommands and
> MoveCommands in it.  Ideally the list could contain both, and then for
> each item in the list I could call the toString method.
>
>I was reading Simon Thompson's Haskell: The Craft of Functional
> Programming  and I read that Haskell 98 does not support dynamic
> binding, which (it seems) is what I'm trying to do.  Does anyone have
> a suggestion on an alternative approach?
>
> thanks,
> -andrew
> ___
> Haskell-Cafe mailing list
> [EMAIL PROTECTED]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] no dynamic binding

2004-09-19 Thread Georg Martius
Hi,
it might be better to use a simple abstact data type for it like:
-- | Command Type has for all possible actions a contructor
data Command = INIT String String Bool -- ^ Teamname, Version, Goalkeeper or not
 | RECONNECT String Integer -- ^ Teamname, Playernum (Unum)
 | CATCH Angle
 | CHANGEVIEW ViewWidth ViewQuality
 | DASH Integer -- ^ Power [-100, 100]
 | KICK Integer Angle -- ^ Power [-100,100], direction
 | MOVE Double Double -- ^ X, Y Coordinate
 | SAY String
 | REQUESTSENSEBODY
 | REQUESTSCORE
 | TURN Angle   -- ^ Angle [-180, 180] see 'Direction'
 | TURNNECK Angle -- ^ Angle [-90,90]
 | BYE
 | NOACTION  -- ^ this means we do nothing
--  Show instance for Command to create syntactical correct command strings
instance Show Command where
show NOACTION = ""
show c = "(" ++ (concat strlist) ++ ")"
where strlist =
  case c of
  INIT team ver True -> ["init ", team, " (version ", ver, ") 
goalie"]
  INIT team ver False -> ["init ", team, " (version ", ver, ")"]
  RECONNECT team unum -> ["reconnect ", team, " ", show unum]
  CATCH dir  -> ["catch ", show dir]
  CHANGEVIEW width quali -> ["change_view ", show width, show 
quali]
  DASH p -> ["dash ", show p]
  KICK p dir -> ["kick ", show p, " ", show dir]
  MOVE x y   -> ["move ", show x, " ", show y]
  SAY msg-> ["say ", msg]
  REQUESTSENSEBODY -> ["sense_body"]
  REQUESTSCORE -> ["score"]
  TURN a   -> ["turn ", show a]
  TURNNECK a   -> ["turn_neck ", show a]
  BYE  -> ["bye"]
  NOACTION -> [""]  -- just for compiler
you can just have a [Command] and use show to get the string representation.
As you might guess I have implemented a socker client in haskell allready. I can mail 
you the code if you want. It is still in the alpha stage, but it can do quite a lot.
Best Regards!
 Georg
On Sun, 19 Sep 2004 13:48:53 -0400, Andrew Harris <[EMAIL PROTECTED]> wrote:
Hi -
   I have another question.  I am still working on a soccer server and
thought it would be neat to create command objects that had a
"toString" method.  Then, I was going to keep a list of these command
objects and at the right time stringify them and send them to the
server.  So I created a class with a toString method:
class ServerCommandClass a where
  toString :: a -> String
And then a few instances:
-- dash command
data DashCommand =
 DashCommand { dashpower :: Double }
instance ServerCommandClass DashCommand where
 toString c = "(dash " ++ show (dashpower c) ++ ")\n"
-- move command
data MoveCommand =
 MoveCommand { x :: Double,
   y :: Double }
instance ServerCommandClass MoveCommand where
 toString c = "(move " ++ show (x c) ++ " " ++ show (y c) ++ ")\n"
   The problem is, I am not quite sure how to describe a *list* of
command objects where the list could have both DashCommands and
MoveCommands in it.  Ideally the list could contain both, and then for
each item in the list I could call the toString method.
   I was reading Simon Thompson's Haskell: The Craft of Functional
Programming  and I read that Haskell 98 does not support dynamic
binding, which (it seems) is what I'm trying to do.  Does anyone have
a suggestion on an alternative approach?
thanks,
-andrew
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe

--
 Georg Martius,  Tel: (034297) 89434 
--- http://www.flexman.homeip.net -
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] no dynamic binding

2004-09-20 Thread Tomasz Zielonka
On Sun, Sep 19, 2004 at 08:54:57PM +0200, Georg Martius wrote:
> Hi,
> 
> it might be better to use a simple abstact data type for it like:

You probably meant algebraic data type. Abstractness isn't essential
here.

Best regards,
Tom

-- 
.signature: Too many levels of symbolic links
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] no dynamic binding

2004-09-20 Thread Tomasz Zielonka
On Sun, Sep 19, 2004 at 02:46:12PM -0400, Abraham Egnor wrote:
> You can use exisential types to do what you'd like.  From memory, so
> there are probably errors:
> 
> newtype ServerCommand = forall a. ServerCommandClass a => ServerCommand a

This can't be newtype, you must use 'data'.

> instance ServerCommandClass ServerCommand where
> toString (ServerCommand c) = toString c
> 
> commands :: [ServerCommand]
> commands = [ServerCommand $ DashCommand ..., ServerCommand $ MoveCommand ...]

As it was pointed to me some time ago, you can also achieve this without
existential types, simply by keeping the partially applied methods in
your ServerCommand data type, eg.

data ServerCommand =
ServerCommand
{ scToString:: String
}

or, if you want to avoid memoizing the toString result in this case

data ServerCommand =
ServerCommand
{ scToString:: () -> String
}

Best regards,
Tom

-- 
.signature: Too many levels of symbolic links
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] no dynamic binding

2004-09-21 Thread Graham Klyne
I think others have provided the immediate answers to your question, so 
I'll skip that.

But, standing back, you seem to have fallen prey to a misunderstanding I 
made when learning Haskell, thinking that a "class" is directly analogous 
to a class in OO languages.  While there are similarities, the Haskell 
notion of a class is much more restrictive, as you have found.

I wrote a few more notes here:
  http://www.ninebynine.org/Software/Learning-Haskell-Notes.html#type-class-misuse
#g
--
At 13:48 19/09/04 -0400, Andrew Harris wrote:
Hi -
   I have another question.  I am still working on a soccer server and
thought it would be neat to create command objects that had a
"toString" method.  Then, I was going to keep a list of these command
objects and at the right time stringify them and send them to the
server.  So I created a class with a toString method:
class ServerCommandClass a where
  toString :: a -> String
And then a few instances:
-- dash command
data DashCommand =
 DashCommand { dashpower :: Double }
instance ServerCommandClass DashCommand where
 toString c = "(dash " ++ show (dashpower c) ++ ")\n"
-- move command
data MoveCommand =
 MoveCommand { x :: Double,
   y :: Double }
instance ServerCommandClass MoveCommand where
 toString c = "(move " ++ show (x c) ++ " " ++ show (y c) ++ ")\n"
   The problem is, I am not quite sure how to describe a *list* of
command objects where the list could have both DashCommands and
MoveCommands in it.  Ideally the list could contain both, and then for
each item in the list I could call the toString method.
   I was reading Simon Thompson's Haskell: The Craft of Functional
Programming  and I read that Haskell 98 does not support dynamic
binding, which (it seems) is what I'm trying to do.  Does anyone have
a suggestion on an alternative approach?
thanks,
-andrew
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Graham Klyne
For email:
http://www.ninebynine.org/#Contact
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe