Giter Site home page Giter Site logo

idris2-python's Introduction

Idris2-Python

A Python backend for Idris 2.

Prerequisites

  • Idris 2 and the Idris 2 API, as per the Idris installation instructions.

  • A C compiler, and the gmp library.

    These can be installed by:

    sudo apt-get install gcc
    sudo apt-get install libgmp3-dev
  • If using Python 3.6, you'll need to manually install dataclasses.

    pip3 install dataclasses

Installation

To build the Idris2-Python backend, and install the Python Idris library, run:

make install

This builds an executable build/exec/idris2-python that can be used to compile Idris 2 code into a Python module.

Compile code

To compile Idris 2 code to a Python module, use idris2-python as you would idris2 when compiling with the standard backend.

eg.

./build/exec/idris2-python tests/Idris2Python/HelloWorld/hello_world.idr -o hello_world

This produces a Python module in build/exec.

Run code

A Python module is a folder containing an __init__.py file. Python modules are not referred to by path, instead by name. Python searches for modules in the current directory (non-recursively), then in $PYTHONPATH, then installed modules.

The module created in the previous section can be run by temporarily adding your build directory to $PYTHONPATH.

$ PYTHONPATH=build/exec python -m hello_world
Hello, world

Python FFIs

You can use both C and Python FFIs in your Idris 2 code when compiling to Python. For convenience, a bindings library is provided for some Python builtins.

Python FFIs may be declared with the %foreign directive, using the format "python: func, module" for a Python function func in module module. For builtins, omit the module.

For example,

%foreign "python: abs"
abs : Int -> Int

and

%foreign "python: floor, math"
floor : Int -> Int

The Python builtins bindings may be accessed by importing the module Python.

For example,

import Python

main : IO ()
main = do
    l <- PythonList.empty

    append l "Orange"
    append l "Apple"

    print l

idris2-python's People

Contributors

madman-bob avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.