Giter Club home page Giter Club logo

psychec's Introduction

NOTE

The Psyche project is going through a major overhaul in a private branch which will (hopefully) be merged soon.

PsycheC

PsycheC is a compiler frontend infrastructure for the C language that is enabled with an ML/Haskell-style (unification-based) type inference engine. This online interface illustrates the essential functionality of PsycheC.

Applications:

  • Enabling static analysis on partial programs.
  • Supporting semantic tools despite of #include failures.
  • Compiling a code snippet (e.g., retrieved from a bug tracker).
  • Generating test-input data for a function, individually.
  • Prototyping of an algorithm without a type specification.

Be creative!

Requirements

To build PsycheC:

  • Cmake
  • C++14 compiler
  • Haskell Stack

To use PsycheC (download here):

  • Python 3

Building

cmake CMakeLists.txt  
make

Setting Up

export PATH=$PATH:/path/to/psychec

Using

The simplest way to use PsycheC is through the Cnippet compiler adaptor.

Let us see an example.

Consider the file node.c below.

// node.c
void f()
{
    T v = 0;
    v->value = 42;
    v->next = v;
}

If you were to compile node.c with GCC/Clang, you would issue a command similar to this one:

gcc -c node.c

As a result, an object file node.o would have been produced. However, this will not happen, since a declaration for T is not available. Instead, you will get an "undeclared identifier" error.

Now, if you invoke Cnippet, the compilation succeeds (flag -f is for non-commercial use).

./cnip.sh -f gcc -c node.c

That is because, under the hood, PsycheC will infer the necessary missing definitions.

typedef struct TYPE_1__
{
    int value;
    struct TYPE_1__* next;
}* T;

Generic Programming

This is a work-in-progress, feedback is welcome through this form.

PsycheC provides an alternative to void* and #macros for writing generic code in C. This is how you would implement a generic linked-list's prepend function.

_Template void prepend(_Forall(node_t)** head,
                      _Forall(value_t) value)
{
    node_t* n = malloc(sizeof(node_t));
    n->value = value;
    n->next = *head;
    *head = n;
}

It is not necessary to define neither node_t nor value_t. The definition of prepend applies "for all" kinds of nodes and values. This way, you can focus on the algorithms (the essence of generic programming).

Let us create 2 lists, one of int and another of point_t, and insert an new head to them.

int main()
{
    _Exists(node_t)* ilst = 0;
    prepend(&ilst, 42);

    _Exists(node_t)* plst = 0;
    struct point_t p;
    p.x = p.y = 42;
    prepend(&plst, p);
}

Now, PsycheC infers that there "exists" a (node) type whose value is an int, together with an specialization of prepend for such arguments. A different (node) type for point_t "exists" too, with its corresponding specialization of prepend.

Check the examples directory for this snippet.

Running the Tests

./psychecgen -t
./test_infer_and_compile.sh
cd solver && stack test && cd -
cd formalism && ./test_muC.sh

Related Publications

PsycheC is an ongoing research project.

An small article about PsycheC:

psychec's People

Contributors

ltcmelo avatar jordiae avatar rurban avatar

Watchers

James Cloos 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.