how to write an haskell binding

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

how to write an haskell binding

Vo Minh Thu
hi,

hopefully (well, i liked the discussions) this message will not spawn
so much messages :)

i'd like to know if there exist some general guidelines/information
about writing an haskell (or is it *a* haskell ?) binding for a c or a
c++ library.

i worry about :
* implementation tools : use tools like c2hs or others, or do that bare hands
* haskell idioms/usage : make good use of monads, type classes and
other haskell related things
* for c++, is it better to first write a c api for the c++ code before
writing the binding
* maybe advice about the c api knowing there will be haskell binding
* any thing i forgot

thanks a lot,
thu
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: how to write an haskell binding

Brian Hulley
minh thu wrote:
> about writing an haskell (or is it *a* haskell ?) binding for a c or a

It is defnitely *a* haskell. There is actually no word in English with a
silent 'h', though this statement is unfortunately controversial and news to
whoever wrote the spell checker used in many printed publications. Of course
some particular dialects use different pronunciation like "me 'otel room 'ad
an 'askell 'mpiler in t' closet as well as tub 'n sink tha knows"

> c++ library.
>
> i worry about :
> * implementation tools : use tools like c2hs or others, or do that
> bare hands

I'd recommend just using "bare hands" :-)

> * haskell idioms/usage : make good use of monads, type classes and
> other haskell related things

You might like to look at some existing libraries to get ideas (
http://www.haskell.org/haskellwiki/Libraries_and_tools )

> * for c++, is it better to first write a c api for the c++ code before
> writing the binding

Personnally, I write everything in C++, then have just one unit of plain C
functions which are exported as the api, so the entire api is contained in
one .h, .cpp (with extern "C" around the bindings), and .def file. I find it
helpful to have a consistent naming convention, so if I have a static C++
class such as:

    class TimerFactory {
        static void Construct();
        static void Destruct();
    };

then the C api functions are given names like:

     xxx_TimerConstruct()

where xxx is the name of the api.

> * maybe advice about the c api knowing there will be haskell binding
> * any thing i forgot

Perhaps to consider using the Haskell types for everything in the C code eg
HsInt, HsWord16 etc instead of plain int, unsigned short etc, so that your
Haskell code isn't messed up by having to use CInt instead of plain Int etc.

Regards, Brian.
--
Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.

http://www.metamilk.com 

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: how to write an haskell binding

Brian Hulley
Brian Hulley wrote:

> minh thu wrote
>> * for c++, is it better to first write a c api for the c++ code before
>> writing the binding
> [snip]
>    class TimerFactory {
>        static void Construct();
>        static void Destruct();
>    };
>
> then the C api functions are given names like:
>
>     xxx_TimerConstruct()
Ooops!
xxx_TimerFactory_Construct()

I think I also missed the point of your question. In the program I'm writing
sometimes I find I need to write the binding first and other times the C
first and other times the C++ first since it is not always clear how to
achieve a goal. One other thing to bear in mind is that foreign calls are
extremely slow, so for example it is much faster to use the
Foreign.Marshal.Array and Foreign.C.String functions to allocate and
populate a temporary array with the contents of a list, and send the pointer
to this array to C with one foreign call, than to send each element of the
list with multiple foreign calls (eg to paste only 19K of text from the
Windows clipboard to my app took over 1 minute!!! with individual foreign
calls but a fraction of a second when I switched to passing a pointer and
using peekCAString (ditto pokeArray0 for copy))

Regards, Brian.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re[2]: how to write an haskell binding

Bulat Ziganshin-2
Hello Brian,

Tuesday, June 27, 2006, 2:43:15 AM, you wrote:

> achieve a goal. One other thing to bear in mind is that foreign calls are
> extremely slow, so for example it is much faster to use the
> Foreign.Marshal.Array and Foreign.C.String functions to allocate and
> populate a temporary array with the contents of a list, and send the pointer
> to this array to C with one foreign call, than to send each element of the
> list with multiple foreign calls (eg to paste only 19K of text from the
> Windows clipboard to my app took over 1 minute!!!

he-he-he :)  just add "unsafe" specifier:

foreign import ccall unsafe  "Compression.h CanonizeCompressionMethod"
   c_CanonizeCompressionMethod   :: CMethod -> CMethod -> IO Int

afair, it was 60k or 600k calls/second on my 1 GHz CPU

in http://www.cse.unsw.edu.au/~chak/haskell/ghc/comm/rts-libs/multi-thread.html
there is description why "safe" calls are so slow:


Presently, GHC handles 'safe' C calls by effectively emitting the
following code sequence:

    ...save thread state...
    t = suspendThread();
    r = foo(arg1,...,argn);
    resumeThread(t);
    ...restore thread state...
    return r;

After having squirreled away the state of a Haskell thread,
Schedule.c:suspendThread() is called which puts the current thread on
a list [Schedule.c:suspended_ccalling_threads] containing threads that
are currently blocked waiting for external calls to complete (this is
done for the purposes of finding roots when garbage collecting).

In addition to putting the Haskell thread on
suspended_ccalling_threads, suspendThread() now also does the following:

- Instructs the Task Manager to make sure that there's a another
native thread waiting in the wings to take over the execution of
Haskell threads. This might entail creating a new worker thread or
re-using one that's currently waiting for more work to do. The Task
Manager section presents the functionality provided by this subsystem.    

- Releases its capability to execute within the RTS. By doing so,
another worker thread will become unblocked and start executing code
within the RTS. See the Capability section for details.

- suspendThread() returns a token which is used to identify the
Haskell thread that was added to suspended_ccalling_threads. This is
done so that once the external call has completed, we know what
Haskell thread to pull off the suspended_ccalling_threads list.


Upon return from suspendThread(), the OS thread is free of its RTS
executing responsibility, and can now invoke the external call.
Meanwhile, the other worker thread that have now gained access to the
RTS will continue executing Concurrent Haskell code. Concurrent
'stuff' is happening!    


--
Best regards,
 Bulat                            mailto:[hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: how to write an haskell binding

Lennart Augustsson
In reply to this post by Brian Hulley
Quoting Brian Hulley <[hidden email]>:

> It is defnitely *a* haskell. There is actually no word in English
> with a silent 'h', though this statement is unfortunately
> controversial and news to whoever wrote the spell checker used in
> many printed publications.

"There is no English word with a silrnt 'h'"... What kind of English do
you speak?
What about words like 'hour' and 'honest'?

  -- Lennart
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: how to write an haskell binding

Vo Minh Thu
In reply to this post by Bulat Ziganshin-2
Brian, Bulat, thank you,
thu
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: how to write an haskell binding

Vo Minh Thu
Brian, mmm, no you wasn't missing the point : actually, i asked if we
bind against c or c++. But that way, you answer the "general
guidelines" part of the question.

thx
thu

2006/6/27, minh thu <[hidden email]>:
> Brian, Bulat, thank you,
> thu
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: how to write an haskell binding

Brian Hulley
In reply to this post by Lennart Augustsson
[hidden email] wrote:

> Quoting Brian Hulley <[hidden email]>:
>
>> It is defnitely *a* haskell. There is actually no word in English
>> with a silent 'h', though this statement is unfortunately
>> controversial and news to whoever wrote the spell checker used in
>> many printed publications.
>
> "There is no English word with a silrnt 'h'"... What kind of English
> do you speak?
> What about words like 'hour' and 'honest'?

Ok apologies for forgetting about these 2 words (and their derivatives) - I
agree they do have a silent 'h'.
I suppose they are the exception that proves the rule... :-)

Regards, Brian.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: how to write an haskell binding

Aaron Denney
On 2006-06-27, Brian Hulley <[hidden email]> wrote:

> [hidden email] wrote:
>> Quoting Brian Hulley <[hidden email]>:
>>
>>> It is defnitely *a* haskell. There is actually no word in English
>>> with a silent 'h', though this statement is unfortunately
>>> controversial and news to whoever wrote the spell checker used in
>>> many printed publications.
>>
>> "There is no English word with a silrnt 'h'"... What kind of English
>> do you speak?
>> What about words like 'hour' and 'honest'?
>
> Ok apologies for forgetting about these 2 words (and their derivatives) - I
> agree they do have a silent 'h'.
> I suppose they are the exception that proves the rule... :-)

Don't forget "honor".

--
Aaron Denney
-><-

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re: how to write an haskell binding

Brian Hulley
Aaron Denney wrote:

> On 2006-06-27, Brian Hulley <[hidden email]> wrote:
>> [hidden email] wrote:
>>> Quoting Brian Hulley <[hidden email]>:
>>>
>>>> It is defnitely *a* haskell. There is actually no word in English
>>>> with a silent 'h', though this statement is unfortunately
>>>> controversial and news to whoever wrote the spell checker used in
>>>> many printed publications.
>>>
>>> "There is no English word with a silrnt 'h'"... What kind of English
>>> do you speak?
>>> What about words like 'hour' and 'honest'?
>>
>> Ok apologies for forgetting about these 2 words (and their
>> derivatives) - I agree they do have a silent 'h'.
>> I suppose they are the exception that proves the rule... :-)
>
> Don't forget "honor".

>From the Collins English dictionary:

"honest" : not given to lying, cheating, stealing etc
"honour" or U.S. honor : personal integrity, allegiance to moral principles
...

So I'd say these two words are closely related, so the search is still on
for another word with silent 'h' not related to time or integrity.

Regards, Brian.

--
Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.

http://www.metamilk.com 

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re: how to write an haskell binding

Jeremy Shaw
At Tue, 27 Jun 2006 20:36:30 +0100,
Brian Hulley wrote:

> >>> What about words like 'hour' and 'honest'?

> > Don't forget "honor".

> So I'd say these two words are closely related, so the search is still on
> for another word with silent 'h' not related to time or integrity.

How about heir? Also, until recently, herb and humble?

Got those from this page:

http://www.askoxford.com/worldofwords/wordfrom/aitches/?view=uk

Which also notes that sometimes we still use 'an' even though the 'h'
is no longer silent. For example, 'an heroic effort', or 'an historic
moment'.

j.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re: how to write an haskell binding

Brian Hulley
Jeremy Shaw wrote:

> At Tue, 27 Jun 2006 20:36:30 +0100,
> Brian Hulley wrote:
>
>>>>> What about words like 'hour' and 'honest'?
>
>>> Don't forget "honor".
>
>> So I'd say these two words are closely related, so the search is
>> still on for another word with silent 'h' not related to time or
>> integrity.
>
> How about heir?

Thanks - I'd not thought of that one!

>Also, until recently, herb and humble?

I think these depend on one's dialect. I'd never think of saying "an herb"
but of course many people would.

>
> Got those from this page:
>
> http://www.askoxford.com/worldofwords/wordfrom/aitches/?view=uk
>
> Which also notes that sometimes we still use 'an' even though the 'h'
> is no longer silent. For example, 'an heroic effort', or 'an historic
> moment'.

This is what I meant by the evil spellchecker writer - I think this is just
some particular dialect trying to take over the whole language...

Still everyone will be pleased to know that I won't post any more about this
subject now that the third true silent 'h' has been found :-)

Best regards, Brian.

--
Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.

http://www.metamilk.com 

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re: how to write an haskell binding

Bill Wood-3
In reply to this post by Jeremy Shaw
On Tue, 2006-06-27 at 13:35 -0700, Jeremy Shaw wrote:
   . . .
> How about heir? Also, until recently, herb and humble?

I grew up in the southern US, and I was taught 'herb' with silent 'h'
but 'humble' with aspirated 'h'.  With the 'h' silent 'humble' sounds
very Dickensian to my ear.

-- Bill Wood


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re: how to write an haskell binding

Vo Minh Thu
In reply to this post by Brian Hulley
old me>hopefully (well, i liked the discussions) this message will not spawn
old me>so much messages :)


> Still everyone will be pleased to know that I won't post any more about this
> subject now that the third true silent 'h' has been found :-)
>
> Best regards, Brian.

my apologies ;)
thu
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: how to write an haskell binding

Fritz Ruehr
In reply to this post by Brian Hulley
On Jun 27, 2006, at 10:35 AM, Brian Hulley wrote:

> I suppose they are the exception that proves the rule... :-)

Seems like there's a real opportunity here for someone who works in the
area of inference systems for error handling ... .

(Hmmm, pun-potential may not be the best way to pick a research topic,
though.)

   --  Fritz

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe