rover_prover 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.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.rspec +1 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/Gemfile +8 -0
  6. data/Gemfile.lock +32 -0
  7. data/LICENSE.txt +31 -0
  8. data/README.md +98 -0
  9. data/Rakefile +2 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/exe/rover_prover +5 -0
  13. data/lib/rover_prover/language/expression.rb +33 -0
  14. data/lib/rover_prover/language/formula/and.rb +41 -0
  15. data/lib/rover_prover/language/formula/for_all.rb +44 -0
  16. data/lib/rover_prover/language/formula/formulae.rb +7 -0
  17. data/lib/rover_prover/language/formula/implies.rb +41 -0
  18. data/lib/rover_prover/language/formula/not.rb +39 -0
  19. data/lib/rover_prover/language/formula/or.rb +41 -0
  20. data/lib/rover_prover/language/formula/predicate.rb +64 -0
  21. data/lib/rover_prover/language/formula/there_exists.rb +42 -0
  22. data/lib/rover_prover/language/formula.rb +5 -0
  23. data/lib/rover_prover/language/sequent.rb +126 -0
  24. data/lib/rover_prover/language/term/function.rb +63 -0
  25. data/lib/rover_prover/language/term/terms.rb +3 -0
  26. data/lib/rover_prover/language/term/unification_term.rb +38 -0
  27. data/lib/rover_prover/language/term/variable.rb +40 -0
  28. data/lib/rover_prover/language/term.rb +10 -0
  29. data/lib/rover_prover/language.rb +3 -0
  30. data/lib/rover_prover/processor/commands.rb +32 -0
  31. data/lib/rover_prover/processor/processor.rb +2 -0
  32. data/lib/rover_prover/processor/rover_lexer.rb +23 -0
  33. data/lib/rover_prover/processor/rover_parser.rb +64 -0
  34. data/lib/rover_prover/prover/prover.rb +277 -0
  35. data/lib/rover_prover/prover/unifier.rb +29 -0
  36. data/lib/rover_prover/version.rb +3 -0
  37. data/lib/rover_prover.rb +111 -0
  38. data/logo.png +0 -0
  39. data/rover_prover.gemspec +28 -0
  40. metadata +138 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6a7050bef420f0cc3f96200719297c7f30b5f25c677ce14c678c7b12117a8026
