* [Caml-list] How to throw away oop?
@ 2002-12-09 21:20 onlyclimb
2002-12-09 13:45 ` Didier Le Botlan
2002-12-09 14:10 ` Olivier Andrieu
0 siblings, 2 replies; 4+ messages in thread
From: onlyclimb @ 2002-12-09 21:20 UTC (permalink / raw)
To: caml-list
Dear Caml List:
I think it is an old question. but it really bother me quite
much . Since i have a java background and touched ML not long, i found
it is hard not to think in a OOP way in some cases. (even though OCaml
has an OO system, i feel that only the Module style is the true way
of programming in Ocaml, so i tried not to use classes as possible )
Here is a situation i can not overcome: for example
in a OOP style.:
DNA and Protein can be a subclass of Seq. And in fact there are many
kind of sequences , so we can suppose the number of the kind of Seq is
infinite. ie. the subclasses of Seq can be infinite. and DNA and
Protein are just two of them
I want to get the length of the Seq. so in OOP Style . we can
define a function :
val length : #seq -> int to solve the problem . or just use
inheritance seq#length
However if do like above , i must define say class dna or class
protein
Now i want not to use classes. How can i achieve the same effect?
Maybe i can use functor like
module DefautSeq = Seq.Make ( DefaultSeqIntf : SeqIntf) struct end
module DNA = Seq.Make ( DNAIntf : SeqIntf ) struct end
module Protein = Seq.Make (DNAIntf : SeqIntf) struct end
now how to get the length of such seqs ?
(in fact length is usually included in the SeqIntf . here i only to
show a simple case.
for example Chromasome is a kind of DNA , some funtions only for
Chromasome and DNA not suitable for Protein
so those functions surely will not be included in SeqIntf . here
length () refers to such functions )
if i do like this,
val length : DefaultSeq.t -> int.
DNA.t will not be accepted by such function . only DefaultSeq .t accepted
I have tryied to use
module DefaultSeq = DNA before the length function . It is OK
But First it is not elegant .
Secd , if module DefaultSeq = DNA is after the length function. It
will not work.
Third , sometime you must restore the orignal module , it is tedious
also not elegant :-(
And maybe can genrate a new functor LENGTH . bu t i can not image to
generate a module in every case just a proper function will do !
Then, How can i do ?
Thanks
climb
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] How to throw away oop?
2002-12-09 21:20 [Caml-list] How to throw away oop? onlyclimb
@ 2002-12-09 13:45 ` Didier Le Botlan
2002-12-09 14:10 ` Olivier Andrieu
1 sibling, 0 replies; 4+ messages in thread
From: Didier Le Botlan @ 2002-12-09 13:45 UTC (permalink / raw)
To: onlyclimb; +Cc: caml-list
May be this solution is not suitable to your problem, however I notice
that you do not use parameterized constructors (nor parameterized
classes). This is very common in ocaml, though.
You talk about Sequences. Fine. Sequences are lists (or whatever
structure you want).
A sequence of amino-acids is of type amino-acid list and a sequence of
dna-bases is of type dna list.
Any function that operates polymorphically on lists (that is, on any 'a
list) will operate as well on dna lists and amino-acids lists.
If you want to get the length of these sequences, you can use
List.length.
If you think that lists are not adequate for your problem, you are free
to write your own structure of type 'a sequence.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] How to throw away oop?
2002-12-09 21:20 [Caml-list] How to throw away oop? onlyclimb
2002-12-09 13:45 ` Didier Le Botlan
@ 2002-12-09 14:10 ` Olivier Andrieu
2002-12-09 18:43 ` brogoff
1 sibling, 1 reply; 4+ messages in thread
From: Olivier Andrieu @ 2002-12-09 14:10 UTC (permalink / raw)
To: onlyclimb; +Cc: caml-list
onlyclimb [Monday 9 December 2002] :
> DNA and Protein can be a subclass of Seq. And in fact there are many
> kind of sequences , so we can suppose the number of the kind of Seq is
> infinite. ie. the subclasses of Seq can be infinite. and DNA and
> Protein are just two of them
Do you really think you'll have to subclass your Seq often ? I mean,
you have DNA, proteins and this is it (you can add RNA if you want to
nitpick). In this case, ie when your datatypes are fixed and you're not
going to extend them, you can simply use a variant type.
module DNA =
type t = ...
let length = ...
end
module Protein =
type t = ...
let length = ...
end
and you can build a generic module for functions common in DNA and
Protein :
module Seq =
type t =
| DNA of DNA.t
| Protein of Protein.t
let length = function
| DNA s -> DNA.length s
| Protein s -> Protein.length s
end
--
Olivier
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] How to throw away oop?
2002-12-09 14:10 ` Olivier Andrieu
@ 2002-12-09 18:43 ` brogoff
0 siblings, 0 replies; 4+ messages in thread
From: brogoff @ 2002-12-09 18:43 UTC (permalink / raw)
To: Olivier Andrieu; +Cc: onlyclimb, caml-list
On Mon, 9 Dec 2002, Olivier Andrieu wrote:
> onlyclimb [Monday 9 December 2002] :
> > DNA and Protein can be a subclass of Seq. And in fact there are many
> > kind of sequences , so we can suppose the number of the kind of Seq is
> > infinite. ie. the subclasses of Seq can be infinite. and DNA and
> > Protein are just two of them
>
> Do you really think you'll have to subclass your Seq often ? I mean,
> you have DNA, proteins and this is it (you can add RNA if you want to
> nitpick). In this case, ie when your datatypes are fixed and you're not
> going to extend them, you can simply use a variant type.
In the case of a function like "length", there are certainly a lot of things
which satisfy it. Most of the time, writing Foo.length is OK, but there
are certainly times when it is less clear IMO. It seems what the original
poster misses here isn't so much OOP but overloading, since there is no
late-binding/open-recursion/implementation-inheritance though it turns out
that OCaml's object system gives you something like overloading. I prefer
Olivier's solution in this case
> module DNA =
> type t = ...
> let length = ...
> end
>
> module Protein =
> type t = ...
> let length = ...
> end
>
> and you can build a generic module for functions common in DNA and
> Protein :
>
> module Seq =
> type t =
> | DNA of DNA.t
> | Protein of Protein.t
> let length = function
> | DNA s -> DNA.length s
> | Protein s -> Protein.length s
> end
and, if we had true generics as in GCaml, we could write this
generic length =
DNA.t -> DNA.length
| Protein -> Protein.length
directly without the tagging and untagging. This would be my preferred
solution if it were in the language.
-- Brian
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2002-12-09 21:17 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-09 21:20 [Caml-list] How to throw away oop? onlyclimb
2002-12-09 13:45 ` Didier Le Botlan
2002-12-09 14:10 ` Olivier Andrieu
2002-12-09 18:43 ` brogoff
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox