{"id":14068508,"url":"https://github.com/dirkschumacher/logician","last_synced_at":"2025-05-15T10:33:19.570Z","repository":{"id":145906595,"uuid":"269970657","full_name":"dirkschumacher/logician","owner":"dirkschumacher","description":"🖖 Prolog-style Logic Programming in pure R","archived":false,"fork":false,"pushed_at":"2020-06-12T18:49:40.000Z","size":35,"stargazers_count":37,"open_issues_count":4,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-12T02:07:33.863Z","etag":null,"topics":["datalog","logic-programming","prolog","r"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dirkschumacher.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-06-06T12:11:20.000Z","updated_at":"2024-11-01T21:00:08.000Z","dependencies_parsed_at":"2024-02-24T13:34:02.016Z","dependency_job_id":"51420057-3ee7-4bca-a727-a68b9e60ef2b","html_url":"https://github.com/dirkschumacher/logician","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Flogician","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Flogician/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Flogician/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Flogician/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dirkschumacher","download_url":"https://codeload.github.com/dirkschumacher/logician/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254323276,"owners_count":22051751,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["datalog","logic-programming","prolog","r"],"created_at":"2024-08-13T07:06:13.702Z","updated_at":"2025-05-15T10:33:19.226Z","avatar_url":"https://github.com/dirkschumacher.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# Logic programming in R\n\n\u003c!-- badges: start --\u003e\n[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)\n[![R build status](https://github.com/dirkschumacher/logician/workflows/R-CMD-check/badge.svg)](https://github.com/dirkschumacher/logician/actions)\n\u003c!-- badges: end --\u003e\n\nThe goal of `logician` is to do logic programming inspired by [datalog](https://en.wikipedia.org/wiki/Datalog)/[prolog](https://en.wikipedia.org/wiki/Prolog) in R.\nIt is written in R without any third-party package dependencies. It targets interactive use\nand smaller instances of logical programs.\n\nNon-goal: be fully prolog compatible and super fast.\n\nSide-goal: experiment and have fun.\n\n## Installation\n\n~~You can install the released version of logician from [CRAN](https://CRAN.R-project.org) with:~~\n\n``` r\ninstall.packages(\"logician\")\n```\n\nOr the current github version from here:\n\n``` r\nremotes::install_github(\"dirkschumacher/logician\")\n```\n\n## General principle\n\nThe general idea is to query a database of *facts* and *rules* and ask questions.\n`logician` tries prove that your query is either `true` or `false`. Since you\ncan use variables, there might be multiple assignments to variables that make\nyour query `true`. `logician` will return these one by one as an iterator.\n\nThis is all work in progress and still a bit hacky, but usable.\n\n## Example\n\n```{r}\nlibrary(logician)\n```\n\n### A graph example\n\nHere we define a database with two types of elements:\n\n1. a fact for each tuple of nodes that are directly connected\n2. a rule that determines if there exists a path between two nodes.\n    * A path between `A` and `B` exists if `A` and `B` are connected OR\n    * if `A` is connected to an intermediate node `Z` and there exists a path from\n      `Z` to `B`.\n\n```{r}\ndatabase \u003c- logician_database(\n  connected(berlin, hamburg),\n  connected(hamburg, chicago),\n  connected(chicago, london),\n  connected(aachen, berlin),\n  connected(chicago, portland),\n  connected(portland, munich),\n  path(A, B) := connected(A, B),\n  path(A, B) := connected(A, Z) \u0026\u0026 path(Z, B)\n)\n```\n\n```{r}\niter \u003c- logician_query(database, path(berlin, hamburg))\niter$next_value()\n```\n```{r}\niter \u003c- logician_query(database, path(berlin, munich))\niter$next_value()\n```\nAt last let's find all nodes `berlin` is connected to.\n\n```{r}\niter \u003c- logician_query(database, path(berlin, X))\niter$next_value()\niter$next_value()\niter$next_value()\niter$next_value()\niter$next_value()\niter$next_value()\n```\n\n## Embracing the host\n\nUnlike prolog, we run on a host language and can integrate R into the evaluation of rules.\nThe only requirement is that the R expression returns a length 1 logical and does not depend\non anything outside the `globalenv`. If that is good idea remains to be seen :)\nOne downside that the list of possible results could be infinite.\n\nAny expression in the `r` clause will be treated as an R expression that is\nnot unified as the usual clauses. All variables need to be bound before\nit can be used.\n\n```{r}\ndatabase \u003c- logician_database(\n    number(1),\n    number(2),\n    number(3),\n    number(4),\n    number(5),\n    sum(A, B) := number(A) \u0026\u0026 number(B) \u0026\u0026 r(A + B \u003e 3)\n)\n```\n\n```{r}\niter \u003c- logician_query(database, sum(1, B))\niter$next_value()\niter$next_value()\niter$next_value()\niter$next_value()\n```\n### Database Example\n\nYou could also think of the `database` as a real database. Where each fact\nis a row of a specific table. `rules` are additional logical structures of\nyour data. Then you can use logical programming instead of SQL to query\nyour data.\n\nAt the moment this is not as practical though as it could be.\n\n```{r}\ndatabase \u003c- logician_database(\n  # the employee table aka relation\n  employee(bart),\n  employee(homer),\n  employee(marge),\n  employee(maggie),\n  employee(lisa),\n  \n  # the payment table\n  salary(bart, 100),\n  salary(homer, 100),\n  salary(marge, 120),\n  salary(maggie, 140),\n  salary(lisa, 180),\n  \n  # reporting hierarchy\n  manages(lisa, maggie),\n  manages(lisa, marge),\n  manages(marge, homer),\n  manages(marge, bart),\n  \n  # direct and indirect reports\n  reports_to(A, B) := manages(B, A),\n  reports_to(A, B) := manages(B, X) \u0026\u0026 reports_to(A, X),\n  \n  # a salary query\n  makes_more(A, X) := salary(A, Y) \u0026\u0026 r(Y \u003e X + pi)\n  # using pi here to show that certain R symbols from the globalenv\n  # can be used.\n)\n```\n\n```{r}\n# who reports to lisa?\niter \u003c- logician_query(database, reports_to(A, lisa))\niter$next_value()\niter$next_value()\niter$next_value()\niter$next_value()\niter$next_value()\n\n# and who makes more than 140 + pi?\niter \u003c- logician_query(database, makes_more(A, 140))\niter$next_value()\niter$next_value()\n```\n\nAnd instead of this manual database, you generate a database with real world data.\nThough the query system might slow for large amount of at this point.\n\n## API\n\nThe API has two components: one aimed at interactive use and another one\nwhen you want to program with it (e.g. embed it into another package).\n\n* `logician_database` a helper function to construct a database with the help of\n non-standard evaluation. A database is just a list of `fact`s and `rule`s\n that you can also construct yourself using helper functions such\n as `fact`, `rule`, `atom`, `char`, `clause`, `variable`, `int` and `r_expr`.\n\n* `logician_query` main query function for interactive use. You can use `head`\n  on an iterator to return the first `n` results (if they exists).\n\n* `logician_query_` same as `logician_query` but expects a `clause` or `r_expr`.\n\n## Datatypes and Terminology\n\n* A database contains `facts` and `rules`.\n* Each `fact` is a `clause`.\n* A `clause` has a name and a list of `arguments`.\n* An argument can either be an `atom`, `int`, `char` or `var`.\n* Per convention, variables start with a capital letter, atoms with a lower case letter.\n* Each `rule` has a `head` and a `body`.\n* A `head` is a `clause`.\n* A `body` is a list of `clauses` or `R expressions`.\n\n## Current Limitations\n\n* Still experimental, so there might be bugs or undefined behavior.\n  API likely will have breaking changes in the future.\n* `logician_query` supports only one clause at the moment. If you want to query\n  for multiple clauses at one, create a rule for that query.\n* No list support ... yet.\n* No cuts. Might never be supported.\n* No higher order terms. Though most likely I will not support them.\n\n## Test Coverage\n\n```{r}\ncovr::package_coverage()\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirkschumacher%2Flogician","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdirkschumacher%2Flogician","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirkschumacher%2Flogician/lists"}