[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

ocaml-findlib: patch for "num" library printers



Hi all,
  I've just added a patch to the findlib package that install,
additionally to the upstram stuff, a .cma library containing toploop
printers for big_int, nat, ratio and num types.

I've also changed the META for the "num" library so that it loads this
.cma when the library is loaded with findlib's #require directive.

The result is as follow:

  zack@fistandantilus:/tmp$ ledit ocaml
        Objective Caml version 3.06

  # #use "topfind";;
  Findlib has been successfully loaded. Additional directives:
    #require "package";;      to load a package
    #list;;                   to list the available packages
    #camlp4o;;                to load camlp4 (standard syntax)
    #camlp4r;;                to load camlp4 (revised syntax)
    Topfind.reset();;         to force that packages will be reloaded

  - : unit = ()
  # #require "num";;
  Loading /usr/lib/ocaml/3.06/nums.cma
  Loading /usr/lib/ocaml/3.06/num_top.cma
  # Big_int.big_int_of_string "1234567890";;
  - : Big_int.big_int = <big_int 1234567890>
  # 

while usually you would have got only a:

  - : Big_int.big_int = <abstr>

If nobody complains I will upload this patch along with a new version of
the ocaml findlib package. Otherwise I would not apply the patch to the
debian package (I've started using dpatch for findlib so that reverting
the change is really easy) and use them only locally.

For the records I attach the patch.

Cheers.

PS Gerd, if you like the attached patch you can obviously embed it
   directly in the findlib sources.

-- 
Stefano Zacchiroli  --  Master in Computer Science @ Uni. Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it}  -  http://www.bononia.it/zack/
"  I know you believe you understood what you think I said, but I am not
sure you realize that what you heard is not what I meant!  " -- G.Romney
#!/bin/sh -e
## 29_num_printers.dpatch by Stefano Zacchiroli <zack@debian.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: No description.

if [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi

[ -f debian/patches/00patch-opts ] && . debian/patches/00patch-opts
patch_opts="${patch_opts:--f --no-backup-if-mismatch}"

case "$1" in
       -patch) patch $patch_opts -p1 < $0;;
       -unpatch) patch $patch_opts -p1 -R < $0;;
        *)
                echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
                exit 1;;
esac

exit 0

@DPATCH@
--- findlib-0.8.orig/contrib/num_top/.depend
+++ findlib-0.8/contrib/num_top/.depend
@@ -0,0 +1,4 @@
+num_top_printers.cmo: num_top_printers.cmi 
+num_top_printers.cmx: num_top_printers.cmi 
+num_top.cmo: num_top.cmi 
+num_top.cmx: num_top.cmi 
--- findlib-0.8.orig/contrib/num_top/Makefile
+++ findlib-0.8/contrib/num_top/Makefile
@@ -0,0 +1,21 @@
+DESTDIR = /usr/lib/ocaml/3.06
+OCAMLC = ocamlc
+OCAMLDEP = ocamldep
+MODULES = num_top_printers.ml num_top.ml
+SOURCES = $(MODULES) $(MODULES:.ml=.mli)
+EXTRA_INST = num_top_printers.mli num_top.mli num_top_printers.cmi
+RESULT = num_top.cma
+all: $(RESULT)
+install:
+	cp $(RESULT) $(EXTRA_INST) $(DESTDIR)/
+$(RESULT): $(MODULES:.ml=.cmo)
+	ocamlc -a -o $@ $^
+%.cmi: %.mli
+	$(OCAMLC) -c $<
+%.cmo: %.ml
+	$(OCAMLC) -c $<
+clean:
+	rm -f *.cm[aio]
+depend:
+	$(OCAMLDEP) $(SOURCES) > .depend
+include .depend
--- findlib-0.8.orig/contrib/num_top/num_top.ml
+++ findlib-0.8/contrib/num_top/num_top.ml
@@ -0,0 +1,29 @@
+
+let print_outcome = false
+let error_fmt = Format.err_formatter
+
+let printers = [
+  "Num_top_printers.nat_printer";
+  "Num_top_printers.big_int_printer";
+  "Num_top_printers.ratio_printer";
+  "Num_top_printers.num_printer";
+]
+
+let eval_phrase s =
+  let lexbuf = Lexing.from_string s in
+  let phrase = !Toploop.parse_toplevel_phrase lexbuf in
+  Toploop.execute_phrase print_outcome error_fmt phrase
+
+let install_all () =
+  List.fold_left
+    (fun outcome phrase ->
+      outcome && eval_phrase (Printf.sprintf "#install_printer %s;;" phrase))
+    true printers
+
+let _ =
+  if not (install_all ()) then begin
+    Format.fprintf error_fmt
+      "Something weird appened while installing Num library printers";
+    Format.pp_print_flush error_fmt ()
+  end
+
--- findlib-0.8.orig/contrib/num_top/num_top.mli
+++ findlib-0.8/contrib/num_top/num_top.mli
@@ -0,0 +1,12 @@
+(**
+  Load this module in the toplevel to install printers for the following types
+  defined in the "num" library:
+    - Nat.nat
+    - Big_int.big_int
+    - Ratio.ratio
+    - Num.num
+
+  No functions exported.
+
+  Copyright (C) 2003  Stefano Zacchiroli <zack@debian.org>
+*)
--- findlib-0.8.orig/contrib/num_top/num_top_printers.ml
+++ findlib-0.8/contrib/num_top/num_top_printers.ml
@@ -0,0 +1,13 @@
+
+let nat_printer fmt v =
+  Format.fprintf fmt "<nat %s>" (Nat.string_of_nat v)
+
+let big_int_printer fmt v =
+  Format.fprintf fmt "<big_int %s>" (Big_int.string_of_big_int v)
+
+let ratio_printer fmt v =
+  Format.fprintf fmt "<ratio %s>" (Ratio.string_of_ratio v)
+
+let num_printer fmt v =
+  Format.fprintf fmt "<num %s>" (Num.string_of_num v)
+
--- findlib-0.8.orig/contrib/num_top/num_top_printers.mli
+++ findlib-0.8/contrib/num_top/num_top_printers.mli
@@ -0,0 +1,16 @@
+(**
+  Printers for types defined in the "num" library. Meant to be used as printers
+  in the ocaml toplevel. See num_top.mli.
+
+  Copyright (C) 2003  Stefano Zacchiroli <zack@debian.org>
+
+  This is free software! you can redistribute it and/or modify it under the
+  terms of the GNU Lesser General Public License as published by the Free
+  Software Foundation; either version 2.1 of the License, or (at your option)
+  any later version.
+*)
+
+val nat_printer : Format.formatter -> Nat.nat -> unit
+val big_int_printer : Format.formatter -> Big_int.big_int -> unit
+val ratio_printer : Format.formatter -> Ratio.ratio -> unit
+val num_printer: Format.formatter -> Num.num -> unit
--- findlib-0.8.orig/Makefile
+++ findlib-0.8/Makefile
@@ -218,3 +218,16 @@
 include Makefile.config
 include depend
 
+# Num library printers contrib by Stefano Zacchiroli <zack@debian.org>
+# Added in the ocaml-findlib debian package
+.PHONY: num_top num_top_clean num_top_install
+all: num_top
+num_top:
+	make -C contrib/num_top/
+clean: num_top_clean
+num_top_clean:
+	make -C contrib/num_top/ clean
+install: num_top_install
+num_top_install:
+	mkdir -p $(PREFIX)$(OCAML_SITELIB) && $(MAKE) -C contrib/num_top/ install DESTDIR=$(PREFIX)$(OCAML_SITELIB)/
+
--- findlib-0.8.orig/site-lib/num/META.in
+++ findlib-0.8/site-lib/num/META.in
@@ -6,3 +6,4 @@
 `browse_interfaces = "Nat,Big_int,Ratio,Num,Arith_status"'
 `archive(byte) = "nums.cma"'
 `archive(native) = "nums.cmxa"'
+`archive(byte,toploop) = "nums.cma num_top.cma"'

Attachment: pgpjQAKjdlPhv.pgp
Description: PGP signature


Reply to: