caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: oleg@pobox.com
To: caml-list@inria.fr, John.Harrison@cl.cam.ac.uk
Subject: Listing toplevel bindings
Date: Tue, 26 Sep 2006 01:01:20 -0700 (PDT)	[thread overview]
Message-ID: <20060926080120.5B3B7AC04@Adric.metnet.fnmoc.navy.mil> (raw)



John Harrison wrote:
> When inside the OCaml toplevel, is there any way of getting a list of
> all (top-level) bindings of names to objects of some specified type 'a?

Yes, there is. No need to recompile anything. However, you need the
OCaml installation directory (after your have compiled the top-level
and before you did make clean).

First, please retrieve the following file
	http://pobox.com/~oleg/ftp/ML/gprint/gprint_toplevel.ml
and adjust the paths in the "directory" directives to point to your
OCaml installation directory. Please run your Ocaml top-level and
execute all #directory and the #load directives 
in that file up to, but not including the loading of genprintval.cmo.
Please do NOT change the order of the load directives! It took about
half an hour to find the right order....

Next, please enter the code given in the appendix of this message. The
code will print the signature of the values in the
existing top-level environment. For example:

binding: get_value_bindings/79  
val get_value_bindings : Env.t -> (Ident.t * Types.value_description) list
binding: print_bindings/107  
val print_bindings :
  Format.formatter -> (Ident.t * Types.value_description) list -> unit
Done
- : unit = ()


We then can enter

# let x = 1;;
val x : int = 1
# let x = 2;;
val x : int = 2
# let y = 10;;
val y : int = 10
# print_int_toplevel Format.std_formatter 
    (get_value_bindings (!Toploop.toplevel_env));;

  
binding: x/186  value: 2
binding: x/187  value: 2
binding: y/188  value: 10
Done
- : unit = ()

As we can see, the type environment keeps track of all the previous
definitions of a name. Because "x" was defined twice, there are two
entries in the type environment: "x/186" and "x/187". The counter is
the timestamp. The top-level value environment keeps the last value,
however.

The function print_int_toplevel cannot, generally, be polymorphic over
type -- unless you're willing to assume responsibility that your type
representation string matches your desired type -- or you're willing
to use MetaOCaml.

Appendix.

open Ident;;
open Env;;


let get_value_bindings env =
   let rec get_val acc = function 
	| Env_empty -> acc
	| Env_value (next, ident, val_descr) -> 
	        get_val ((ident,val_descr)::acc) next 
	| Env_type (next,_,_) -> get_val acc next
	| Env_exception (next,_,_) -> get_val acc next
	| Env_module (next,_,_) -> get_val acc next
	| Env_modtype (next,_,_) -> get_val acc next
	| Env_class (next,_,_) -> get_val acc next
	| Env_cltype (next,_,_) -> get_val acc next
	| Env_open (next,_) -> get_val acc next
  in get_val [] (summary env);
;;


let print_bindings ppf bindings =
  List.iter (fun (ident,val_descr) ->
	Format.fprintf ppf "@\nbinding: ";
	Ident.print ppf ident; 
	Format.fprintf ppf "@ @ @ ";
	Printtyp.value_description ident ppf val_descr)
    bindings;
  Format.fprintf ppf "@\nDone@."   
;;

print_bindings Format.std_formatter 
    (get_value_bindings (!Toploop.toplevel_env));;


let type_to_str (x : Types.type_expr) = 
  Printtyp.type_expr Format.str_formatter x;
         Format.flush_str_formatter ();;

(* Print all top-level int bindings *)

let print_int_toplevel ppf bindings =
  let print_int_binding (ident,val_descr) =
   if type_to_str val_descr.Types.val_type = "int" then
   begin
    Format.fprintf ppf "@\nbinding: ";
    Ident.print ppf ident; 
    Format.fprintf ppf "  value: %d" (Obj.obj (Toploop.getvalue (name ident)));
   end else ()
 in
   List.iter print_int_binding bindings;
   Format.fprintf ppf "@\nDone@."   
;;


print_int_toplevel Format.std_formatter 
    (get_value_bindings (!Toploop.toplevel_env));;


             reply	other threads:[~2006-09-26  8:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-26  8:01 oleg [this message]
2006-09-27  2:46 ` John Harrison
2006-09-27  2:57   ` [Caml-list] " Jonathan Roewen
  -- strict thread matches above, loose matches on Subject: below --
2006-09-25 22:40 John Harrison

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=20060926080120.5B3B7AC04@Adric.metnet.fnmoc.navy.mil \
    --to=oleg@pobox.com \
    --cc=John.Harrison@cl.cam.ac.uk \
    --cc=caml-list@inria.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;
as well as URLs for NNTP newsgroup(s).