Calling Haskell functions from Python

后端 未结 6 2018
醉话见心
醉话见心 2020-12-04 18:22

I want to use some Haskell libraries (e.g. Darcs, Pandoc) from Python, but it seems there’s no direct foreign function interface to Haskell in Python. Is there any way to do

6条回答
  •  我在风中等你
    2020-12-04 18:51

    There is a wrapper that allows one to call Haskell functions from Python here:

    https://github.com/sakana/HaPy

    From a cursory inspection, it seems to require that the Haskell functions have relatively simple type signatures (basically, all the types involved had better be things like Int and Float which c knows about, or lists of things of this form, or lists of lists, or so on).

    An example is provided where one has this Haskell code:

    module ExampleModule where
    
    import Data.Char
    
    foo :: Double -> Double -> Double
    foo = (*)
    
    bar :: Int -> Int
    bar i = sum [1..i]
    
    baz :: Int -> Bool
    baz = (> 5)
    
    arr_arg :: [Int] -> Int
    arr_arg = sum
    
    arr_ret :: Int -> [Int]
    arr_ret i = [1..i]
    
    arr_complex :: [[Int]] -> [[Int]]
    arr_complex = map (map (* 2))
    
    string_fun :: String -> String
    string_fun str = str ++ reverse str
    
    char_test :: Char -> Int
    char_test = ord
    

    and one accesses it like this:

    from HaPy import ExampleModule
    
    print "3 * 7 is", ExampleModule.foo(3,7)
    print "sum from 1 to 10 is", ExampleModule.bar(10)
    print "3 > 5 is", ExampleModule.baz(3)
    
    print "sum from 1 to 100 is", ExampleModule.arr_arg(range(101))
    print "numbers from 1 to 10 are", ExampleModule.arr_ret(10)
    
    print "complex array passing:", ExampleModule.arr_complex([range(3), [], range(100)])
    print "string fun:", ExampleModule.string_fun("This isn't really a palindrome.")
    
    s = ExampleModule.string_fun("abc\000def")
    print "string fun with nulls:", s,
    for c in s:
        print ord(c),
    print
    
    print "char test:", ExampleModule.char_test("t")
    

    Unfortunately, you do need to do some export plumbing on the Haskell side.

提交回复
热议问题