newbie question: implementation inheritance and interface specifications in Haskell 98 
Author Message
 newbie question: implementation inheritance and interface specifications in Haskell 98

I'm new to functional programming and teaching myself via Thomson's "The
Craft of
Functional Programming" (2nd ed). I scanned appendix A comparing Functional,
imperative and OO programming, but I have a few additional questions. Please
excuse
me if I don't use the terminology exactly right:

1) Does Haskell 98 (or Hugs) support some form of implementational
inheritance ala Java
or C++? Can I use the implementation of an ADT to implement another ADT?
How? Is
this an advanced subject or will I get there if I'm just patient and work
through Thomson's
text?

2) Can I specify/state/declare that a particular ADT *must* statisfy some
kind of protocol
(set of methods or functions) and have the compiler/interpreter check my ADT
to make sure
that I've implemented the protocol.

I'm just trying to "look ahead" so I'll know I'm learning something when I'm
learning it.

Thanks.



Mon, 26 Jul 2004 00:09:46 GMT  
 newbie question: implementation inheritance and interface specifications in Haskell 98

Quote:

> 2) Can I specify/state/declare that a particular ADT *must* statisfy
> some kind of protocol (set of methods or functions) and have the
> compiler/interpreter check my ADT to make sure that I've implemented
> the protocol.

Mostly.  Take a look at "type classes".  You can specify that there must
be a certain set of functions that take this number of arguments with
these types.  The compiler can't check that you've obeyed any bit
of the protocol more higher level (ie. that a container not lose any
elements, or that set only has one of a given element) unless you can
encode this in the types.  But neither can C++ or Java.

Quote:
> 1) Does Haskell 98 (or Hugs) support some form of implementational
> inheritance ala Java or C++?

Not as such, no.  You can define type classes that have default implementations
though.  Take, for exampe the Eq type class:
class  Eq a  where
    (==), (/=)       :: a -> a -> Bool

        -- Minimal complete defintion:
        --      (==) or (/=)
    x /= y           =  not (x == y)
    x == y           =  not (x /= y)

This says that all types that want to be in the EQ type class must define
both == and /= (not equal to).  If the class defines only one though,
the implementation of the other will be filled in from here.

Note that this will interfere with the checking abovein part 2, above.
Because both of the required functions have a default implementation,
not implementing either will still pass the check, though actuall calling
either will cause infinite regress.

Quote:
> Can I use the implementation of an ADT to
> implement another ADT? How?

Sure.  Have one ADT contain another and just define all functions of
one in terms of the other.

--
Aaron Denney
-><-



Mon, 26 Jul 2004 00:49:34 GMT  
 newbie question: implementation inheritance and interface specifications in Haskell 98
Mike,
   This is an interesting question, to which I hope the gurus respond
:). But here is my take on it.
   For question 2: Yes haskell supports
very directly the concept of declaring that an ADT follow a certain
protocal through 'type classes'. 'Craft ...' discusses this quite well,
so i wont belabor it, but type classes have much in common with java's
'interface' concept.  There can also be a hierachy of type classes,
where one type is a subtype of another. However, in answer to question
1), this is not how one reuses implementations. There is no concept of
implementation inheritance in haskell. Reuse is supported in rather
different ways that I hope you will learn from 'Craft...': mainly
through higher order functions and by reusing existing ADT's inside your
new ADT. ('part-of' relationship? in oop?).

Ron

Quote:

> I'm new to functional programming and teaching myself via Thomson's "The
> Craft of
> Functional Programming" (2nd ed). I scanned appendix A comparing Functional,
> imperative and OO programming, but I have a few additional questions. Please
> excuse
> me if I don't use the terminology exactly right:

> 1) Does Haskell 98 (or Hugs) support some form of implementational
> inheritance ala Java
> or C++? Can I use the implementation of an ADT to implement another ADT?
> How? Is
> this an advanced subject or will I get there if I'm just patient and work
> through Thomson's
> text?

> 2) Can I specify/state/declare that a particular ADT *must* statisfy some
> kind of protocol
> (set of methods or functions) and have the compiler/interpreter check my ADT
> to make sure
> that I've implemented the protocol.

> I'm just trying to "look ahead" so I'll know I'm learning something when I'm
> learning it.

> Thanks.



Mon, 26 Jul 2004 00:59:23 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Inheritance: Interface vs Implementation (was: Questions on inheritance)

2. Looking for Interface Inheritance Implementation

3. implementation inheritance question (maybe)

4. Newbie question: Inheritance

5. COM: Interface Implementation question

6. a newbie question (advice on implementation)

7. Newbie's Questions about the Learning of FPLs Implementation

8. HIDING CONST IN IMPLEMENTATION FILE? (newbie question)

9. newbie question: OS implementation in ada

10. newbie question about free implementations

11. Another Newbie question about interfacing to C

12. Newbie question about interface to C

 

 
Powered by phpBB® Forum Software