Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

PySwip is a Python-Prolog interface that enables querying SWI-Prolog in your Python programs.

License

NotificationsYou must be signed in to change notification settings

yuce/pyswip

Repository files navigation

PySwip

PySwip logo

What's New?

See theChange Log.

Install

If you have SWI-Prolog installed, it's just:

pip install -U pyswip

SeeGet Started for detailed instructions.

Introduction

PySwip is a Python-Prolog interface that enables queryingSWI-Prolog in your Python programs.It features an SWI-Prolog foreign language interface, a utility class that makes it easy querying with Prolog and also a Pythonic interface.

Since PySwip uses SWI-Prolog as a shared library and ctypes to access it, it doesn't require compilation to be installed.

PySwip was brought to you by the PySwip community.Thanks to allcontributors.

Documentation

Examples

Using Prolog

frompyswipimportPrologProlog.assertz("father(michael,john)")Prolog.assertz("father(michael,gina)")list(Prolog.query("father(michael,X)"))== [{'X':'john'}, {'X':'gina'}]forsolninProlog.query("father(X,Y)"):print(soln["X"],"is the father of",soln["Y"])# michael is the father of john# michael is the father of gina

An existing knowledge base stored in a Prolog file can also be consulted, and queried.Assuming the filename "knowledge_base.pl" and the Python is being run in the same working directory, it is consulted like so:

frompyswipimportPrologProlog.consult("knowledge_base.pl")

Foreign Functions

frompyswipimportProlog,registerForeigndefhello(t):print("Hello,",t)hello.arity=1registerForeign(hello)Prolog.assertz("father(michael,john)")Prolog.assertz("father(michael,gina)")print(list(Prolog.query("father(michael,X), hello(X)")))

Pythonic interface (Experimental)

frompyswipimportFunctor,Variable,Query,callassertz=Functor("assertz",1)father=Functor("father",2)call(assertz(father("michael","john")))call(assertz(father("michael","gina")))X=Variable()q=Query(father("michael",X))whileq.nextSolution():print("Hello,",X.value)q.closeQuery()# Outputs:#    Hello, john#    Hello, gina

The core functionality ofProlog.query is based on Nathan Denny's public domain prolog.py.

Help!

PySwip Community Home

PySwip was used in scientific articles, dissertations, and student projects over the years.Head out toPySwip Community for more information and community links.

Do you have a project, video or publication that uses/mentions PySwip?file an issue or send a pull request.

If you would like to reference PySwip in a LaTeX document, you can use the providedBibTeX file.You can also use the following information to refer to PySwip:

License

PySwip is licensed under theMIT license.


[8]ページ先頭

©2009-2025 Movatter.jp