Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9109699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:06:24+00:00 2026-06-17T03:06:24+00:00

I am trying to call Haskell functions from python. I have the following make

  • 0

I am trying to call Haskell functions from python.
I have the following make file:

GHC=ghc
GHC_RUNTIME_LINKER_FLAG=-lHSrts-ghc7.4.1

libffi-example.so: Example.o wrapper.o
    $(GHC) -o $@ -shared -dynamic -fPIC $^ $(GHC_RUNTIME_LINKER_FLAG)

Example_stub.h Example.o: Example.hs
    $(GHC) -c -dynamic -fPIC Example.hs

wrapper.o: wrapper.c Example_stub.h
    $(GHC) -c -dynamic -fPIC wrapper.c

clean:
    rm -f *.hi *.o *_stub.[ch]

clean-all:
    rm -f *.hi *.o *_stub.[ch] *.so


# Runs the example Python program
example: libffi-example.so
    python program.py

The only thing wrapper.c does it creating wrappers for hs_init by calling hs_init(0,0);. The wrapper is called example_init

I get a segmentation fault when running make example, in example_init (ie when calling hs_init(0,0)).

Can someone tell me why this is and/or how to fix it?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:06:26+00:00Added an answer on June 17, 2026 at 3:06 am

    It ought to work:

    “Moreover, hs_init() may be called with NULL for both argc and argv, signalling the absence of command line arguments.”

    It seems to be a bug in ghc-7.2.1 to ghc-7.4.1:

    RFib.hs:

    {-# LANGUAGE ForeignFunctionInterface #-}
    module RFib where
    
    fib :: Int -> Int
    fib n
        | n >= 0    = go 0 1 n
        | even n    = -go 0 1 (-n)
        | otherwise = go 0 1 (-n)
          where
            go a _ 0 = a
            go a b k = go b (a+b) (k-1)
    
    foreign export ccall "rfib" fib :: Int -> Int
    

    rfib.c:

    #include <stdio.h>
    #include <HsFFI.h>
    
    int rfib(int);
    
    int main(void) {
        hs_init(0,0);
        printf("%d\n", rfib(35));
        hs_exit();
        return 0;
    }
    

    Compile and run:

    $ rm rfib.o RFib.hi RFib.o a.out
    $ ghc-7.0.2 -O2 -Wall -no-hs-main RFib.hs rfib.c && ./a.out
    [1 of 1] Compiling RFib             ( RFib.hs, RFib.o )
    Linking a.out ...
    9227465
    $ rm rfib.o RFib.hi RFib.o a.out
    $ ghc-7.0.4 -O2 -Wall -no-hs-main RFib.hs rfib.c && ./a.out
    [1 of 1] Compiling RFib             ( RFib.hs, RFib.o )
    Linking a.out ...
    9227465
    $ rm rfib.o RFib.hi RFib.o a.out
    $ ghc-7.2.1 -O2 -Wall -no-hs-main RFib.hs rfib.c && ./a.out
    [1 of 1] Compiling RFib             ( RFib.hs, RFib.o )
    Linking a.out ...
    Speicherzugriffsfehler
    $ rm rfib.o RFib.hi RFib.o a.out
    $ ghc-7.2.2 -O2 -Wall -no-hs-main RFib.hs rfib.c && ./a.out
    [1 of 1] Compiling RFib             ( RFib.hs, RFib.o )
    Linking a.out ...
    Speicherzugriffsfehler
    $ rm rfib.o RFib.hi RFib.o a.out
    $ ghc-7.4.1 -O2 -Wall -no-hs-main RFib.hs rfib.c && ./a.out
    [1 of 1] Compiling RFib             ( RFib.hs, RFib.o )
    Linking a.out ...
    Speicherzugriffsfehler
    $ rm rfib.o RFib.hi RFib.o a.out
    $ ghc-7.4.2 -O2 -Wall -no-hs-main RFib.hs rfib.c && ./a.out
    [1 of 1] Compiling RFib             ( RFib.hs, RFib.o )
    Linking a.out ...
    9227465
    $ rm rfib.o RFib.hi RFib.o a.out
    $ ghc -O2 -Wall -no-hs-main RFib.hs rfib.c && ./a.out
    [1 of 1] Compiling RFib             ( RFib.hs, RFib.o )
    Linking a.out ...
    9227465
    

    Changing two lines in rfib.c:

    int main(int argc, char *argv[]) {
        hs_init(&argc,&argv);
    

    it works with all versions (>= 7.0 that I have installed).

    So to fix it

    • pass &argc and &argv
    • upgrade GHC

    are the two most obvious ways.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to call a function from another file in C with the following code:
I am trying to call c# function from javascript the code i have tried
I am trying to implement the Maybe monad from Haskell using the lambda functions
In the following I am trying call action script function from js function. What
Trying to call a SAP SOAP Web Service from a generated sudzc app shows
Im trying to call a method that will add or subtract 1 from a
I trying to call 2 functions in the same page to submit jquery-ajax at
I am trying to call some functions whenever the mouse is moved wherever it
I'm trying to call a method from another class with a simple button in
I'm trying to call a function present in one class from another class by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.