Skip to main content

Import Haskell modules as if they were Python modules

Project description

Pythas Python package Documentation Status

Import Haskell modules as if they were Python modules. If an imported name does not exist as Python module/package, Pythas will traverse the specified subdirectory below your cwd to look for a matching Haskell file. If one is found it is imported just as if it was a Python module.

If you have a file Example.hs like in the example directory, it will be imported and can be used like such:

>>>import pythas
>>>import example.example as e
>>>e.hello()
Hello from Haskell!

You can also just from * import. Try:

>>>from example.example import multisin
>>>from math import pi
>>>multisin(2,pi)
2.4492935982947064e-16

and you'll see: It doesn't stop at invoking side-effects.

Sequences

Python Sequences can be passed as linked lists or as arrays. Depending on which flavour of programming you want to embrace. Currently for speed and space reasons arrays are used by the backend, but it should be no problem to change that for those hardcore FP nerds stuck in Haskell-land. Try things like:

>>>from example.example import mapQuarter
>>>mapQuarter(range(1000,5000,1000))
[250.0, 500.0, 750.0, 1000.0]

While in Haskell lists have to be used, in Python any kind of sequence can be handed over. Needless to say, varying types won't be supported.

Tuples

You can use tuples to pack results of different types into a single one. It is no problem to nest them and lists or vice versa. Checkout test.hs.Test.hs to see what Pythas is (successfully) tested for.

>>>from example.example import tuple, hsnd
>>>tuple()
(1,"a")
>>>hsnd((1,2))
2

Requirements

Please make sure that GHC is located in your $PATH. Pythas requires at least GHC version 8.0.2. It defaults to using stack ghc. To check if your GHC version is compatible run:

$ stack ghc -- --version

or without stack installed:

$ ghc --version

Pythas is written with compatibility and ease of use in mind. All libraries used in the Haskell backend are contained in the standard installation of GHC. No requirements exist on the Pythonic side of life.

Install

Pythas can be installed using pip. Run the following command to install and compile it at once:

$ pip install pythas && python -c "import pythas"

or from source:

$ git clone https://github.com/pinselimo/Pythas.git && cd Pythas
$ pip install . && python -c "import pythas"

Constraints

Only Python versions 3.7 and up are supported. Unfortunately, only [PEP 562] introduced __getattr__ for modules. This renders the level of abstraction Pythas aims for impossible on lower Python versions.

Only functions having their type declared will be imported. You can handle the export of the function yourself by adding a foreign export ccall for the function, otherwise Pythas will do that for you. To exclude a function just omit the functions type. Functions of types that are not supported won't get exported either.

All Haskell constants in the IO monad are imported as functions. Due to lists being turned into CArrays even constant lists must be called like a function without arguments:

>>>from example.example import someConstant, haskellList
>>>someConstant
63
>>>haskellList()
[63]

The same is true for tuples which are turned into or CTuple{x}s.

Pythas enforces the file naming scheme of Haskell for .hs files as does the GHC! This is primarily due to GHC failing to find the imported module at compile time. Thus, we fail early and raise a ModuleNotFoundError.

Docs

Contributing

Meaningful contributions are always welcome, please refer to CONTRIBUTING.md for details.

Testing

The sub-repositories Pythas incorporates all ship their own testing facilities. Testing of the pythas package itself therefore focuses on the main functionality and user interface. It incorporates some unit tests and property based testing mostly focusing on ensuring the interface does not alter any data. To execute the tests in bulk just move to the project root and execute:

$ pytest

License

The Software in this repository is licensed under the LGPLv3 License. See COPYING.LESSER file for details.

Pythas  Copyright (C) 2020  Simon Plakolb
This program comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome to redistribute it
under certain conditions; see COPYING and COPYING.LESSER.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pythas-0.1b1.tar.gz (66.1 kB view details)

Uploaded Source

Built Distribution

pythas-0.1b1-py3-none-any.whl (74.7 kB view details)

Uploaded Python 3

File details

Details for the file pythas-0.1b1.tar.gz.

File metadata

  • Download URL: pythas-0.1b1.tar.gz
  • Upload date:
  • Size: 66.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.5.0.1 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.7

File hashes

Hashes for pythas-0.1b1.tar.gz
Algorithm Hash digest
SHA256 aaac0691f5306db8dfaf013cbabe2c80ecc2bbe00321f89536a9b7692f554ab6
MD5 99e4b01aa29207d60c51b04e5a9ac2a7
BLAKE2b-256 0b1b2980b22b9cab5eace023c2f26e683358e5dac58a39cfd612e258ec166b96

See more details on using hashes here.

File details

Details for the file pythas-0.1b1-py3-none-any.whl.

File metadata

  • Download URL: pythas-0.1b1-py3-none-any.whl
  • Upload date:
  • Size: 74.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.5.0.1 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.7

File hashes

Hashes for pythas-0.1b1-py3-none-any.whl
Algorithm Hash digest
SHA256 000675bbb2cee54b7c4e45e1c2c3d1f78fe35924f10f0ae201b18d42c3f437ca
MD5 5bf3ce942b27d749811857ffa08b3f16
BLAKE2b-256 8a380b641d52f8c3af65f44baed47fa00a0d281dd1e53f7e81571997cd8cd025

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page