Last active
September 2, 2020 18:15
-
-
Save adolfont/b7f82b0c92b9718ffcdeea5372476e03 to your computer and use it in GitHub Desktop.
Syntax checker for Classical Propositional Logic in Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Syntax checker for Classical Propositional Logic | |
# Examples: | |
# atomic formulas: | |
# :p | |
# :q | |
# :r | |
# negation: | |
# [:not, :p] | |
# [:not, [:not, :q]]] | |
# conjunction: | |
# [:p, :and, :q] | |
# [:p, :and, [:q, :and, :r]] | |
# disjunction: | |
# [:p, :or, :q] | |
# [:p, :or, [:q, :or, :r]] | |
# implication: | |
# [:p, :implies, :q] | |
# [:p, :implies, [:q, :implies, :r]] | |
defmodule Syntax do | |
def check([:not, formula]) do | |
check(formula) | |
end | |
def check([left, connective, right]) do | |
if connective==:and or connective==:or or connective==:implies do | |
check(left) and check(right) | |
else | |
false | |
end | |
end | |
def check(atom) do | |
is_atom(atom) and not (atomo==:and or atomo==:or or atomo==:implies or atomo==:not) | |
end | |
end | |
{formula, _} = Code.eval_string IO.gets "Type a formula: " | |
if Syntax.check(formula) do | |
IO.puts "The syntax is correct!" | |
else | |
IO.puts "The syntax is not correct." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read this https://medium.com/@adolfont/classical-propositional-logic-in-elixir-bd18ceff2c93#:~:text=In%20Classical%20Propositional%20Logic%20there,Elixir%20is%20a%20programming%20language.