Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Jean-Christophe Filliatre <filliatr@lri.fr>
Cc: Andrej.Bauer@andrej.com, Caml list <caml-list@inria.fr>
Subject: Re: [Caml-list] toplevel with pre-installed printers
Date: Fri, 20 Jan 2006 14:13:53 +0100	[thread overview]
Message-ID: <1137762833.11593.162.camel@localhost.localdomain> (raw)
In-Reply-To: <17360.40829.639123.509366@gargle.gargle.HOWL>

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


  reply	other threads:[~2006-01-20 13:14 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-19 16:28 Andrej Bauer
2006-01-19 16:53 ` [Caml-list] " Daniel Bünzli
2006-01-19 16:57 ` Eric Stokes
2006-01-19 17:08   ` Andrej Bauer
2006-01-19 17:49     ` Richard Jones
2006-01-19 19:12       ` Eric Cooper
2006-01-19 20:18         ` Richard Jones
2006-01-20  2:24           ` skaller
2006-01-20 16:49             ` David Brown
2006-01-20 19:29               ` skaller
2006-01-20 14:13     ` code17
2006-01-20  8:29 ` [Caml-list] " Jean-Christophe Filliatre
2006-01-20 13:13   ` Gerd Stolpmann [this message]
2006-01-19 19:40 Harrison, John R
2006-01-20  1:30 ` skaller
2006-01-20 20:19 Harrison, John R

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1137762833.11593.162.camel@localhost.localdomain \
    --to=info@gerd-stolpmann.de \
    --cc=Andrej.Bauer@andrej.com \
    --cc=caml-list@inria.fr \
    --cc=filliatr@lri.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox