Giter VIP home page Giter VIP logo

dereasy / propositional-calculator Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 7.21 MB

Console application for calculating propositional calculus formulas. Can create truth tables, show steps to result, check for validity, etc. Programmed in C#.

License: GNU General Public License v3.0

C# 100.00%
propositional-logic propositional-calculus truth-table truth-table-generator logic logic-gates satisfiability-solver satisfiability console-application

propositional-calculator's Introduction

pcl / Propositional Calculator

Console application for calculating propositional calculus formulas. Can create truth tables, show steps to result, check for validity, etc. Programmed in C#.

How to use

Running pcl with a file

You write a file (.txt, but any extension works except .pcl) containing proper syntax, then you call the executable in a console window and it does the rest for you.

  • The first program argument must be the file you want to process
  • Any further program arguments can be placed in any order you like pcl also accepts .pcl files, which are its own compilations. They contain all the necessary data to perform the same calculations as the source code in a binary format.

Running pcl shell

Starting pcl without arguments or using the "shell" program argument will make pcl start in shell mode. In this mode, you just type the formula you want to calculate in the console.

  • The first program argument acts as the filename. Shell mode does not read from any files, but if you run pcl with "compile" or "out" options on, a filename is still needed
  • The default filename is "shell", which will be used if pcl is run without any arguments

Possible arguments

Argument Effect
no_table Prevents a table from being printed.
props Shows whether the formula is valid, satisfiable, invalid.
compile Compiles the code to a .pcl file and runs that. Does nothing if run with a .pcl file.
steps Prints all the steps to arrive at the result. (Can also be easier to read for humans if the formula is complex.)
out Makes all print commands write to a .txt file. If your input file is called prop.txt, it will appear as prop_out.txt.
shell Start pcl in shell mode. Default argument if pcl is run without any explicit arguments.
no_calc Prevents the code from being executed. Useful if you only want to compile the formula or show its steps. Makes arguments, which rely on calculations being performed, effectless.

Syntax

Binary operators Explanation
and AND gate
nand NAND gate
or OR gate
nor NOR gate
xor XOR gate
xnor XNOR gate
impl Material implication

The operator precedence is in that exact order.

Unary operator
! NOT gate/Negation/Inverter; inverts the value to the right of it

Can negate variables, constants and brackets (must be placed left of the left bracket). Adjacent ones negate each other.

Brackets
(
)

Can be used to change the precedence of operations or to make it easier to read the formula.

Constants
false
true

Can be used to substitute a variable, does not change its value over the course of the program though.

Variables can be denoted with following characters:

  • Lowercase English alphabet a-z
  • Uppercase English alphabet A-Z
  • Common digits 0-9

The first character may not be a digit. Variables can be of any character length you desire.

By default, variables are sorted alphabetically when they are printed next to the truth table. You can change the order by making use of the order instruction anywhere in the file: order c b a;. The order instruction must end with at least one semicolon. You cannot leave out any variables in the instruction. The variables must be separated with spaces.

Working example formulas

Formulas may include as many spaces and line breaks as you want.

a and !(b or !c)

!b

true

(example impl !probablyWorks) xnor !false and u123

a or b

order b a; a or b

What to expect

Pretend we have a file logical.txt with following content:

order b a c;

a and !(b or !c)

Now when we run: pcl logical.txt steps props

We get:

  0 = b or !c
out = a and !0

Valid: False
Satisfiable: True
Invalid: False

[b a c]  a and !(b or !c)
[0 0 0]  0
[0 0 1]  0
[0 1 0]  0
[0 1 1]  1
[1 0 0]  0
[1 0 1]  0
[1 1 0]  0
[1 1 1]  0

As you can see, pcl prints the steps, then the properties of the formula, then the truth table with the correct variable order. It also shows the formula it has just calculated. If you ran the program with a .pcl file, this makes it possible to see what is actually being calculated here.

Known bugs

The program works well if you use the correct syntax. The syntax also isn't really that difficult and it allows for as much whitespace as you need. Nevertheless, I have yet to implement exceptions that catch incorrect syntax, like two variables/binary operators next to each other, negating a binary operator, leaving out brackets, etc. Some of these problems will cause the program to crash, others will result in an infinite loop. None will just continue and pretend nothing happened (to my knowledge).

propositional-calculator's People

Contributors

dereasy avatar

Stargazers

 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.