logic_analyzer 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/logic_analyzer.rb +6 -0
- data/lib/logic_analyzer/boolean.rb +41 -0
- data/lib/logic_analyzer/exception.rb +5 -0
- data/lib/logic_analyzer/logic_evaluation.rb +14 -0
- data/lib/logic_analyzer/logical_context.rb +33 -0
- data/lib/logic_analyzer/proposition.rb +57 -0
- data/lib/logic_analyzer/truth_table.rb +29 -0
- metadata +49 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 93f454e3f8b567810914170b9850e7023993048d27fafa9a83c21be7e7718846
|
|
4
|
+
data.tar.gz: ebf24d0492a3e4b6c4884ad679e3cffe6a69c93cb5d8c742bbad13fbc4f36f5d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e0744be2c9a9558fe72a455381e2bca3a0875a23c9741e5eda2cbb4a09137c24e168bca04e5dbec51f2780ebd0d1142f8b3087f36c8e829ec2eb8e727bc782eb
|
|
7
|
+
data.tar.gz: 9107fb46e8b05a0a3cc8bd84a761a4e805a4bdccbe53f63463046e2674b55844392a3624a088fa46d78ddf411bb6263f4fccf3e7f154946d2323ff24cdd1a6af
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
require_relative 'logic_analyzer/boolean'
|
|
2
|
+
require_relative 'logic_analyzer/exception'
|
|
3
|
+
require_relative 'logic_analyzer/logic_evaluation'
|
|
4
|
+
require_relative 'logic_analyzer/logical_context'
|
|
5
|
+
require_relative 'logic_analyzer/proposition'
|
|
6
|
+
require_relative 'logic_analyzer/truth_table'
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module Boolean
|
|
2
|
+
def and(other)
|
|
3
|
+
self && other
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def or(other)
|
|
7
|
+
self || other
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def xor(other)
|
|
11
|
+
(self && !other) || (other && !self)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def then(other)
|
|
15
|
+
!self || other
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def if_and_only_if(other)
|
|
19
|
+
self.then(other) && other.then(self)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def not(expression = nil)
|
|
23
|
+
if expression.nil?
|
|
24
|
+
!self
|
|
25
|
+
else
|
|
26
|
+
!expression
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
alias > :then
|
|
31
|
+
alias <=> if_and_only_if
|
|
32
|
+
alias != xor
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
class TrueClass
|
|
36
|
+
include Boolean
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
class FalseClass
|
|
40
|
+
include Boolean
|
|
41
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require_relative 'boolean'
|
|
2
|
+
|
|
3
|
+
class LogicalContext
|
|
4
|
+
def connectors
|
|
5
|
+
%i[and or xor then if_and_only_if not]
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def define_premise(premise, value)
|
|
9
|
+
define_singleton_method(premise) do
|
|
10
|
+
value
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def truth_value(sentence)
|
|
15
|
+
eval('lambda { ' + sentence + ' } ').call
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def >(a, b)
|
|
19
|
+
a.then b
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def <=>(a, b)
|
|
23
|
+
a.if_and_only_if b
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def !=(a, b)
|
|
27
|
+
a.xor b
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def not(expression)
|
|
31
|
+
!expression
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require_relative 'logical_context'
|
|
2
|
+
require_relative 'exception'
|
|
3
|
+
require_relative 'truth_table'
|
|
4
|
+
|
|
5
|
+
class Proposition
|
|
6
|
+
attr_accessor :sentence, :variables
|
|
7
|
+
|
|
8
|
+
def initialize(sentence)
|
|
9
|
+
@sentence = syntax_sugars(sentence)
|
|
10
|
+
|
|
11
|
+
@sentence.define_singleton_method(:words) do
|
|
12
|
+
split(/\W+/)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
@sentence.define_singleton_method(:operands) do
|
|
16
|
+
split(/\s|\w|\s/).reject(&:empty?)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def syntax_sugars(sentence)
|
|
21
|
+
sugared = sentence.gsub(/and|&&/, '.and')
|
|
22
|
+
.gsub(/or|\|\|/, '.or')
|
|
23
|
+
.gsub(/then|>/, '.then')
|
|
24
|
+
.gsub(/if_and_only_if|<=>/, '.if_and_only_if')
|
|
25
|
+
.gsub(/xor|!=/, '.xor')
|
|
26
|
+
.gsub(/not/, '!')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def parse
|
|
30
|
+
@variables = sentence.words
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def evaluate
|
|
34
|
+
context = LogicalContext.new
|
|
35
|
+
values = []
|
|
36
|
+
table = TruthTable.new(variables)
|
|
37
|
+
table.generate_table
|
|
38
|
+
|
|
39
|
+
for iteration in 0...amount_of_variables
|
|
40
|
+
variables.each do |var|
|
|
41
|
+
context.define_premise(var, table.truth_value_of(var, iteration))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
values.append(context.truth_value(sentence))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
if values.include?(true) && values.include?(false)
|
|
48
|
+
raise Contingency
|
|
49
|
+
else
|
|
50
|
+
values.send(:all?)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def amount_of_variables
|
|
55
|
+
2**variables.length
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
class TruthTable
|
|
2
|
+
attr_accessor :variables, :iterations, :table
|
|
3
|
+
|
|
4
|
+
def initialize(variables)
|
|
5
|
+
@variables = variables
|
|
6
|
+
@iterations = 2 ** variables.size
|
|
7
|
+
@table = []
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def generate_table
|
|
11
|
+
for iteration in 0...iterations
|
|
12
|
+
row = []
|
|
13
|
+
|
|
14
|
+
for column in (variables.size - 1).downto 0
|
|
15
|
+
value = (iteration / 2 ** column).odd?
|
|
16
|
+
row.append(value)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
table.append(row)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
table
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def truth_value_of(variable, row)
|
|
26
|
+
column = variables.find_index(variable)
|
|
27
|
+
table.at(row).at(column)
|
|
28
|
+
end
|
|
29
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: logic_analyzer
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Alejandro Peralta Bazas
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-01-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Ruby implementation of a propositional logic parser
|
|
14
|
+
email:
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/logic_analyzer.rb
|
|
20
|
+
- lib/logic_analyzer/boolean.rb
|
|
21
|
+
- lib/logic_analyzer/exception.rb
|
|
22
|
+
- lib/logic_analyzer/logic_evaluation.rb
|
|
23
|
+
- lib/logic_analyzer/logical_context.rb
|
|
24
|
+
- lib/logic_analyzer/proposition.rb
|
|
25
|
+
- lib/logic_analyzer/truth_table.rb
|
|
26
|
+
homepage:
|
|
27
|
+
licenses:
|
|
28
|
+
- MIT
|
|
29
|
+
metadata: {}
|
|
30
|
+
post_install_message:
|
|
31
|
+
rdoc_options: []
|
|
32
|
+
require_paths:
|
|
33
|
+
- lib
|
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
requirements: []
|
|
45
|
+
rubygems_version: 3.0.2
|
|
46
|
+
signing_key:
|
|
47
|
+
specification_version: 4
|
|
48
|
+
summary: Propositional Logic Analyzer
|
|
49
|
+
test_files: []
|