caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Another q about many types
@ 2002-02-15  1:48 Ryan Tarpine
  2002-04-15  8:06 ` Christian Gillot
  0 siblings, 1 reply; 11+ messages in thread
From: Ryan Tarpine @ 2002-02-15  1:48 UTC (permalink / raw)
  To: caml-list

Many thanks for the advice on polymorphic variants; they seemed to be just 
what I needed.  But I've run into one small problem:  I can't define 
exceptions with arguments of polymorphic types.

In the language I'm trying to implement, to raise an exception, the user 
needs to create an instance of an exception class.  So, in my 
implementation, I wanted to define an exception whose sole argument would be 
an instance of a class.  But, I designed instances to be records containing 
a polymorphic variant, which makes the record type itself polymorphic.  Is 
there some way to raise exceptions with polymorphic arguments, or some other 
possible way of implementing this?

Thanks in advance,

Ryan Tarpine, rtarpine@hotmail.com
"To err is human, to compute divine.  Trust your computer but not its 
programmer."
  - Morris Kingston

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.

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


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

* [Caml-list] Weak hash table
  2002-04-15  8:06 ` Christian Gillot
@ 2002-03-05 16:28   ` Christophe Raffalli
  2002-03-05 21:40     ` Charles Martin
  2002-03-06  8:33     ` Jean-Christophe Filliatre
  0 siblings, 2 replies; 11+ messages in thread
From: Christophe Raffalli @ 2002-03-05 16:28 UTC (permalink / raw)
  Cc: caml-list


did anyone implemented weak hash table using weak pointers in OCaml ?

I need that, and if I can save work ...

-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
-------------------
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] 11+ messages in thread

* Re: [Caml-list] Weak hash table
  2002-03-05 16:28   ` [Caml-list] Weak hash table Christophe Raffalli
@ 2002-03-05 21:40     ` Charles Martin
  2002-03-05 22:24       ` Dave Mason
  2002-03-06  8:33     ` Jean-Christophe Filliatre
  1 sibling, 1 reply; 11+ messages in thread
From: Charles Martin @ 2002-03-05 21:40 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: caml-list

At 05:28 PM 3/5/02 +0100, Christophe Raffalli wrote:
>did anyone implemented weak hash table using weak pointers in OCaml ?

Perhaps the standard library Weak has what you need?... :)


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

-------------------
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] 11+ messages in thread

* Re: [Caml-list] Weak hash table
  2002-03-05 21:40     ` Charles Martin
@ 2002-03-05 22:24       ` Dave Mason
  2002-03-06 10:09         ` Christophe Raffalli
  2002-03-06 10:39         ` Christophe Raffalli
  0 siblings, 2 replies; 11+ messages in thread
From: Dave Mason @ 2002-03-05 22:24 UTC (permalink / raw)
  To: Charles Martin; +Cc: Christophe Raffalli, caml-list

A while ago I bugged Xavier about this.  He agreed it would be nice to
have weak hash tables, so if anyone volunteered an implementation, I
suspect it would quickly make its way into the standard library.

I haven't thought hard about it, but I think it needs some additional
hooks in the garbage collector.  (Without even *considering* the whole
heirarchy of weakness in Java!)

../Dave
-------------------
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] 11+ messages in thread

* Re: [Caml-list] Weak hash table
  2002-03-05 16:28   ` [Caml-list] Weak hash table Christophe Raffalli
  2002-03-05 21:40     ` Charles Martin
@ 2002-03-06  8:33     ` Jean-Christophe Filliatre
  1 sibling, 0 replies; 11+ messages in thread
From: Jean-Christophe Filliatre @ 2002-03-06  8:33 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 436 bytes --]


Christophe Raffalli writes:
 > 
 > did anyone implemented weak hash table using weak pointers in OCaml ?
 > 
 > I need that, and if I can save work ...

I did  it once,  but a very  naive implementation with  only functions
"create",  "add" and  "find" (I  finally ended  using some  other data
structure,  so I  didn't pursue).  Anyhow,  I attach  these two  small
files.

-- 
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)


