Coder Social home page Coder Social logo

lpp's Introduction

<λ++>

turing-complete idiom to express types in C++

Donate License: MIT

see https://github.com/neu-rah/yolanda for a runtime/compile time alternative to this.

Based on lambda calculus, therefor using functional paradigm on top of modern C++ metaprogramming. Expressions on this idiom are metaprogramming and evaluate at compile time, yelding C++ types as a result.

inspired by the excellent videos of @glebec (https://youtu.be/3VQ382QG-y4) (thank you!)

Scope

This idiom uses lambda calculus as its background on a close cope with c++ templates. It only operates at compile time.
Results in β normal form translate to C++ types (native or user defined).
So this expressions can only be used in replacement of C++ types.
A layer to provide constexpr values is available through dependent types. It is therefor a turing-complete idiom to decide types at compile time. Types have to be extracted after expression evaluation (::App) to yeld valid C++ types. Using Lazyness, static immutable "data" (here expressed as c++ types) and partial application.

lambda base

lambda calculus base lambda core doc.

lpp

Syntatic sugar for "lambda.h"

because Head<L2> looks much better that Expr<Head,L2>::App

LPP doc.

r-lambda

RCurry (runtime curry) turns regular c++ functions into curry versions.

A C++ function that accepts lets say 2 arguments a and b, and returns c is then turned into a function that accepts one argument a and return a second function that accepts b and returns c (considering the previous a).

curry is the only part implemented, and still testing. No lambda calculus yet

#include <iostream>
using namespace std;

#include <r-lambda.h>//runtime lambda core
using namespace rlambda;

template<typename I>
inline I _id(I i) {return i;}
template<typename I>
constexpr auto id=RCurry<decltype(_id<I>),_id,I>();

//regular function with single param
int _d(int x){return x<<1;}
constexpr auto d=RCurry<decltype(_d),_d,int>();
// constexpr auto d=typename decltype(fun(_d))::template curried<decltype(_d),_d>;

//regular function with multiple param
int _m(int x,int y){return x*y;}
constexpr auto m=RCurry<decltype(_m),_m,int,int>();

template<typename O> constexpr auto _true=rK<O>;

template<typename O> O __false(O,O o) {return o;}
template<typename O> constexpr auto _false=RCurry<decltype(__false<O>),__false<O>,O,O>();

int main(int argc, char **argv) {
  cout<<"d(2):"<<d(2)<<endl;
  cout<<"m(2)(3):"<<m(2)(3)<<endl;
  auto x=d(d);
  cout<<"x(2):"<<x(2)<<endl;
  cout<<"id<int>(d)(1967:"<<id<int>(d)(1967)<<endl;
  cout<<"m(d)(5)(3):"<<m(d)(5)(3)<<endl;
  cout<<"d(m)(2)(3):"<<d(m)(2)(3)<<endl;
  cout<<"m(5)(d)(3):"<<m(5)(d)(3)<<endl;
  cout<<"d(m)(10)(2):"<<d(m)(10)(2)<<endl;
  cout<<"d(m(10))(2):"<<d(m(10))(2)<<endl;//same as above
  cout<<"d(m)(5)(m)(3)(2):"<<d(m)(5)(m)(3)(2)<<endl;
  cout<<_true<const char*>("Ok")("Fail")<<endl;
  cout<<_false<const char*>("Fail")("Ok")<<endl;
  return 0;
}

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.