Calling Haskell from other languages?

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

Calling Haskell from other languages?

Colin Paul Adams
Is there a way to call Haskell code from other languages? I have looked on
the wiki, and as far as I can see, it only talks about the other way
round (when Haskell is the main program).
--
Colin Adams
Preston Lancashire
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Calling Haskell from other languages?

Jake Mcarthur-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 11, 2008, at 10:45 AM, Colin Paul Adams wrote:

> Is there a way to call Haskell code from other languages? I have  
> looked on
> the wiki, and as far as I can see, it only talks about the other way
> round (when Haskell is the main program).

http://www.haskell.org/haskellwiki/GHC/Using_the_FFI#Calling_Haskell_from_C
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkkZuCkACgkQye5hVyvIUKk1pwCfRbVnERADZPygCNX2wjNkdQOC
FXMAoKodV1TitVzr5ZJF/AUSmCXuRuKY
=BUkK
-----END PGP SIGNATURE-----
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Calling Haskell from other languages?

Jake Mcarthur-2
In reply to this post by Colin Paul Adams
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Actually, that's not the whole story. I didn't realize until I sent  
it. There does exist good documentation for this, I promise.

- - Jake
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkkZuHoACgkQye5hVyvIUKnGeACfZN+9nNy+nGvQHDsG1FrI4Puu
Zw0AmgKK0WOYTjWWeQX93rrnSJApG0pa
=Vazy
-----END PGP SIGNATURE-----
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Calling Haskell from other languages?

Donn Cave-4
In reply to this post by Colin Paul Adams
Quoth Colin Paul Adams <[hidden email]>:

| Is there a way to call Haskell code from other languages? I have looked on
| the wiki, and as far as I can see, it only talks about the other way
| round (when Haskell is the main program).

There sure is a way to call from other languages - cf. "foreign export" -
but don't know how hard it would be to make Haskell live without its "main",
which performs a lot of runtime initialization.

For me (with nhc98) it's a lot easier to use a Haskell "main" that's just
a brief wrapper:

  module Main (main) where
  import NHC.FFI

  foreign import ccall "cmain" cmain :: IO ()

  main = cmain

(... where "cmain" is an external entry point in the C code, which can
then call Haskell as required.)

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

Re: Calling Haskell from other languages?

Colin Paul Adams
In reply to this post by Jake Mcarthur-2
>>>>> "Jake" == Jake Mcarthur <[hidden email]> writes:

    Jake> Actually, that's not the whole story. I didn't realize until
    Jake> I sent it. There does exist good documentation for this, I
    Jake> promise.

Good. Let me know where it is when you track it down.

The link you pointed me too doesn't seem to address my question
directly. Also, it only talks about C.

If I want to call Haskell (and I do, perhaps) from another
garbage-collected language (Eiffel, in particular) using C as the
mutually understood language, am I not going to run into big problems?
--
Colin Adams
Preston Lancashire
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re[2]: Calling Haskell from other languages?

Bulat Ziganshin-2
Hello Colin,

Tuesday, November 11, 2008, 8:09:09 PM, you wrote:

> If I want to call Haskell (and I do, perhaps) from another
> garbage-collected language (Eiffel, in particular) using C as the
> mutually understood language, am I not going to run into big problems?

of course not. there will be two independent heaps. of course you
can't directly send pointers to gc-collected structures between two
languages, they should be converted through buffers in C world
(i.e. malloced ones)


--
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: Calling Haskell from other languages?

Derek Elkins
In reply to this post by Colin Paul Adams
On Tue, 2008-11-11 at 17:09 +0000, Colin Paul Adams wrote:

> >>>>> "Jake" == Jake Mcarthur <[hidden email]> writes:
>
>     Jake> Actually, that's not the whole story. I didn't realize until
>     Jake> I sent it. There does exist good documentation for this, I
>     Jake> promise.
>
> Good. Let me know where it is when you track it down.
>
> The link you pointed me too doesn't seem to address my question
> directly. Also, it only talks about C.
>
> If I want to call Haskell (and I do, perhaps) from another
> garbage-collected language (Eiffel, in particular) using C as the
> mutually understood language, am I not going to run into big problems?

