ddfisher / hapy Goto Github PK
View Code? Open in Web Editor NEWHaskell bindings for Python
License: MIT License
Haskell bindings for Python
License: MIT License
This is a peculiar bug which I experience on python 2.7.3
To reproduce, load any module from HaPy in python interpreter.. after which you cannot load
$ python
Python 2.7.3 (default, Feb 27 2014, 20:00:17)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'successful import of json module, it can be any module for that matter'
successful import of json module, it can be any module for that matter
>>> import json
>>>
>>> from HaPy import ExampleModule
>>>
>>> import yaml
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named yaml
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from io import StringIO
ImportError: No module named io
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named yaml
I checked the sys.modules after loading the HaPy module and found it intact (to verify possibility if loading HaPy corrupts it)
The issue is in loading the module somewhere, my bet is on load_module function..
Special ASCII characters like '\NUL' and '\DEL' are dropped when called from python, to reproduce..
MyModule.hs
sFunc :: String
sFunc = "b\NUL\DELa"
Calling it from Python
>> from HaPy import MyModule as mm
>> mm.sFunc()
>> 'b'
Yours is the most recently updated project I could find as far as calling Haskell shared libraries from python. I get this linker error when trying to make your example. Thanks for sharing this project!
nelson@cesium:~/projects/interweave/HaPy/example$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.6.3
nelson@cesium:~/projects/interweave/HaPy/example$ make all
ghc --make -no-hs-main -optl '-shared' HaPy_init.c -o ExampleModule.so Export.hs
[1 of 2] Compiling ExampleModule ( ExampleModule.hs, ExampleModule.o )
[2 of 2] Compiling Export ( Export.hs, Export.o )
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading object (static) HaPy_init.o ... done
final link ... done
Loading package pretty-1.1.1.0 ... linking ... done.
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package containers-0.5.0.0 ... linking ... done.
Loading package template-haskell ... linking ... done.
Loading package th-lift-0.5.5 ... linking ... done.
Loading package HaPy-0.1.0.0 ... linking ... done.
Linking ExampleModule.so ...
/usr/bin/ld: Export.o: relocation R_X86_64_32S against `stg_upd_frame_info' can not be used when making a shared object; recompile with -fPIC
Export.o: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
Can this library installed on Windows10?
I've been interested in making a strict typed system in Python that would be mapable to Haskell.
Type enforcement could be as expensive as we want, and done through assert statements. Asserts go away when the program is compiled with optimization. Ie: python -O myprog.py
. Thus in testing, we can iterate through a list just to check that every element is actually an integer, and in deployment we can be confident that lies won't happen and get a seg fault or something if it does.
For instance, if you had
#haskell
data Point = Point Float Float
data Line = Line Point Point
lineLinesIntersect :: Line -> [Line] -> [Maybe Point]
--etc.
Then in python one could have:
#@haskell("Int(a) WorldObject(b) => [a] -> b -> [Maybe Point]")
@haskell("f -> [Maybe Point]")
def line_lines_intersect(indexs,world_object):
#put together a line and some lines in python
#haskell.lineLinesIntersect obviously type enforces before calling haskell
return haskell.lineLinesIntersect(line,lines)
So python functions could be strictly typed, or not. And haskell functions would get garunteed structures suitable for the type constructor. It's cool how Haskell's polymorphism is (or can be) like duck typing and python's class heirarchy, so one could go nuts with this if they wanted to. I think just the minimal to get into Haskell land is enough.
Some mapping of Nothing to None would be awesome. Exceptions could also be passed this way.
Haskell records are so much like named tuples, I think it might make sense to require the Haskell side to define everything as records. These could easily become c structs, Right? But I really don't know enough about the details of communication to know what make sense.
Anyways, I think this is all cool to think about, but would anything I can do towards this help HaPy support, uh, algebraic data types, or custom data types, or whatever they're called? I'm solid with Python and Linux but a noob at Haskell and ctypes/ffi.
I tried to use HaPy with stack, but failed. Could you please update the example?
What I found up to now is: the extra libraries options has to go to stack.yaml
, e.g.
ghc-options:
package-name: -lHSrts-ghc7.10.3
The _.so file will be somewhere in .stack-work/install/x86_64-linux/lts-5.8/7.10.3/lib/x86_64-linux-ghc-7.10.3/modulename_/lib*.so
. (Tough I haven't used--enable-shared
anywhere, how'd I supply that to stack?) So far so good. Copying that *.so-file to the python directory and using it there gives me:
OSError: /usr/lib/ghc/ghcpr_8TmvWUcS1U1IKHT0levwg3/libHSghc-prim-0.4.0.0-8TmvWUcS1U1IKHT0levwg3-ghc7.10.3.so: undefined symbol: stg_forkOnzh
Any suggestions?
Is it active?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.