Skip to content

IPodtsepko/logic-primitives

Repository files navigation

Logic Primitives Library

This project is an implementation of a library for mathematical and logical operations using combinatory logic. It provides a set of templates and utilities for constructing complex functions using recursive, combinatorial, and functional programming principles.

Features

  • Combinators: Implements standard combinators such as Z, N, S, U, and R for building mathematical logic.
  • Recursive Formulas: Provides recursive definitions for arithmetic operations like addition, subtraction, multiplication, division, and modulus.
  • Fast Arithmetic Operations: Includes optimized (fast) versions of operations for improved runtime performance of more complex operations.
  • Boolean Logic: Constructs logical operations such as cast to boolean, negation, boolean folds, and all type of comparisons.
  • Prime Number Utilities:
    • Check if a number is prime.
    • Count primes.
    • Retrieve the nth prime.
  • Stack Implementation:
    • Create stacks.
    • Append elements.
    • Peek at the top element.
    • Pop elements.
    • Check if the stack is empty.

Code Structure

Core Types

  • Natural: A type alias for int, representing natural numbers.

Combinators

  • Z: Always returns 0.
  • N: Increments a value by 1.
  • U<K, N>: Projection combinator for selecting the K-th argument from N arguments.
  • S<G, Fs...>: Super combinator for applying functions in sequence.
  • R<F, G>: Recursive combinator for defining functions with base and recursive cases.

Recursive Functions

  • Arithmetic:
    • add: Addition.
    • sub: Subtraction.
    • mul: Multiplication.
    • power: Exponentiation.
  • Comparisons:
    • eq: Equality.
    • leq: Less than or equal.
    • geq: Greater than or equal.
    • greater: Greater than.
    • less: Less than.
  • Division and Modulus:
    • divide: Integer division.
    • mod: Modulus.

Boolean Logic

  • return_yes and return_no: Boolean constants.
  • negate: Logical NOT.
  • or_fold and and_fold: Logical OR and AND.

Prime Utilities

  • is_complex: Check if a number is complex.
  • is_prime: Check if a number is prime.
  • prime_count: Count primes up to a given number.
  • get_prime_by_index: Get the nth prime number.

Stack Interface

  • create_stack: Create an empty stack.
  • append: Append elements to a stack.
  • peek: Retrieve the top element of a stack.
  • pop: Remove the top element of a stack.
  • empty: Check if the stack is empty.

Usage

Basic Arithmetic

apply<add>(3, 4); // Returns 7

Prime Checking

apply<fast::is_prime>(7); // Returns true

Stack Operations

Natural stack = apply<create_stack>();
stack = apply<append>(stack, 5);
Natural top = apply<peek>(stack); // Returns 5
stack = apply<pop>(stack);
bool stack_is_empty = apply<empty>(stack); // Returns true

Build

Clone the repository:

git clone git@github.com:IPodtsepko/logic-primitives.git
cd logic-primitives

Install dependencies (e.g., GoogleTest as a submodule):

git submodule update --init --recursive

Build the project using CMake:

mkdir build && cd build
cmake ..
make

Run tests:

./tests

About

Implementation of mathematical logic primitives based on C++ templates.

Topics

Resources

Stars

Watchers

Forks