[Haskell-cafe] Re. I miss OO

Philippos Apolinarius phi500ac at yahoo.ca
Wed Nov 25 21:47:23 EST 2009


My
experience with objects comes from Common List, Clean, Hasekell and
Nice.  In all these languages, the function behavior is determined by
many objects.  I think that this is more powerful and clear than the
Java way.  In Nice one can see the superiority of type-driven behavior
over the Java dot notation, since the dot notation is a particular case
of the type driven behavior.  To make a long story short, one can apply
the dot notation to the first argument, or put it together with the
other arguments. For instance,  consider the followin init() function
(entirely in dot notation):

c l a s s DrawLines ext ends Applet {
   Button ext = new Button ( ”Exi t ” ) ;
   Button dbutton= new Button ( ”Exec” ) ;
  TextFi e ld t f= new TextFi e ld ( 1 5 ) ;
  i n t xx= 5 ;
  i n i
 t ( ) {
       this.setBackground ( Color . l ightGray ) ;
       t h i s . add ( dbutton ) ; t h i s . add ( t f ) ; 
       t h i s . add ( ext ) ;
       t h i s . setLayout ( n u l l ) ;
       ext . r e shape (50 , 30 , 50 , 2 0 ) ;
       dbutton . r e shape ( 50 , 80 , 80 , 3 0 ) ;
      t f . r e shape (140 , 80 , 200 , 3 0 ) ; } ... etc. }

However,
in Nice the method is selected not only by the dotted object, but by
all arguments. This becomes clear by placing the dotted object inside
the argument list:

c l a s s DrawLines ext ends Applet {

   Button ext = new Button ( ”Exi t ” ) ;

   Button dbutton= new Button ( ”Exec” ) ;

  TextFi e ld t f= new TextFi e ld ( 1 5 ) ;

  i n t xx= 5 ;

  i n i t ( ) {

       setBackground (this,  Color . l ightGray ) ;

       add (this,  dbutton ) ; t h i s . add ( t f ) ; 

       add (this,  ext ) ;

       setLayout (this,  n u l l ) ;

       reshape (ext, 50 , 30 , 50 , 2 0 ) ;

       reshape (dbutton,  50 , 80 , 80 , 3 0 ) ;

       reshape (tf, 140 , 80 , 200 , 3 0 ) ; } ... etc. }

In
order to see how useful and practical is the generalization of dot
selection, let us suppose that one needs to write Lisp-like lists in
Nice. I am using Nice because it has both the simpler, more restricted
dot notation as Java and Python, and the argument selection, more
general and powerful, as Haskell and Lisp.  Here is the Nice definition
of polymorphic lists:

import java . i o . * ;

class Lst<T> { T head ; ?Lst<T> t a i l ; }
 
?Lst<int> cons ( i n t x , ?Lst<int> xs ) {
   ?Lst<int> newList= new Lst ( head : x , tail : xs ) ;
   return ( newList ) ; }
   
?Lst<String> cons ( String x , ?Lst<String> xs ) {
   ?Lst<String> newList= newLst ( head : x , tail : xs ) ;
    return ( newList ) ; }

String
 car (? Lst<String> xs ) {
  if ( xs==null ) return ( "" ) ; else return ( xs.head ) ; }
  
int car (? Lst<int> xs ) {
  if ( xs==null ) return ( 0 ) ; else return ( xs.head ) ; }
  
boolean isEmpty (? Lst<String> xs )= xs==null ;
  ?Lst<String> cdr (? Lst<String> xs ) {
  if ( xs== null ) return ( xs ) ; else return ( xs.tail ) ; }

?Lst<String> cdr (? Lst<String> xs ) {
  if ( xs== null ) return ( xs ) ; else return ( xs.tail ) ; }

?Lst<int> cdr (? Lst<int> xs ) {
 if ( xs== null ) return ( xs ) ; else return ( xs.tail ) ; }
 

void main ( String [ ] args ) {
  InputStreamReader converter =
      new InputStreamReader ( System.in ) ;
  Buf feredReader in = new BufferedReader ( converter ) ;
  ?String s= "Type a sentence and press <Enter>"
 ;
  println ( s ) ; s= in.readLine ( ) ;
  ?Lst<String> words=null ;
  StringTokenizerst = new StringTokenizer ( s ) ;
  while ( st.hasMoreTokens ( ) ) {
  words= cons ( st.nextToken ( ), words ) ; }
  var xs= words ;
  while ( ! isEmpty ( xs ) ) { println ( car ( xs ) ) ;
  xs= cdr ( xs ) ; } }

If
you want to see how the above program can be generalized into a full
fledged Lisp like stucture, take a look at my page on Nice:
www.discenda.org.  In any case, since Haskell has the more general
method selection, there is no need to have the more restricted dot
selector. Nice has both method selectors, but I seldom use the dot
selector. 


--- On Wed, 11/25/09, Michael Mossey <mpm at alumni.caltech.edu> wrote:

From: Michael Mossey
 <mpm at alumni.caltech.edu>
Subject: [Haskell-cafe] I miss OO
To: haskell-cafe at haskell.org
Received: Wednesday, November 25, 2009, 1:51 PM

I'm
fairly new to Haskell, and starting to write some big projects.
Previously I used OO exclusively, mostly Python. I really miss the
"namespace" capabilities... a class can have a lot of generic method
names which may be identical for several different classes because
there is no ambiguity.

In my musical application, many "objects"
(or in Haskell, data) have a time associated with them. In Python I
would have an accessor function called "time" in every class.

So if I have objects/data note1, cursor1, and staff1,

Python:
  note1.time()
  cursor1.time()
  staff1.time()

Haskell needs something like
  note_time note1
  cursor_time cursor1
  staff_time staff1

which is a lot more visually
 disorganized.

What's
worse, I have a moderate case of RSI (repetitive strain injury) so I
type slowly and depend on abbreviations a lot. I use the souped-up
abbreviation capabilities of Emacs. Let's say I have a
field/member-variable called orientedPcSet that is used across many
classes. In Python, I can create an abbreviation for that and it is
useful many times. In Haskell, I might need

someType_orientedPcSet
someOtherType_orientedPcSet
thirdType_orientedPcSet

which
prevents me from using abbreviations effectively (especially the
dynamic-completion feature). (It would help to make the underscore not
part of word syntax, but that's not ideal.)

So I'm thinking of
moving to a scheme in Haskell using modules, most types being defined
in their own modules, and doing qualified imports. Generic names like
'time' can be defined in each module w/o clashing. Then I have

Note.time note1
Cursor.time
 cursor1
Staff.time staff1

This
is very useful because I can define abbreviations for the type name and
for oft-used accessor function names and these abbrevs are more
organized, easier to remember, and easier to combine.

I would be
interested in comments... is this a good way to do things? Am I trying
too hard to impose OO on Haskell and is there a better way?

Thanks,
Mike

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
resizeLeftPane();gLaunchProfile.start('RT_AD_FOOT');


      __________________________________________________________________
The new Internet Explorer® 8 - Faster, safer, easier.  Optimized for Yahoo!  Get it Now for Free! at http://downloads.yahoo.com/ca/internetexplorer/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091125/61bc9280/attachment.html


More information about the Haskell-Cafe mailing list