Say I have the following
module IntPairs =
struct
type t = int * int
let compare (x0,y0) (x1,y1) =
match Stdlib.compare x0 x1 with
0 -> Stdlib.compare y0 y1
| c -> c
end
module PairsMap = Map.Make(IntPairs)
And I add a few elements:
let m = PairsMap.(empty |> add (1,1) 1 |> add (2,1) 1 |> add (1,2) |> add (2,2))
How would i use to_seq to print keys in ascending order?
I'm not to familiar with iterators in ocaml
This is more of a request for OCaml tutoring than a question about a specific problem in your code. It would generally be faster to read the documentation than to ask individual questions here on StackOverflow.
With that said, the Seq interface looks like this:
type 'a t = unit -> 'a node
and 'a node = Nil | Cons of 'a * 'a t
val empty : 'a t
val return : 'a -> 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
val filter : ('a -> bool) -> 'a t -> 'a t
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
val flat_map : ('a -> 'b t) -> 'a t -> 'b t
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
val iter : ('a -> unit) -> 'a t -> unit
As you can see, it offers many higher-order traversal functions. The one you would want to use is probably iter since you want to print the values rather than calculate with them. I.e., there is no return value for your desired usage.
However, you should note that the Map interface already has an iter function. As near as I can tell, there's no reason to convert to a sequence before doing your iteration. The Map.iter documentation says this:
The bindings are passed to f in increasing order with respect to the ordering over the type of the keys.
This is what you want, so Map.iter seems like it will do the trick.
PairsMap.iter my_print_function m
Related
I am learning OCaml and so far I am having trouble to understand the concepts of types.
For example, if we have this following code:
# let func x = x;;
val func : 'a -> 'a = <fun>
From the official website, it tells me that 'a before the arrow is the unknown input type and 'a after the arrow is the output.
However, when I try to use function composition:
# let composition f x = f(x);;
val composition : ('a -> 'b) -> 'a -> 'b = <fun>
What ('a -> 'b) means?
Is 'a related to f and 'b related to x?
Another function composition that made me even more confused is:
# let composition2 f x = f(f(x));;
val composition2 : ('a -> 'a) -> 'a -> 'a = <fun>
I don't really understand why we don't have the 'b in this case.
Thank you in advance!
'a -> 'b is the type of a function that takes one argument of type 'a and returns a value of type 'b.
val composition : ('a -> 'b) -> 'a -> 'b means that composition is a function of two arguments:
the first one is of type ('a -> 'b), so a function as above
the second one is of type 'a
So, this function returns something of the same type as the return type of the first argument, 'b. Indeed, it takes a function and its argument and applies that function to the argument.
In the second case, you can work backwards from the inner call. Let's have a look at f(f(x))
x is something of any type 'b. We have no idea what kind of type this is yet.
Since we have f(x), f must be a function of type 'b -> 'c. It's 'b because we know it takes x as input, and x is of type 'b.
Thus, the type of composition2 is ('b -> 'c) -> 'b
Since we have f(f(x)), the type of f's argument must be the same as the type of its return value. So, 'b == 'c. Call that type 'a.
Since x is of type 'b, which is the same as 'a, x must be of type 'a.
Since f is of type 'b -> 'c, where 'b == 'a and 'c == 'a, f must be of type 'a -> 'a.
Thus, the type of composition2 is ('a -> 'a) -> 'a
is it possible to write in sml/nj a function with the signature:
fn : 'a -> 'b
my initial purpose was to make a function with the signature:
fn: ( 'a -> 'b ) -> ( 'b -> 'a ) -> 'a -> 'b -> 'c
after many tries I got:
fn: ( 'a -> 'b ) -> ( 'b -> 'a ) -> 'a -> 'b -> 'c -> 'c
but I never succeeded to make as it was asked, and I realized that if it is possible for me to make a function from 'a to 'b I could find the solution.
There are only two possible implementations that give a 'a -> 'b function, and they're rarely useful:
Throwing an Exception
- fun foo a = raise Fail "error";
val foo = fn : 'a -> 'b
Looping Indefinitely
- fun bar a = bar a;
val bar = fn : 'a -> 'b
I have a hunch that some details have been lost in the translation, because the second type signature you've given doesn't make much sense.
is it possible to write in sml/nj a function with the signature:
fn : 'a -> 'b
Yes -- see this StackOverflow question (and make it a habit of checking for duplicates as you ask. ;-)
my initial purpose was to make a function with the signature:
foo : ('a -> 'b) -> ('b -> 'a) -> 'a -> 'b -> 'c
It could be like this:
exception Done
fun foo f g x y _ = (f x; g y; raise Done)
Or like this:
fun foo f g x y _ =
let fun inf () = inf ()
in f x; g y; inf () end
What makes these functions similar to functions with type 'a -> 'b is in fact the 'c part, since this type does not relate to the input types, just like 'b does not relate to 'a. The 'as and 'bs in foo actually serve a purpose and allow for some function application (f x, g y) even though these values cannot be a part of the result, since you have no type-safe way of transforming a value of type 'a or 'b into a value of type 'c.
(*function f's definition*)
fun f l j y = l j y
val f = fn : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c
What does the below line say about the function f ?
val f = fn : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c
To answer this question it helps to rename your identifiers:
fun apply g x y = g x y;
val apply = fn : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c
apply is equivalent to what you are calling f. What apply does, informally, is take a function, g, of two variables (in curried form) and two arguments x,y and actually applies g to those arguments.
As an example g, define:
fun sum x y = x + y;
val sum = fn : int -> int -> int
Then, for example:
apply sum 5 7;
val it = 12 : int
In the type signature of apply the part
('a -> 'b -> 'c)
is the type of g. It is polymorphic. You can apply any function to arguments of an appropriate type. When you actually use apply (as I did above with sum) then the SML compiler will have enough information to figure out what concrete types correspond to the type variables 'a, 'b, 'c. the overall type of apply is
('a -> 'b -> 'c) -> 'a -> 'b -> 'c
which says that apply is a function which, when fed a function of type ('a -> 'b -> 'c) and arguments of type 'a and 'b, produces a result of type 'c. I'm not sure of any immediate use of apply, though it does serve to emphasize the perspective that functional application is itself a higher-order function.
When I write an Ocaml function to recursively compose the same function n times, I did this:
let rec compose f n =
(fun x -> if n = 1 then f x else ((compose f (n-1))) (f x));;
It gives the type
val compose : ('a -> 'a) -> int -> 'a -> 'a = <fun>
what is the difference between type
('a -> 'a) -> int -> 'a -> 'a
and type
('a -> 'a) -> int -> ('a -> 'a)
?
How would a similar compose function look with the latter type?
There is no difference between them. But sometimes authors of libraries use parens to denote, that the computation is actually staged, so that it is better to apply it partially, so that you can get a more efficient function, rather then applying it every time. But from the type system perspective this functions are exactly the same, since -> type operator associates to the right.
As an addition to #ivg's answer, here is a mistake i made. Consider these two functions which have the same type int -> int -> int. (;; added for pasting in the toplevel)
let f a b =
let ai =
Printf.printf "incrementing %d to %d\n" a (a + 1);
a + 1 in
b + ai;;
let f' a =
let ai =
Printf.printf "incrementing %d to %d\n" a (a + 1);
a + 1 in
function b -> b + ai;;
If you partially apply
let f_1 = f 1;;
let f'_1 = f' 1;;
you'll see the difference. What I thought is that f does what f' does. In reality, Ocaml is eager but not so eager as to start evaluating away in partial function applications until it runs out of arguments. To point out the difference, it can make sense to write f''s type as int -> (int -> int).
Is there a way to have a recursive call but with different type parameters?
Here's an example which I think should compile, but doesn't.
let swap (a, b) = (b, a)
module Test : sig
val test : bool option ->
('a -> 'b -> 'c) * ('b -> 'a -> 'c)
-> 'a -> 'b -> 'c
end = struct
let rec test (b : bool option)
(f : ('a -> 'b -> 'c) * ('b -> 'a -> 'c))
(x : 'a) (y : 'b) : 'c =
match b with
| None -> (fst f) x y
| Some true -> (test None f x y)
| Some false -> (test None (swap f) y x)
end
The compiler insists that 'a and 'b must be the same type in test even though there's no reason for it. Is this a bug?
This is actually an very interesting question.
I didn't know the answer before, but by reading through the two answers before me, plus a bit research, I will try explain and answer below.
Basically, what you want to achieve is the signature like this
val test : bool option -> ('a -> 'b -> 'c) * ('b -> 'a -> 'c) -> 'a -> 'b -> 'c
First of all, I have to emphasise that trying to force the parameters to be different polymorphic types, i.e., use 'a, 'b, 'c etc, will not necessarily force OCaml compiler to think that the parameters must have different types.
for example, if you do
let f (x:'a) (y:'b) = x + y;;
It seems you are forcing x and y to be different types, but after compiling, it gives
val f : int -> int -> int = < fun >
Basically, OCaml will anyway do its type inference, and apply the conclusion if it is not just against the forced type.
You may think the 'a and 'b in let f (x:'a) (y:'b) = x + y;; to be maybe x and y will have different types and also possibly same types. So it is pointless to force types of parameters like that, right?
So, let's remove all the types forced on parameters and we get
let rec test b f x y =
match b with
| None -> (fst f) x y
| Some true -> test None f x y
| Some false -> test None (swap f) y x
The type of test will be given by OCaml like this:
val test : bool option -> ('a -> 'a -> 'b) * ('a -> 'a -> 'b) -> 'a ->
'a -> 'b = < fun >
So, basically, OCaml thinks x and y must have the same types, and c is not there because the next available type tag for OCaml to use is 'b.
Why x and y must have same types?
When OCaml meets let rec test b f x y, ok, it will think x has type of 'a and y has type of 'b'.
When OCaml meets | Some true -> test None f x y, no problem, the above type inference still stand because you are pass same x and y to test again.
The the funny part is when OCaml meets | Some false -> test None (swap f) y x. You are trying pass y and x (notice the order) to test. In order to let test work, x and y must have the same type, right?
Basically, above is what the counter side of polymorphism recursion #Jeffrey Scofield answered.
polymorphism recursion means, a function can have some parameters whose types can be changed during recursion, instead of stay constant.
OCaml by default of course only allow constant parameter types.
So how does rank 2 polymorphism work?
So how to solve it?
You need a for_all type 'a.. have a look at my question: In OCaml, what type definition is this: 'a. unit -> 'a.
If we use 'a. or 'a 'b. in type definition, then it means it is really for all types, real polymorphic types, and please, ocaml, do not narrow them down as long as it does not harm.
let rec test : 'a 'b. bool option -> ('a -> 'b -> 'c) * ('b -> 'a -> 'c) -> 'a -> 'b -> 'c =
fun b f x y ->
match b with
| None -> (fst f) x y
| Some true -> test None f x y
| Some false -> test None (swap f) y x
Above is the new version of test.
You force type with 'a 'b. for the function test and for 'a and 'b, ocaml will think they are really both polymorphic, and thus the parameter x and y can be accepted in both orders.
You're asking for polymorphic recursion, for which type inference is undecidable in the general case: Wikipedia on Polymorphic Recursion. So I don't think it's a bug.
I think there are ways to get what you want using rank-2 polymorphism.
I think it's a bug because if you create a new function (identical to the first one), then the type is correct:
# let rec t b f x y = match b with
| true -> (fst f) x y
| false -> u true (swap f) y x
and u b f x y = match b with
| true -> (fst f) x y
| false -> t true (swap f) y x;;
val t : bool -> ('a -> 'b -> 'c) * ('b -> 'a -> 'c) -> 'a -> 'b -> 'c = <fun>
val u : bool -> ('a -> 'b -> 'c) * ('b -> 'a -> 'c) -> 'a -> 'b -> 'c = <fun>
using OCaml 4.01.0:
module rec Test : sig
val test : bool option -> ('a -> 'b -> 'c) * ('b -> 'a -> 'c) -> 'a -> 'b -> 'c
end = struct
let rec test b f x y =
match b with
| None -> (fst f) x y
| Some true -> (Test.test None f x y)
| Some false -> (Test.test None (swap f) y x)
end;;
See the section on Recursive modules.
Small example as a test case:
let add_float_int x y = x +. (float y);;
let add_int_float x y = add_float_int y x;;
let adds = add_float_int, add_int_float;;
List.map (fun x -> Test.test x adds 10 10.) [None; Some true; Some false];;