Home

Awesome

Logician

SPM Compatible

Logic programming in Swift

Logic Programming

Logic programming is a declarative style of programming that uses constraints to describe problems. Instead of writing a solution to the problem, you describe the characteristics of the solution and let the computer solve it for you.

Here are some example problems that are a good fit for logic programming:

Different logic programming implementations contain different types of constraints.

Installation

Logician is compatible with Swift Package Manager. Add the following line into your Package.swift dependencies: .package(url: "https://github.com/mdiep/Logician.git", .branch("master")

Using Logician

In order to use Logician, you need to be familiar with 3 concepts:

  1. Variable

    A variable describes an unknown value in a logic problem, much like variables in algebra. Logician variables are generic over a value type.

  2. Goal

    A goal represents some condition that should be true in the solved state. It’s currently implemented as a (State) -> Generator<State>. A goal can diverge and return multiple possible states or terminate, signaling that a constraint was violated.

    Logician provides a number of built-in goals—like ==, !=, distinct, &&, ||, all, and any—that should provide a good start in most cases.

  3. solve

    This function is the interface to Logician’s solver. Its block takes Variables to solve as input and returns Goals to solve for.

Logician is still in its early stages. Its current implementation is based on the miniKanren approach of using functions that return generators. This is likely to change in the future in order to enable optimizations.

Examples

Logician includes playgrounds with a sudoku solver and an n-queens solver that demonstrate usage of the library.

License

Logician is available under the MIT License

Learn More

The following are good resources for learning more about logic programming: