caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Listing toplevel bindings
@ 2006-09-26  8:01 oleg
  2006-09-27  2:46 ` John Harrison
  0 siblings, 1 reply; 4+ messages in thread
From: oleg @ 2006-09-26  8:01 UTC (permalink / raw)
  To: caml-list, John.Harrison



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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Listing toplevel bindings
  2006-09-26  8:01 Listing toplevel bindings oleg
@ 2006-09-27  2:46 ` John Harrison
  2006-09-27  2:57   ` [Caml-list] " Jonathan Roewen
  0 siblings, 1 reply; 4+ messages in thread
From: John Harrison @ 2006-09-27  2:46 UTC (permalink / raw)
  To: oleg; +Cc: caml-list


Hi Oleg,

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

Brilliant! I easily adapted it for my actual application, and it works
fine. 

Would a few small changes to the OCaml sources render all that reloading
unnecessary, I wonder?

John.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Re: Listing toplevel bindings
  2006-09-27  2:46 ` John Harrison
@ 2006-09-27  2:57   ` Jonathan Roewen
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Roewen @ 2006-09-27  2:57 UTC (permalink / raw)
  To: John Harrison; +Cc: oleg, caml-list

> Would a few small changes to the OCaml sources render all that reloading
> unnecessary, I wonder?

It's not the sources :) You should be able to rebuild the toplevel,
and skip the expunge step (grep for expunge in the base makefile) --
it erases compiler modules such that they act is if not present
(though I'm not entirely sure if it erases them completely; my guess
would be it doesn't).

For distribution, you'd need to patch the makefile (not a big change
though), or provide a toplevel that's not expunged.

I believe that'd do it.

Jonathan


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Listing toplevel bindings
@ 2006-09-25 22:40 John Harrison
  0 siblings, 0 replies; 4+ messages in thread
From: John Harrison @ 2006-09-25 22:40 UTC (permalink / raw)
  To: caml-list; +Cc: John.Harrison


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? For example, given the type ":int" it might return an
association list:

 ["x",1; "y",2; ...] : (string * int)list

I'd be happy to compile more stuff into the toplevel if it's needed
to make this possible.

John.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-09-27  2:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-26  8:01 Listing toplevel bindings oleg
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

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