{"id":21432977,"url":"https://github.com/robjsliwa/pyprolog","last_synced_at":"2025-07-14T13:30:43.746Z","repository":{"id":37050089,"uuid":"369655678","full_name":"robjsliwa/pyprolog","owner":"robjsliwa","description":"Prolog implemented in Python","archived":false,"fork":false,"pushed_at":"2024-09-06T19:59:08.000Z","size":143,"stargazers_count":10,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-08T09:02:36.297Z","etag":null,"topics":["interpreter","prolog","prolog-interpreter","python","unification"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robjsliwa.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":"2021-05-21T21:14:54.000Z","updated_at":"2025-03-21T13:25:27.000Z","dependencies_parsed_at":"2024-07-05T19:33:32.955Z","dependency_job_id":null,"html_url":"https://github.com/robjsliwa/pyprolog","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/robjsliwa/pyprolog","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robjsliwa%2Fpyprolog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robjsliwa%2Fpyprolog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robjsliwa%2Fpyprolog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robjsliwa%2Fpyprolog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robjsliwa","download_url":"https://codeload.github.com/robjsliwa/pyprolog/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robjsliwa%2Fpyprolog/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265297455,"owners_count":23742586,"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":["interpreter","prolog","prolog-interpreter","python","unification"],"created_at":"2024-11-22T23:23:47.113Z","updated_at":"2025-07-14T13:30:43.433Z","avatar_url":"https://github.com/robjsliwa.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple Prolog Interpreter in Python\n\n![badges](https://github.com/robjsliwa/pyprolog/actions/workflows/python-package.yml/badge.svg)\n[![license](https://img.shields.io/badge/License-MIT-purple.svg)](LICENSE)\n\n## Set up environment\n\n```bash\npython -m venv .venv\n.\\.venv\\Scripts\\activate\n```\n\n## Install dependencies\n\n```bash\npip install -r requirements.txt\n```\n\n## Run REPL\n\n\n```bash\npython -m prolog.prolog [options] path\n```\n\nFor example,\n\n```bash\npython -m prolog.prolog tests/data/puzzle1.prolog\n```\n\nSample REPL session output:\n\n```bash\npython -m prolog.prolog tests/data/myadven.prolog \n\nWelcome to Simple Prolog\nctrl-c to quit\n\u003e location(desk, office).\nyes\n\u003e location(desk, office1).\nno\n\u003e location(X, Y).\nX = desk Y = office \nX = apple Y = kitchen \nX = flashlight Y = desk \nX = 'washing machine' Y = cellar \nX = nani Y = 'washing machine' \nX = broccoli Y = kitchen \nX = crackers Y = kitchen \nX = computer Y = office \nno\n\u003e door(kitchen, R), location(T, R).\nR = office T = desk \nR = office T = computer \nR = cellar T = 'washing machine' \nno\n```\n\nSimple Prolog supports following built-ins: write, tab, nl and fail.\n\nYou can use them to display values of variables or text:\n\n```\nWelcome to Simple Prolog\nctrl-c to quit\n\u003e room(X), tab, write(X), nl.\n        kitchen\nX = kitchen \n        office\nX = office \n        hall\nX = hall \n        'dinning room'\nX = 'dinning room' \n        cellar\nX = cellar \nno\n\u003e\n```\n\nor if you do not want to see the solutions just print out:\n\n```\n\u003e write('This is the list of rooms:'), nl, room(X), tab, write(X), nl, fail.\n'This is the list of rooms:'\n        kitchen\n        office\n        hall\n        'dinning room'\n        cellar\nno\n\u003e\n```\n\nYou can also perform simple arithmetic operations.  For example given following rule:\n\n```\nc_to_f(C, F) :- F is C * 9 / 5 + 32.\n```\n\nYou can ask Prolog to convert Celsius to Fahrenheit:\n\n```\nWelcome to Simple Prolog\nctrl-c to quit\n\u003e c_to_f(0, F).\nF = 32.0\nyes\n\u003e c_to_f(100, F).\nF = 212.0\nyes\n\u003e\n```\n\nYou can also ask Prolog REPL to do calculation directly:\n\n```\nWelcome to Simple Prolog\nctrl-c to quit\n\u003e Z is 4 * 10 - 2 * 4.\nZ = 32.0\nyes\n\u003e Z is 4 * (10 - 2) * 4.\nZ = 128.0\nyes\n\u003e\n```\n\nFollowing logical operators are supported:\n\n```\n==\n=/\n\u003c\n=\u003c\n\u003e\n\u003e=\n```\n\nHere is an example of rule that uses logical operator:\n\n```\nfreezing(X) :- X =\u003c 32.\n```\n\n```\nWelcome to Simple Prolog\nctrl-c to quit\n\u003e freezing(30).\nyes\n\u003e\n```\n\nOr you can use it directly from REPL:\n\n```\nWelcome to Simple Prolog\nctrl-c to quit\n\u003e X is 2+2, X \u003e 1.\nyes\n\u003e X is 2+2, X \u003e 5.\nno\n\u003e   \n```\n\nSimple Prolog also has support for lists.  Here are some examples:\n\n```\nWelcome to Simple Prolog\nctrl-c to quit\n\u003e rgb([red, green, blue]).\nyes\n\u003e rgb([R, G, B]).\nR = red G = green B = blue \nyes\n\u003e rgb([_, G, _]).\nG = green \nyes\n\u003e rgb([R, green, B]).\nR = red B = blue \nyes\n\u003e rgb([red, green | H]).\nH = [blue] \nyes\n\u003e rgb([H | T]).\nH = red T = [green, blue] \nyes\n\u003e rgb([H | [X, Y]]).\nH = red X = green Y = blue \nyes\n\u003e \n```\n\n## Test\n\nLinter:\n\n```\npython -m flake8\n```\n\nTests:\n\n```\npoetry run  pytest --cov=prolog tests\n```\n\n## How to Use PyProlog as a Library\n\nInstall pyprolog:\n\n```bash\npip install pieprolog\n```\n\nHere is an example how to use PyProlog as a library:\n\n```python\nfrom prolog import Scanner, Parser, Runtime\n\n\ndef main():\n    source = '''\n    location(computer, office).\n    location(knife, kitchen).\n    location(chair, office).\n    location(shoe, hall).\n\n    isoffice(X) :- location(computer, X), location(chair, X).\n    '''\n\n    tokens = Scanner(source).tokenize()\n    rules = Parser(tokens).parse_rules()\n\n    runtime = Runtime(rules)\n\n    goal_text = 'location(X, office).'\n\n    goal = Parser(Scanner(goal_text).tokenize()).parse_terms()\n\n    x = goal.args[0]\n\n    has_solution = False\n    for index, item in enumerate(runtime.execute(goal)):\n        has_solution = True\n        print(str(item))\n        print(str(goal.match(item).get(x)))\n\n    if has_solution:\n        print('Query has solution')\n    else:\n        print('Query has no solution')\n\n\nif __name__ == \"__main__\":\n    main()\n```\n\n## Acknoledgments\n\nThis was inspired and based on this [article](https://curiosity-driven.org/prolog-interpreter)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobjsliwa%2Fpyprolog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobjsliwa%2Fpyprolog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobjsliwa%2Fpyprolog/lists"}