[-- Attachment #2: whash.mi --]
[-- Type: application/octet-stream, Size: 124 bytes --]


type ('a,'b) t

val create : int -> ('a,'b) t

val add : ('a,'b) t -> 'a -> 'b -> unit

val find : ('a,'b) t -> 'a -> 'b



[-- Attachment #3: whash.ml --]
[-- Type: application/octet-stream, Size: 426 bytes --]


type ('a,'b) t = ('a * 'b) list Weak.t

let create = Weak.create

let add t x y =
  let n = Weak.length t in
  let i = (Hashtbl.hash x) mod n in
  let old  = match Weak.get t i with
    | None -> []
    | Some l -> l
  in
  Weak.set t i (Some ((x,y) :: old))

let find t x =
  let n = Weak.length t in
  let i = (Hashtbl.hash x) mod n in
  match Weak.get t i with
    | None -> raise Not_found
    | Some l -> List.assoc x l

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

* Re: [Caml-list] Weak hash table
  2002-03-05 22:24       ` Dave Mason
@ 2002-03-06 10:09         ` Christophe Raffalli
  2002-03-06 10:39         ` Christophe Raffalli
  1 sibling, 0 replies; 11+ messages in thread
From: Christophe Raffalli @ 2002-03-06 10:09 UTC (permalink / raw)
  To: Dave Mason; +Cc: Charles Martin, caml-list

Dave Mason a écrit :
> 
> A while ago I bugged Xavier about this.  He agreed it would be nice to
> have weak hash tables, so if anyone volunteered an implementation, I
> suspect it would quickly make its way into the standard library.
> 
> I haven't thought hard about it, but I think it needs some additional
> hooks in the garbage collector.  (Without even *considering* the whole
> heirarchy of weakness in Java!)
> 
> ../Dave

If you want key or value weak hashtable (that is entry are kept in the
hash table is the key of the value 
are pointed from somewhere outside the hashtable), it can be done with
just the Weak module
of Caml (you can almost do it by regexp query replace in hashtbl.ml). 

If you just want value (resp. key) weak hashtable (that is entry are
kept in the hash table if the value 
is pointed from somewhere outside the hashtable), It is possible also,
you just need to do a shallow copy of the key (resp. the value) before
insertion in the table. If you do not want to loose a little memory, you
need help from the GC.

Anyway if your key are integers or floats both are equivalent !

if you key are strings possibly long, the cost of shallow copy may be a
problem.

Just one trouble: if you want hashtabl with unique key all this is true.

If you want hashtable with multiple key (as in module Hashtbl) you need
a weak array of weak array ... it starts
to be a bit complex and may be help from the GC would be useful to get a
nicer code.

-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
-------------------
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] 11+ messages in thread

* Re: [Caml-list] Weak hash table
  2002-03-05 22:24       ` Dave Mason
  2002-03-06 10:09         ` Christophe Raffalli
@ 2002-03-06 10:39         ` Christophe Raffalli
  1 sibling, 0 replies; 11+ messages in thread
From: Christophe Raffalli @ 2002-03-06 10:39 UTC (permalink / raw)
  To: Dave Mason; +Cc: Charles Martin, caml-list


Sorry, my last mail may be wrong !

The semantics of weak pointer are just some kind of cache where value
can be erased at any time by the GC.

The kind of weak array you need is array where value are erased by the
GC if and only if the value is not pointed from outside the weak array.
That is the mark phase of the GC does not traverse weak array.



-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
-------------------
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] 11+ messages in thread

* Re: [Caml-list] Another q about many types
  2002-02-15  1:48 [Caml-list] Another q about many types Ryan Tarpine
@ 2002-04-15  8:06 ` Christian Gillot
  2002-03-05 16:28   ` [Caml-list] Weak hash table Christophe Raffalli
  0 siblings, 1 reply; 11+ messages in thread
From: Christian Gillot @ 2002-04-15  8:06 UTC (permalink / raw)
  To: Ryan Tarpine; +Cc: caml-list

El dv, 15-02-2002 a las 02:48, Ryan Tarpine escribió:
> 
> In the language I'm trying to implement, to raise an exception, the user 
> needs to create an instance of an exception class.  So, in my 
> implementation, I wanted to define an exception whose sole argument would be 
> an instance of a class.  But, I designed instances to be records containing 
> a polymorphic variant, which makes the record type itself polymorphic.  Is 
> there some way to raise exceptions with polymorphic arguments, or some other 
> possible way of implementing this?
I stumbled on this too ;o) Basically exception must only contains
concrete types (no polymorphics ones) otherwise it would break type
system. (Gurus can correct if this statement is wrong). This is also
why we need to use different operators for float (+.) and ints (+).
  So you've got to redesign your application in a way that it doesn't 
need polymorphic exceptions. 
  What you would need is Gcaml : pauillac.inria.fr/~furuse/generics,
but I recommend you to stick with Ocaml.

Regards,

-- 
Christian Gillot <cgillot@gruposbd.com>
GNU/Linux programmer
-------------------
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


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

* Re: [Caml-list] Another q about many types
  2002-02-16  3:16 Ryan Tarpine
@ 2002-02-18 14:04 ` Martin Jambon
  0 siblings, 0 replies; 11+ messages in thread
From: Martin Jambon @ 2002-02-18 14:04 UTC (permalink / raw)
  To: Ryan Tarpine; +Cc: caml-list

On Fri, 15 Feb 2002, Ryan Tarpine wrote:

> Unfortunately, this means that in order to raise an exception with an object
> of type my_object, not only its own object_data but the data of all the
> variables stored in object_ivs must be of the same type.  I've tried making
> the exception type like this:
>   type allowed_exc = [ `PNone | `PInteger of int | `PFloat of float |
> `PString of string ];;
>   exception EError of allowed_exc my_object;;
> to make the type of allowed exceptions somewhat lax.  But, given a single
> ('a my_object), how can I coerce it to an (allowed_exc my_object) if
> possible?  It has to recursively coerce all the variables in object_ivs.
> I've never done coercion at all before.

Try the following

  raise (EError (obj :> allowed_exc my_object))

instead of

  raise (EError obj)


Martin

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


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

* Re: [Caml-list] Another q about many types
@ 2002-02-16  3:16 Ryan Tarpine
  2002-02-18 14:04 ` Martin Jambon
  0 siblings, 1 reply; 11+ messages in thread
