* string_of_polymorphic
@ 2005-01-03 9:52 Luca Pascali
2005-01-03 10:18 ` [Caml-list] string_of_polymorphic Jon Harrop
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Luca Pascali @ 2005-01-03 9:52 UTC (permalink / raw)
To: caml-list
Hi everyone and Happy 2005.
As the subject says, my aim is to write a function that is able to
covert into a string a generic polymorphic constructor, or at least its
name, without using patter matching.
Something like the operation that the ocaml toplevel already does:
# let a = `Hi;;
val a : [> `Hi ] = `Hi
-------------------^ I'd like to have this string
I tried using the Obj module, but I got only segmentation faults.
I know that the Obj module has not to be used, but what I want to write
will be placed into a library with a signature like this:
val string_of_polymorphic : [> `Dummy ] -> string
Thanks in advance to anyone for hints, or links, or wathever help you
can give me.
Luca
--
*********************************************************************
Luca Pascali
luca@barettadeit.com
asxcaml-guru@barettadeit.com
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL
tel. 02 370 111 55
fax. 02 370 111 54
Our technology:
http://www.asxcaml.org/
http://www.freerp.org/
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] string_of_polymorphic
2005-01-03 9:52 string_of_polymorphic Luca Pascali
@ 2005-01-03 10:18 ` Jon Harrop
2005-01-03 11:01 ` Luca Pascali
[not found] ` <41D9211D.7060003@yahoo.it>
2005-01-03 16:27 ` pad
2005-01-05 18:17 ` Alex Baretta
2 siblings, 2 replies; 6+ messages in thread
From: Jon Harrop @ 2005-01-03 10:18 UTC (permalink / raw)
To: caml-list
On Monday 03 January 2005 09:52, Luca Pascali wrote:
> As the subject says, my aim is to write a function that is able to
> covert into a string a generic polymorphic constructor, or at least its
> name, without using patter matching.
Why do you want to do this?
> ...
> Thanks in advance to anyone for hints, or links, or wathever help you
> can give me.
I think the run-time representation of a polymorphic variants' value is a hash
of its name and, therefore, cannot be mapped back onto a string in general.
There may be something else you can do specifically for the top-level but I
can't think what...
Cheers,
Jon.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] string_of_polymorphic
2005-01-03 10:18 ` [Caml-list] string_of_polymorphic Jon Harrop
@ 2005-01-03 11:01 ` Luca Pascali
[not found] ` <41D9211D.7060003@yahoo.it>
1 sibling, 0 replies; 6+ messages in thread
From: Luca Pascali @ 2005-01-03 11:01 UTC (permalink / raw)
To: caml-list
I resend the message, because I sent it only to Jon
Sorry.
Jon Harrop wrote:
> On Monday 03 January 2005 09:52, Luca Pascali wrote:
>
>
>> As the subject says, my aim is to write a function that is able to
>> covert into a string a generic polymorphic constructor, or at least its
>> name, without using patter matching.
>>
>
>
> Why do you want to do this?
>
>
>
Logging purposes, Error handling and so on.
I have a function that accept some polymorphic constructors as input
type, and I want to log what is arrived before possibly without writing
everytime a pattern matching ad-hoc, or raising an exception, I'd like
to attach the constructor that raised the exception.
>> ...
>> Thanks in advance to anyone for hints, or links, or wathever help you
>> can give me.
>>
>
>
> I think the run-time representation of a polymorphic variants' value
> is a hash of its name and, therefore, cannot be mapped back onto a
> string in general. There may be something else you can do specifically
> for the top-level but I can't think what...
>
> Cheers,
> Jon.
>
>
>
I don't know.
Polymorphic constructors survive to the marshalling-unmarshalling.
In the marshalled string, there is a representation of their name.
That's why I thought there was a method to get their names into a function.
I think that if Obj is strongly not recommended, analizing a marshalled
string has to be avoided, so I was looking for other ways.
Luca
--
*********************************************************************
Luca Pascali
luca@barettadeit.com
asxcaml-guru@barettadeit.com
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL
tel. 02 370 111 55
fax. 02 370 111 54
Our technology:
http://www.asxcaml.org/
http://www.freerp.org/
^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <41D9211D.7060003@yahoo.it>]
* Re: [Caml-list] string_of_polymorphic
2005-01-03 9:52 string_of_polymorphic Luca Pascali
2005-01-03 10:18 ` [Caml-list] string_of_polymorphic Jon Harrop
@ 2005-01-03 16:27 ` pad
2005-01-05 18:17 ` Alex Baretta
2 siblings, 0 replies; 6+ messages in thread
From: pad @ 2005-01-03 16:27 UTC (permalink / raw)
To: Luca Pascali; +Cc: caml-list
Luca Pascali <pasckosky2000@yahoo.it> writes:
> Hi everyone and Happy 2005.
>
> As the subject says, my aim is to write a function that is able to
> covert into a string a generic polymorphic constructor, or at least its
> name, without using patter matching.
> Something like the operation that the ocaml toplevel already does:
>
> # let a = `Hi;;
> val a : [> `Hi ] = `Hi
> -------------------^ I'd like to have this string
>
> I tried using the Obj module, but I got only segmentation faults.
> I know that the Obj module has not to be used, but what I want to write
> will be placed into a library with a signature like this:
>
> val string_of_polymorphic : [> `Dummy ] -> string
I have a function generic_print that can do some of the stuff you want
(but it is done in a ugly way):
example:
let _ =
(print_string (generic_print [[1;3];[2;9;8];[3;4]] "int list list");
print_string (generic_print [1;3;2;9;8;3;4] "int list");
print_string (generic_print `Hi "[> `Hi | `Ho]") in
()
==>
[[1; 3]; [2; 9; 8]; [3; 4]]
[1; 3; 2; 9; 8; 3; 4]
`Hi
code:
(*********************************************************************************************************)
(*
a poor's man haskell 'show' function for O'Caml
requirment: must have a 'corresponding' toplevel called calc.top
(but your program can be a normal program, even a natively compiled program)
cons:
need provide type information (via string so not robust)
slow
ugly
pro:
it works
compilation:
ocamlc unix.cma str.cma generic_print.ml -o test_generic
ocamlmktop -o calc.top unix.cma str.cma generic_print.ml
example:
let _ = print_string (generic_print [[1;3];[2;9;8];[3;4]] "int list list" in
let _ = print_string (generic_print [1;3;2;9;8;3;4] "int list") in
(see end of file)
test:
./test_generic
==>
[[1; 3]; [2; 9; 8]; [3; 4]]
[1; 3; 2; 9; 8; 3; 4]
*)
(*********************************************************************************************************)
let write_value valu filename =
let chan = open_out filename in
((* output_value chan valu;*) (* <=> Marshal.to_channel *)
Marshal.to_channel chan valu [Marshal.Closures];
close_out chan)
let get_value filename =
let chan = open_in filename in
let x = input_value chan in (* <=> Marshal.from_channel *)
(close_in chan; x)
let (=~) s re = Str.string_match (Str.regexp re) s 0
(* beurk, side effect code, but hey, it is convenient *)
let (matched: int -> string -> string) = fun i s ->
Str.matched_group i s
let matched1 = fun s -> matched 1 s
let cat file =
let chan = open_in file in
let rec aux () =
try
(* cant do input_line chan::aux() cos ocaml eval from right to left ! *)
let l = input_line chan in
l :: aux ()
with End_of_file -> [] in
aux()
let (+>) o f = f o
let rec drop_while p = function
| [] -> []
| x::xs -> if p x then drop_while p xs else x::xs
let (unlines: string list -> string) = fun s -> (String.concat "\n" s)
let tail = List.tl
let pr s = (print_string s; print_string "\n"; flush stdout)
(*********************************************************************************************************)
(* TODO optimisation: use a pipe, so dont fork each time for each print (and will be reentrant) *)
let (generic_print: 'a -> string -> string) = fun v typ ->
(write_value v "/tmp/generic_print";
ignore(Unix.system("printf 'let (v:" ^ typ ^ ") = Generic_print.get_value \"/tmp/generic_print\" in v;;' | ./calc.top > /tmp/result_generic_print"));
cat "/tmp/result_generic_print"
+> drop_while (fun e -> not (e =~ "^#.*"))
+> tail
+> (fun xs ->
let (hd, tl) = (List.hd xs, List.tl xs) in
if (hd =~ ".*=[ ]*\\(.*\\)")
then (matched1 hd::tl) +> unlines
else "error in generic_print, not good format:" ^ (unlines xs)
)
)
(*********************************************************************************************************)
(* example: *)
let main () =
(pr (generic_print [[1;3];[2;9;8];[3;4]] "int list list");
pr (generic_print [1;3;2;9;8;3;4] "int list");
pr (generic_print `Hi "[> `Hi | `Ho]");
)
let _ = if not !Sys.interactive then (main ())
>
> Thanks in advance to anyone for hints, or links, or wathever help you
> can give me.
>
> Luca
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] string_of_polymorphic
2005-01-03 9:52 string_of_polymorphic Luca Pascali
2005-01-03 10:18 ` [Caml-list] string_of_polymorphic Jon Harrop
2005-01-03 16:27 ` pad
@ 2005-01-05 18:17 ` Alex Baretta
2 siblings, 0 replies; 6+ messages in thread
From: Alex Baretta @ 2005-01-05 18:17 UTC (permalink / raw)
To: Luca Pascali; +Cc: caml-list
Luca Pascali wrote:
> # let a = `Hi;;
> val a : [> `Hi ] = `Hi
> -------------------^ I'd like to have this string
>
> I tried using the Obj module, but I got only segmentation faults.
> I know that the Obj module has not to be used, but what I want to write
> will be placed into a library with a signature like this:
>
> val string_of_polymorphic : [> `Dummy ] -> string
Ciao Luca,
I have a solution to the problem: it requires Camlp4. It is possible to
build a syntax extension such that
type poly = [ `Pizza | `Pie ]
actually compiles down to
type poly = [ `Pizza | `Pie ]
let string_of_poly = function
| `Pizza -> "Pizza"
| `Pie -> "Pie"
Let me refer you to the relevant documentation:
http://caml.inria.fr/camlp4/manual/manual005.html#toc11
A similar job is done by IoXml. Take a look at it: you probably want to
start out by modifying Daniel's code rather than starting from scratch.
http://pauillac.inria.fr/~ddr/IoXML/
Have fun!
Alex
--
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL
tel. +39 02 370 111 55
fax. +39 02 370 111 54
Our technology:
The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>
The FreerP Project
<http://www.freerp.org/>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2005-01-05 18:17 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-03 9:52 string_of_polymorphic Luca Pascali
2005-01-03 10:18 ` [Caml-list] string_of_polymorphic Jon Harrop
2005-01-03 11:01 ` Luca Pascali
[not found] ` <41D9211D.7060003@yahoo.it>
[not found] ` <200501031100.27306.jon@jdh30.plus.com>
2005-01-03 11:06 ` Luca Pascali
2005-01-03 16:27 ` pad
2005-01-05 18:17 ` Alex Baretta
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox