English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Listing toplevel bindings
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-09-26 (08:05)
From: oleg@p...
Subject: Listing toplevel bindings

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

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.


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)
  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
    Format.fprintf ppf "@\nbinding: ";
    Ident.print ppf ident; 
    Format.fprintf ppf "  value: %d" (Obj.obj (Toploop.getvalue (name ident)));
   end else ()
   List.iter print_int_binding bindings;
   Format.fprintf ppf "@\nDone@."   

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