From: Ryan Tarpine @ 2002-02-16  3:16 UTC (permalink / raw)
  To: caml-list

>From: Martin Jambon <m.jambon@ibcp.fr>
>To: Ryan Tarpine <rtarpine@hotmail.com>
>Subject: Re: [Caml-list] Another q about many types
>Date: Fri, 15 Feb 2002 11:21:29 +0100 (CET)
>...
>Maybe something like this:
>
>exception E of exn
>...
>exception E_1 of t1
>...
>exception E_2 of t2
>...
>
>try ... with
>   E e ->
>     match e with
>        E_1 x1 -> x1
>      | E_2 x2 -> some_conversion_to_t1 x2
>
>
>Martin

I though this was a great idea, but then I hit another roadblock.  You 
couldn't see this was coming from the info I've given so far, of course  :)  
In actuality, my objects are more like this:
  type 'a my_object = { object_ivs : 'a my_object my_table; object_data : 'a 
ref }

object_ivs stores the object's independent variables (my_table is basically 
an association table, where a name lookup finds a variable; ignore it).  
object_data stores the object's primitive, the actual OCaml data (int, 
float, string, etc.), in a polymorphic variant.  Instances of the integer 
class, for example, will have something like (`PInteger x) stored in 
object_data.  object_ivs stores whole other objects, not primitives.  A 
user-defined class, for example, would have something like (`PNone) in 
object_data and all member variables in object_ivs.

Unfortunately, this means that in order to raise an exception with an object 
of type my_object, not only its own object_data but the data of all the 
variables stored in object_ivs must be of the same type.  I've tried making 
the exception type like this:
  type allowed_exc = [ `PNone | `PInteger of int | `PFloat of float | 
`PString of string ];;
  exception EError of allowed_exc my_object;;
to make the type of allowed exceptions somewhat lax.  But, given a single 
('a my_object), how can I coerce it to an (allowed_exc my_object) if 
possible?  It has to recursively coerce all the variables in object_ivs.  
I've never done coercion at all before.

Thanks in advance (everyone's helped me so much already!),

Ryan Tarpine, rtarpine@hotmail.com
"To err is human, to compute divine.  Trust your computer but not its 
programmer."
  - Morris Kingston

Ryan Tarpine, rtarpine@hotmail.com
"To err is human, to compute divine.  Trust your computer but not its 
programmer."
  - Morris Kingston

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.

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


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

* Re: [Caml-list] Another q about many types
@ 2002-02-15 11:04 Martin Jambon
  0 siblings, 0 replies; 11+ messages in thread
From: Martin Jambon @ 2002-02-15 11:04 UTC (permalink / raw)
  To: caml-list

On Thu, 14 Feb 2002, Ryan Tarpine wrote:

> Many thanks for the advice on polymorphic variants; they seemed to be just
> what I needed.  But I've run into one small problem:  I can't define
> exceptions with arguments of polymorphic types.
>
> In the language I'm trying to implement, to raise an exception, the user
> needs to create an instance of an exception class.  So, in my
> implementation, I wanted to define an exception whose sole argument would be
> an instance of a class.  But, I designed instances to be records containing
> a polymorphic variant, which makes the record type itself polymorphic.  Is
> there some way to raise exceptions with polymorphic arguments, or some other
> possible way of implementing this?

Maybe something like this:

exception E of exn
...
exception E_1 of t1
...
exception E_2 of t2
...

try ... with
  E e ->
    match e with
       E_1 x1 -> x1
     | E_2 x2 -> some_conversion_to_t1 x2



Martin

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


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

end of thread, other threads:[~2002-03-07  9:21 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-15  1:48 [Caml-list] Another q about many types Ryan Tarpine
2002-04-15  8:06 ` Christian Gillot
2002-03-05 16:28   ` [Caml-list] Weak hash table Christophe Raffalli
2002-03-05 21:40     ` Charles Martin
2002-03-05 22:24       ` Dave Mason
2002-03-06 10:09         ` Christophe Raffalli
2002-03-06 10:39         ` Christophe Raffalli
2002-03-06  8:33     ` Jean-Christophe Filliatre
2002-02-15 11:04 [Caml-list] Another q about many types Martin Jambon
2002-02-16  3:16 Ryan Tarpine
2002-02-18 14:04 ` Martin Jambon

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