From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id E84FDBB9C for ; Fri, 20 Jan 2006 14:14:00 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k0KDE0C5005424 for ; Fri, 20 Jan 2006 14:14:00 +0100 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA26702 for ; Fri, 20 Jan 2006 14:13:59 +0100 (MET) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.184]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k0KDDxW5016354 for ; Fri, 20 Jan 2006 14:13:59 +0100 Received: from [212.227.126.160] (helo=mrelayng.kundenserver.de) by moutng.kundenserver.de with esmtp (Exim 3.35 #1) id 1Ezw5S-0003hN-00; Fri, 20 Jan 2006 14:13:54 +0100 Received: from [84.58.146.30] (helo=gate.lan.gerd-stolpmann.de) by mrelayng.kundenserver.de with asmtp (Exim 3.35 #1) id 1Ezw5S-0004rR-00; Fri, 20 Jan 2006 14:13:54 +0100 Received: from flakew.lan.gerd-stolpmann.de (flakew.lan.gerd-stolpmann.de [192.168.0.32]) by gate.lan.gerd-stolpmann.de (Postfix) with ESMTP id AB3F0C10E; Fri, 20 Jan 2006 14:13:53 +0100 (CET) Subject: Re: [Caml-list] toplevel with pre-installed printers From: Gerd Stolpmann To: Jean-Christophe Filliatre Cc: Andrej.Bauer@andrej.com, Caml list In-Reply-To: <17360.40829.639123.509366@gargle.gargle.HOWL> References: <43CFBE3E.1090808@andrej.com> <17360.40829.639123.509366@gargle.gargle.HOWL> Content-Type: text/plain Date: Fri, 20 Jan 2006 14:13:53 +0100 Message-Id: <1137762833.11593.162.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.2.1.1 Content-Transfer-Encoding: 7bit X-Provags-ID: kundenserver.de abuse@kundenserver.de auth:a6865a839c0178d9aa0ce41878507ea2 X-Miltered: at concorde with ID 43D0E218.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 43D0E217.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 toplevel:01 gerd:01 stolpmann:01 filliatre:01 ocaml:01 ocamlnet:01 lexing:01 toploop:01 toplevel:01 toploop:01 netbuffer:01 buffer:01 netstream:01 bytecode:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.3 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 ------------------------------------------------------------