Coder Social home page Coder Social logo

logician's Introduction

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:

  • Coloring a map/graph so adjacent regions don’t use the same color
  • Resolving dependencies in a package manager
  • Solving puzzles like sudoku, n-queens, etc.

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:

  • Logic Programming in Swift

    An explanation of how logic programming works in Swift.

  • μKanren: A Minimal Functional Core for Relational Programming (pdf) by Jason Hemann and Daniel P. Friedman

    This paper explores what forms the minimal logic programming language in Scheme. It strips away the complexity of more capable solvers to expose the core idea.

  • Kanren.swift

    Swift playgrounds with implementations of various Kanrens.

  • Hello, declarative world by @tomstuart

    A brief explanation of logic programming and a minimal language in Ruby.

  • The Reasoned Schemer by Daniel P. Friedman, William E. Byrd and Oleg Kiselyov

    An unorthodox book, in the style of The Little Schemer, that has pages of exercises that demonstrate how a kanren-style logic programming works.

  • Constraint Processing by Rina Dechter

    An in-depth look at constraints, algorithms, and optimizations. This book explains all you need to know to write a complex solver.

logician's People

Contributors

335g avatar ferranpujolcamins avatar kastiglione avatar mdiep avatar uraimo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

logician's Issues

Add a bimap method to Variables

Followup from #4.

map lets you create a Property, which acts as a unidirectional goal.

Bidirectional goals require mapping in both directions; hence bimap.

Add a map method to Variables

As a way of adding constraints

// should find "mouse", "moose"
let fourLetterWords: AnyIterator<String> = solve { word in
    return word.map { $0.length } == 5
        && any(
            word == "dog",
            word == "cat",
            word == "mouse",
            word == "moose",
        )
}

Build up complex values

You should be able to solve for something more than just a primitive type by solving based on its properties.

Use Result

try/catch will work well in some places yet, but it'd be nice to have the types and it will enable some source improvements.

Add a Stream type

Using AnyIterator has worked well so far, but it's started to feel a bit cumbersome. I think this will simplify the code.

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.