caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] a generic print (ugly hack)
@ 2004-10-15 16:57 pad
  2004-10-15 17:20 ` Nicolas Cannasse
  0 siblings, 1 reply; 3+ messages in thread
From: pad @ 2004-10-15 16:57 UTC (permalink / raw)
  To: caml-list

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1256 bytes --]


the goal is to allow the programmer to write in his program for instance:

 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
 let v = ... (* big computation, big data structure *)
 let _ = print_string (generic_print v "(int * float * color) assoc") in
 ...
  

and to get on stdout:
 [[1; 3]; [2; 9; 8]; [3; 4]]
 [1; 3; 2; 9; 8; 3; 4]
 [(1, 2.02, Red); (2, 4.02, Yellow)];


the type of generic_print is
 'a -> string -> string

code:
 http://www.irisa.fr/prive/padiolea/hacks/generic_print.ml

the principle is that the toplevel of O'Caml know how to print value,
so by "reusing" the toplevel, our program can too.


It is slow, ugly, not robust,  but it can be helpful.
any suggestion or critics are welcome.


-- 
Yoann  Padioleau,  INSA de Rennes, France   
Opinions expressed here are only mine. Je n'écris qu'à titre personnel.
**____   Get Free. Be Smart.  Simply use Linux and Free Software.   ____**

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

* Re: [Caml-list] a generic print (ugly hack)
  2004-10-15 16:57 [Caml-list] a generic print (ugly hack) pad
@ 2004-10-15 17:20 ` Nicolas Cannasse
  2004-10-15 18:52   ` pad
  0 siblings, 1 reply; 3+ messages in thread
From: Nicolas Cannasse @ 2004-10-15 17:20 UTC (permalink / raw)
  To: padiolea, caml-list

> the goal is to allow the programmer to write in his program for instance:
>
>  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
>  let v = ... (* big computation, big data structure *)
>  let _ = print_string (generic_print v "(int * float * color) assoc") in
>  ...
>
>
> and to get on stdout:
>  [[1; 3]; [2; 9; 8]; [3; 4]]
>  [1; 3; 2; 9; 8; 3; 4]
>  [(1, 2.02, Red); (2, 4.02, Yellow)];
>
>
> the type of generic_print is
>  'a -> string -> string
>
> code:
>  http://www.irisa.fr/prive/padiolea/hacks/generic_print.ml
>
> the principle is that the toplevel of O'Caml know how to print value,
> so by "reusing" the toplevel, our program can too.
>
>
> It is slow, ugly, not robust,  but it can be helpful.
> any suggestion or critics are welcome.

I was thinking doing the same some times ago, but without relying on
toplevel (which stucks you to bytecode).
The idea was to be able to load CMI at runtime, and extract type
informations from it in order to correctly print and match types (this is
already what's doing ODLL with functions). It's quite a work and I didn't
finished it. CMI contains *exact* types informations which are not present
at runtime, but not structured in a convenient way for printing or matching
since they're directly dumped from the compiler type representation. But
once done, this would include some dynamism that ocaml lacks when
interacting with outter world (serialization for example).

Nicolas Cannasse

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

* Re: [Caml-list] a generic print (ugly hack)
  2004-10-15 17:20 ` Nicolas Cannasse
@ 2004-10-15 18:52   ` pad
  0 siblings, 0 replies; 3+ messages in thread
From: pad @ 2004-10-15 18:52 UTC (permalink / raw)
  To: Nicolas Cannasse; +Cc: padiolea, caml-list

"Nicolas Cannasse" <warplayer@free.fr> writes:

> I was thinking doing the same some times ago, but without relying on
> toplevel (which stucks you to bytecode).

I am not stuck to bytecode.
the main program can be compiled  to native code.


I can also do:
  ocamlopt unix.cmxa str.cmxa  generic_print.ml -o test_generic
  ocamlmktop -o calc.top unix.cma str.cma  generic_print.ml 


> The idea was to be able to load CMI at runtime, and extract type
> informations from it in order to correctly print and match types (this is
> already what's doing ODLL with functions). It's quite a work and I didn't
> finished it. CMI contains *exact* types informations which are not present
> at runtime, but not structured in a convenient way for printing or matching
> since they're directly dumped from the compiler type representation. But
> once done, this would include some dynamism that ocaml lacks when
> interacting with outter world (serialization for example).

I've coded too in a more cleaner way a trick to achieve the same kind of result (a generic print) by relying on camlp4.
I auto-generate string_of_....   function for each type (by doing an induction
 over the AST of the type definition via  camlp4).
It was cleaner.
But i tend to prefer this uglier version :)


> 
> Nicolas Cannasse

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

end of thread, other threads:[~2004-10-15 18:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-15 16:57 [Caml-list] a generic print (ugly hack) pad
2004-10-15 17:20 ` Nicolas Cannasse
2004-10-15 18:52   ` pad

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