How can I call a Haskell function from Golang? - haskell

I am a python developer making the shift to Golang, so I'm sorry for the noob question. I am responsible for taking some Haskell code, for which we have python bindings, and making it callable from Go. I have a shared object file, _foo.so, that I want to somehow import into Go and call a la:
import (
f "_foo.so"
)
func DoBar() {
return f.Bar()
}
Is this possible? I don't even have the first idea of where to begin, but I'm hoping that pseudo code gets the idea across.

As already mentioned in the comments, you need to go through C.
Good news: the python binding you have goes through C already. It means that haskell code exposes all the necessary API as C functions, you just need to find out how the API looks like and call it using cgo. You probably don't need to know anything about haskell.
Assuming you have access to the source code, you should look for *.c and *.h files (often located in a cbits folder). If you don't know C, then ask your teammates to help.
If you don't have access to the code, then you may try to guess the C API using the python binding. Though it'll be quite hard.

I was intrigued by this question, so I implemented it.
See this repo: https://github.com/rusq/gohaskell/
Go program calls fibonacci Haskell function.

Related

Difference between R::runif() and Rcpp::runif()

I'm learning to use Rcpp in R. Would you please explain me the difference between R::runif() and Rcpp::runif().
I mean 3 questions:
Do these 2 functions produce the same stream of random numbers given that we set the same seed before running each of them ?
Which function is preferable when using Rcpp ? I mean, it seems to me that the 2 functions produce the same thing, but Rcpp::runif() will run more fastly.
How to call Rcpp::runif() in a .R file ? Is it true that the Rcpp::runif() can be called only from a .cpp file and not in R? (I mean, it seems to me that the function Rcpp::runif() is of extensively used to write other C++ functions, then I will import that function by sourcecpp() to use in R)
Thank you very much for your help!
I suspect this question is a duplicate so I may close this but here goes:
Yes they do. The whole point of the RNG interfaces is guaranteeing just that
Entirely up to you. Sometimes you want to wrap or use a C API example and you have R::runif() for that. Sometimes you want efficient vector operations for which you have Rcpp::runif().
You write a C++ function accessing the C++ API. Note that not all those functions will be faster than calling what R offers when what R offers is already vectorised. Your wrapping of Rcpp::runif() will not be much different in performance from calling stats::runif(). You use the C++ accessor in C++ code writing something you cannot easily get from R.
Edit: This Rcpp Gallery post has some background and examples.

Rcpp::export - exporting only the C++ interface, not the R functions

I do not need roxygen2 and Rcpp to create for me the R functions (or maybe I do?) for the exported C++ functions - is there any way to tell Rcpp::export not to create them? I would be perfectly happy with just .Call-ing them directly.
I went through Writing R Extensions, and Rcpp Attributes and Writing a package that uses Rcpp vignettes, documentation of roxygen2 and multiple threads on SO (like here) but I did not find anything helpful.
If I understand your question correctly, then it is as simple "well if you don't want a stub function created, do no put an [[Rcpp::export]] tag there".
You also confuse what roxygen2 does for documentation with what the compileAttributes() function does for exporting.
To be plain, only the latter has anything to do with creating interfaces between R and C++. And on the margin, you do want them for the free exception handling and RNG setting they give you. But hey, if you'd rather do without, you can, and that is documented.

How to show all function of exe file

I have a .exe file and i want to list all function of it. It is possible and have some tool to do it?
P/s: i tried with IDA but it is difficult to understand it. Can it resolve my problem?.
To achieve this, you must have a very good understanding of Assembly Language, and maybe learning from NASM documentation and the Intel Instruction set will get you to a good level. Before you can continue in this path.
Furthermore, for those who already know little or more Assembly Language, is possible to call the function of an exe, mostly if they are standalone or doesn't depend on any other functions using something like Asmjit. And also you can get this function addresses, and cast them into functions if you know the calling convention used here. For example, __stdcall, __cdecl and others. And if then you know the calling convention, you can use this to construct the function signature, including the return type, usually through the POP, RETURN instruction or the registry that was affected before the function exit, or return to the caller.
You have to find a good debugger, to help you find all this. As you said IDA is good but too complicated, so you can use something like x86dbg, or others like ollyDbg those can help you find the entry point of those functions and use that to construct the signature
so a core like this, assuming that 0x749593 is an address of a a function with maybe takes two argument and return sum.
typedef int (*sum)(int first, int second);
Sum mySumFunc;
Changing the address of mySumFunc to point to 0x749593, you can call this function mySumFunc(arg1, arg2 ) and it will still work.

Trying to use a function in Data.Map.hs but it says not in scope ... Is it because of #if defined(TESTING)

In Data.Map there are some functions like merge, glue, that I want to use but the compiler says they're not in scope. I notice at the top they are listed here:
#if defined(TESTING)
-- * Internals
, bin
, balanced
, join
, merge
#endif
I think this means I can't use them directly unless I've somehow defined TESTING but I've no clue how to do that and where. Please answer as if I'm retarded; don't assume I know you mean to type something in the command line instead of typing it in the program.
I'm on Windows XP and using GHCi if it matters.
You can't. These functions are only meant to be used by the library's internal tests. TESTING is determined at compile-time, so you couldn't change it even if you wanted to.
The internal tree structure of the map shouldn't be relevant to someone using the code, so it's difficult to say what would be a better solution without some concrete information about what you're trying to do.

Importing modules as a function, with string as input

I want to make a function called 'load' which imports definitions of functions from another file. I know how to import modules, but in my program I want the definitions of the functions to change depending on which module is 'loaded' with this new function. Is there a way to do this? Is there a better way to write my program so that this is not necessary?
I think it's type signature would look something like:
load :: String -> IO ()
where the string is the name of the module to be loaded (and the module is in the same directory).
Edit: Thanks for all the replies. Most people agree that this is not the best way to do what I want. Instead, is there a way to declare a global variable from within an I/O program. That is, I want it so that if I type (function "thing") into a function of type String -> IO(), I can still type 'thing' into GHCi to get the value assigned to it... Any suggestions?
There is almost certainly a better way to write your program so that this is not necessary. It's hard to say what without knowing more details about your situation, though. You could, for instance, represent the generic interface each module implements as a data-type, and have each module export a value of that type with the implementation.
Basically, the set of loaded modules is a static, compile-time property, so it makes no sense to want your program's behaviour to change based on its contents. Are you trying to write a library? Your users probably won't appreciate it doing such evil magic to their import lists :) (And it probably isn't possible without Template Haskell in that case, anyway.)
The exception is if you're trying to implement a Haskell tool (e.g. REPL, IDE, etc.) or trying to do plugins; i.e. dynamically-loaded modules of Haskell source code to integrate into your Haskell program. The first thing to try for those should be hint, but you may find you need something more advanced; in that case, the GHC API is probably your best bet. plugins used to be the de-facto standard in this area, but it doesn't seem to compile with GHC 7; you might want to check out direct-plugins, a simplified implementation of a similar interface that does.
mueval might be relevant; it's designed for executing short (one-line) snippets of Haskell code in a safe sandbox, as used by lambdabot.
Unless you're building a Haskell IDE or something like that, you most likely don't need this (^1).
But, in the case you do, there is always the hint-package, which allows you to embed a haskell interpreter into your program. This allows you to both load haskell modules and to convert strings into haskell values at runtime. There is a nice example of how to use it here
^1: If you're looking for a way to make things polymorphic, i.e. changing some, but not all definitions of in your code, you're probably looking for typeclasses.
With regards to your edit, perhaps you might be interested in IORef.

Resources