
Hello, I am getting what is to me a mysterious error in a test case that I am writing: vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o ) ./timer.hs:11:45: Not in scope: data constructor `FunPtr' It seems like the compiler is complaining about the lack of FunPtr in it's symbol table but System.Posix is imported: module Main where import System.Posix import Foreign import Foreign.C import Foreign.Ptr main = do let event = Sigevent{sigevFunction=(FunPtr (notifyFunc))} <<<<<< error here timerId <- timerCreate Clock_Realtime Nothing timerDelete timerId return () notifyFunc :: Sigval -> IO () notifyFunc sigval = do putStrLn "timer POP!!!!!!!" return () I am probably looking right at the answer and not seeing it. ?? Thanks, Vasili

2008/6/8 Galchin, Vasili
Hello,
I am getting what is to me a mysterious error in a test case that I am writing: vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:11:45: Not in scope: data constructor `FunPtr'
There is a *type* called FunPtr in scope, but not a data constructor as you are using it. That is, you could say: foo :: FunPtr (Int -> IO ()) That is, use the type called FunPtr, but you may not use a *function* called FunPtr, because it doesn't exist. You need to use functions like nullFunPtr, castPtrToFunPtr, etc. to construct FunPtrs. Luke

ah ..,. right ,..... my bad.
Vasili
On Sun, Jun 8, 2008 at 10:01 PM, Luke Palmer
2008/6/8 Galchin, Vasili
: Hello,
I am getting what is to me a mysterious error in a test case that I am writing: vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:11:45: Not in scope: data constructor `FunPtr'
There is a *type* called FunPtr in scope, but not a data constructor as you are using it. That is, you could say:
foo :: FunPtr (Int -> IO ())
That is, use the type called FunPtr, but you may not use a *function* called FunPtr, because it doesn't exist. You need to use functions like nullFunPtr, castPtrToFunPtr, etc. to construct FunPtrs.
Luke

Hello Vasili, Monday, June 9, 2008, 6:17:14 AM, you wrote: 1. standard place to import FunPtr from is Foreign.Ptr, not System.Posix 2. FunPtr is exported as abstract type, without constructors. you can't construct values of this type directly. instead you should use "wrapper" generators as in the example that Clause has wrote. read it carefully :)
Hello,
I am getting what is to me a mysterious error in a test case that I am writing: vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:11:45: Not in scope: data constructor `FunPtr'
It seems like the compiler is complaining about the lack of FunPtr in it's symbol table but System.Posix is imported:
module Main where
import System.Posix import Foreign import Foreign.C import Foreign.Ptr
main = do
let event = Sigevent{sigevFunction=(FunPtr (notifyFunc))} <<<<<< error here
timerId <- timerCreate Clock_Realtime Nothing
timerDelete timerId
return ()
notifyFunc :: Sigval -> IO () notifyFunc sigval = do putStrLn "timer POP!!!!!!!" return ()
I am probably looking right at the answer and not seeing it. ??
Thanks, Vasili
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Thanks. Clause?
regards, Vasili
On Mon, Jun 9, 2008 at 12:54 AM, Bulat Ziganshin
Hello Vasili,
Monday, June 9, 2008, 6:17:14 AM, you wrote:
1. standard place to import FunPtr from is Foreign.Ptr, not System.Posix 2. FunPtr is exported as abstract type, without constructors. you can't construct values of this type directly. instead you should use "wrapper" generators as in the example that Clause has wrote. read it carefully :)
Hello,
I am getting what is to me a mysterious error in a test case that I
am writing:
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:11:45: Not in scope: data constructor `FunPtr'
It seems like the compiler is complaining about the lack of FunPtr in it's symbol table but System.Posix is imported:
module Main where
import System.Posix import Foreign import Foreign.C import Foreign.Ptr
main = do
let event = Sigevent{sigevFunction=(FunPtr (notifyFunc))}
<<<<<< error here
timerId <- timerCreate Clock_Realtime Nothing
timerDelete timerId
return ()
notifyFunc :: Sigval -> IO () notifyFunc sigval = do putStrLn "timer POP!!!!!!!" return ()
I am probably looking right at the answer and not seeing it. ??
Thanks, Vasili
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

In any case, what I want to do is store FunPtr in a data type and marshall
into a C struct as a C function pointer.
Vasili
On Mon, Jun 9, 2008 at 1:24 AM, Galchin, Vasili
Thanks. Clause?
regards, Vasili
On Mon, Jun 9, 2008 at 12:54 AM, Bulat Ziganshin < bulat.ziganshin@gmail.com> wrote:
Hello Vasili,
Monday, June 9, 2008, 6:17:14 AM, you wrote:
1. standard place to import FunPtr from is Foreign.Ptr, not System.Posix 2. FunPtr is exported as abstract type, without constructors. you can't construct values of this type directly. instead you should use "wrapper" generators as in the example that Clause has wrote. read it carefully :)
Hello,
I am getting what is to me a mysterious error in a test case that I
am writing:
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:11:45: Not in scope: data constructor `FunPtr'
It seems like the compiler is complaining about the lack of FunPtr in it's symbol table but System.Posix is imported:
module Main where
import System.Posix import Foreign import Foreign.C import Foreign.Ptr
main = do
let event = Sigevent{sigevFunction=(FunPtr (notifyFunc))}
<<<<<< error here
timerId <- timerCreate Clock_Realtime Nothing
timerDelete timerId
return ()
notifyFunc :: Sigval -> IO () notifyFunc sigval = do putStrLn "timer POP!!!!!!!" return ()
I am probably looking right at the answer and not seeing it. ??
Thanks, Vasili
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

