|
| 1 | +# Lambda lib |
| 2 | + |
| 3 | +Header only library that provides functions to create, rename, and beta reduce lambda calculus expressions. |
| 4 | + |
| 5 | +Made for c++20 but may work on earlier standards |
| 6 | + |
| 7 | +Front matter: |
| 8 | +- [Instalation](#instalation) |
| 9 | +- [Usage](#usage) |
| 10 | +- [Namespaces](#namespaces) |
| 11 | +- [Classes](#classes) |
| 12 | +- [Functions](#functions) |
| 13 | +- [Errors](#errors) |
| 14 | +- [Special Definitions](#special-definitions) |
| 15 | + |
| 16 | +## Instalation |
| 17 | + |
| 18 | +Copy the insides of "include" directory into your include directory, depending on how you structure your project. |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +To include library in standard way: |
| 23 | + |
| 24 | +`#include "lambda_lib/lambda_lib.hpp"` |
| 25 | + |
| 26 | +Basic way to create expressions: |
| 27 | + |
| 28 | +`std::make_unique<LambdaBase> node = std::make_unique<Variable>('f');` |
| 29 | + |
| 30 | +Using `defaults.hpp` it's easier. |
| 31 | + |
| 32 | +`std::make_unique<LambdaBase> node = VARIABLE('f');` |
| 33 | + |
| 34 | +Use `rename` to make sure all operations will be completed correctly. |
| 35 | + |
| 36 | +Use `reduce` to fully reduce it. |
| 37 | + |
| 38 | +Use `print` the result. |
| 39 | + |
| 40 | +```cpp |
| 41 | +std::make_unique<LambdaBase> node = |
| 42 | + APPLICATION( |
| 43 | + FUNCTION( |
| 44 | + 'x', |
| 45 | + VARIABLE('x') |
| 46 | + ), |
| 47 | + VARIABLE('y') |
| 48 | + ); |
| 49 | + |
| 50 | +rename(node); |
| 51 | + |
| 52 | +reduce(node); |
| 53 | + |
| 54 | +print(node); |
| 55 | +``` |
| 56 | +
|
| 57 | +## Namespaces |
| 58 | +
|
| 59 | +Every class and function provided will be located in `lambda` namespace. |
| 60 | +
|
| 61 | +Some headerfiles have namespaces that start with `_` (`_print` namespace). |
| 62 | +Those are used inside the library itself and sometimes contain unsafe functions, only use function that are directly in `lambda` namespace. |
| 63 | +
|
| 64 | +## Classes |
| 65 | +
|
| 66 | +Library provides these classes: |
| 67 | +- [LambdaBase](#lambdabase) - virtual |
| 68 | +- [Variable](#variable) : LambdaBase |
| 69 | +- [Function](#function) : LambdaBase |
| 70 | +- [Application](#application) : LambdaBase |
| 71 | +
|
| 72 | +### LambdaBase |
| 73 | +
|
| 74 | +Provides virtual destructor and virtual function that return the type of the object from `LambdaType` class enum. |
| 75 | +
|
| 76 | +```cpp |
| 77 | +virtual inline LambdaType getType() const noexcept = 0; |
| 78 | +``` |
| 79 | + |
| 80 | +### Variable |
| 81 | + |
| 82 | +Stores a `char` name and `uint16_t` count. Variable are the same only if both of these values are the same. |
| 83 | + |
| 84 | +```cpp |
| 85 | +Variable::Variable(char name, uint16_t count = 0); |
| 86 | +``` |
| 87 | +
|
| 88 | +Also provides `==` operator overload. |
| 89 | +
|
| 90 | +```cpp |
| 91 | +inline bool operator==(const Variable& other) const noexcept; |
| 92 | +``` |
| 93 | + |
| 94 | +### Function |
| 95 | + |
| 96 | +Stores a `Variable` as a parameter (bound) and `std::unique_ptr<LambdaBase>` as a body. |
| 97 | + |
| 98 | +Constructor throws `LambdaLibError` if body is `nullptr` |
| 99 | + |
| 100 | +```cpp |
| 101 | +Function::Function(Variable bound, std::unique_ptr<LambdaBase> body); |
| 102 | +``` |
| 103 | +
|
| 104 | +### Application |
| 105 | +
|
| 106 | +Stores `std::unique_ptr<LambdaBase>` for both left and right arguments. |
| 107 | +
|
| 108 | +```cpp |
| 109 | +Application::Application(std::unique_ptr<LambdaBase> left, |
| 110 | + std::unique_ptr<LambdaBase> right); |
| 111 | +``` |
| 112 | + |
| 113 | +## Functions |
| 114 | + |
| 115 | +Library provides these functions: |
| 116 | +- [getSize](#getsize) |
| 117 | +- [copy](#copy) |
| 118 | +- [print](#print) |
| 119 | +- [rename](#rename) |
| 120 | +- [apply](#apply) |
| 121 | +- [reduceOnce](#reduceonce) |
| 122 | +- [reduce](#reduce) |
| 123 | + |
| 124 | +### getSize |
| 125 | + |
| 126 | +```cpp |
| 127 | +inline size_t getSize(std::unique_ptr<LambdaBase>& node) noexcept; |
| 128 | +``` |
| 129 | +
|
| 130 | +Walks the tree and returns the abount of bytes the tree is taking up. |
| 131 | +
|
| 132 | +Returns `0` if failed. (if `node` is `nullptr`) |
| 133 | +
|
| 134 | +### copy |
| 135 | +
|
| 136 | +```cpp |
| 137 | +inline std::unique_ptr<LambdaBase> copy(std::unique_ptr<LambdaBase>& node); |
| 138 | +``` |
| 139 | + |
| 140 | +Walks the tree and returns copy of the tree. |
| 141 | + |
| 142 | +Return `nullptr` if failed. (if `node` is `nullptr`) |
| 143 | + |
| 144 | +May throw exception if allocation fails. (stdlib fail) |
| 145 | + |
| 146 | +### print |
| 147 | + |
| 148 | +```cpp |
| 149 | +inline void print(std::unique_ptr<LambdaBase>& node) noexcept; |
| 150 | +``` |
| 151 | +
|
| 152 | +Walks the tree and prints it. (lambda symbol is `&`) |
| 153 | +
|
| 154 | +Does nothing if failed. (if `node` is `nullptr`) |
| 155 | +
|
| 156 | +### rename |
| 157 | +
|
| 158 | +```cpp |
| 159 | +inline void rename(std::unique_ptr<LambdaBase>& node) noexcept; |
| 160 | +``` |
| 161 | + |
| 162 | +Walks the tree and does alpha conversion on all fuctions to make names unique. |
| 163 | + |
| 164 | +Does nothing if failed. (if `node` is `nullptr`) |
| 165 | + |
| 166 | +### apply |
| 167 | + |
| 168 | +```cpp |
| 169 | +inline void apply(std::unique_ptr<LambdaBase>& node, const Variable& target, |
| 170 | + std::unique_ptr<LambdaBase> value); |
| 171 | +``` |
| 172 | +
|
| 173 | +Walks the tree and replaces all instances of `target` by `value`. Via the rules of beta reduction. |
| 174 | +
|
| 175 | +Does nothing if failed. (if `node` or `value` is `nullptr`) |
| 176 | +
|
| 177 | +May throw exception if allocation fails. (stdlib fail) |
| 178 | +
|
| 179 | +### reduceOnce |
| 180 | +
|
| 181 | +```cpp |
| 182 | +inline bool reduceOnce(std::unique_ptr<LambdaBase>& node); |
| 183 | +``` |
| 184 | + |
| 185 | +Does one beta reduction with "standard" algoritm. |
| 186 | + |
| 187 | +Returns `true` if beta reduction was done. |
| 188 | + |
| 189 | +Returns `false` if it's fully reduced or if `node` is `nullptr`. |
| 190 | + |
| 191 | +May throw exception if allocation fails. (stdlib fail) |
| 192 | + |
| 193 | +### reduce |
| 194 | + |
| 195 | +```cpp |
| 196 | +inline void reduce(std::unique_ptr<LambdaBase>& node) |
| 197 | +``` |
| 198 | +
|
| 199 | +Calls `reduceOnce` until fully reduced. |
| 200 | +
|
| 201 | +## Errors |
| 202 | +
|
| 203 | +Library has a custom exception `LambdaLibException` that holds a string with error message. Inherites from `std::exception`. |
| 204 | +
|
| 205 | +```cpp |
| 206 | +LambdaLibException::LambdaLibException(std::string msg); |
| 207 | +``` |
| 208 | + |
| 209 | +Contains `std::exception::what()` overload. |
| 210 | + |
| 211 | +```cpp |
| 212 | +inline const char* LambdaLibException::what() const noexcept override; |
| 213 | +``` |
| 214 | + |
| 215 | +## Special Definitions |
| 216 | + |
| 217 | +You can define special names to change how library works: |
| 218 | +- [LAMBDA_NOALIGN](#lambda_noalign) |
| 219 | +- [LAMBDA_NODEFAULTS](#lambda_nodefaults) |
| 220 | +- [LAMBDA_ADDITIONALS](#lambda_additionals) |
| 221 | + |
| 222 | +### LAMBDA_NOALIGN |
| 223 | + |
| 224 | +Turns off variable padding for lambda classes, makes them take up less space. |
| 225 | + |
| 226 | +Makes access to variables slower. |
| 227 | + |
| 228 | +### LAMBDA_NODEFAULTS |
| 229 | + |
| 230 | +`default.hpp` will no be included. |
| 231 | + |
| 232 | +### LAMBDA_ADDITIONALS |
| 233 | + |
| 234 | +If `LAMBDA_NODEFAULTS` not defined, includes `additionals.hpp`. |
0 commit comments