caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Converting "fun ... (type t) ..." into a caml < 3.12 code
@ 2011-01-26 12:58 Julien Signoles
  2011-01-26 15:17 ` Stefan Holdermans
  2011-01-26 16:55 ` Jeremy Yallop
  0 siblings, 2 replies; 5+ messages in thread
From: Julien Signoles @ 2011-01-26 12:58 UTC (permalink / raw)
  To: Caml List

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

Hello,

How to convert the following ocaml 3.12 code into a typable ocaml < 3.12
code?
I have a solution using Obj. Is it possible without Obj?

=====
(* val f: 'a -> 'a list *)
let f (type u) (x:u) =
  let module S =
    Set.Make(struct
      type t = u
      let compare = Pervasives.compare
      let equal = ( = )
    end)
  in
  S.elements (S.singleton x)
=====

The contraints are:
* do not change the type of f
* use the functor Set.Make where type of keys are the same that type of x

Thanks,
Julien

[-- Attachment #2: Type: text/html, Size: 600 bytes --]

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

* Re: [Caml-list] Converting "fun ... (type t) ..." into a caml < 3.12 code
  2011-01-26 12:58 [Caml-list] Converting "fun ... (type t) ..." into a caml < 3.12 code Julien Signoles
@ 2011-01-26 15:17 ` Stefan Holdermans
       [not found]   ` <AANLkTinyQn4L0C8hHHXJP20upzi++2oKU6RC-2ZTEjDA@mail.gmail.com>
  2011-01-26 16:55 ` Jeremy Yallop
  1 sibling, 1 reply; 5+ messages in thread
From: Stefan Holdermans @ 2011-01-26 15:17 UTC (permalink / raw)
  To: Julien Signoles; +Cc: Caml List

Julien,

> How to convert the following ocaml 3.12 code into a typable ocaml < 3.12 code?


Coincidentally, earlier today, I ran into a situation not that different from the one you describe. I haven't come up with a satisfying solution yet.

> I have a solution using Obj. Is it possible without Obj?

Are you willing to share this solution?

> =====
> (* val f: 'a -> 'a list *)
> let f (type u) (x:u) =
>   let module S =
>     Set.Make(struct
>       type t = u
>       let compare = Pervasives.compare
>       let equal = ( = )
>     end)
>   in
>   S.elements (S.singleton x)
> =====
> 
> The contraints are:
> * do not change the type of f
> * use the functor Set.Make where type of keys are the same that type of x

Cheers,

  Stefan


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

* [Caml-list] Converting "fun ... (type t) ..." into a caml < 3.12 code
       [not found]   ` <AANLkTinyQn4L0C8hHHXJP20upzi++2oKU6RC-2ZTEjDA@mail.gmail.com>
@ 2011-01-26 16:30     ` Julien Signoles
  0 siblings, 0 replies; 5+ messages in thread
From: Julien Signoles @ 2011-01-26 16:30 UTC (permalink / raw)
  To: Caml List

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

Sorry, forgot the list...

---------- Forwarded message ----------
From: Julien Signoles <julien.signoles@gmail.com>
Date: 2011/1/26
Subject: Re: [Caml-list] Converting "fun ... (type t) ..." into a caml <
3.12 code
To: Stefan Holdermans <stefan@vectorfabrics.com>




2011/1/26 Stefan Holdermans <stefan@vectorfabrics.com>

 > How to convert the following ocaml 3.12 code into a typable ocaml < 3.12
> code?
>
> Coincidentally, earlier today, I ran into a situation not that different
> from the one you describe. I haven't come up with a satisfying solution yet.
>
> > I have a solution using Obj. Is it possible without Obj?
>
> Are you willing to share this solution?
>

====
let g (x:'a) =

  let module S =
    Set.Make(struct
      type t = Obj.t

      let compare = Pervasives.compare
      let equal = ( = )
    end)
  in
  (Obj.magic (S.elements (S.singleton (Obj.repr x))) : 'a list)
(* less efficient but using Obj.obj instead of Obj.magic: *)
(*  List.rev (S.fold (fun o acc -> (Obj.obj o:'a) :: acc) (S.singleton
(Obj.repr x)) []) *)
====

Note that:
1) I'm pretty sure someone who initials are XL will be unhappy with such a
solution
2) For a real-world application [*], I prefer to leave a known bug instead
of fixing it using such a solution... And either waiting for a solution
without Obj or waiting for a future day where our software will require
ocaml 3.12

--
Julien

[*] Frama-C (http://frama-c.com)

[-- Attachment #2: Type: text/html, Size: 2222 bytes --]

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

* Re: [Caml-list] Converting "fun ... (type t) ..." into a caml < 3.12 code
  2011-01-26 12:58 [Caml-list] Converting "fun ... (type t) ..." into a caml < 3.12 code Julien Signoles
  2011-01-26 15:17 ` Stefan Holdermans
@ 2011-01-26 16:55 ` Jeremy Yallop
  2011-01-27 14:18   ` Julien Signoles
  1 sibling, 1 reply; 5+ messages in thread
From: Jeremy Yallop @ 2011-01-26 16:55 UTC (permalink / raw)
  To: Julien Signoles; +Cc: Caml List

On 26 January 2011 12:58, Julien Signoles <julien.signoles@gmail.com> wrote:
> How to convert the following ocaml 3.12 code into a typable ocaml < 3.12
> code?
> I have a solution using Obj. Is it possible without Obj?

There are safer approaches (than Obj) to a "universal type" described here:

    http://ocaml.janestreet.com/?q=node/18

Whether your problem can be solved with such an approach depends on
how flexible your definition of "the same [type]" is.  You can
certainly write a function of the same type and behaviour as 'f' that
way, though.

Jeremy.

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

* Re: [Caml-list] Converting "fun ... (type t) ..." into a caml < 3.12 code
  2011-01-26 16:55 ` Jeremy Yallop
@ 2011-01-27 14:18   ` Julien Signoles
  0 siblings, 0 replies; 5+ messages in thread
From: Julien Signoles @ 2011-01-27 14:18 UTC (permalink / raw)
  To: Jeremy Yallop; +Cc: Caml List

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

Hello,

2011/1/26 Jeremy Yallop <yallop@gmail.com>

> On 26 January 2011 12:58, Julien Signoles <julien.signoles@gmail.com>
> wrote:
> > How to convert the following ocaml 3.12 code into a typable ocaml < 3.12
> > code?
> > I have a solution using Obj. Is it possible without Obj?
>
> There are safer approaches (than Obj) to a "universal type" described here:
>
>    http://ocaml.janestreet.com/?q=node/18
>
> Whether your problem can be solved with such an approach depends on
> how flexible your definition of "the same [type]" is.  You can
> certainly write a function of the same type and behaviour as 'f' that
> way, though.
>

I'm aware of such an universal type. It works fine for the small example of
my original post. However I see no way to use it on my real case because it
is a bit more complex. Actually it looks like:
=====
(* val f: 'a u -> 'a list u *)
type 'a u = ... (* a phantom type, defined in another file *)
let f (type v) (x:v u) =
  let module S =
    F(struct
      type t = v u (* waiting a type t of the form w u for some w *)
      let x = x
      (* an additional list of operations over t *)
      let equal = ( = ) (* ... *)
    end)
  in
  (S.y : v list u) (* S.y is opaque: no way to destruct it *)
=====
I see no way to inject and project values [x] and/or [S.y] to any universal
type since the conversion from [x] to [S.y] is done internally by the
functor F.

Thanks for your help,
Julien

[-- Attachment #2: Type: text/html, Size: 2002 bytes --]

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

end of thread, other threads:[~2011-01-27 14:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-26 12:58 [Caml-list] Converting "fun ... (type t) ..." into a caml < 3.12 code Julien Signoles
2011-01-26 15:17 ` Stefan Holdermans
     [not found]   ` <AANLkTinyQn4L0C8hHHXJP20upzi++2oKU6RC-2ZTEjDA@mail.gmail.com>
2011-01-26 16:30     ` Julien Signoles
2011-01-26 16:55 ` Jeremy Yallop
2011-01-27 14:18   ` Julien Signoles

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