Git Product home page Git Product logo

first-order's Introduction

first-order

Pure Python first-order logic evaluator

from first_order import ForAll, Term

w = Term("w")
x = Term("x")
y = Term("y")

s1 = x & y
s2 = w | ~x
s3 = s1 >> s2

print(ForAll(x, s3))
# output: (โˆ€ x: ((x & y) >> (w | ~x)))

print(s3 @ {"x": True, "y": True, "w": False})
# output: False

Installation

From pypi:

pip install first-order

Latest commit:

pip install git+https://github.com/olivi-r/first-order.git#egg=first-order

Syntax

This project uses simple constructs to abstract the creation of first-order logic sentences:

  • & conjunction
  • | disjunction
  • ~ negation
  • >> implication

Two special functions are required to create sentences with quantifiers:

  • Exists existential quantifier
  • ForAll universal quantifier
from first_order import Exists, ForAll, Term

x = Term("x")
z = Term("z")

s_forall = ForAll(z, x | z)
s_exists = Exists(z, x | z)

print(s_forall @ {"x": False})
# output: False

print(s_exists @ {"x": False})
# output: True

Interpretations

Interpretations can be applied to the sentence through the usage of the @ operator (as seen in previous examples), this takes a mapping from the names of terms used in the sentence and their respective values.

Unbound terms result in an error being thrown.

from first_order import Term

x = Term("x")
y = Term("y")

print((x | y) @ {"x": True})
# output:
# Traceback (most recent call last):
#     ...
# KeyError: 'y'

Terms attatched to quantifiers do not need to be in the interpretation (unless the same term name is used elsewhere)

from first_order import Exists, Term

x = Term("x")
y = Term("y")

print(Exists(y, x | y) @ {"x": True})
# output: True

first-order's People

Contributors

olivi-r avatar

Watchers

 avatar

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.