What I would like to do is trace the function aux that has restricted scope in a definition like:
let example = function
arg ->
let rec aux =
(* some recursive function body*)
in
aux arg
;;
with #trace or a similar toplevel command, so that the trace appears when example is called.
(without just copy-pasting the auxiliary function under some other name and passing it to the toplevel)
note: I am using tuareg mode on emacs
No. In the toplevel, you can trace only toplevel functions.
Related
I'm trying to write a multi-line function to use in an OCaml REPL. I've already seen this question, but the syntax suggested doesn't seem to work when using it in a REPL.
To use a contrived example, I can't get the following to compile:
let theFun: int -> int = fun x ->
let foo = x;
foo;;
When I enter it without the ";;" in this online REPL, they get added in anyway and it gives me a syntax error. When I use a REPL on my local machine, the input won't get evaluated unless I include the ";;", and that gives me a syntax error as well.
Your example is incorrect, a proper way to do it will be
let theFun: int -> int = fun x ->
let foo = x in
foo;;
I am writing a lwt-based Twitter API library and I want to implement the Twitter API's cursor functionality using Lwt_stream library.
I decide to use Lwt_stream.from and supply this function an argument f.
Here is what I currently do
let get_follower ~access_token ~screen_name ?(count=5000) ?(wait=true) () =
let base_uri = "https://api.twitter.com/1.1/followers/ids.json" in
let cursor = ref (-1) in
let f () =
Client.do_get_request
~uri_parameters:
["screen_name", screen_name;
"cursor", (string_of_int (!cursor));
"count", (string_of_int count)]
~uri:(Uri.of_string base_uri)
~access_token:access_token
() >>= fun res ->
match res with
| Ok (header, str) -> begin
match (Yojson.Safe.from_string str |> user_ids_of_yojson) with
| `Ok uids ->
cursor := uids.next_cursor;
return (Some (Ok uids))
| `Error msg -> failwith msg
end
| Error e -> return (Some (Error (process_error_exn e))) in
Lwt_stream.from f
I am not sure if I should use ref. The reason I use ref is that the behavior of f depends on the value it returns previously. To be specific, the value of cursor which will be used next time depends on current next_cursor, and if cursor is zero, f knows it reaches the end and returns None.
Is using ref here considered a good design choice? Is there any better way to implement this functionality?
Antron gave an excellent answer to my opinion, but I would like just to share with few practical advice. If I were you, I wouldn't use one big function named f with a ref, well, mostly because it smells, and it is quite unreadable and doesn't scale. I would create a stream using Lwt_stream.create and work with it using a recursive function, that do the main loop, with all the logic separated in helper functions. The main loop function can have state with which it recurs, so that we don't need ugly references or explicit mutability.
So, below is an example, of how your code can be restructured. I also didn't find an explicit check for a zero cursor, that should be used to stop the stream (as you mentioned in text), so I added this. Otherwise the code should work as yours.
So, I've split it into three functions: make_request is responsible for making the request. parse_response is a pure transformation, that deconstructs the answer. And finally, loop function performs the main loop, makes requests, parses them and stops on zero.
Note: looks like that you're using Or_error monad and mixing it with exceptions. This is a bad idea, since it breaks precondition, that a well formed function that returns Or_error.t should never throw exception.
let get_follower ~access_token ~screen_name ?(count=5000) ?(wait=true) () =
let base_uri = "https://api.twitter.com/1.1/followers/ids.json" in
let make_request cursor =
Client.do_get_request
~uri:(Uri.of_string base_uri) ~access_token
~uri_parameters:[
"screen_name", screen_name;
"cursor", string_of_int cursor;
"count", string_of_int count
] () in
let parse_response = function
| Error e -> Error (process_error_exn e)
| Ok (header, str) ->
match Yojson.Safe.from_string str |> user_ids_of_yojson with
| `Ok uids -> Ok uids
| `Error msg -> failwith msg in
let stream,push = Lwt_stream.create () in
let rec loop cursor =
make_request cursor >|= parse_response >>= function
| Ok {next_cursor=0} -> push None; return_unit
| Ok {next_cursor=n} as r -> push (Some r); loop n
| error -> push (Some error); loop cursor in
async (fun () -> loop (-1));
stream
Update
But of course this implementation will eagerly pull data from the server, and push it downstream. It is like a pump and pipe system. As soon as function is invoked, the pump it turned on and it will constantly pour water into the system. You can put a tap, by using bounded push, that can be obtained with Lwt_stream.create_bounded. But you will still have some amount of prefetching (or back to our pipe system analogy, you will have some kind of extension tank). Usually, this is not bad, as it removes some latency, but sometimes this is not what you want. In this case the only choice, that is left is to use explicit reference cell, to control your loop (and build stream using Lwt_stream.from). Other approach would be to change the interface, maybe you're trying to pack to much stuff into one abstraction. Maybe returning follower list Lwt.t or even follower Lwt.t list would be better. Or you can even create an abstract type follower that would hide a thread and return follower list, and lift it's accessors into the _ Lwt.t, e.g.,
module Follower : sig
type t
val name : t -> string Lwt.t
...
end
In that case, this interface can be quite usable with Lwt_list module.
Since f takes unit and produces different results each time, as you've said, it will have to depend on some state, as I think you realize. It already does that by depending on the results of I/O, and I think this is what is making the question of the ref non-trivial to answer. Otherwise, the answer would be yes, it is necessary (see point (2) below).
I think there are two main possibilities for eliminating the syntactic ref, but neither of them makes sense.
Hide these state bits somehow inside the I/O f is already doing, i.e. on the peer across the API. This doesn't seem possible, in fact it looks like the bits have to be on the client for the API to work at all.
Hide these state bits somewhere else on the client. But there will still be something spiritually equivalent to a ref somewhere in the client, in one way or another. You could write or use some kind of wrapper that factors out this ref, but unless that wrapper is useful in many places, it would only risk making it less obvious that there is extra client-side state here. I would prefer to keep the state as local and as close to its usage as possible. Since f already necessarily does "dirty" things, f is the right place.
So, in summary, I would say keep the ref as it is. It is common for streams to be stateful anyway.
The above discussion assumes that you have to use Lwt_stream. If not, you can provide an alternative interface whose type looks something like get_follower : cursor:int -> ... -> (results * cursor) Lwt.t and let the caller worry about state if it has to. This would probably be what I'd try first.
EDIT
There is of course a disadvantage to that, which is the possibility of writing the calling code to supply the wrong cursor. You could hide the cursor by returning a partially applied function, but the calling code might be written to call the wrong function. Without linear types, the stream approach is safer if you are worried about these possibilities, so it has its advantages.
I have implemented a pretty-printer for a module. Currently I start up utop, load the dependencies then do #install_printer pp_custom;; where pp_custom is the pretty-printer.
I would like to automate this so that I can have it in a way that is similar to the lacaml library where the pretty-printer for the matrix is "installed" by default.
How would I go about to doing that?
In short, you need to run #install_printer directive whenever you load your library in the top. I'm using the following code to evaluate code in the toplevel:
open Core_kernel.Std
open Or_error
let eval_exn str =
let lexbuf = Lexing.from_string str in
let phrase = !Toploop.parse_toplevel_phrase lexbuf in
Toploop.execute_phrase false Format.err_formatter phrase
let eval str = try_with (fun () -> eval_exn str)
It depends on Core_kernel, but you can get rid of this easily, by just using eval_exn instead of eval (the last one wraps a possible exception into the Or_error monad). Once you've got the eval function, it can be used to load your printers:
let () = eval (sprintf "#install_printer %s;;" printer)
where printer is the name of the pretty printing function (usually qualified with a module name). Usually, one put such code into the separate library, named library.top, where library is the name of your library.
For further automation, you can require all types, that you want to be auto-printable in toplevel, to register itself in a central registry, and then call all registered printers, instead of enumerating them by hand. To see all this working at once, you can take a look at the BAP library. It has a sublibrary called bap.top that automatically installs all printers. Each type that is desired to be printable implements Printable signature, using Printable.Make functor, that not only derives many printing functions from the basic definition, but also registers the generated pretty-printers in Core_kernel's Pretty_printer registry (you can use your own registry, this is just a set of strings, nothing more). When bap.top library is loaded into the toplevel (using require or load directive), it enumerates all registered printers and install them.
I am trying to achieve something similar to eval() in OCaml.
I have a string and I want to get an OCaml function out of it.
Currently I am doing the following:
I dump the string to new.ml and compile the file:
Compile.implementation Format.std_formatter "new.ml" "New"
Then I try to dynlink the file:
Dynlink.loadfile "new.cmo";
But if I try to do New.foo it fails. I am not sure why I cannot access the module New after Dynlinking. Am I missing something?
Thanks!
The comment of Dynlink.loadfile says:
No facilities are provided to
access value names defined by the unit. Therefore, the unit
must register itself its entry points with the main program,
e.g. by modifying tables of functions.
The loader program cannot access values of dyn-loaded module without any hint, since it has no idea what values are defined in it just from the .cmo file. The dynamic loaded module must register its entry point to some state defined in the loader program.
Here is such a minimal example. First, the module for the entry points:
(* entry.ml *)
let f : (unit -> unit) ref = ref (fun () -> assert false)
The loader program:
(* loader.ml *)
let () =
Dynlink.loadfile "plugin.cmo";
!Entry.f ()
The plugin to be dyn-loaded:
(* plugin.ml *)
let () = Entry.f := (fun () -> prerr_endline "hello world")
Here, Plugin registers its function to Entry.f which is statically linked to Loader, so that Loader can access the function.
They must be compiled as follows:
$ ocamlc -o loader.exe dynlink.cma entry.ml loader.ml
$ ocamlc -c plugin.ml
Execution of loader.exe should demonstrate how the dyn loading works:
$ ./loader.exe
hello world
Note that Entry and Loader must be different modules. Otherwise, you get Uninitialized_global exception at dyn-loading Plugin. Dyn-loaded modules can only access values in "already initialized modules", and the loader module thinks itself not yet initialized when Dynlink.loadfile is called, since the entire evaluation of the module is not finished yet.
Entry.f is the simplest state to have only one entry point. To dyn-load many values, you may want to have more complicated data structure, such as (string, (unit -> unit)) list ref or (string, (unit -> unit)) Hashtbl.t.
I have a number of events that happen in a game. I want to control the time and order at which these events occur.
For example:
Event 1: Show some text on screen for N frames & play a sound effect
Event 2: Clear the text on the screen
My solution (maybe there is a better one), is to have a list of functions that contain the events. The events perform their behavior then return the next events to occur in the game. I thought of using List.map or List.collect because I am essentially mapping a list of events to a new list of events while performing some behavior code.
In the example above, Event1 can be composed of two functions: One that shows text and one that plays a sound (hence the need for a list). The function that shows text would return a copy of itself for N-1 frames then it would return Event2 which clears the text. The play sound function would return the equivalent of a no-op.
If this is a good solution, I could probably do it in C++ or C#. My goal is to do an equivalent or better solution in F#.
Did you mean something like this?
let myActions =
[fun () -> printfn "You've woken up a dragon."
fun () -> printfn "You hit the dragon for 0 points of damage."
fun () -> printfn "The dragon belches."
fun () -> printfn "You have died."]
let actionBuilder actionList =
let actions = ref actionList
fun () ->
match !actions with
| [] -> ()
| h::t -> h(); actions := t
Usage (F# interactive):
> let doSomething = actionBuilder myActions;;
val doSomething : (unit -> unit)
> doSomething();;
You've woken up a dragon.
val it : unit = ()
> doSomething();;
You hit the dragon for 0 points of damage.
val it : unit = ()
> doSomething();;
The dragon belches.
val it : unit = ()
> doSomething();;
You have died.
val it : unit = ()
> doSomething();;
val it : unit = ()
>
**Edit: ** if you want to be able to add actions, maybe it's better to make an action dispenser that uses a Queue internally, since appending is O(N) with a list and O(1) with a Queue:
type actionGenerator(myActions: (unit->unit) list) =
let Q = new System.Collections.Generic.Queue<_>(Seq.ofList myActions)
member g.NextAction =
fun () ->
if Q.Count = 0 then ()
else Q.Dequeue()()
member g.AddAction(action) = Q.Enqueue(action)
Not quite sure what you're trying to achieve here... it can be helpful to think through the exact types that you're looking for. It sounds like perhaps you want to map a (unit->(unit->unit)) list to a (unit->unit) list by applying each function in the first. If that's the case, you can do it like so:
let l = [(fun () -> (fun () -> printfn "first nested fn")); (fun () -> (fun () -> printfn "second nested fn"))]
let l' = List.map (fun f -> f()) l
If you are looking for a syntax to declare your list type then here is one way to do this:
List<`a->`b>
This assumes that the function takes a single parameter.
But the very fact that you are trying to figure out the syntax for the type is a hint that you are still looking at this as if you are coding in procedural language.
The "functional" way of doing it is to concentrate on the logic of generating the list and let the compiler to infer the type based on your code
I've read your question twice, and am still not sure I understand exactly what you want. But from what I understand, your 'events' aren't necessarily invoked in the order they appear in the 'list'. If this is the case, you don't really want an F# list, you want some kind of lookup.
Now the other question is whether it is really a good idea for an event to determine what should follow it? That kind of amounts to hard coding your functionality once and for all, doesn't it?
EDIT
I see in a comment that you say you want to 'chain function calls together'.
How about writing them one after the other? We aren't in Haskell after all, F# will fire them in the order you write them.
If you want to be a little bit more functional, you could use continuations - each function takes an extra parameter which is the next function to execute. Almost monadic (I believe), except that in your case they appear to be actions, so there are no values to string through from one function to the next.
Not sure if that helps: I think you'll have to try rephrasing your question, judging by the diversity of answers here.
It seems like you are trying to do something in a very complicated way. That is sometimes necessary, but usually it isn't.
Since you are asking this question, I assume that you have more experience in imperative languages. It seems that the real solution to your problem is something completely different than a list of functions.