caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Read-only arrays ?
@ 2007-04-10 21:03 David Teller
  2007-04-10 21:35 ` [Caml-list] " Martin Jambon
  0 siblings, 1 reply; 7+ messages in thread
From: David Teller @ 2007-04-10 21:03 UTC (permalink / raw)
  To: OCaml

	Hi everyone,

  Is there a simple manner of providing read-only/write-only arrays ? I
was hoping that a simple hack using co-variance or contra-variance would
work, but, well, after a few minutes of thought, turns out that it
doesn't.

Cheers,
 David

-- 
David Teller ------------------------------------------
Security of Distributed Systems -----------------------
-- http://www.univ-orleans.fr/lifo/Members/David.Teller
----- Laboratoire d'Informatique Fondamentale d'Orleans


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Read-only arrays ?
  2007-04-10 21:03 Read-only arrays ? David Teller
@ 2007-04-10 21:35 ` Martin Jambon
  2007-04-10 21:57   ` Harrison, John R
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Jambon @ 2007-04-10 21:35 UTC (permalink / raw)
  To: David Teller; +Cc: OCaml

On Tue, 10 Apr 2007, David Teller wrote:

> 	Hi everyone,
>
>   Is there a simple manner of providing read-only/write-only arrays ? I
> was hoping that a simple hack using co-variance or contra-variance would
> work, but, well, after a few minutes of thought, turns out that it
> doesn't.

This topic comes up from time to time... My take on this is that read-only
arrays or strings are not badly needed.

http://caml.inria.fr/pub/ml-archives/caml-list/2006/05/b34caf7bc9a5b651e91706392de65845.en.html

I wrote this small interface for R/W/RW strings last year, for fun; I
don't use it at all:

http://martin.jambon.free.fr/ocaml.html#gstring


Martin

--
Martin Jambon
http://martin.jambon.free.fr


^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [Caml-list] Read-only arrays ?
  2007-04-10 21:35 ` [Caml-list] " Martin Jambon
@ 2007-04-10 21:57   ` Harrison, John R
  2007-04-10 23:12     ` Martin Jambon
  0 siblings, 1 reply; 7+ messages in thread
From: Harrison, John R @ 2007-04-10 21:57 UTC (permalink / raw)
  To: Martin Jambon; +Cc: OCaml, David Teller

| This topic comes up from time to time... My take on this is that
read-only
| arrays or strings are not badly needed.

Of course, this is a matter of opinion. In the end you didn't offer
a solution to the specific problem I had. I'll still need to adopt
an expensive and/or ugly workaround if I really want to protect myself
against OCaml's mutable strings. However, I accept that my application
is not typical.

John.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [Caml-list] Read-only arrays ?
  2007-04-10 21:57   ` Harrison, John R
@ 2007-04-10 23:12     ` Martin Jambon
  2007-04-11  5:08       ` David Teller
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Jambon @ 2007-04-10 23:12 UTC (permalink / raw)
  To: Harrison, John R; +Cc: Martin Jambon, OCaml, David Teller

On Tue, 10 Apr 2007, Harrison, John R wrote:

> | This topic comes up from time to time... My take on this is that
> read-only
> | arrays or strings are not badly needed.
>
> Of course, this is a matter of opinion. In the end you didn't offer
> a solution to the specific problem I had. I'll still need to adopt
> an expensive and/or ugly workaround if I really want to protect myself
> against OCaml's mutable strings. However, I accept that my application
> is not typical.

It seems to me that you don't have a problem, but you imagine you have
one... which is much better than the other way around :-)

http://caml.inria.fr/pub/ml-archives/caml-list/2006/05/f603bc5e2fec357d8a36d807664072e6.en.html


Cheers,

Martin

--
Martin Jambon
http://martin.jambon.free.fr


^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [Caml-list] Read-only arrays ?
  2007-04-10 23:12     ` Martin Jambon
@ 2007-04-11  5:08       ` David Teller
  2007-04-11  5:31         ` Jacques Garrigue
  0 siblings, 1 reply; 7+ messages in thread
From: David Teller @ 2007-04-11  5:08 UTC (permalink / raw)
  To: OCaml

In my case, I'm writing a small board game my students will need to
complete in an assignment. So, I can trust that, if something can go
wrong, they'll find a way to make it happen.

Essentially, the board is a matrix, abstracted in a module.

Among other things, module Board provides 
* a type Board.square of squares
* a type Board.t of boards (internally defined as a Board.square array
array)
* a conversion function Board.to_array : Board.t -> Board.square array
array (a copy function for the matrix)
* a conversion function Board.from_array : Board.square array array ->
Board.t (a copy function for the matrix)

While performance is hardly critical, it's intellectually unsatisfying
to have and copy the whole board each time the borders of the module are
crossed, just to ensure that my students won't do anything wrong enough
to break the module.

Cheers,
 David

On Tue, 2007-04-10 at 16:12 -0700, Martin Jambon wrote:
> On Tue, 10 Apr 2007, Harrison, John R wrote:
> 
> > | This topic comes up from time to time... My take on this is that
> > read-only
> > | arrays or strings are not badly needed.
> >
> > Of course, this is a matter of opinion. In the end you didn't offer
> > a solution to the specific problem I had. I'll still need to adopt
> > an expensive and/or ugly workaround if I really want to protect myself
> > against OCaml's mutable strings. However, I accept that my application
> > is not typical.
> 
> It seems to me that you don't have a problem, but you imagine you have
> one... which is much better than the other way around :-)
> 
> http://caml.inria.fr/pub/ml-archives/caml-list/2006/05/f603bc5e2fec357d8a36d807664072e6.en.html
> 
> 
> Cheers,
> 
> Martin
> 
> --
> Martin Jambon
> http://martin.jambon.free.fr
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
-- 
David Teller ------------------------------------------
Security of Distributed Systems -----------------------
-- http://www.univ-orleans.fr/lifo/Members/David.Teller
----- Laboratoire d'Informatique Fondamentale d'Orleans


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Read-only arrays ?
  2007-04-11  5:08       ` David Teller
@ 2007-04-11  5:31         ` Jacques Garrigue
  2007-04-11  5:50           ` Jon Harrop
  0 siblings, 1 reply; 7+ messages in thread
From: Jacques Garrigue @ 2007-04-11  5:31 UTC (permalink / raw)
  To: David.Teller; +Cc: caml-list

From: David Teller <David.Teller@univ-orleans.fr>

> In my case, I'm writing a small board game my students will need to
> complete in an assignment. So, I can trust that, if something can go
> wrong, they'll find a way to make it happen.
> 
> Essentially, the board is a matrix, abstracted in a module.
> 
> Among other things, module Board provides 
> * a type Board.square of squares
> * a type Board.t of boards (internally defined as a Board.square array
> array)
> * a conversion function Board.to_array : Board.t -> Board.square array
> array (a copy function for the matrix)
> * a conversion function Board.from_array : Board.square array array ->
> Board.t (a copy function for the matrix)
> 
> While performance is hardly critical, it's intellectually unsatisfying
> to have and copy the whole board each time the borders of the module are
> crossed, just to ensure that my students won't do anything wrong enough
> to break the module.

If your board is already abstract, why convert it to arrays?
You could provide the right access functions.
Or is it because you want to be able to update it destructively: then
you need mutable arrays.
If you don't care at all about performance, using lists of lists is
yet another option.

This is actually the strongest argument against immutable arrays: they
are just an abstraction over arrays, that you can easily make yourself
when needed. This is more cumbersome with strings, as we use literals
everywhere. 

Jacques Garrigue


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Read-only arrays ?
  2007-04-11  5:31         ` Jacques Garrigue
@ 2007-04-11  5:50           ` Jon Harrop
  0 siblings, 0 replies; 7+ messages in thread
From: Jon Harrop @ 2007-04-11  5:50 UTC (permalink / raw)
  To: caml-list

On Wednesday 11 April 2007 06:31, Jacques Garrigue wrote:
> If your board is already abstract, why convert it to arrays?

Yes, the API is the problem and not the lack of immutable arrays. Just pull 
the array functions that you use into the Board module, e.g. Board.init. 
Essentially, you'll be coalescing an immutable array implementation with your 
Board module.

You may also want to add a "where" function that accepts a continuation and 
applies it to the board with one element changed, undoing the change upon 
return. I find that to be a useful idiom.

> This is actually the strongest argument against immutable arrays: they
> are just an abstraction over arrays, that you can easily make yourself
> when needed.

I disagree. Syntax is important enough that strings and arrays have 
abbreviated (and clearer) syntaxes for getting and setting elements in OCaml. 
You cannot add that to your own immutable array implementation (unless you 
use macros as well, in which case you've forked the language itself).

F# is very nice in this respect, allowing the a.[i] syntax to be overloaded so 
that strings, arrays, vectors, matrices, maps, hash tables and so on can all 
share the same syntax.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-04-11  5:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-10 21:03 Read-only arrays ? David Teller
2007-04-10 21:35 ` [Caml-list] " Martin Jambon
2007-04-10 21:57   ` Harrison, John R
2007-04-10 23:12     ` Martin Jambon
2007-04-11  5:08       ` David Teller
2007-04-11  5:31         ` Jacques Garrigue
2007-04-11  5:50           ` Jon Harrop

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).