type Notify = Sigval -> IO () foreign import ccall "wrapper" mkNotify :: Notify -> IO (FunPtr Notify)
then
main = do notifyFPtr <- mkNotify notifyFunc -- rest of code here
-- then, when you are done and nothing is referencing the pointer any more freeHaskellFunPtr notifyFPtr
On 6/9/08, Galchin, Vasili
In any case, what I want to do is store FunPtr in a data type and marshall into a C struct as a C function pointer.
Vasili
This will be suitable for that purpose. -- ryan

Ryan,
I tried but the compiler didn't seem to like the keyword "import":
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell
Setup.lhs build
Preprocessing executables for Test-1.0...
Building Test-1.0...
[1 of 1] Compiling Main ( ./timer.hs,
dist/build/timer/timer-tmp/Main.o )
./timer.hs:29:8: parse error on input `import'
.... source ...:
module Main where
import System.Posix
import Foreign
import Foreign.C
import Foreign.Ptr
type Notify = Sigval -> IO ()
main = do
notifyFPtr <- mkNotify notifyFunc
let event = Sigevent{sigevFunction=notifyFPtr}
timerId <- timerCreate Clock_Realtime Nothing
timerDelete timerId
return ()
notifyFunc :: Sigval -> IO ()
notifyFunc sigval = do
putStrLn "timer POP!!!!!!!"
return ()
foreign import ccall "wrapper"
mkNotify :: Notify -> IO (FunPtr Notify)
~
Everything looks ok to me. ??
Regards, Vasili
On Mon, Jun 9, 2008 at 2:16 PM, Ryan Ingram
type Notify = Sigval -> IO () foreign import ccall "wrapper" mkNotify :: Notify -> IO (FunPtr Notify)
then
main = do notifyFPtr <- mkNotify notifyFunc -- rest of code here
-- then, when you are done and nothing is referencing the pointer any more freeHaskellFunPtr notifyFPtr
On 6/9/08, Galchin, Vasili
wrote: In any case, what I want to do is store FunPtr in a data type and marshall into a C struct as a C function pointer.
Vasili
This will be suitable for that purpose.
-- ryan

2008/6/9 Galchin, Vasili
Ryan,
I tried but the compiler didn't seem to like the keyword "import":
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:29:8: parse error on input `import'
Hi Vasili, To fix that error, you probably just need to add the line "Extensions: ForeignFunctionInterface" to the .cabal file. (That is the equivalent of calling ghc by itself with the command-line arguments -fffi or -XForeignFunctionInterface.) Hope that helps, -Judah

