logic-db
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

LogicDB

Embedding a Prolog-like logic programming language in JavasScript and TypeScript.

  • Combining logic programming and database management.
  • Can declare relations between JSON documents.
  • Well typed relation in TypeScript.
  • Practical and simple API.

Install

npm i logic-db

Usage

import Logic, { v, ne, ty } from "logic-db"

// NOTE Drinking Pairs -- example from "Clause and Effect"

const drinks = new Logic.Table({
  name: "drinks",
  schema: ty.object({
    person: ty.string(),
    alcohol: ty.string(),
  }),
})

drinks.i({ person: "john", alcohol: "martini" })
drinks.i({ person: "mary", alcohol: "gin" })
drinks.i({ person: "susan", alcohol: "vodka" })
drinks.i({ person: "john", alcohol: "gin" })
drinks.i({ person: "fred", alcohol: "gin" })
drinks.i({ person: "fred", alcohol: "vodka" })

const pair = new Logic.Table({
  name: "pair",
  schema: ty.object({
    p1: ty.string(),
    p2: ty.string(),
    alcohol: ty.string(),
  }),
})

pair.i({ p1: v`p1`, p2: v`p2`, alcohol: v`alcohol` }, (v) => [
  drinks.o({ person: v`p1`, alcohol: v`alcohol` }),
  drinks.o({ person: v`p2`, alcohol: v`alcohol` }),
  ne(v`p1`, v`p2`),
])

console.log(pair.query({ p1: v`x`, p2: "mary", alcohol: "gin" }))
console.log(pair.query({ p1: v`x`, p2: v`y`, alcohol: "gin" }))
console.log(pair.query({ p1: v`x`, p2: v`y`, alcohol: v`alcohol` }))

Examples

Clause and Effect

  • By William F. Clocksin.

Structure and Interpretation of Computer Programs (SICP)

The Power of Prolog

API Docs

TODO

Contributions

Be polite, do not bring negative emotion to others.

License

Readme

Keywords

none

Package Sidebar

Install

npm i logic-db

Weekly Downloads

2

Version

0.1.4

License

GPL-3.0-or-later

Unpacked Size

108 kB

Total Files

107

Last publish

Collaborators

  • xieyuheng