Read the FFI Report.  It is relatively readable and comprehensive.
http://www.cse.unsw.edu.au/~chak/haskell/ffi/

And yes, you will have to use C as an intermediary, though you may not
have to actually write any C.  You simply expose the Haskell functions
in whatever form the other language expects.  You'll almost certainly
have to write marshalling code of some sort.

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

Re: Calling Haskell from other languages?

John Goerzen-3
On Tue, Nov 11, 2008 at 12:35:05PM -0600, Derek Elkins wrote:

> On Tue, 2008-11-11 at 17:09 +0000, Colin Paul Adams wrote:
> > >>>>> "Jake" == Jake Mcarthur <[hidden email]> writes:
> >
> >     Jake> Actually, that's not the whole story. I didn't realize until
> >     Jake> I sent it. There does exist good documentation for this, I
> >     Jake> promise.
> >
> > Good. Let me know where it is when you track it down.
> >
> > The link you pointed me too doesn't seem to address my question
> > directly. Also, it only talks about C.
> >
> > If I want to call Haskell (and I do, perhaps) from another
> > garbage-collected language (Eiffel, in particular) using C as the
> > mutually understood language, am I not going to run into big problems?
>
> Read the FFI Report.  It is relatively readable and comprehensive.
> http://www.cse.unsw.edu.au/~chak/haskell/ffi/
>
> And yes, you will have to use C as an intermediary, though you may not
> have to actually write any C.  You simply expose the Haskell functions
> in whatever form the other language expects.  You'll almost certainly
> have to write marshalling code of some sort.

I often like to look at these situations as an opportunity to
introduce modularity and piping.  Do you really need them running in
the same address space?


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

Re: Calling Haskell from other languages?

Don Stewart-2
In reply to this post by Colin Paul Adams
colin:
> Is there a way to call Haskell code from other languages? I have looked on
> the wiki, and as far as I can see, it only talks about the other way
> round (when Haskell is the main program).

http://haskell.org/haskellwiki/Calling_Haskell_from_C

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

Re: Calling Haskell from other languages?

Don Stewart-2
In reply to this post by Colin Paul Adams
colin:

> >>>>> "Jake" == Jake Mcarthur <[hidden email]> writes:
>
>     Jake> Actually, that's not the whole story. I didn't realize until
>     Jake> I sent it. There does exist good documentation for this, I
>     Jake> promise.
>
> Good. Let me know where it is when you track it down.
>
> The link you pointed me too doesn't seem to address my question
> directly. Also, it only talks about C.
>
> If I want to call Haskell (and I do, perhaps) from another
> garbage-collected language (Eiffel, in particular) using C as the
> mutually understood language, am I not going to run into big problems?

No, you can manage objects via e.g. ForeignPtr's. The community
has a whole has done bridges between Haskell and pretty much anything
you care to name, via the C FFI. Just depends on how well you understand
the memory management models of both players.

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

Re: Calling Haskell from other languages?

Colin Paul Adams
In reply to this post by John Goerzen-3
>>>>> "John" == John Goerzen <[hidden email]> writes:

    >> Read the FFI Report.  It is relatively readable and
    >> comprehensive.  http://www.cse.unsw.edu.au/~chak/haskell/ffi/
    >>
    >> And yes, you will have to use C as an intermediary, though you
    >> may not have to actually write any C.  You simply expose the
    >> Haskell functions in whatever form the other language expects.
    >> You'll almost certainly have to write marshalling code of some
    >> sort.

    John> I often like to look at these situations as an opportunity
    John> to introduce modularity and piping.  Do you really need them
    John> running in the same address space?

Good point.

And thanks everyone for your help. I have enough to go on now.
--
Colin Adams
Preston Lancashire
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Calling Haskell from other languages?

Fraser Wilson
In reply to this post by Colin Paul Adams

I had a proof of concept lying around a couple of years ago in which a  
big complicated Ada program called a big complicated Haskell program  
and vice versa. The tricky bit from memory was making it link, and  
satisfying their rumtime initialisation requirements. No explicit C  
was required I think.

Fraser
On 11 nov 2008, at 18:09, Colin Paul Adams <[hidden email]>  
wrote:

>>>>>> "Jake" == Jake Mcarthur <[hidden email]> writes:
>
>    Jake> Actually, that's not the whole story. I didn't realize until
>    Jake> I sent it. There does exist good documentation for this, I
>    Jake> promise.
>
> Good. Let me know where it is when you track it down.
>
> The link you pointed me too doesn't seem to address my question
> directly. Also, it only talks about C.
>
> If I want to call Haskell (and I do, perhaps) from another
> garbage-collected language (Eiffel, in particular) using C as the
> mutually understood language, am I not going to run into big problems?
> --
> Colin Adams
> Preston Lancashire
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Calling Haskell from other languages?

Dave Tapley-2
In reply to this post by Colin Paul Adams
Hi Colin

As an alternative you may consider using Thrift:
http://incubator.apache.org/thrift/

Cheers,
Dave


On Tue, 2008-11-11 at 16:45 +0000, Colin Paul Adams wrote:
> Is there a way to call Haskell code from other languages? I have looked on
> the wiki, and as far as I can see, it only talks about the other way
> round (when Haskell is the main program).

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

Re: Calling Haskell from other languages?

Colin Paul Adams
In reply to this post by Colin Paul Adams
>>>>> "Colin" == Colin Paul Adams <[hidden email]> writes:

    Colin> And thanks everyone for your help. I have enough to go on
    Colin> now.

I followed the example at
http://www.haskell.org/haskellwiki/Calling_Haskell_from_C

(and renamed the files from A* to fib1*, as I want to pursue this
further).

But I get:

fib1.o: In function `main':
fib1.c:(.text+0x0): multiple definition of `main'
fib1.o:fib1.c:(.text+0x0): first defined here
fib1_stub.o: In function `stginit_export_Safe_zdffibonaccizuhszuaDN':
fib1_stub.c:(.text+0x5): undefined reference to `Safe_zdffibonaccizuhszuaDN_closure'
fib1_stub.o: In function `fibonacci_hs':
fib1_stub.c:(.text+0x32): undefined reference to `Safe_zdffibonaccizuhszuaDN_closure'
collect2: ld returned 1 exit status

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

Re: Calling Haskell from other languages?

Don Stewart-2
colin:

> >>>>> "Colin" == Colin Paul Adams <[hidden email]> writes:
>
>     Colin> And thanks everyone for your help. I have enough to go on
>     Colin> now.
>
> I followed the example at
> http://www.haskell.org/haskellwiki/Calling_Haskell_from_C
>
> (and renamed the files from A* to fib1*, as I want to pursue this
> further).
>
> But I get:
>
> fib1.o: In function `main':
> fib1.c:(.text+0x0): multiple definition of `main'
> fib1.o:fib1.c:(.text+0x0): first defined here
> fib1_stub.o: In function `stginit_export_Safe_zdffibonaccizuhszuaDN':
> fib1_stub.c:(.text+0x5): undefined reference to `Safe_zdffibonaccizuhszuaDN_closure'
> fib1_stub.o: In function `fibonacci_hs':
> fib1_stub.c:(.text+0x32): undefined reference to `Safe_zdffibonaccizuhszuaDN_closure'
> collect2: ld returned 1 exit status

Not linking in the 'Safe' module on the command line?

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

Re: Calling Haskell from other languages?

Colin Paul Adams
>>>>> "Don" == Don Stewart <[hidden email]> writes:

    Don> Not linking in the 'Safe' module on the command line?

Could be - I don't know any of this stuff. I was just following the
instructions on the wiki page.

How should these be amended?
--
Colin Adams
Preston Lancashire
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Calling Haskell from other languages?

Colin Paul Adams
>>>>> "Colin" == Colin Paul Adams <[hidden email]> writes:

>>>>> "Don" == Don Stewart <[hidden email]> writes:
    Don> Not linking in the 'Safe' module on the command line?

    Colin> Could be - I don't know any of this stuff. I was just
    Colin> following the instructions on the wiki page.

Got it working now. It seemed to be some case dependency on the file name.
--
Colin Adams
Preston Lancashire
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe