caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Patrick M Doane <patrick@watson.org>
To: John Max Skaller <skaller@ozemail.com.au>
Cc: Jun Furuse <Jun.Furuse@inria.fr>, caml-list@inria.fr
Subject: Re: "Re: [Caml-list] A G'Caml question" + additional info
Date: Sat, 30 Jun 2001 12:01:45 -0400 (EDT)	[thread overview]
Message-ID: <Pine.BSF.3.96.1010630113150.62324A-100000@fledge.watson.org> (raw)
In-Reply-To: <3B3D503C.E91DDE34@ozemail.com.au>

On Sat, 30 Jun 2001, John Max Skaller wrote:

> 	G'caml makes it easier to write readable algorithms,
> and to do 'cut and paste' genericity. But it can't do what
> you can do in C++. 
> 
> 	This is a good thing, since you can do some pretty
> unprincipled things in C++.

I agree.  Although I'm not advocating that it do everything you can do in
C++.

> 	Sure, and in C++ the iterators just don't work.

Could you elaborate on this?

> > The proposed 'include' feature makes it much easier to
> > extend generic values, but doesn't help with derived generics. 
> 
> 	One step at a time. If you _really_ want to do high level
> polymorphism, you'll have to use a more modern research language
> like FISh 2. 

What's in the works for the next version of FISh? The current version is
very interesting, but not too useful for me until more support for data
structures is added.

> 	Objects can help, but really that isn't the answer.
> The answer is more like: what can we do to make C++ like
> generics properly parametric?
> 
> 	It is possible to make STL like algorithms in Ocaml now.
> The problem is that you have to pass in a LOT of data, such as
> functions to deref and increment the iterator.

I think that one can implement much of the STL algorithms in the Caml
object system (avoiding the need to pass lots of data). It's not very
convenient because of the need to convert primitive values into objects
though. I'm still a novice with the object type system so we can probably
improve this a bit, but here is a basic implementation of STL iterators in
Caml object style:

class ['a] list_iterator list_init = object
  (* is there a better way to define eq here? *)
  val list = list_init
  method get_list = list
  method eq (x : 'a list_iterator) = x#get_list = list
  method get : 'a = List.hd list
  method incr = new list_iterator (List.tl list)
end

class ['a] list_obj l = object
  method iter_begin : 'a list_iterator = new list_iterator l
  method iter_end : 'a list_iterator   = new list_iterator []
end

class ['a] array_iterator arr_init idx_init = object
  val arr = arr_init
  val idx = idx_init
  method get_arr = arr
  method get_idx = idx
  method eq (x : 'a array_iterator) = x#get_arr == arr && x#get_idx = idx
  method get : 'a = Array.get arr idx
  method incr = new array_iterator arr (idx + 1)
end

class ['a] array_obj a = object
  method iter_begin : 'a array_iterator = new array_iterator a 0
  method iter_end : 'a array_iterator   = new array_iterator a
(Array.length a)
end

let rec for_each f first last =
  if first#eq last then ()
  else (
    f first#get;
    for_each f (first#incr) last
  )

let l = new list_obj [1;2;3]
let a = new array_obj [|1;2;3|]
;;
for_each print_int l#iter_begin l#iter_end;
for_each print_int a#iter_begin a#iter_end

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


       reply	other threads:[~2001-06-30 16:02 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <3B3D503C.E91DDE34@ozemail.com.au>
2001-06-30 16:01 ` Patrick M Doane [this message]
2001-06-30 20:59   ` Brian Rogoff
2001-07-01  5:32     ` Patrick M Doane
2001-07-02 15:55       ` Brian Rogoff
2001-07-10 18:08         ` Patrick M Doane
2001-07-16 18:24 John R Harrison
  -- strict thread matches above, loose matches on Subject: below --
2001-07-11 14:30 Krishnaswami, Neel
2001-07-11 16:22 ` Brian Rogoff
2001-07-11 16:35   ` Bruce Hoult
2001-07-11 19:12     ` Markus Mottl
2001-07-12  3:15   ` Patrick M Doane
2001-07-10 18:21 Krishnaswami, Neel
2001-07-11  6:09 ` Sven
     [not found] <3B3BB6EC.3DEB6CBF@ozemail.com.au>
2001-06-29  4:18 ` Patrick M Doane
2001-06-20  3:16 [Caml-list] A G'Caml question Brian Rogoff
2001-06-25 17:11 ` "Re: [Caml-list] A G'Caml question" + additional info Jun Furuse
2001-06-28  2:21   ` Patrick M Doane
2001-06-28  4:40     ` Brian Rogoff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.BSF.3.96.1010630113150.62324A-100000@fledge.watson.org \
    --to=patrick@watson.org \
    --cc=Jun.Furuse@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=skaller@ozemail.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).