{"id":18497583,"url":"https://github.com/csb6/backtrack","last_synced_at":"2025-05-14T05:14:10.445Z","repository":{"id":115656061,"uuid":"218884799","full_name":"csb6/Backtrack","owner":"csb6","description":"A C++17 library with Prolog-like functionality (Work-In-Progress)","archived":false,"fork":false,"pushed_at":"2020-02-06T02:09:58.000Z","size":209,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-25T17:42:12.846Z","etag":null,"topics":["cpp","cpp17","logic-programming","prolog","rules-engine"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/csb6.png","metadata":{"files":{"readme":"README.md","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,"publiccode":null,"codemeta":null}},"created_at":"2019-11-01T00:39:57.000Z","updated_at":"2020-10-01T23:37:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"b5bfeb85-9591-4387-bbd9-d5669e485b4e","html_url":"https://github.com/csb6/Backtrack","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/csb6%2FBacktrack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csb6%2FBacktrack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csb6%2FBacktrack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csb6%2FBacktrack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/csb6","download_url":"https://codeload.github.com/csb6/Backtrack/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239213603,"owners_count":19601007,"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":["cpp","cpp17","logic-programming","prolog","rules-engine"],"created_at":"2024-11-06T13:35:01.287Z","updated_at":"2025-02-17T00:20:01.611Z","avatar_url":"https://github.com/csb6.png","language":"C++","readme":"# Backtrack\n\nBacktrack is a single-header C++ library intended to serve as a rules engine\nalong the lines of Prolog. Currently, facts and rules similar to those in\nProlog can be defined, as well as queries without variables,\nbut support for backtracking and queries such as `predicate(X,5)` are not yet supported.\n\nI see the irony of naming a library `backtrack` without supporting\nbacktracking, but my intention is to eventually support it in a way similar\nto Prolog. For now, think of the name as me backtracking on the implication\nthat this library supports backtracking!\n\n## Installation\n\nThe only file you need is `backtrack.hpp`. Simply `#include` it into any\nsource/header file.\n\nThis library is written in C++17.\n\n## Usage\n\n### Creating a database\n\nThe main class is `Database`. You can create as many databases as you\nlike, but typically using just one is most useful so that knowledge stays centralized.\nHere is a possible instantiation of a database:\n\n```\n\t#include \"backtrack.hpp\"\n\n\tDatabase\u003cconst char*, int, float\u003e db;\n```\n\nThe first parameterized type, `N`, is the type used to represent the names of\nthe facts and rules. Strings work great for creating names quickly, but\nusing an enum class called `FactName` or something similar is recommended\nto minimize memory usage and reduce the range of possible fact/rule names.\n\nThe last two parameterized types represent the possible types, `A` and\n`B`, of any arguments used in facts and rules.\n\n**Note**: While `A` and `B` cannot be the same type, and `A` and `B` must be\na different type than `N`. This enforces a clear separation between what is\na fact/rule name and what is an argument.\n\nFacts and rules can have as many overloads as you like, just like in Prolog\nyou can have `nameA(A).`, `nameA(A,B).`, and `name(5,6).` all in the same database.\nHowever, all facts and rules must have at least 1 argument, meaning that you\ncan not define an zero-argument rule like `nameA.` in Prolog. Since C++ supports\nconstants and enums, it should be possible to make a `nameA` constant or enum for\nthe same purpose as it would be used in Prolog.\n\n### Adding facts to the database\n\nFacts are added by calling a member function on your database instance.\nThe following code adds two facts equivalent to `P(8,6)` and `Y(5)` in\nProlog.\n\n```\n\tdb.add(\"P\", new Fact\u003cint,float\u003e(8, 6.0));\n\tdb.add(\"Y\", new Fact\u003cint,float\u003e(5.0));\n```\n\nThe first argument is the name of the fact, and it must be the same type\nas the `N` type specified in the `Database`'s constructor. The second argument\nis a `Fact` object, which must be parameterized with the same `\u003cA,B\u003e` as the\n`A` and `B` used in the `Database` constructor.\n\nThe arguments given to a Fact will be evaluated when added and will added immediately\nto the database, so be sure that the arguments fed to the constructor are the values you\nwould like the Fact to use. You can specify as many arguments as like, as long as they\nare all of type `A` or `B`.\n\nAdditionally, type-checking is enforced through use of templates, so the compiler\nwill let you know if you use arguments of types other than `A` or `B`.\n\n## Querying the database\n\nAs seen in the last example, the `()` operator overload, `db()`, can query the\ndatabase with a given request, and the database will indicate if the query is\ntrue or false based on its current knowledge.\nHere is an example of a simple query, equivalent to the Prolog query `?- isPositive(1)`:\n\n```\n\tdb(\"isPositive\", 1);\n```\n\nThe first argument is the name of the rule/fact to look for; the second argument\nis the first argument passed to the rule/fact. This function would return true\nif there was some fact or rule named \"isPositive\" that evaluated to true\ngiven an input of 1.\n\nI hope you find the library useful and/or interesting!","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsb6%2Fbacktrack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcsb6%2Fbacktrack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsb6%2Fbacktrack/lists"}