Skip to main content

A parser for logical formulas

Project description

implies: a Pybound Rust crate for logical formulas

implies is a Rust crate for storing logical formulas as parse trees and performing complex operations on them, like substitution, rotation, conversion to conjunctive normal form, and more. Propositional logic comes pre-implemented, but this crate operates on a generic struct Formula<B,U,A> which can easily be used with your own Binary and Unary operators and Atomic formula types: if you can implement those types for your own preferred logic (modal, temporal, predicate, etc...) you can use the full functionality of this crate for your own language. A lot more information is in the docs for this crate.

There are Python bindings for propositional logic, but using the API in Python gives much less control and flexibility. You can use the Python APIs from Rust if you want by enabling the "python" feature when compiling, which will add "pyo3" as a dependency.

Here is a simple example of implementing a basic modal logic with implies, so you can see how easily you can use implies for your own logical language:

use crate::formula::*;
use crate::parser::Match;
use crate::prop::*;
use crate::symbol::{Atom, Symbolic};

/// The usual unary operators for modal logic: negation,
/// box and diamond. Most of the traits you need to get your
/// symbol types to work with implies are derivable.
///
/// Pro tip:
/// Write operator enums like this in the ascending precedence order
/// want for your operators, so that deriving Ord freely gives you the
/// precedence you expect. In the case of unary operators like these,
/// it doesn't matter, but it's useful for binary operators.
#[derive(PartialEq, Eq, Ord, PartialOrd, Copy, Clone, Default, Hash)]
enum ModalUnary {
    Box,
    Diamond,
    // Give any value as the default, it just allows
    // fast swap operations under the hood without
    // unsafe Rust.
    #[default]
    Not,
}

/// Specify how your type should be pretty-printed.
impl std::fmt::Display for ModalUnary {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ModalUnary::Box => write!(f, "◻"),
            ModalUnary::Diamond => write!(f, "◊"),
            ModalUnary::Not => write!(f, "¬"),
        }
    }
}

/// This marker trait shows you've covered all the bases.
impl Symbolic for ModalUnary {}

/// Implement this simple trait (whose methods are partial inverses)
/// for immediate access to parsing formulae from strings.
impl Match for ModalUnary {
    fn match_str(s: &str) -> Option<Self> {
        match s {
            "◻" => Some(ModalUnary::Box),
            "◊" => Some(ModalUnary::Diamond),
            "¬" | "not" | "~" => Some(ModalUnary::Not),
            _ => None,
        }
    }

    fn get_matches(&self) -> Vec<String> {
        match self {
            ModalUnary::Box => vec!["◻".to_owned()],
            ModalUnary::Diamond => vec!["◊".to_owned()],
            ModalUnary::Not => vec!["¬".to_owned(), "not".to_owned(), "~".to_owned()],
        }
    }
}

/// The binary operators for modal logic are the same as those for propositional.
type ModalBinary = PropBinary;

/// Just write a type alias and that's it, all of implies' functionality for free.
type ModalFormula = Formula<ModalBinary, ModalUnary, Atom>;

Once you have a type like ModalFormula set up, you can just use all the provided methods in the Formula struct for free. To initialize a formula you can start with just an atom

let mut f = ModalFormula::new(Atom("p"))  // p

or use the cascade macro for builder syntax

let mut f = cascade! {
  let f = ModalFormula::new(Atom("p"))   // p
  ..unify(ModalUnary::Box)               // ◻p
  ..left_combine(PropBinary::Implies, ModalFormula::new(Atom("q"))) // q -> ◻p
}

or if your type implements Match just use a string!

use implies::parser::build_formula;

let mut f: ModalFormula = build_formula("q -> ◻p")?;

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

implies-0.4.5.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

implies-0.4.5-cp39-cp39-macosx_10_12_x86_64.whl (250.8 kB view details)

Uploaded CPython 3.9macOS 10.12+ x86-64

File details

Details for the file implies-0.4.5.tar.gz.

File metadata

  • Download URL: implies-0.4.5.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.4.0

File hashes

Hashes for implies-0.4.5.tar.gz
Algorithm Hash digest
SHA256 69a64240bf8acb916c547aded91af1786a97d4b216051051e652c71ae2038913
MD5 fc5b4c88ccd1acf03511f38b1de844e3
BLAKE2b-256 565d0e15947f489ae2daebac67488f102bc620551f507fe03b6319773c6102ef

See more details on using hashes here.

File details

Details for the file implies-0.4.5-cp39-cp39-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for implies-0.4.5-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2b73261ebf688006909c27c630493f50d0d1e6bd02153bf5135ffa64701c04c0
MD5 6c5bda9f7e9c93736b0f58f388d6fe8b
BLAKE2b-256 f6f48a4906b04ddf4c36086728cdcc44a7faeaa0392cbdfd418496f6977fcd63

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page