logical 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bb55f971f918847f159a6a7fdcc67f17c51c6736d73dc2ee091db074434325fc
4
+ data.tar.gz: 9ba1f2d2899cbc7a727d30b1da242dfdd33d894fc11ca1c7ed5ecfc6f1a5223d
5
+ SHA512:
6
+ metadata.gz: 0b1a3b9c292a16e9ac176260b34ec45b79ca1efaf74aaaaa1ab365c9cbe20e56e6a9ce7a62cf38bc88ca942a7beb23b28100784e9f0c8d2703616f830858810f
7
+ data.tar.gz: cb983b292c5cb3bcd2efd41243be84975a26ffc8f16389de81a32ce6c4cdff88d783d6343fc5f2b3b1ff0bb9aa5341785deb222c594644a2c027487d37c7f364
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TruthTable
4
+ attr_reader :evaluations
5
+
6
+ def initialize(formula)
7
+ @formula = formula
8
+ letters = formula.literals.map(&:letter)
9
+ @evaluations = Interpretation.all(letters).map { |i| Evaluation.new(i, formula) }
10
+ end
11
+
12
+ def valid?
13
+ @evaluations.map(&:value).all?
14
+ end
15
+
16
+ def satisfied?
17
+ @evaluations.map(&:value).any?
18
+ end
19
+
20
+ def invalid?
21
+ !satisfied?
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Evaluation
4
+ attr_reader :interpretation, :formula, :value
5
+
6
+ def initialize(interpretation, formula)
7
+ @interpretation = interpretation
8
+ @formula = formula
9
+ @value = formula.evaluate(interpretation)
10
+ end
11
+
12
+ def to_s
13
+ "#{interpretation.to_s} ⇒ #{formula.to_s} = #{value}"
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Conjunction
4
+ def initialize(f, g)
5
+ @f = f
6
+ @g = g
7
+ end
8
+
9
+ def evaluate(interpretation)
10
+ @f.evaluate(interpretation) && @g.evaluate(interpretation)
11
+ end
12
+
13
+ def literals
14
+ @f.literals | @g.literals
15
+ end
16
+
17
+ def to_s
18
+ "(#{@f.to_s}∧#{@g.to_s})"
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Disjunction
4
+ def initialize(f, g)
5
+ @f = f
6
+ @g = g
7
+ end
8
+
9
+ def evaluate(interpretation)
10
+ @f.evaluate(interpretation) || @g.evaluate(interpretation)
11
+ end
12
+
13
+ def literals
14
+ @f.literals | @g.literals
15
+ end
16
+
17
+ def to_s
18
+ "(#{@f.to_s}∨#{@g.to_s})"
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Implication
4
+ def initialize(f, g)
5
+ @f = f
6
+ @g = g
7
+ end
8
+
9
+ def evaluate(interpretation)
10
+ # Since F → G ≡ ¬F ∨ G
11
+ !(@f.evaluate(interpretation)) || @g.evaluate(interpretation)
12
+ end
13
+
14
+ def literals
15
+ @f.literals | @g.literals
16
+ end
17
+
18
+ def to_s
19
+ "(#{@f.to_s}→#{@g.to_s})"
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Literal
4
+ attr_reader :letter
5
+
6
+ def initialize(letter)
7
+ @letter = letter
8
+ end
9
+
10
+ def evaluate(interpretation)
11
+ interpretation.evaluate(@letter)
12
+ end
13
+
14
+ def to_s
15
+ @letter
16
+ end
17
+
18
+ def literals
19
+ Set[self]
20
+ end
21
+
22
+ def ==(other)
23
+ other.class == Literal && other.letter == letter
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Negation
4
+ def initialize(f)
5
+ @f = f
6
+ end
7
+
8
+ def evaluate(interpretation)
9
+ !(@f.evaluate(interpretation))
10
+ end
11
+
12
+ def to_s
13
+ "¬(#{@f.to_s})"
14
+ end
15
+
16
+ def literals
17
+ @f.literals
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Interpretation
4
+ def initialize(hash)
5
+ @hash = hash
6
+ end
7
+
8
+ def evaluate(letter)
9
+ @hash[letter]
10
+ end
11
+
12
+ def to_s
13
+ @hash.to_s
14
+ end
15
+
16
+ def self.all(letters)
17
+ [true, false]
18
+ .repeated_permutation(letters.size)
19
+ .map do |disposition|
20
+ mapping = {}
21
+ letters.each_with_index do |letter, i|
22
+ mapping[letter] = disposition[i]
23
+ end
24
+ Interpretation.new(mapping)
25
+ end
26
+ end
27
+ end
data/lib/logical.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logical
4
+ require 'logical/interpretation'
5
+ require 'logical/evaluation'
6
+
7
+ require 'logical/algorithms/truth_table'
8
+
9
+ require 'logical/formulas/conjunction'
10
+ require 'logical/formulas/disjunction'
11
+ require 'logical/formulas/implication'
12
+ require 'logical/formulas/negation'
13
+ require 'logical/formulas/literal'
14
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logical
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luka Giga
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.8'
27
+ description: ''
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/logical.rb
34
+ - lib/logical/algorithms/truth_table.rb
35
+ - lib/logical/evaluation.rb
36
+ - lib/logical/formulas/conjunction.rb
37
+ - lib/logical/formulas/disjunction.rb
38
+ - lib/logical/formulas/implication.rb
39
+ - lib/logical/formulas/literal.rb
40
+ - lib/logical/formulas/negation.rb
41
+ - lib/logical/interpretation.rb
42
+ homepage: https://github.com/lgig/logical
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.4.6
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: A silly Ruby implementation of some mathematical logic concepts.
65
+ test_files: []