I'm trying to implement the FFI part for my codegen, and although passing values like Int
works, my biggest issue is figuring out how to pass a function.
So, in the docs there is this example with the JS FFI, that passes a function to the JS world, calls it there, and gets the result back, which is what I'm trying to implement. The code in the docs is this:
function twice(f, x) {
return f(f(x));
}
and the Idris code for the FFI binding is this one:
twice : (Int -> Int) -> Int -> IO Int
twice f x = mkForeign (
FFun "twice(%0,%1)" [FFunction FInt FInt, FInt] FInt
) f x
I don't have access to the mkForeign
function, because it looks that is from the JS codegen RTS. So I tried to use foreign
from Idris builtins:
twice : (Int -> Int) -> Int -> IO Int
twice =
foreign FFI_AHK "twice"
((Int -> Int) -> Int -> IO Int)
Which errors in:
When checking right hand side of twice with expected type
(Int -> Int) -> Int -> IO Int
When checking argument fty to function foreign:
Can't find a value of type
FTy FFI_AHK [] ((Int -> Int) -> Int -> IO Int)
Which I guess it is due to the last parameter of foreign
, which has type
{auto fty : FTy f [] ty}
Documented as:
fty - an automatically-found proof that the Idris type works with the FFI in question
How does one satisfy this proof? I'm pretty lost at this point
FFI_AHK
coming from? – Joomy Korkut