Version française
Home     About     Download     Resources     Contact us    
Browse thread
toplevel with pre-installed printers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Gerd Stolpmann <info@g...>
Subject: Re: [Caml-list] toplevel with pre-installed printers
Am Freitag, den 20.01.2006, 09:29 +0100 schrieb Jean-Christophe
Filliatre:
> First define  your pretty-printers using the Ocaml  module Format. For
> instance, the GMP pretty-printers look like

In Ocamlnet, we use a similar trick, but get around Obj.magic:

let exec s =
  let l = Lexing.from_string s in
  let ph = !Toploop.parse_toplevel_phrase l in
  assert(Toploop.execute_phrase false Format.err_formatter ph)
;;

exec "#install_printer Mimestring.print_s_param;;";;
exec "#install_printer Neturl.print_url;;";;
exec "#install_printer Netbuffer.print_buffer;;";;
exec "#install_printer Netstream.print_in_obj_stream;;";;

This module can be simply compiled to bytecode and can be loaded into
the toplevel. As we are using findlib, a second trick automates this. In
META we have

archive(byte,toploop) =
    "netstring.cma netstring_top.cmo"

where netstring.cma contains the generic code and netstring_top.cma the
toplevel-specific code. So when the user types #require "netstring" the
toplevel printers are automatically available.

Gerd


> 
> == gmp_pp.ml =========================================================
> open Gmp
> open Format
> let z z = print_string (Z.string_from z)
> let q q = ...
> ======================================================================
> 
> then introduce another file installing the pretty-printers:
> 
> == install_gmp_pp.ml =================================================
> (* This is a hack to install the pretty-printers in the customized toplevel. *)
> 
> (* Caml longidents. *)
> type t =
>   | Lident of string
>   | Ldot of t * string
>   | Lapply of t * t
> 
> let _ = Topdirs.dir_directory "+creal"
> 
> let _ = Topdirs.dir_install_printer Format.std_formatter 
> 	  (Obj.magic (Ldot (Lident "Gmp_pp", "z")) : 'a)
> 
> let _ = Topdirs.dir_install_printer Format.std_formatter 
> 	  (Obj.magic (Ldot (Lident "Gmp_pp", "q")) : 'a)
> ======================================================================
> 
> Finally,  build your  ocaml toplevel  the usual  way, linking  the two
> files above:
> 
> ======================================================================
> ocamlgmp: gmp.cma gmp_pp.cmo install_gmp_pp.cmo
> 	ocamlmktop -custom -o $@ $^
> ======================================================================
> 
> I know  this is a  hack (the infamous  Obj.magic is used and  the Caml
> longident  type could change  in a  next ocaml  version) but  it works
> fine.
> 
> Hope this helps,
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Telefon: 06151/153855                  Telefax: 06151/997714
------------------------------------------------------------