• Stars
    star
    131
  • Rank 275,867 (Top 6 %)
  • Language
    Haskell
  • Created about 13 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Haskell-C++ Foreign Function Interface Generator

What is fficxx?

Build

fficxx ("eff fix") is an automatic haskell Foreign Function Interface (FFI) generator to C++.

To use fficxx, you write a Haskell model of the C++ public interfaces and fficxx generates both a C wrapper and associated haskell functions and type classes which reflect specified model of the C++ interfaces. It is currently the user's responsibility to specify a correct model of the C++ interfaces, because fficxx does not presently check for model correctness.

While haskell has a well-specified standard for C FFI, making haskell-C++ FFI is an arbitrary and painful process. Since Object-Oriented Programming (OOP) paradigm and Functional Programming (FP) paradigm are different, automatic translation of C++ libraries to haskell libraries is not a straightforward task. The goal of fficxx is to minimize this disparity and maximize user's convenience by providing familiar interface to the original C++ library as a result.

Public Haskell-C++ binding generated by fficxx are now collected in fficxx-projects.

fficxx is separated into generator part and runtime part:

  • fficxx : FFI types and binding generator library
  • fficxx-runtime : runtime modules needed for various common routines

Haskell packages that are generated from fficxx will be dependent on fficxx-runtime.

In addition, C++ standard library under std namespace is being generated as a package from fficxx.

  • stdcxx: generated by ./stdcxx-gen/Gen.hs

Getting Started

fficxx is mainly packaged in nix. shell.nix is for development,

nix-shell shell.nix

and use.nix is for using the generated binding package.

nix-shell use.nix

For all build,

nix-build release.nix

OOP Model in fficxx

fficxx generates haskell codes at raw level (C wrapper and haskell foreign pointer that are directly matched with C/C++ types) and at high level (newtype wrapper for raw level pointer and haskell typeclasses reflecting OOP class hierarchy). Haskell does not have a concept of subtyping, i.e. we do not provide any easy way to create a new subclass and overload member functions from existing classes on haskell side. However, fortunately, one can describe the OOP subclass relationship using a typeclass interface as a contract for a class b to be equal to or a subclass of a. In a sense, typeclasses are Interface Definition Language (IDL) for describing OOP classes. Thus, a C++ class is represented by both haskell concrete type (for a C++ class itself) and typeclass (a set of classes that can be equal to or a subclass of the C++ class).

Assuming that there is a C++ class A. fficxx generates a haskell type A. This haskell type A is nothing but a newtype wrapping ForeignPtr tagged by RawA.

data RawA

newtype A = A (ForeignPtr RawA)

RawA exists only for the purpose of a phantom type to be used as tags for Ptr in FFI imports (foreign import statements.) When programming with fficxx-generated code at high level, programmers should seldom encounter Raw types. An instance object of C++ class A is a value of haskell type A. To create an instance, fficxx provides a smart constructor (newA) if specified with a corresponding constructor.

Therefore, one can create an instance from a concrete haskell type, and pass it to any functions which needs them. On Haskell side, member functions of a C++ class are nothing but functions whose first argument is the same as the corresponding haskell type to the class. For example, if the class A has a member function foo of signature void A::foo( int param ), then fficxx generates a high level function

foo :: A -> CInt -> IO ()

which is a wrapper for a raw level FFI call defined by

foriegn import ccall "A_foo" c_foo :: Ptr RawA -> CInt -> IO ()

where A_foo is a generated C shim function for A::foo. So one can translate the following C++ code

A* a = new A();
a->foo(3);

to the haskell code (in do-block of IO monad)

do a <- newA
   foo a 3

Haskell type A can be used in the arbitrary argument position. Assume there is another member function bar of A which takes an object of A as an argument like void A::bar( A* a ). Then, we have

bar :: A -> A -> IO ()

for which x->bar(y) (x,y are of class A) corresponds to bar x y.

In this example, the C++ class A may have the following declaration:

class A
{
  public:
    A();
    virtual void foo( int );
    virtual void bar( A* );
};

To reflect subtype relations, fficxx creates an interface typeclass IA for A, which is defined as

class IA a where
  foo :: a -> CInt -> IO ()
  bar :: (IA b) => a -> b -> IO ()

which declares all C++ virtual functions as members. Then, haskell type A is a typeclass instance of IA:

instance IA A where
  -- foo :: A -> CInt -> IO ()
  foo = ...
  -- bar :: (IA b) => A -> b -> IO ()
  bar = ...

so that foo and bar functions we considered in the above example were actually defined in the IA instance definition of A. Note that the type signature of bar allows generic typeclass instances of IA as the argument (paraterized by b).

Now consider another C++ class B which is a subclass of A:

class B : public A
{
  public:
    B();
    virtual int baz() ;
}

Again, we will have a concrete haskell type B and an object of B will be created as a value from the newB constructor function. A typeclass IB is also generated as well, and it reflects the inheritance relationship for C++ class as constraints:

class (IA b) => IB b where
  baz :: b -> IO CInt

Thanks to the constraints (IA b) => in the declaration, every instance of IB must have implementation of IA. This is true for B, too. So fficxx generates

instance IA B where
  foo = ...
  bar = ...

instance IB B where
  baz = ...

This instance generation (implemenation of C++ class) is automaticaly done by fficxx, but it's not guaranteed for future subclassing. Any type which implements instances of IA and IB can be regarded as a subclass of B, but it's not automatically done as we have in OOP. The scope of fficxx is to generate such implementations only for existing C++ classes.

References

C Macro tricks

We use C Macro tricks described in the following:

C++ Template tricks

C++ Template Peculiarity

More Repositories

1

hoodle

hoodle : A pen notetaking program written in haskell
Haskell
140
star
2

nix-build-ghc-android

ghc-android build using nix
Nix
68
star
3

haskell-android-example

android app example linked with haskell library using android NDK
Haskell
32
star
4

HROOT

HROOT: Haskell binding to ROOT
Haskell
11
star
5

hxournal

hxournal : A pen notetaking program written in haskell
Haskell
11
star
6

HROOT.old

Haskell
8
star
7

hep-nix-overlay

nix expression repository for high energy physics programs
Nix
7
star
8

closure-playground

experiment with distributed-closure
Haskell
6
star
9

ghcjs-dom-delegator

dom-delegator binding for ghcjs
JavaScript
5
star
10

poppler

haskell binding to poppler
Haskell
5
star
11

fficxx-projects

Collection of public Haskell/C++ binding projects using fficxx
Nix
4
star
12

hs-imgui

dear imgui binding via fficxx
Haskell
4
star
13

lbfgs-hs

Haskell binding for liblbfgs
C
4
star
14

reflex-polymer

reflex binding to polymer web components
Nix
4
star
15

hgdal

Haskell binding to GDAL
Haskell
3
star
16

devadmin

Haskell
3
star
17

madgraph-auto-model

Python
3
star
18

HepMC

Haskell HepMC parser and builder
Haskell
3
star
19

xournal-convert

Haskell
3
star
20

HFastJet-generate

Haskell
3
star
21

cvmaker

Haskell
3
star
22

hoodle-publish

publish hoodle files as a static web site
Haskell
3
star
23

lhc-analysis-collection

analysis collection for LHC physics
Haskell
3
star
24

cloud-haskell-playground

my experiments with cloud haskell
Haskell
3
star
25

hs-ogdf

Haskell
3
star
26

harmadillo

Haskell Armadillo
Haskell
3
star
27

HStringTemplateHelpersIW

Haskell
2
star
28

configparser

Haskell
2
star
29

madgraph-auto-dataset

Haskell
2
star
30

xournal-parser

Haskell
2
star
31

vega-renderer

Haskell Vega Renderer
Haskell
2
star
32

linear-playground

Linear Haskell Playground
Nix
2
star
33

hneon

Haskell
2
star
34

xfigparser

parser for xfig format
Haskell
2
star
35

coroutine-object

oop-like coroutine process
Haskell
2
star
36

iteratee-util

Haskell
2
star
37

pipeline-eventgen

event generation using pipeline interface
Haskell
2
star
38

enumerator-util

Haskell
2
star
39

model-server

Haskell
2
star
40

xournal-render

Haskell
2
star
41

hxournalclip-type

Haskell
2
star
42

evchain

Haskell
2
star
43

feynrules-auto

Haskell
2
star
44

hoodle-core

core library for hoodle program
Haskell
2
star
45

hxournalclip-server

Haskell
2
star
46

www-hoodle

web home for hoodle project
CSS
2
star
47

yesodcrud-type

Haskell
2
star
48

hoodle-tools

Command Line Interface tools for Hoodle file management
Haskell
2
star
49

ttbar

Haskell
2
star
50

phabricator

haskell binding to phabricator API
Haskell
2
star
51

hxournalclip-client

Haskell
2
star
52

HFastJet

Haskell
2
star
53

xournal-types

Haskell
2
star
54

hxournal-idmap-client

Haskell
2
star
55

simplen3

Haskell
2
star
56

MSSMPlot

Haskell
2
star
57

detector-yaml

detector description using yaml
Haskell
2
star
58

json-sig

signature data type generation from JSON
Haskell
2
star
59

hxournal-idmap-type

Haskell
2
star
60

misc-utils

Haskell
2
star
61

vvdom

virtual virtual dom
Haskell
2
star
62

hoodle-db

Haskell
2
star
63

ghc-clang

provide GHC with only clang
Nix
2
star
64

EventAnalysis

Haskell
2
star
65

MSSMType

Haskell
2
star
66

hxournal-idmap-server

Haskell
2
star
67

accel-o-diff

Haskell
2
star
68

LHEParser

Haskell
2
star
69

hournal

Haskell
2
star
70

HsStdHep

Haskell
2
star
71

jobqueue-common

Haskell
2
star
72

diagdrawer

Haskell
2
star
73

webcanvas-server

server for webcanvas
Haskell
2
star
74

jobtester

Haskell
2
star
75

HEPUtil

Haskell
2
star
76

hoodle-types

data types for hoodle program
Haskell
2
star
77

chatter

simple chat server written in haskell
Haskell
2
star
78

yesodcrud

Haskell
2
star
79

hoodle-cache

Haskell
2
star
80

hoodle-builder

xml builder from hoodle data type
Haskell
2
star
81

cmdmanager

command line executor using coroutine-object, doing weblogging
Haskell
2
star
82

hxournal-store

Haskell
2
star
83

conduit-util

Haskell
2
star
84

LHAPDF

Haskell
2
star
85

LHE-sanitizer

Haskell
2
star
86

jobqueue-server

Haskell
2
star
87

model-client

Haskell
2
star
88

nix-build-ghc-eta

nix environment for building eta
Nix
2
star
89

mathematica-data

Haskell
2
star
90

xournal-builder

Haskell
2
star
91

hMC

Haskell
2
star
92

hoodle-contrib

user contribution to hoodle program (like xmonad-contrib)
Haskell
2
star
93

yesodcrud-client

Haskell
2
star
94

docmanager

Haskell
2
star
95

fficxx-runtime

runtime for programs/libraries generated by fficxx
Haskell
2
star
96

qft

Haskell
2
star
97

LHCOAnalysis-type

Haskell
2
star
98

pipeline

Haskell
2
star
99

jobqueue-sender

Haskell
2
star
100

jobqueue-client

Haskell
2
star