4
+ data.tar.gz: bd3a374772ab8996e65821b575a14b47711c341ade5551eff58241fca424e151
5
+ SHA512:
6
+ metadata.gz: 61deceed57fe3fd5c6c5eed18e6891f3b2ed40656b2d0aff9680ab551384c1877a6f59d53c6ebdc0bceecf1ab6dba6b55f229c3844cd39dc91e29fc3a161a804
7
+ data.tar.gz: 68c79dcc633ddb5b70f9b8303ce151ea82bcf2564b54d02d79f45807935a6ab3285b5c3c657038244abd7b568b53fb0970729faf88bf16214281c226814061df
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at liukoki@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ gem "rspec"
8
+ gem "rltk"
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.3)
5
+ ffi (1.11.1)
6
+ filigree (0.4.1)
7
+ rltk (3.0.1)
8
+ ffi (>= 1.0.0)
9
+ filigree (>= 0.2.0)
10
+ rspec (3.8.0)
11
+ rspec-core (~> 3.8.0)
12
+ rspec-expectations (~> 3.8.0)
13
+ rspec-mocks (~> 3.8.0)
14
+ rspec-core (3.8.0)
15
+ rspec-support (~> 3.8.0)
16
+ rspec-expectations (3.8.3)
17
+ diff-lcs (>= 1.2.0, < 2.0)
18
+ rspec-support (~> 3.8.0)
19
+ rspec-mocks (3.8.0)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-support (3.8.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ rltk
29
+ rspec
30
+
31
+ BUNDLED WITH
32
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,31 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Koki Ryu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
23
+ Copyright (C) 2014 Stephan Boyer
24
+
25
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
26
+
27
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
28
+
29
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
+
31
+
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ ![Rover](./logo.png)
2
+ Rover is an automated theorem prover for first-order logic written in Ruby.It is translation of [theorem-prover](https://github.com/stepchowfun/theorem-prover) from Python.
3
+ For any provable formula, this program is guaranteed to find the proof (eventually). However, as a consequence of the negative answer to Hilbert's *Entscheidungsproblem*, there are some unprovable formulae that will cause this program to loop forever.
4
+
5
+
6
+ To get started, please `gem install rover_prover`:
7
+
8
+ ```
9
+ $ rover_prover
10
+ Rover: First-Order Logic Theorem Prover
11
+ 2019 Koki Ryu
12
+ 2014 Stephan Boyer
13
+ Terms:
14
+
15
+ x (variable)
16
+ f(term, ...) (function)
17
+
18
+ Formulae:
19
+
20
+ P(term) (predicate)
21
+ not P (complement)
22
+ P or Q (disjunction)
23
+ P and Q (conjunction)
24
+ P implies Q (implication)
25
+ forall x. P (universal quantification)
26
+ exists x. P (existential quantification)
27
+
28
+ Enter formulae at the prompt. The following commands are also available for manipulating axioms:
29
+
30
+ axioms (list axioms)
31
+ lemmas (list lemmas)
32
+ axiom <formula> (add an axiom)
33
+ lemma <formula> (prove and add a lemma)
34
+ remove <formula> (remove an axiom or lemma)
35
+ reset (remove all axioms and lemmas)
36
+
37
+ Enter 'exit' command to exit the prompt.
38
+
39
+ Rover>
40
+ ```
41
+
42
+ Example session:
43
+ ```
44
+ Rover> P or not P
45
+ 0. ⊢ (P ∨ ¬P)
46
+ 1. ⊢ P, ¬P
47
+ 2. P ⊢ P
48
+ Formula proven: (P ∨ ¬P)
49
+
50
+ Rover> P and not P
51
+ 0. ⊢ (P ∧ ¬P)
52
+ 1. ⊢ ¬P
53
+ 2. P ⊢
54
+ Formula unprovable: (P ∧ ¬P)
55
+
56
+ Rover> forall x. P(x) implies (Q(x) implies P(x))
57
+ 0. ⊢ (∀x. (P(x) -> (Q(x) -> P(x))))
58
+ 1. ⊢ (P(v1) -> (Q(v1) -> P(v1)))
59
+ 2. P(v1) ⊢ (Q(v1) -> P(v1))
60
+ 3. P(v1), Q(v1) ⊢ P(v1)
61
+ Formula proven: (∀x. (P(x) -> (Q(x) -> P(x))))
62
+
63
+ Rover> exists x. (P(x) implies forall y. P(y))
64
+ 0. ⊢ (∃x. (P(x) -> (∀y. P(y))))
65
+ 1. ⊢ (∃x. (P(x) -> (∀y. P(y)))), (P(t1) -> (∀y. P(y)))
66
+ 2. ⊢ (∃x. (P(x) -> (∀y. P(y)))), (P(t1) -> (∀y. P(y))), (P(t2) -> (∀y. P(y)))
67
+ 3. P(t1) ⊢ (∃x. (P(x) -> (∀y. P(y)))), (P(t2) -> (∀y. P(y))), (∀y. P(y))
68
+ 4. P(t1) ⊢ (∃x. (P(x) -> (∀y. P(y)))), (P(t2) -> (∀y. P(y))), (∀y. P(y)), (P(t3) -> (∀y. P(y)))
69
+ 5. P(t1), P(t2) ⊢ (∃x. (P(x) -> (∀y. P(y)))), (∀y. P(y)), (P(t3) -> (∀y. P(y))), (∀y. P(y))
70
+ 6. P(t1), P(t2) ⊢ (∃x. (P(x) -> (∀y. P(y)))), (P(t3) -> (∀y. P(y))), (∀y. P(y)), P(v1)
71
+ 7. P(t1), P(t2) ⊢ (∃x. (P(x) -> (∀y. P(y)))), (P(t3) -> (∀y. P(y))), (∀y. P(y)), P(v1), (P(t4) -> (∀y. P(y)))
72
+ 8. P(t1), P(t2), P(t3) ⊢ (∃x. (P(x) -> (∀y. P(y)))), (∀y. P(y)), P(v1), (P(t4) -> (∀y. P(y))), (∀y. P(y))
73
+ 9. P(t1), P(t2), P(t3) ⊢ (∃x. (P(x) -> (∀y. P(y)))), P(v1), (P(t4) -> (∀y. P(y))), (∀y. P(y)), P(v2)
74
+ 10. P(t1), P(t2), P(t3) ⊢ (∃x. (P(x) -> (∀y. P(y)))), P(v1), (P(t4) -> (∀y. P(y))), (∀y. P(y)), P(v2), (P(t5) -> (∀y. P(y)))
75
+ 11. P(t1), P(t2), P(t3), P(t4) ⊢ (∃x. (P(x) -> (∀y. P(y)))), P(v1), (∀y. P(y)), P(v2), (P(t5) -> (∀y. P(y))), (∀y. P(y))
76
+ t4 = v1
77
+ Formula proven: (∃x. (P(x) -> (∀y. P(y))))
78
+
79
+ Rover> axiom forall x. Equals(x, x)
80
+ Axiom added: (∀x. Equals(x, x))
81
+
82
+ Rover> axioms
83
+ (∀x. Equals(x, x))
84
+
85
+ Rover> lemma Equals(a, a)
86
+ 0. (∀x. Equals(x, x)) ⊢ Equals(a, a)
87
+ 1. (∀x. Equals(x, x)), Equals(t1, t1) ⊢ Equals(a, a)
88
+ t1 = a
89
+ Lemma proven: Equals(a, a)
90
+
91
+ Rover> lemmas
92
+ Equals(a, a)
93
+
94
+ Rover> remove forall x. Equals(x, x)
95
+ Axiom removed: (∀x. Equals(x, x))
96
+ Dependent axioms are also removed:
97
+ Equals(a, a)
98
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rover_prover"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/rover_prover ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/rover_prover.rb'
4
+
5
+ RoverProver::Main.run
@@ -0,0 +1,33 @@
1
+ module Expression
2
+ def free_variables
3
+ raise NotImplementedError
4
+ end
5
+
6
+ def free_unification_terms
7
+ raise NotImplementedError
8
+ end
9
+
10
+ def replace(old, new)
11
+ raise NotImplementedError
12
+ end
13
+
14
+ def occurs(unification_term)
15
+ raise NotImplementedError
16
+ end
17
+
18
+ def to_s
19
+ raise NotImplementedError
20
+ end
21
+
22
+ def set_instantiation_time(time)
23
+ raise NotImplementedError
24
+ end
25
+
26
+ def eql?(term)
27
+ raise NotImplementedError
28
+ end
29
+
30
+ def unify(term)
31
+ nil
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ require_relative '../formula.rb'
2
+
3
+ class And < Formula
4
+ attr_reader :formula_a, :formula_b
5
+
6
+ def initialize(formula_a, formula_b)
7
+ @formula_a = formula_a
8
+ @formula_b = formula_b
9
+ end
10
+
11
+ def free_variables
12
+ @formula_a.free_variables | @formula_b.free_variables
13
+ end
14
+
15
+ def free_unification_terms
16
+ @formula_a.free_unification_terms | @formula_b.free_unification_terms
17
+ end
18
+
19
+ def replace(old, new)
20
+ return new if eql?(old)
21
+ And.new(@formula_a.replace(old, new), @formula_b.replace(old, new))
22
+ end
23
+
24
+ def occurs(unification_term)
25
+ @formula_a.occurs(unification_term) || @formula_b.occurs(unification_term)
26
+ end
27
+
28
+ def set_instantiation_time(time)
29
+ @formula_a.set_instantiation_time(time)
30
+ @formula_b.set_instantiation_time(time)
31
+ end
32
+
33
+ def to_s
34
+ "(#{@formula_a.to_s} ∧ #{@formula_b.to_s})"
35
+ end
36
+
37
+ def eql?(exp)
38
+ return false unless exp.is_a?(And)
39
+ @formula_a.eql?(exp.formula_a) && @formula_b.eql?(exp.formula_b)
40
+ end
41
+ end
@@ -0,0 +1,44 @@
1
+ require_relative '../formula.rb'
2
+
3
+ class ForAll < Formula
4
+ attr_reader :variable, :formula
5
+
6
+ def initialize(variable, formula)
7
+ @variable = variable
8
+ @formula = formula
9
+ end
10
+
11
+ def free_variables
12
+ @formula.free_variables.reject{ |var| var.eql?(@variable) }
13
+ end
14
+
15
+ def free_unification_terms
16
+ @formula.free_unification_terms
17
+ end
18
+
19
+ def replace(old, new)
20
+ return new if eql?(old)
21
+ ForAll.new(
22
+ @variable.replace(old, new),
23
+ @formula.replace(old, new)
24
+ )
25
+ end
26
+
27
+ def occurs(unification_term)
28
+ @formula.occurs(unification_term)
29
+ end
30
+
31
+ def set_instantiation_time(time)
32
+ @variable.set_instantiation_time(time)
33
+ @formula.set_instantiation_time(time)
34
+ end
35
+
36
+ def to_s
37
+ "(∀#{@variable.to_s}. #{@formula.to_s})"
38
+ end
39
+
40
+ def eql?(exp)
41
+ return false unless exp.is_a?(ForAll)
42
+ @variable.eql?(exp.variable) && @formula.eql?(exp.formula)
43
+ end
44
+ end
@@ -0,0 +1,7 @@
1
+ require_relative 'and.rb'
2
+ require_relative 'implies.rb'
3
+ require_relative 'or.rb'
4
+ require_relative 'there_exists.rb'
5
+ require_relative 'for_all.rb'
6
+ require_relative 'not.rb'
7
+ require_relative 'predicate.rb'
@@ -0,0 +1,41 @@
1
+ require_relative '../formula.rb'
2
+
3
+ class Implies < Formula
4
+ attr_reader :formula_a, :formula_b
5
+
6
+ def initialize(formula_a, formula_b)
7
+ @formula_a = formula_a
8
+ @formula_b = formula_b
9
+ end
10
+
11
+ def free_variables
12
+ @formula_a.free_variables | @formula_b.free_variables
13
+ end
14
+
15
+ def free_unification_terms
16
+ @formula_a.free_unification_terms | @formula_b.free_unification_terms
17
+ end
18
+
19
+ def replace(old, new)
20
+ return new if eql?(old)
21
+ Implies.new(@formula_a.replace(old, new), @formula_b.replace(old, new))
22
+ end
23
+
24
+ def occurs(unification_term)
25
+ @formula_a.occurs(unification_term) || @formula_b.occurs(unification_term)
26
+ end
27
+
28
+ def set_instantiation_time(time)
29
+ @formula_a.set_instantiation_time(time)
30
+ @formula_b.set_instantiation_time(time)
31
+ end
32
+
33
+ def to_s
34
+ "(#{@formula_a.to_s} -> #{@formula_b.to_s})"
35
+ end
36
+
37
+ def eql?(exp)
38
+ return false unless exp.is_a?(Implies)
39
+ @formula_a.eql?(exp.formula_a) && @formula_b.eql?(exp.formula_b)
40
+ end
41
+ end
@@ -0,0 +1,39 @@
1
+ require_relative '../formula.rb'
2
+
3
+ class Not < Formula
4
+ attr_reader :formula
5
+
6
+ def initialize(formula)
7
+ @formula = formula
8
+ end
9
+
10
+ def free_variables
11
+ @formula.free_variables
12
+ end
13
+
14
+ def free_unification_terms
15
+ @formula.free_unification_terms
16
+ end
17
+
18
+ def replace(old, new)
19
+ return new if eql?(old)
20
+ Not.new(@formula.replace(old, new))
21
+ end
22
+
23
+ def set_instantiation_time(time)
24
+ @formula.set_instantiation_time(time)
25
+ end
26
+
27
+ def occurs(unification_term)
28
+ @formula.occurs(unification_term)
29
+ end
30
+
31
+ def to_s
32
+ "¬#{@formula.to_s}"
33
+ end
34
+
35
+ def eql?(exp)
36
+ return false unless exp.is_a?(Not)
37
+ @formula.eql?(exp.formula)
38
+ end
39
+ end
@@ -0,0 +1,41 @@
1
+ require_relative '../formula.rb'
2
+
3
+ class Or < Formula
4
+ attr_reader :formula_a, :formula_b
5
+
6
+ def initialize(formula_a, formula_b)
7
+ @formula_a = formula_a
8
+ @formula_b = formula_b
9
+ end
10
+
11
+ def free_variables
12
+ @formula_a.free_variables | @formula_b.free_variables
13
+ end
14
+
15
+ def free_unification_terms
16
+ @formula_a.free_unification_terms | @formula_b.free_unification_terms
17
+ end
18
+
19
+ def replace(old, new)
20
+ return new if eql?(old)
21
+ Or.new(@formula_a.replace(old, new), @formula_b.replace(old, new))
22
+ end
23
+
24
+ def occurs(unification_term)
25
+ @formula_a.occurs(unification_term) || @formula_b.occurs(unification_term)
26
+ end
27
+
28
+ def set_instantiation_time(time)
29
+ @formula_a.set_instantiation_time(time)
30
+ @formula_b.set_instantiation_time(time)
31
+ end
32
+
33
+ def to_s
34
+ "(#{@formula_a.to_s} ∨ #{@formula_b.to_s})"
35
+ end
36
+
37
+ def eql?(exp)
38
+ return false unless exp.is_a?(Or)
39
+ @formula_a.eql?(exp.formula_a) && @formula_b.eql?(exp.formula_b)
40
+ end
41
+ end
@@ -0,0 +1,64 @@
1
+ require_relative '../formula.rb'
2
+
3
+ class Predicate < Formula
4
+ attr_reader :name, :terms
5
+
6
+ def initialize(name, terms)
7
+ @name = name
8
+ @terms = terms
9
+ end
10
+
11
+ def free_variables
12
+ @terms.map(&:free_variables).flatten.uniq
13
+ end
14
+
15
+ def free_unification_terms
16
+ @terms.map(&:free_unification_terms).flatten.uniq
17
+ end
18
+
19
+ def replace(old, new)
20
+ return new if eql?(old)
21
+ Predicate.new(@name, @terms.map { |term| term.replace(old, new) })
22
+ end
23
+
24
+ def set_instantiation_time(time)
25
+ @terms.each{ |term| term.set_instantiation_time(time) }
26
+ end
27
+
28
+ def occurs(unification_term)
29
+ @terms.any? { |term| term.occurs(unification_term) }
30
+ end
31
+
32
+ def to_s
33
+ terms_s = @terms.empty? ? "" : "(#{@terms.map(&:to_s).join(', ')})"
34
+ "#{@name}#{terms_s}"
35
+ end
36
+
37
+ def eql?(exp)
38
+ return false unless exp.is_a?(Predicate)
39
+ return false unless @name == exp.name
40
+ return false unless @terms.length == exp.terms.length
41
+ @terms.zip(exp.terms).all? { |t1, t2| t1.eql?(t2) }
42
+ end
43
+
44
+ def unify(term)
45
+ return term.unify(self) if term.is_a?(UnificationTerm)
46
+ return nil unless term.is_a?(Predicate)
47
+ return nil unless @name == term.name
48
+ return nil unless @terms.length == term.terms.length
49
+ term_pair = @terms.zip(term.terms)
50
+ subst = {}
51
+
52
+ term_pair.each do |term_a, term_b|
53
+ subst.each do |k, v|
54
+ term_a = term_a.replace(k, v)
55
+ term_b = term_b.replace(k, v)
56
+ end
57
+ sub = term_a.unify(term_b)
58
+ return nil if sub == nil
59
+ subst.merge!(sub)
60
+ end
61
+ subst
62
+ end
63
+
64
+ end
@@ -0,0 +1,42 @@
1
+ class ThereExists < Formula
2
+ attr_reader :variable, :formula
3
+
4
+ def initialize(variable, formula)
5
+ @variable = variable
6
+ @formula = formula
7
+ end
8
+
9
+ def free_variables
10
+ @formula.free_variables.reject{ |var| var.eql?(@variable) }
11
+ end
12
+
13
+ def free_unification_terms
14
+ @formula.free_unification_terms
15
+ end
16
+
17
+ def replace(old, new)
18
+ return new if eql?(old)
19
+ ThereExists.new(
20
+ @variable.replace(old, new),
21
+ @formula.replace(old, new)
22
+ )
23
+ end
24
+
25
+ def occurs(unification_term)
26
+ @formula.occurs(unification_term)
27
+ end
28
+
29
+ def set_instantiation_time(time)
30
+ @variable.set_instantiation_time(time)
31
+ @formula.set_instantiation_time(time)
32
+ end
33
+
34
+ def to_s
35
+ "(∃#{@variable.to_s}. #{@formula.to_s})"
36
+ end
37
+
38
+ def eql?(exp)
39
+ return false unless exp.is_a?(ThereExists)
40
+ @variable.eql?(exp.variable) && @formula.eql?(exp.formula)
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'expression.rb'
2
+
3
+ class Formula
4
+ include Expression
5
+ end