Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Listing toplevel bindings
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
John Harrison  
View profile  
 More options Sep 25 2006, 6:43 pm
Newsgroups: fa.caml
From: John Harrison <John.Harri...@cl.cam.ac.uk>
Date: Mon, 25 Sep 2006 22:43:33 UTC
Local: Mon, Sep 25 2006 6:43 pm
Subject: [Caml-list] Listing toplevel bindings

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.

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
o...@pobox.com  
View profile  
 More options Sep 26 2006, 4:07 am
Newsgroups: fa.caml
From: o...@pobox.com
Date: Tue, 26 Sep 2006 08:07:46 UTC
Local: Tues, Sep 26 2006 4:07 am
Subject: [Caml-list] 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
        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));;

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Harrison  
View profile  
 More options Sep 26 2006, 10:50 pm
Newsgroups: fa.caml
From: John Harrison <John.Harri...@cl.cam.ac.uk>
Date: Wed, 27 Sep 2006 02:50:15 UTC
Local: Tues, Sep 26 2006 10:50 pm
Subject: [Caml-list] Re: Listing toplevel bindings

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.

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonathan Roewen  
View profile  
 More options Sep 26 2006, 11:00 pm
Newsgroups: fa.caml
From: "Jonathan Roewen" <jonathan.roe...@gmail.com>
Date: Wed, 27 Sep 2006 03:00:06 UTC
Local: Tues, Sep 26 2006 11:00 pm
Subject: Re: [Caml-list] Re: Listing toplevel bindings

> 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

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »