* Reference to polymorphic function ?
@ 2005-09-13 12:38 Ingo Bormuth
2005-09-14 21:08 ` [Caml-list] " Stéphane Glondu
2005-09-14 23:48 ` Jacques Garrigue
0 siblings, 2 replies; 6+ messages in thread
From: Ingo Bormuth @ 2005-09-13 12:38 UTC (permalink / raw)
To: caml-list; +Cc: ingo
Hi list,
the following declaration of put_to_screen nicely accepts all kinds
of values as v. This is the expected behaviour.
# let put_to_screen v = print_string ( Marshal.to_string v [] ) ;;
val put_to_screen : 'a -> unit = <fun>
# put_to_screen "test" ;;
%%%%%test- : unit = ()
# put_to_screen 5 ;;
%%%%%E- : unit = ()
Nevertheless, if I store the polymorphic function in a reference to
create a closure for all kinds of output methodeis, any attemps to use
that function are thwarted by the type interferer.
# let put = ref put_to_screen ;;
val put : ('_a -> unit) ref = {contents = <fun>}
# !put "test" ;;
%%%%%%test- : unit = ()
# !put 5 ;;
This expression has type int but is here used with type string
How can I keep the interferer from explicitly resolving the type of v ?
# put;;
- : (string -> unit) ref = {contents = <fun>}
Thanks !
--
+--------------------------------------------------------+
| Ingo Bormuth, voicebox & telefax: +49-12125-10226517 |
| GnuPG key 86326EC9 at http://ibormuth.efil.de/contact |
+--------------------------------------------------------+
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Reference to polymorphic function ?
2005-09-13 12:38 Reference to polymorphic function ? Ingo Bormuth
@ 2005-09-14 21:08 ` Stéphane Glondu
2005-09-14 23:48 ` Jacques Garrigue
1 sibling, 0 replies; 6+ messages in thread
From: Stéphane Glondu @ 2005-09-14 21:08 UTC (permalink / raw)
To: Ingo Bormuth; +Cc: caml-list
Hi,
Ingo Bormuth wrote:
> How can I keep the interferer from explicitly resolving the type of v ?
It is not possible using mutable values. However, I didn't catch what
you want to do with thoses references: maybe there is another way to
achieve what you want?
--
Stephane Glondu
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Reference to polymorphic function ?
2005-09-13 12:38 Reference to polymorphic function ? Ingo Bormuth
2005-09-14 21:08 ` [Caml-list] " Stéphane Glondu
@ 2005-09-14 23:48 ` Jacques Garrigue
2005-09-15 6:31 ` Olivier Andrieu
2005-09-15 10:31 ` [SOLVED] " Ingo Bormuth
1 sibling, 2 replies; 6+ messages in thread
From: Jacques Garrigue @ 2005-09-14 23:48 UTC (permalink / raw)
To: ibormuth; +Cc: caml-list
From: Ingo Bormuth <ibormuth@efil.de>
> Nevertheless, if I store the polymorphic function in a reference to
> create a closure for all kinds of output methodeis, any attemps to use
> that function are thwarted by the type interferer.
>
> # let put = ref put_to_screen ;;
> val put : ('_a -> unit) ref = {contents = <fun>}
>
> # !put "test" ;;
> %%%%%%test- : unit = ()
>
> # !put 5 ;;
> This expression has type int but is here used with type string
>
> How can I keep the interferer from explicitly resolving the type of v ?
You cannot: this would be unsound.
Actually, this is the opposite: you want to tell in advance the
inferer that put is polymorphic, and that only polymorphic values
should be accepeted. The simplest way to do this is to define a new
type:
type put = {put: 'a -> unit} ;;
let put = {put = put_to_screen} ;;
put.put "test";;
put.put 5;;
---------------------------------------------------------------------------
Jacques Garrigue Nagoya University garrigue at math.nagoya-u.ac.jp
<A HREF=http://www.math.nagoya-u.ac.jp/~garrigue/>JG</A>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Reference to polymorphic function ?
2005-09-14 23:48 ` Jacques Garrigue
@ 2005-09-15 6:31 ` Olivier Andrieu
2005-09-15 7:00 ` Jacques Garrigue
2005-09-15 10:31 ` [SOLVED] " Ingo Bormuth
1 sibling, 1 reply; 6+ messages in thread
From: Olivier Andrieu @ 2005-09-15 6:31 UTC (permalink / raw)
To: Jacques Garrigue; +Cc: ibormuth, caml-list
Jacques Garrigue [Thursday 15 September 2005] :
>
> From: Ingo Bormuth <ibormuth@efil.de>
>
> > Nevertheless, if I store the polymorphic function in a reference
> > to create a closure for all kinds of output methodeis, any
> > attemps to use that function are thwarted by the type interferer.
> >
> > # let put = ref put_to_screen ;;
> > val put : ('_a -> unit) ref = {contents = <fun>}
> >
> > # !put "test" ;;
> > %%%%%%test- : unit = ()
> >
> > # !put 5 ;;
> > This expression has type int but is here used with type string
> >
> > How can I keep the interferer from explicitly resolving the type of v ?
>
> You cannot: this would be unsound.
> Actually, this is the opposite: you want to tell in advance the
> inferer that put is polymorphic, and that only polymorphic values
> should be accepeted. The simplest way to do this is to define a new
> type:
>
> type put = {put: 'a -> unit} ;;
> let put = {put = put_to_screen} ;;
> put.put "test";;
> put.put 5;;
did you mean :
type put = {put: 'a. 'a -> unit} ;;
or is the explicit polymorphic parameter not required anymore (like
polymorphic methods in class types) ?
--
Olivier
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Reference to polymorphic function ?
2005-09-15 6:31 ` Olivier Andrieu
@ 2005-09-15 7:00 ` Jacques Garrigue
0 siblings, 0 replies; 6+ messages in thread
From: Jacques Garrigue @ 2005-09-15 7:00 UTC (permalink / raw)
To: andrieu; +Cc: ibormuth, caml-list
From: Olivier Andrieu <andrieu@ijm.jussieu.fr>
> did you mean :
>
> type put = {put: 'a. 'a -> unit} ;;
>
> or is the explicit polymorphic parameter not required anymore (like
Thank you for the correction. I forgot the explicit quantifier, which
is required.
Jacques Garrigue
^ permalink raw reply [flat|nested] 6+ messages in thread
* [SOLVED] Reference to polymorphic function ?
2005-09-14 23:48 ` Jacques Garrigue
2005-09-15 6:31 ` Olivier Andrieu
@ 2005-09-15 10:31 ` Ingo Bormuth
1 sibling, 0 replies; 6+ messages in thread
From: Ingo Bormuth @ 2005-09-15 10:31 UTC (permalink / raw)
To: Jacques Garrigue, Stéphane Glondu; +Cc: caml-list, ingo
[-- Attachment #1: Type: text/plain, Size: 2298 bytes --]
On 2005-09-15 08:48, Jacques Garrigue wrote:
>
> The simplest way to do this is to define a new type:
>
> type put = {put: 'a -> unit} ;;
> let put = {put = put_to_screen} ;;
> put.put "test";;
> put.put 5;;
>
Thank you !!! That's exactly what I was looking for.
On 2005-09-14 23:08, Stéphane Glondu wrote:
>
> However, I didn't catch what
> you want to do with thoses references: maybe there is another way to
> achieve what you want?
>
Just for the notes:
I wrote a serilisation function to pack a quite complex data structure
into one string. That string is then used to feed a file, socket,
sha1 or encryption algorithm.
I want to reduce overhead and directly marshal the values to the
destination without generating the intermediate string whenever possible.
This is a simple profe of principal prototype :
type data =
| Int of int
| Float of float
| Str of string
;;
type target =
| Screen
| Channel of out_channel
;;
type put =
{
str: string -> unit ;
raw: 'a. 'a -> unit
}
;;
let serialize data target =
let put = ref {
str = ( fun s -> () );
raw = ( fun v -> () ) } in
let write_value key value =
!put.str ( "<" ^ key ^ ">" ) ;
!put.raw value ;
!put.str ( "</" ^ key ^ ">" ) in
let write_data () =
match data with
| Int i -> write_value "int" i
| Float f -> write_value "float" f
| Str s -> write_value "str" s in
let init_target =
match target with
| Screen -> put := {
str = print_string ;
raw = let f v = print_string ( Marshal.to_string v [] ) in f }
| Channel chan -> put := {
str = output_string chan ;
raw = let f v = ( Marshal.to_channel chan v [] ) in f } in
init_target ;
write_data ()
;;
serialize ( Int 123 ) Screen
;;
serialize ( Float 3.14 ) Screen
;;
serialize ( Str "Hello" ) ( Channel ( open_out "/tmp/seri" ) )
;;
--
+--------------------------------------------------------+
| Ingo Bormuth, voicebox & telefax: +49-12125-10226517 |
| GnuPG key 86326EC9 at http://ibormuth.efil.de/contact |
+--------------------------------------------------------+
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2005-09-15 10:34 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-13 12:38 Reference to polymorphic function ? Ingo Bormuth
2005-09-14 21:08 ` [Caml-list] " Stéphane Glondu
2005-09-14 23:48 ` Jacques Garrigue
2005-09-15 6:31 ` Olivier Andrieu
2005-09-15 7:00 ` Jacques Garrigue
2005-09-15 10:31 ` [SOLVED] " Ingo Bormuth
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox