Let's say I have an OCaml function
let _ = register "cbf_coh_insert" (fun k v -> print_endline ("Inserted key=" ^ k ^ " value=" ^ v))
That is a function that takes two arguments. On the C side, I would call that with caml_callback2(*caml_named_value("cbf_coh_insert"), k, v);. Is there a way, on the C side, to check that the number of arguments (2 in this case) match? Other than I guess calling it and trying to trap a SIGSEGV. Thanks!
UPDATE: some background.
NO WAI
This should be ensured at compile time (either manually or by code generation or by parsing and checking whether C and OCaml code are in sync)
UPDATE
Example register function :
let on_cbf_coh_insert (f : string -> string -> unit) = register "cbf_coh_insert" f
UPDATE
I wish it was possible to pass a closure/let binding straight into C.
Why do you think it is not possible? Look at existing bindings that do this all the time.
BTW This question is a perfect illustration for XY problem.
Related
I want to understand how Array.length is implemented. I managed to write it with Array.fold_left:
let length a = Array.fold_left (fun x _ -> x + 1) 0 a
However in the standard library, fold_left uses length so that can't be it. For length there is just this line in the stdlib which I don't understand:
external length : 'a array -> int = "%array_length"
How can I write length without usingfold_left?
EDIT:
I tried to do it with pattern matching, however it is not exhaustive, how can I make the matching more precise? (The aim is to remove the last element and return i+1 when only one element is left)
let length a =
let rec aux arr i =
match arr with
| [|h|] -> i+1
| [|h;t|] -> aux [|h|] (i+1)
in aux a 0;;
The array type is a primitive type, like the int type.
The implementation of many primitives functions on those primitive type is done with either C functions or compiler primitives.
The Array.length function belongs to the compiler primitive category and it is defined in the standard library by:
external length : 'a array -> int = "%array_length"
Here, this declaration bind the value length to the compiler primitive %array_length (compiler primitive names start with a % symbol) with type 'a array -> int. The compiler translates such compiler primitive to a lower level implementation during the translation process from the source code to either native code or bytecode.
In other words, you cannot reimplement Array.length or the array type in general in an efficient way yourself because this type is a basic building block defined by the compiler itself.
For length there is just this line in the stdlib which I don't understand:
The external keyword indicates that this function is implemented in C, and "%array_length" is the C symbol naming this function. The OCaml runtime is implemented in C and some types, like arrays, are built-in (also called primitives).
See for example Implementing primitives in Chapter 20: Interfacing C with OCaml
I tried to do it with pattern matching, however it is not exhaustive, how can I make the matching more precise
Note that OCaml tells you which pattern is not matched:
Here is an example of a case that is not matched:
[| |]
So you have to account for empty vectors as well.
I'am learning OCaml and currently i'am trying to undertand how iteration works in OCaml and how to create a matrix. I want an array 5 x 5 filled with 0. I know there is an issue with shared references so i created a new array at each iteration however iam having issues in other places, specifically at line 6. Let me know of other issues like indentation practices.
open Array;;
let n = ref 5 and i = ref 0 in
let m = Array.make !n 0 in
while !i < !n do
m.(!i) <- Array.make !n 0;;
i := !i + 1;;
done
m;;
You are using ;; too much. Contrary to popular belief, ;; is not part of ordinary OCaml syntax (in my opinion anyway). It's just a special way to tell the toplevel (the REPL) that you want it to evaluate what you've typed so far.
Leave the ;; after open Array. But change all but the last ;; to ; instead.
(Since you reference the Array module by name in your code, which IMHO is good style, you can also just leave out the open Array;; altogether.)
You want the last ;; because you do want the toplevel to evaluate what you've typed so far.
Your syntax error is caused by the fact that your overall code is like this
let ... in
let ... in
while ... do
...
done
m
The while is one expression (in OCaml everything is an expression) and m is another expression. If you want to have two expressions in a row you need ; between them. So you need ; after done.
You also have a type error. When you create m you're creating an array of ints (your given initial value is 0). So you can't make it into a matrix (an array of arrays) later in the code.
Also (not trying to overload you with criticisms :-) this code reads like imperative code. It's not particularly idiomatic OCaml code. In most people's code, using ref is pretty rare. One immediate improvement I see would just be to say let n = 5. You're not changing the value of n anywhere that I see (though maybe this is part of a larger chunk of code). Another improvement would be to use for instead of while.
Finally, you can do this entire operation in one function call:
let n = 5 in
let m = Array.init n (fun i -> Array.make n 0) in
m
Using explicit loops is actually also quite rare in OCaml (at least in my code).
Or you could try this:
let n = 5 in
let m = Array.make_matrix n n 0 in
m
What is the fastest way to flatten an array of arrays in ocaml? Note that I mean arrays, and not lists.
I'd like to do this linearly, with the lowest coefficients possible.
OCaml Standard Library is rather deficient and requires you to implement so many things from scratch. That's why we have extended libraries like Batteries and Core. I would suggest you to use them, so that you will not face such problems.
Still, for the sake of completeness, let's try to implement our own solution, and then compare it with a proposed fun xxs -> Array.(concat (to_list xxs)) solution.
In the implementation we have few small problems. First of all in order to construct an array we need to provide a value for each cell. We can't just create an uninitialized array, this will break a type system. We can, of course use Obj module, but this is rather ugly. Another problem, is that the input array can be empty, so we need to handle this case somehow. We can, of course, just raise an exception, but I prefer to make my functions total. It is not obvious though, how to create an empty array, but it is not impossible:
let empty () = Array.init 0 (fun _ -> assert false)
This is a function that will create an empty polymorphic array. We use a bottom value (a value that is an inhabitant of every type), denoted as assert false. This is typesafe and neat.
Next is how to create an array, without having a default value. We can, write a very complex code, that will use Array.init and translate ith index to j'th index of n'th array. But this is tedious, error prone and quite ineffective. Another approach would be to find a first value in the input array and use it as a default. Here comes another problem, as in Standard Library we don't have an Array.find function. Sic. It's a shame that in 21th century we need to write an Array.find function, but this is how life is made. Again, use Core (or Core_kernel) library or Batteries. There're lots of excellent libraries in OCaml community available via opam. But back to our problem, since we don't have a find function we will use our own custom solution. We can use fold_left, but it will traverse the whole array, albeit we need to find only the first element. There is a solution, we can use exceptions, for non-local exits. Don't be afraid, this is idiomatic in OCaml. Also raising and catching an exception in OCaml is very fast. Other than non local exit, we also need to send the value, that we've found. We can use a reference cell as a communication channel. But this is rather ugly, and we will use the exception itself to bear the value for us. Since we don't know the type of an element in advance, we will use two modern features of OCaml language. Local abstract types and local modules. So let's go for the implementation:
let array_concat (type t) xxs =
let module Search = struct exception Done of t end in
try
Array.iter (fun xs ->
if Array.length xs <> 0
then raise_notrace (Search.Done xs.(0))) xxs;
empty ()
with Search.Done default ->
let len =
Array.fold_left (fun n xs -> n + Array.length xs) 0 xxs in
let ys = Array.make len default in
let _ : int = Array.fold_left (fun i xs ->
let len = Array.length xs in
Array.blit xs 0 ys i len;
i+len) 0 xxs in
ys
Now, the interesting part. Benchmarking! Let's use a proposed solution for comparison:
let default_concat xxs = Array.concat (Array.to_list xxs)
Here goes our testing harness:
let random_array =
Random.init 42;
let max = 100000 in
Array.init 1000 (fun _ -> Array.init (Random.int max) (fun i -> i))
let test name f =
Gc.major ();
let t0 = Sys.time () in
let xs = f random_array in
let t1 = Sys.time () in
let n = Array.length xs in
printf "%s: %g sec (%d bytes)\n%!" name (t1 -. t0) n
let () =
test "custom " array_concat;
test "default" default_concat
And... the results:
$ ./array_concat.native
custom : 0.38 sec (49203647 bytes)
default: 0.20 sec (49203647 bytes)
They don't surprise me, by the way. Our solution is two times slower than the standard library. The moral of this story is:
Always benchmark before optimizing
Use extended libraries (core, batteries, containers, ...)
Update (concatenating arrays using Base)
With the base library, we can concatenate arrays easily,
let concat_base = Array.concat_map ~f:ident
And here's our benchmark:
./example.native
custom : 0.524071 sec (49203647 bytes)
default: 0.308085 sec (49203647 bytes)
base : 0.201688 sec (49203647 bytes)
So now the base implementation is the fastest and the smallest.
I have a function that calculates f(n) in Haskell.
I have to write a loop so that it will start calculating values from f(0) to f(n), and will every time compare the value of f(i) with some fixed value.
I am an expert in OOP, hence I am finding it difficult to think in the functional way.
For example, I have to write something like
while (number < f(i))
i++
How would I write this in Haskell?
The standard approach here is
Create an infinite list containing all values of f(n).
Search this list until you find what you're after.
For example,
takeWhile (number <) $ map f [0..]
If you want to give up after you reach "n", you can easily add that as a separate step:
takeWhile (number <) $ take n $ map f [0..]
or, alternatively,
takeWhile (number <) $ map f [0 .. n]
You can do all sorts of other filtering, grouping and processing in this way. But it requires a mental shift. It's a bit like the difference between writing a for-loop to search a table, versus writing an SQL query. Think about Haskell as a bit like SQL, and you'll usually see how to structure your code.
You can generate the list of the is such that f i is larger than your number:
[ i | i<-[0..] , f i > number ]
Then, you can simply take the first one, if that's all you want:
head [ i | i<-[0..] , f i > number ]
Often, many idiomatic loops in imperative programming can be rephrased as list comprehensions, or expressed through map, filter, foldl, foldr. In the general case, when the loop is more complex, you can always exploit recursion instead.
Keep in mind that a "blind" translation from imperative to functional programming will often lead to non-idiomatic, hard-to-read code, as it would be the case when translating in the opposite direction. Still, I find it relieving that such translation is always possible.
If you are new to functional programming, I would advise against learning it by translating what you know about imperative programming. Rather, start from scratch following a good book (LYAH is a popular choice).
The first thing that's weird from a functional approach is that it's unclear what the result of your computation is. Do you care about the final result of f (i)? Perhaps you care about i itself. Without side effects everything neends to have a value.
Let's assume you want the final value of the function f (i) as soon as some comparison fails. You can simulate your own while loops using recursion and guards!
while :: Int -> Int -> (Int -> Int) -> Int
while start number f
| val >= number = val
| otherwise = while (start + 1) number f
where
val = f start
Instead of explicit recursion, you can use until e.g.
findGreaterThan :: (Int -> Int) -> Int -> Int -> (Int, Int)
findGreaterThan f init max = until (\(v, i) -> v >= max) (\(v, i) -> (f v, i + 1)) (init, 0)
this returns a pair containing the first value to fail the condition and the number of iterations of the given function.
As I've been learning haskell I've enjoyed the pure parts but now I am stumbling through the monadic and IO parts and probably experiencing what some people find truly infuriating about the language. I solving a project euler problem and I simple want a mutable array because I have to update elements frequently by index. I tried Vectors but couldn't get them working so I tried Data.Array.IO. I can read and write elements fine but I can't display the array in terminal the way I want. So far I have this.
test = do
arr <- newArray (1,10) 37 :: IO (IOArray Int Int)
a <- readArray arr 1
writeArray arr 1 64
b <- readArray arr 1
dispArray arr
return ()
dispArray arr = do
(a,b) <- getBounds arr
printf "["
dispArray' arr a
printf "]\n"
where dispArray' arr i = do
(a,b) <- getBounds arr
if i < a || i > b
then return ()
else do
v <- readArray arr i
print v
dispArray' arr (i+1)
The ouput of this as you would expect is this:
[64
37
37
37
37
37
37
37
37
37
]
But this is inconvenient and I want this [64,37,37,37.... like this. I've seen functions that are something like toList, but I don't want this. I don't want to convert to a list everytime I display. So I figured I would need to use printf. So I replaced print v with printf " %s," (show v). But this doesn't compile. I don't know why. I thought it would because print :: Show a => a -> IO () and show :: Show a => a -> String so why wouldn't it work because %s signifies a string? So I then put to calls next to each other. To see if printf would even work.
printf " %s," "hello"
print v
Which compiles and displays:
[ hello,64
hello,37
hello,37
hello,37
hello,37
hello,37
hello,37
hello,37
hello,37
hello,37
]
Why can I not use show v? Why is haskell IO so infuriating to beginners?
This is an interesting type-checking puzzle.
The error message that the call to printf produces is
Could not deduce (PrintfType (m a0))
arising from the ambiguity check for `dispArray'
The phrases Could not deduce and ambiguity typically hint at the fact that GHC has
insufficient type information in order to conclude how this program should be typed. This might be a real type error, but it's also possible that it can be fixed simply by providing more type information (and this is the case here).
The culprit here is really printf, combined with the flexibility of the mutable array interface, and not so much Haskell's IO system. The type of printf is an ingenious hack, but still a hack. In order to know a flexible number of parameters of various types that depend on just the format string, printf has a type that isn't very safe nor very informative:
printf :: PrintfType r => String -> r
So all we really know for sure is that the first argument is of type String. The rest can be any type r that is in the type class PrintfType.
The details of the instances do not matter. What's interesting is that show produces a String, and if we apply printf to a format string and then a show-produced second string, we are still left with a rather uninformative type:
> :t printf "%s," (show 2)
printf "%s," (show 2) :: PrintfType t => t
In particular, there's no indication here that the result is to be in the IO monad.
This normally wouldn't be a problem, if GHC could conclude from the context that you're in IO. But within dispArray', the only other functions you are calling are readArray, getBounds, return (and dispArray' recursively). None of these functions specifies that it lives in IO either. In particular, all of the array functions are overloaded over the monad, for example:
getBounds :: (Ix i, MArray a e m) => a i e -> m (i, i)
(And indeed, getBounds could, for example, also work in an ST monad context.) So there's simply nothing in dispArray' that determines that you live in IO. And that in turn means that GHC cannot resolve the type of printf.
As I said, it's a consequence of the desired flexibility of printf that printf itself cannot provide this information, and it has to be available externally.
The solution is easy enough. As suggested in one of the comments, it is sufficient to annotate the result type of the call to printf:
printf "%s," (show v) :: IO ()
As you're using printf anyway (and if you're actually only interested in arrays of decimal numbers), you could also use:
printf "%d," v :: IO ()
It would also sufficient (but less clear to the reader) to give a type signature for anything else within the definition of dispArray' so that it fixes the return type to be IO (). For example, you could annotate the return () in the then-branch of the if expression:
return () :: IO ()
The incantation you want is:
putStr (show v)
That prints out v without a newline.