Thanks Judah ... getting closer now.
Vasili
On Mon, Jun 9, 2008 at 11:25 PM, Judah Jacobson
2008/6/9 Galchin, Vasili
: Ryan,
I tried but the compiler didn't seem to like the keyword "import":
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:29:8: parse error on input `import'
Hi Vasili,
To fix that error, you probably just need to add the line "Extensions: ForeignFunctionInterface" to the .cabal file. (That is the equivalent of calling ghc by itself with the command-line arguments -fffi or -XForeignFunctionInterface.)
Hope that helps, -Judah

I have tried various things to no avail ....
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell
Setup.lhs build
Preprocessing executables for Test-1.0...
Building Test-1.0...
[1 of 1] Compiling Main ( ./timer.hs,
dist/build/timer/timer-tmp/Main.o )
./timer.hs:22:0:
Unacceptable argument type in foreign declaration: Sigval
When checking declaration:
foreign import ccall safe "wrapper" mkNotify
:: Notify -> IO (FunPtr Notify)
=> here is my Sigval def
data Sigval = SivalInt Int | SivalPtr (Ptr Char)
I did a find/grep for "Unacceptable argument" in the ghc compiler source and
assuming no typo I didn't find. ??
Thanks.
Kind regards, Vasili
On Mon, Jun 9, 2008 at 11:25 PM, Judah Jacobson
2008/6/9 Galchin, Vasili
: Ryan,
I tried but the compiler didn't seem to like the keyword "import":
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:29:8: parse error on input `import'
Hi Vasili,
To fix that error, you probably just need to add the line "Extensions: ForeignFunctionInterface" to the .cabal file. (That is the equivalent of calling ghc by itself with the command-line arguments -fffi or -XForeignFunctionInterface.)
Hope that helps, -Judah

I'm not super experienced with the FFI (foreign function interface); I
only used C types that you can get from #include
I have tried various things to no avail ....
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:22:0: Unacceptable argument type in foreign declaration: Sigval When checking declaration: foreign import ccall safe "wrapper" mkNotify :: Notify -> IO (FunPtr Notify)
=> here is my Sigval def
data Sigval = SivalInt Int | SivalPtr (Ptr Char)
I did a find/grep for "Unacceptable argument" in the ghc compiler source and assuming no typo I didn't find. ??
Thanks.
Kind regards, Vasili
On Mon, Jun 9, 2008 at 11:25 PM, Judah Jacobson
wrote: 2008/6/9 Galchin, Vasili
: Ryan,
I tried but the compiler didn't seem to like the keyword "import":
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell
Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:29:8: parse error on input `import'
Hi Vasili,
To fix that error, you probably just need to add the line "Extensions: ForeignFunctionInterface" to the .cabal file. (That is the equivalent of calling ghc by itself with the command-line arguments -fffi or -XForeignFunctionInterface.)
Hope that helps, -Judah

Also, if you always plan to partially apply your notification function and you just want a simple callback: void (*pCallbackFn)(void); You should be able to do something like this:
type Callback = IO () foreign import ccall "wrapper" mkCallback :: Callback -> IO (FunPtr Callback)
You can then do something like
main = do let event = Sigval { ... fill in here ... } callback <- mkCallback (notify event) ... freeHaskellFunPtr callback
On 6/10/08, Ryan Ingram
I'm not super experienced with the FFI (foreign function interface); I only used C types that you can get from #include
, like Word32. You might need to make Sigval an instance of Storable, or do some magic with ForeignPtrs. Good luck! :)
-- ryan
On 6/9/08, Galchin, Vasili
wrote: I have tried various things to no avail ....
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:22:0: Unacceptable argument type in foreign declaration: Sigval When checking declaration: foreign import ccall safe "wrapper" mkNotify :: Notify -> IO (FunPtr Notify)
=> here is my Sigval def
data Sigval = SivalInt Int | SivalPtr (Ptr Char)
I did a find/grep for "Unacceptable argument" in the ghc compiler source and assuming no typo I didn't find. ??
Thanks.
Kind regards, Vasili
On Mon, Jun 9, 2008 at 11:25 PM, Judah Jacobson
wrote: 2008/6/9 Galchin, Vasili
: Ryan,
I tried but the compiler didn't seem to like the keyword "import":
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell
Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:29:8: parse error on input `import'
Hi Vasili,
To fix that error, you probably just need to add the line "Extensions: ForeignFunctionInterface" to the .cabal file. (That is the equivalent of calling ghc by itself with the command-line arguments -fffi or -XForeignFunctionInterface.)
Hope that helps, -Judah

