Giter Club home page Giter Club logo

pyprolog's Introduction

Simple Prolog Interpreter in Python

badges license

Install dependencies

poetry install

Run REPL

Using poetry:

poetry run prolog [options] path

or

python -m prolog.prolog [options] path

For example,

poetry run prolog tests/data/puzzle1.prolog

Sample REPL session output:

poetry run prolog tests/data/myadven.prolog 

Welcome to Simple Prolog
ctrl-c to quit
> location(desk, office)
yes
> location(desk, office1)
no
> location(X, Y)
X = desk Y = office 
X = apple Y = kitchen 
X = flashlight Y = desk 
X = 'washing machine' Y = cellar 
X = nani Y = 'washing machine' 
X = broccoli Y = kitchen 
X = crackers Y = kitchen 
X = computer Y = office 
no
> door(kitchen, R), location(T, R).
R = office T = desk 
R = office T = computer 
R = cellar T = 'washing machine' 
no

Simple Prolog supports following built-ins: write, tab, nl and fail.

You can use them to display values of variables or text:

Welcome to Simple Prolog
ctrl-c to quit
> room(X), tab, write(X), nl.
        kitchen
X = kitchen 
        office
X = office 
        hall
X = hall 
        'dinning room'
X = 'dinning room' 
        cellar
X = cellar 
no
>

or if you do not want to see the solutions just print out:

> write('This is the list of rooms:'), nl, room(X), tab, write(X), nl, fail.
'This is the list of rooms:'
        kitchen
        office
        hall
        'dinning room'
        cellar
no
>

You can also perform simple arithmetic operations. For example given following rule:

c_to_f(C, F) :- F is C * 9 / 5 + 32.

You can ask Prolog to convert Celsius to Fahrenheit:

Welcome to Simple Prolog
ctrl-c to quit
> c_to_f(0, F).
F = 32.0
yes
> c_to_f(100, F).
F = 212.0
yes
>

You can also ask Prolog REPL to do calculation directly:

Welcome to Simple Prolog
ctrl-c to quit
> Z is 4 * 10 - 2 * 4.
Z = 32.0
yes
> Z is 4 * (10 - 2) * 4.
Z = 128.0
yes
>

Following logical operators are supported:

==
=/
<
=<
>
>=

Here is an example of rule that uses logical operator:

freezing(X) :- X =< 32.
Welcome to Simple Prolog
ctrl-c to quit
> freezing(30).
yes
>

Or you can use it directly from REPL:

Welcome to Simple Prolog
ctrl-c to quit
> X is 2+2, X > 1.
yes
> X is 2+2, X > 5.
no
>   

Simple Prolog also has support for lists. Here are some examples:

Welcome to Simple Prolog
ctrl-c to quit
> rgb([red, green, blue]).
yes
> rgb([R, G, B]).
R = red G = green B = blue 
yes
> rgb([_, G, _]).
G = green 
yes
> rgb([R, green, B]).
R = red B = blue 
yes
> rgb([red, green | H]).
H = [blue] 
yes
> rgb([H | T]).
H = red T = [green, blue] 
yes
> rgb([H | [X, Y]]).
H = red X = green Y = blue 
yes
> 

Test

Linter:

poetry run flake8

Tests:

poetry run  pytest --cov=prolog tests

Acknoledgments

This was inspired and based on this article

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.