If I change
data Sigval = SivalInt Int | SivalPtr (Ptr Char)
to ...
newtype Sigval = Sivalint Int | SivalPtr (Ptr Char)
then my test case builds and links. ??
Regards, Vasili
On Mon, Jun 9, 2008 at 11:01 PM, Galchin, Vasili
I have tried various things to no avail ....
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:22:0: Unacceptable argument type in foreign declaration: Sigval When checking declaration: foreign import ccall safe "wrapper" mkNotify :: Notify -> IO (FunPtr Notify)
=> here is my Sigval def
data Sigval = SivalInt Int | SivalPtr (Ptr Char)
I did a find/grep for "Unacceptable argument" in the ghc compiler source and assuming no typo I didn't find. ??
Thanks.
Kind regards, Vasili
On Mon, Jun 9, 2008 at 11:25 PM, Judah Jacobson
wrote: 2008/6/9 Galchin, Vasili
: Ryan,
I tried but the compiler didn't seem to like the keyword "import":
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:29:8: parse error on input `import'
Hi Vasili,
To fix that error, you probably just need to add the line "Extensions: ForeignFunctionInterface" to the .cabal file. (That is the equivalent of calling ghc by itself with the command-line arguments -fffi or -XForeignFunctionInterface.)
Hope that helps, -Judah

oops .. my bad ...
If I change data Sigval = SivalInt Int
to newtype Sigval = SivalInt Int
OR
data Sigval = SivalPtr (Ptr Char)
to newtype Sigval = SivalPtr (Ptr Char).
Why should "newtype" instead of a data type allow my test case to build?
Vasili
On Fri, Jun 13, 2008 at 10:59 PM, Galchin, Vasili
If I change
data Sigval = SivalInt Int | SivalPtr (Ptr Char) to ...
newtype Sigval = Sivalint Int | SivalPtr (Ptr Char)
then my test case builds and links. ??
Regards, Vasili
On Mon, Jun 9, 2008 at 11:01 PM, Galchin, Vasili
wrote: I have tried various things to no avail ....
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:22:0: Unacceptable argument type in foreign declaration: Sigval When checking declaration: foreign import ccall safe "wrapper" mkNotify :: Notify -> IO (FunPtr Notify)
=> here is my Sigval def
data Sigval = SivalInt Int | SivalPtr (Ptr Char)
I did a find/grep for "Unacceptable argument" in the ghc compiler source and assuming no typo I didn't find. ??
Thanks.
Kind regards, Vasili
On Mon, Jun 9, 2008 at 11:25 PM, Judah Jacobson < judah.jacobson@gmail.com> wrote:
2008/6/9 Galchin, Vasili
: Ryan,
I tried but the compiler didn't seem to like the keyword "import":
vigalchin@ubuntu:~/FTP/Haskell/unix-2.2.0.0/tests/timer$ runhaskell Setup.lhs build Preprocessing executables for Test-1.0... Building Test-1.0... [1 of 1] Compiling Main ( ./timer.hs, dist/build/timer/timer-tmp/Main.o )
./timer.hs:29:8: parse error on input `import'
Hi Vasili,
To fix that error, you probably just need to add the line "Extensions: ForeignFunctionInterface" to the .cabal file. (That is the equivalent of calling ghc by itself with the command-line arguments -fffi or -XForeignFunctionInterface.)
Hope that helps, -Judah

The simple explanation is "because the FFI standard says so"; primitive types wrapped in newtypes automatically get wrapped and unwrapped during FFI calls. See http://www.cse.unsw.edu.au/~chak/haskell/ffi/ffi/ffise3.html#x6-120003.2 ; the FFI uses "renamed datatype" to mean newtype. Consider the following declarations translated to C:
type Weak = Word32 {- typedef HsWord32 Weak; }
Weak is just a type alias; you can use Word32 and Weak interchangeably in your code after delcaring this type.
newtype Strong = Strong Word32 {- doesn't really exist in C; google for "c strong typedef" -}
Strong behaves exactly like a Word32 at runtime, in terms of storage, but the typechecker can distinguish it from Word32.
data Holder = Holder Word32 {- typedef struct { HsWord32 x; } Holder; -}
Holder, on the other hand, is entirely separate from Word32; in fact, there are strictly more values in Haskell of type Holder than there are of type Word32:
ha, hb, hc :: Holder -- can all be distinguished at runtime ha = undefined hb = Holder undefined hc = Holder 0
sa, sb :: Strong -- cannot be distinguished sa = undefined sb = Strong undefined
participants (5)
-
Bulat Ziganshin
-
Galchin, Vasili
-
Judah Jacobson
-
Luke Palmer
-
Ryan Ingram