mini_kraken 0.2.04 → 0.3.00
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +16 -16
- data/lib/mini_kraken/atomic/all_atomic.rb +1 -0
- data/lib/mini_kraken/atomic/atomic_term.rb +32 -17
- data/lib/mini_kraken/atomic/k_integer.rb +0 -4
- data/lib/mini_kraken/atomic/k_string.rb +17 -0
- data/lib/mini_kraken/atomic/k_symbol.rb +0 -6
- data/lib/mini_kraken/composite/all_composite.rb +4 -0
- data/lib/mini_kraken/composite/composite_term.rb +2 -18
- data/lib/mini_kraken/composite/cons_cell.rb +178 -11
- data/lib/mini_kraken/composite/cons_cell_visitor.rb +12 -64
- data/lib/mini_kraken/composite/list.rb +32 -0
- data/lib/mini_kraken/core/all_core.rb +8 -0
- data/lib/mini_kraken/core/any_value.rb +31 -7
- data/lib/mini_kraken/core/arity.rb +69 -0
- data/lib/mini_kraken/core/association.rb +29 -4
- data/lib/mini_kraken/core/association_copy.rb +50 -0
- data/lib/mini_kraken/core/base_term.rb +13 -0
- data/lib/mini_kraken/core/blackboard.rb +315 -0
- data/lib/mini_kraken/core/bookmark.rb +46 -0
- data/lib/mini_kraken/core/context.rb +624 -0
- data/lib/mini_kraken/core/duck_fiber.rb +21 -19
- data/lib/mini_kraken/core/entry.rb +40 -0
- data/lib/mini_kraken/core/fail.rb +20 -18
- data/lib/mini_kraken/core/fusion.rb +29 -0
- data/lib/mini_kraken/core/goal.rb +20 -29
- data/lib/mini_kraken/core/log_var.rb +4 -30
- data/lib/mini_kraken/core/log_var_ref.rb +72 -48
- data/lib/mini_kraken/core/nullary_relation.rb +2 -9
- data/lib/mini_kraken/core/parametrized_term.rb +61 -0
- data/lib/mini_kraken/core/relation.rb +14 -28
- data/lib/mini_kraken/core/scope.rb +67 -0
- data/lib/mini_kraken/core/solver_adapter.rb +58 -0
- data/lib/mini_kraken/core/specification.rb +48 -0
- data/lib/mini_kraken/core/succeed.rb +21 -17
- data/lib/mini_kraken/core/symbol_table.rb +137 -0
- data/lib/mini_kraken/core/term.rb +15 -4
- data/lib/mini_kraken/glue/dsl.rb +35 -69
- data/lib/mini_kraken/glue/run_star_expression.rb +28 -30
- data/lib/mini_kraken/rela/all_rela.rb +8 -0
- data/lib/mini_kraken/rela/binary_relation.rb +30 -0
- data/lib/mini_kraken/rela/conde.rb +146 -0
- data/lib/mini_kraken/rela/conj2.rb +65 -0
- data/lib/mini_kraken/rela/def_relation.rb +64 -0
- data/lib/mini_kraken/rela/disj2.rb +70 -0
- data/lib/mini_kraken/rela/fresh.rb +98 -0
- data/lib/mini_kraken/{core → rela}/goal_relation.rb +6 -8
- data/lib/mini_kraken/rela/unify.rb +258 -0
- data/lib/mini_kraken/version.rb +1 -1
- data/spec/atomic/atomic_term_spec.rb +23 -20
- data/spec/atomic/k_symbol_spec.rb +0 -5
- data/spec/composite/cons_cell_spec.rb +116 -0
- data/spec/composite/cons_cell_visitor_spec.rb +16 -3
- data/spec/composite/list_spec.rb +50 -0
- data/spec/core/any_value_spec.rb +52 -0
- data/spec/core/arity_spec.rb +91 -0
- data/spec/core/association_copy_spec.rb +69 -0
- data/spec/core/association_spec.rb +25 -0
- data/spec/core/blackboard_spec.rb +287 -0
- data/spec/core/bookmark_spec.rb +40 -0
- data/spec/core/context_spec.rb +221 -0
- data/spec/core/core_spec.rb +40 -0
- data/spec/core/duck_fiber_spec.rb +22 -46
- data/spec/core/fail_spec.rb +5 -6
- data/spec/core/goal_spec.rb +20 -11
- data/spec/core/log_var_ref_spec.rb +80 -5
- data/spec/core/log_var_spec.rb +35 -6
- data/spec/core/nullary_relation_spec.rb +33 -0
- data/spec/core/parametrized_tem_spec.rb +39 -0
- data/spec/core/relation_spec.rb +33 -0
- data/spec/core/scope_spec.rb +73 -0
- data/spec/core/solver_adapter_spec.rb +70 -0
- data/spec/core/specification_spec.rb +43 -0
- data/spec/core/succeed_spec.rb +5 -5
- data/spec/core/symbol_table_spec.rb +142 -0
- data/spec/glue/dsl_chap1_spec.rb +88 -99
- data/spec/glue/dsl_chap2_spec.rb +59 -41
- data/spec/glue/run_star_expression_spec.rb +69 -896
- data/spec/{core → rela}/conde_spec.rb +50 -46
- data/spec/rela/conj2_spec.rb +123 -0
- data/spec/rela/def_relation_spec.rb +119 -0
- data/spec/rela/disj2_spec.rb +117 -0
- data/spec/rela/fresh_spec.rb +147 -0
- data/spec/rela/unify_spec.rb +369 -0
- data/spec/support/factory_atomic.rb +7 -0
- data/spec/support/factory_composite.rb +21 -0
- metadata +71 -48
- data/lib/mini_kraken/core/association_walker.rb +0 -183
- data/lib/mini_kraken/core/base_arg.rb +0 -10
- data/lib/mini_kraken/core/binary_relation.rb +0 -63
- data/lib/mini_kraken/core/composite_goal.rb +0 -46
- data/lib/mini_kraken/core/conde.rb +0 -143
- data/lib/mini_kraken/core/conj2.rb +0 -79
- data/lib/mini_kraken/core/def_relation.rb +0 -53
- data/lib/mini_kraken/core/designation.rb +0 -55
- data/lib/mini_kraken/core/disj2.rb +0 -72
- data/lib/mini_kraken/core/environment.rb +0 -73
- data/lib/mini_kraken/core/equals.rb +0 -191
- data/lib/mini_kraken/core/formal_arg.rb +0 -22
- data/lib/mini_kraken/core/formal_ref.rb +0 -25
- data/lib/mini_kraken/core/freshness.rb +0 -45
- data/lib/mini_kraken/core/goal_arg.rb +0 -12
- data/lib/mini_kraken/core/goal_template.rb +0 -102
- data/lib/mini_kraken/core/outcome.rb +0 -63
- data/lib/mini_kraken/core/tap.rb +0 -46
- data/lib/mini_kraken/core/vocabulary.rb +0 -446
- data/lib/mini_kraken/glue/fresh_env.rb +0 -108
- data/lib/mini_kraken/glue/fresh_env_factory.rb +0 -83
- data/spec/core/association_walker_spec.rb +0 -194
- data/spec/core/conj2_spec.rb +0 -116
- data/spec/core/def_relation_spec.rb +0 -99
- data/spec/core/disj2_spec.rb +0 -100
- data/spec/core/environment_spec.rb +0 -144
- data/spec/core/equals_spec.rb +0 -319
- data/spec/core/goal_template_spec.rb +0 -74
- data/spec/core/outcome_spec.rb +0 -56
- data/spec/core/vocabulary_spec.rb +0 -220
- data/spec/glue/fresh_env_factory_spec.rb +0 -99
- data/spec/glue/fresh_env_spec.rb +0 -62
@@ -1,99 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../spec_helper' # Use the RSpec framework
|
4
|
-
require_relative '../support/factory_atomic'
|
5
|
-
require_relative '../../lib/mini_kraken/core/disj2'
|
6
|
-
require_relative '../../lib/mini_kraken/core/equals'
|
7
|
-
require_relative '../../lib/mini_kraken/core/formal_arg'
|
8
|
-
require_relative '../../lib/mini_kraken/core/formal_ref'
|
9
|
-
require_relative '../../lib/mini_kraken/core/goal'
|
10
|
-
require_relative '../../lib/mini_kraken/core/goal_template'
|
11
|
-
require_relative '../../lib/mini_kraken/core/log_var_ref'
|
12
|
-
require_relative '../../lib/mini_kraken/core/environment'
|
13
|
-
|
14
|
-
# Load the class under test
|
15
|
-
require_relative '../../lib/mini_kraken/core/def_relation'
|
16
|
-
|
17
|
-
module MiniKraken
|
18
|
-
module Core
|
19
|
-
describe DefRelation do
|
20
|
-
include MiniKraken::FactoryAtomic # Use mix-in module
|
21
|
-
|
22
|
-
# (defrel (teao t) (== 'tea t))
|
23
|
-
let(:tea) { k_symbol(:tea) }
|
24
|
-
let(:formal_t) { FormalArg.new('t') }
|
25
|
-
let(:t_ref) { FormalRef.new('t') }
|
26
|
-
let(:equals_tea) { GoalTemplate.new(Equals.instance, [tea, t_ref]) }
|
27
|
-
subject { DefRelation.new('teao', equals_tea, [formal_t]) }
|
28
|
-
|
29
|
-
context 'Initialization:' do
|
30
|
-
it 'should be initialized with a name, a goal template, formal args' do
|
31
|
-
expect { DefRelation.new('teao', equals_tea, [formal_t]) }.not_to raise_error
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'should know its name' do
|
35
|
-
expect(subject.name).to eq('teao')
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'should know its goal template' do
|
39
|
-
expect(subject.goal_template).to eq(equals_tea)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should know its formals' do
|
43
|
-
expect(subject.formals).to eq([formal_t])
|
44
|
-
end
|
45
|
-
end # context
|
46
|
-
|
47
|
-
context 'Provided services:' do
|
48
|
-
let(:cup) { Atomic::KSymbol.new(:cup) }
|
49
|
-
let(:ref_x) { LogVarRef.new('x') }
|
50
|
-
let(:equals_cup) { GoalTemplate.new(Equals.instance, [cup, t_ref]) }
|
51
|
-
let(:g_template) { GoalTemplate.new(Disj2.instance, [equals_tea, equals_cup]) }
|
52
|
-
subject { DefRelation.new('teacup', g_template, [formal_t]) }
|
53
|
-
let(:env) { Environment.new }
|
54
|
-
|
55
|
-
it 'should provide solver for a single-node goal without var ref' do
|
56
|
-
defrel = DefRelation.new('teao', equals_tea, [formal_t])
|
57
|
-
solver = defrel.solver_for([tea], env)
|
58
|
-
outcome = solver.resume
|
59
|
-
expect(outcome).to be_success
|
60
|
-
outcome = solver.resume
|
61
|
-
expect(outcome).to be_nil
|
62
|
-
|
63
|
-
solver = defrel.solver_for([cup], env)
|
64
|
-
outcome = solver.resume
|
65
|
-
expect(outcome).not_to be_success
|
66
|
-
outcome = solver.resume
|
67
|
-
expect(outcome).to be_nil
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'should provide solver for a single-node goal' do
|
71
|
-
defrel = DefRelation.new('teao', equals_tea, [formal_t])
|
72
|
-
env.add_var(LogVar.new('x'))
|
73
|
-
solver = defrel.solver_for([ref_x], env)
|
74
|
-
outcome = solver.resume
|
75
|
-
expect(outcome).to be_success
|
76
|
-
expect(ref_x.value(outcome)).to eq(tea)
|
77
|
-
|
78
|
-
outcome = solver.resume
|
79
|
-
expect(outcome).to be_nil
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'should provide solver for a single-node goal' do
|
83
|
-
env.add_var(LogVar.new('x'))
|
84
|
-
solver = subject.solver_for([ref_x], env)
|
85
|
-
outcome = solver.resume
|
86
|
-
expect(outcome).to be_success
|
87
|
-
expect(ref_x.value(outcome)).to eq(tea)
|
88
|
-
|
89
|
-
outcome = solver.resume
|
90
|
-
expect(outcome).to be_success
|
91
|
-
expect(ref_x.value(outcome)).to eq(cup)
|
92
|
-
|
93
|
-
outcome = solver.resume
|
94
|
-
expect(outcome).to be_nil
|
95
|
-
end
|
96
|
-
end # context
|
97
|
-
end # describe
|
98
|
-
end # module
|
99
|
-
end # module
|
data/spec/core/disj2_spec.rb
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../spec_helper' # Use the RSpec framework
|
4
|
-
require_relative '../support/factory_atomic'
|
5
|
-
require_relative '../../lib/mini_kraken/core/fail'
|
6
|
-
require_relative '../../lib/mini_kraken/core/succeed'
|
7
|
-
require_relative '../../lib/mini_kraken/core/equals'
|
8
|
-
require_relative '../../lib/mini_kraken/core/environment'
|
9
|
-
require_relative '../../lib/mini_kraken/core/log_var'
|
10
|
-
require_relative '../../lib/mini_kraken/core/log_var_ref'
|
11
|
-
|
12
|
-
# Load the class under test
|
13
|
-
require_relative '../../lib/mini_kraken/core/disj2'
|
14
|
-
|
15
|
-
module MiniKraken
|
16
|
-
module Core
|
17
|
-
describe Disj2 do
|
18
|
-
include MiniKraken::FactoryAtomic # Use mix-in module
|
19
|
-
subject { Disj2.instance }
|
20
|
-
|
21
|
-
context 'Initialization:' do
|
22
|
-
it 'should be initialized without argument' do
|
23
|
-
expect { Disj2.instance }.not_to raise_error
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'should know its name' do
|
27
|
-
expect(subject.name).to eq('disj2')
|
28
|
-
end
|
29
|
-
end # context
|
30
|
-
|
31
|
-
context 'Provided services:' do
|
32
|
-
let(:corn) { k_symbol(:corn) }
|
33
|
-
let(:meal) { k_symbol(:meal) }
|
34
|
-
let(:oil) { k_symbol(:oil) }
|
35
|
-
let(:olive) { k_symbol(:olive) }
|
36
|
-
let(:pea) { k_symbol(:pea) }
|
37
|
-
let(:fails) { Goal.new(Fail.instance, []) }
|
38
|
-
let(:succeeds) { Goal.new(Succeed.instance, []) }
|
39
|
-
let(:var_q) { LogVar.new('q') }
|
40
|
-
let(:ref_q) { LogVarRef.new('q') }
|
41
|
-
let(:env) do
|
42
|
-
e = Environment.new
|
43
|
-
e.add_var(var_q)
|
44
|
-
e
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'should complain when one of its argument is not a goal' do
|
48
|
-
err = StandardError
|
49
|
-
expect { subject.solver_for([succeeds, pea], env) }.to raise_error(err)
|
50
|
-
expect { subject.solver_for([pea, succeeds], env) }.to raise_error(err)
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'should fails if both arguments fail' do
|
54
|
-
# Covers frame 1:55
|
55
|
-
solver = subject.solver_for([fails, fails], env)
|
56
|
-
expect(solver.resume).not_to be_success
|
57
|
-
expect(solver.resume).to be_nil
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'yield success if first argument succeeds' do
|
61
|
-
# Covers frame 1:56
|
62
|
-
subgoal = Goal.new(Equals.instance, [olive, ref_q])
|
63
|
-
solver = subject.solver_for([subgoal, fails], env)
|
64
|
-
outcome = solver.resume
|
65
|
-
expect(outcome).to be_success
|
66
|
-
expect(outcome.associations['q'].first.value).to eq(olive)
|
67
|
-
expect(solver.resume).to be_nil
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'yield success if second argument succeeds' do
|
71
|
-
# Covers frame 1:57
|
72
|
-
subgoal = Goal.new(Equals.instance, [oil, ref_q])
|
73
|
-
solver = subject.solver_for([fails, subgoal], env)
|
74
|
-
outcome = solver.resume
|
75
|
-
expect(outcome).to be_success
|
76
|
-
expect(outcome.associations['q'].first.value).to eq(oil)
|
77
|
-
expect(solver.resume).to be_nil
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'yield two solutions if both arguments succeed' do
|
81
|
-
# Covers frame 1:58
|
82
|
-
subgoal1 = Goal.new(Equals.instance, [olive, ref_q])
|
83
|
-
subgoal2 = Goal.new(Equals.instance, [oil, ref_q])
|
84
|
-
solver = subject.solver_for([subgoal1, subgoal2], env)
|
85
|
-
|
86
|
-
# First solution
|
87
|
-
outcome1 = solver.resume
|
88
|
-
expect(outcome1).to be_success
|
89
|
-
expect(outcome1.associations['q'].first.value).to eq(olive)
|
90
|
-
|
91
|
-
# Second solution
|
92
|
-
outcome2 = solver.resume
|
93
|
-
expect(outcome2).to be_success
|
94
|
-
expect(outcome2.associations['q'].first.value).to eq(oil)
|
95
|
-
expect(solver.resume).to be_nil
|
96
|
-
end
|
97
|
-
end # context
|
98
|
-
end # describe
|
99
|
-
end # module
|
100
|
-
end # module
|
@@ -1,144 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../spec_helper' # Use the RSpec framework
|
4
|
-
require_relative '../support/factory_atomic'
|
5
|
-
require_relative '../../lib/mini_kraken/core/log_var'
|
6
|
-
require_relative '../../lib/mini_kraken/core/log_var_ref'
|
7
|
-
require_relative '../../lib/mini_kraken/core/outcome'
|
8
|
-
|
9
|
-
# Load the class under test
|
10
|
-
require_relative '../../lib/mini_kraken/core/environment'
|
11
|
-
|
12
|
-
module MiniKraken
|
13
|
-
module Core
|
14
|
-
describe Environment do
|
15
|
-
include MiniKraken::FactoryAtomic # Use mix-in module
|
16
|
-
|
17
|
-
subject { Environment.new }
|
18
|
-
|
19
|
-
context 'Initialization:' do
|
20
|
-
it 'should be initialized with an optional parent' do
|
21
|
-
expect { Environment.new }.not_to raise_error
|
22
|
-
parent = Environment.new
|
23
|
-
expect { Environment.new(parent) }.not_to raise_error
|
24
|
-
end
|
25
|
-
|
26
|
-
it "shouldn't have variable by default" do
|
27
|
-
expect(subject.vars).to be_empty
|
28
|
-
end
|
29
|
-
|
30
|
-
it "shouldn't have associations by default" do
|
31
|
-
expect(subject.associations).to be_empty
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'shold know its parent (if any)' do
|
35
|
-
# Case: no parent
|
36
|
-
expect(subject.parent).to be_nil
|
37
|
-
|
38
|
-
# Case: there is a parent
|
39
|
-
child = Environment.new(subject)
|
40
|
-
expect(child.parent).to eq(subject)
|
41
|
-
end
|
42
|
-
end # context
|
43
|
-
|
44
|
-
context 'Provided services:' do
|
45
|
-
let(:var_a) { LogVar.new('a') }
|
46
|
-
let(:var_b) { LogVar.new('b') }
|
47
|
-
let(:var_c) { LogVar.new('c') }
|
48
|
-
let(:var_c_bis) { LogVar.new('c') }
|
49
|
-
let(:pea) { k_symbol(:pea) }
|
50
|
-
let(:pod) { k_symbol(:pod) }
|
51
|
-
let(:pad) { k_symbol(:pad) }
|
52
|
-
|
53
|
-
it 'should accept the addition of a variable' do
|
54
|
-
subject.add_var(var_a)
|
55
|
-
expect(subject.vars).not_to be_empty
|
56
|
-
expect(subject.vars['a']).to eq(var_a)
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'should accept the addition of multiple variables' do
|
60
|
-
subject.add_var(var_a)
|
61
|
-
expect(subject.vars).not_to be_empty
|
62
|
-
subject.add_var(var_b)
|
63
|
-
expect(subject.vars['a']).to eq(var_a)
|
64
|
-
expect(subject.vars['b']).to eq(var_b)
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'should accept the addition of an association' do
|
68
|
-
subject.add_var(var_a)
|
69
|
-
assoc = subject.add_assoc('a', pea)
|
70
|
-
expect(subject.associations.size).to eq(1)
|
71
|
-
expect(subject.associations['a']).to eq([assoc])
|
72
|
-
end
|
73
|
-
|
74
|
-
it 'should tell that a newborn variable is fresh' do
|
75
|
-
subject.add_var(var_a)
|
76
|
-
|
77
|
-
# By default, a variable is fresh...
|
78
|
-
expect(subject.fresh?(var_a)).to be_truthy
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should tell variable associated with a literal value isn't fresh" do
|
82
|
-
subject.add_var(var_a)
|
83
|
-
|
84
|
-
# Let's associate an atomic term...
|
85
|
-
subject.add_assoc('a', pea)
|
86
|
-
|
87
|
-
expect(subject.fresh?(var_a)).to be_falsey
|
88
|
-
end
|
89
|
-
|
90
|
-
it 'should cope with a variable associated with another variable' do
|
91
|
-
subject.add_var(var_a)
|
92
|
-
subject.add_var(var_b)
|
93
|
-
|
94
|
-
# Let's associate a with (fresh) b
|
95
|
-
subject.add_assoc(var_a, var_b)
|
96
|
-
expect(subject.fresh?(var_a)).to be_truthy
|
97
|
-
|
98
|
-
# Now associate b with something ground...
|
99
|
-
subject.add_assoc(var_b, pea)
|
100
|
-
|
101
|
-
# b is no more fresh, so is ... a
|
102
|
-
expect(subject.fresh?(var_b)).to be_falsey
|
103
|
-
expect(subject.fresh?(var_a)).to be_falsey
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'should remove all associations' do
|
107
|
-
subject.add_var(var_a)
|
108
|
-
subject.add_assoc(var_a, pea)
|
109
|
-
|
110
|
-
subject.add_var(var_b)
|
111
|
-
subject.add_assoc(var_b, pod)
|
112
|
-
|
113
|
-
subject.clear
|
114
|
-
expect(subject.fresh?(var_a)).to be_truthy
|
115
|
-
expect(subject.fresh?(var_a)).to be_truthy
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'should propagate associations up in the environment hierarchy' do
|
119
|
-
parent = Environment.new
|
120
|
-
parent.add_var(var_a)
|
121
|
-
instance = Environment.new(parent)
|
122
|
-
instance.add_var(var_b)
|
123
|
-
|
124
|
-
outcome = Outcome.new(:"#s", instance)
|
125
|
-
outcome.add_assoc(var_a, pea)
|
126
|
-
outcome.add_assoc(var_b, pod)
|
127
|
-
expect(outcome.associations.size).to eq(2)
|
128
|
-
|
129
|
-
# Propagate: outcome -> .. -> instance
|
130
|
-
instance.propagate(outcome)
|
131
|
-
expect(outcome.associations.size).to eq(1)
|
132
|
-
expect(instance.associations[var_b.name]).not_to be_nil
|
133
|
-
expect(parent.associations[var_a.name]).to be_nil
|
134
|
-
|
135
|
-
# Propagate: outcome -> .. -> parent
|
136
|
-
parent.propagate(outcome)
|
137
|
-
expect(outcome.associations).to be_empty
|
138
|
-
expect(parent.associations[var_b.name]).to be_nil
|
139
|
-
expect(parent.associations[var_a.name]).not_to be_nil
|
140
|
-
end
|
141
|
-
end # context
|
142
|
-
end # describe
|
143
|
-
end # module
|
144
|
-
end # module
|
data/spec/core/equals_spec.rb
DELETED
@@ -1,319 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../spec_helper' # Use the RSpec framework
|
4
|
-
require_relative '../../lib/mini_kraken/core/environment'
|
5
|
-
|
6
|
-
require_relative '../support/factory_atomic'
|
7
|
-
require_relative '../support/factory_methods'
|
8
|
-
# Load the class under test
|
9
|
-
require_relative '../../lib/mini_kraken/core/equals'
|
10
|
-
|
11
|
-
module MiniKraken
|
12
|
-
module Core
|
13
|
-
describe Equals do
|
14
|
-
include MiniKraken::FactoryAtomic # Use mix-in module
|
15
|
-
include FactoryMethods
|
16
|
-
|
17
|
-
subject { Equals.instance }
|
18
|
-
|
19
|
-
context 'Initialization:' do
|
20
|
-
it 'should be created without argument' do
|
21
|
-
expect { Equals.instance }.not_to raise_error
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'should know its name' do
|
25
|
-
expect(subject.name).to eq('equals')
|
26
|
-
end
|
27
|
-
end # context
|
28
|
-
|
29
|
-
context 'Provided services:' do
|
30
|
-
def build_var(aName)
|
31
|
-
new_var = LogVar.new(aName)
|
32
|
-
env.add_var(new_var)
|
33
|
-
new_var
|
34
|
-
end
|
35
|
-
|
36
|
-
def build_var_ref(aName)
|
37
|
-
LogVarRef.new(aName)
|
38
|
-
end
|
39
|
-
|
40
|
-
def solve_for(arg1, arg2)
|
41
|
-
solver = subject.solver_for([arg1, arg2], env)
|
42
|
-
outcome = solver.resume
|
43
|
-
env.propagate(outcome)
|
44
|
-
outcome
|
45
|
-
end
|
46
|
-
|
47
|
-
let(:pea) { k_symbol(:pea) }
|
48
|
-
let(:pod) { k_symbol(:pod) }
|
49
|
-
let(:sample_cons) { Composite::ConsCell.new(pea) }
|
50
|
-
let(:a_composite) { Composite::ConsCell.new(pod) }
|
51
|
-
let(:env) { Environment.new }
|
52
|
-
let(:var_q) { build_var('q') }
|
53
|
-
let(:ref_q) do
|
54
|
-
var_q # Force dependency
|
55
|
-
build_var_ref('q')
|
56
|
-
end
|
57
|
-
let(:ref_q_bis) do
|
58
|
-
var_q # Force dependency
|
59
|
-
build_var_ref('q')
|
60
|
-
end
|
61
|
-
let(:var_x) { build_var('x') }
|
62
|
-
let(:ref_x) do
|
63
|
-
var_x # Force dependency
|
64
|
-
build_var_ref('x')
|
65
|
-
end
|
66
|
-
let(:var_y) { build_var('y') }
|
67
|
-
let(:ref_y) do
|
68
|
-
var_y # Force dependency
|
69
|
-
build_var_ref('y')
|
70
|
-
end
|
71
|
-
|
72
|
-
|
73
|
-
it 'should succeed for equal literal arguments' do
|
74
|
-
result = solve_for(pea, pea)
|
75
|
-
|
76
|
-
expect(result).to be_kind_of(Outcome)
|
77
|
-
expect(result.resultant).to eq(:"#s")
|
78
|
-
expect(result.associations).to be_empty
|
79
|
-
expect(var_q.fresh?(env)).to be_truthy
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'should fail for inequal literal arguments' do
|
83
|
-
result = solve_for(pea, pod)
|
84
|
-
|
85
|
-
expect(result.resultant).to eq(:"#u")
|
86
|
-
expect(result.associations).to be_empty
|
87
|
-
expect(var_q.fresh?(env)).to be_truthy
|
88
|
-
end
|
89
|
-
|
90
|
-
it 'should fail for one left literal and one composite arguments' do
|
91
|
-
result = solve_for(pea, sample_cons)
|
92
|
-
|
93
|
-
expect(result.resultant).to eq(:"#u")
|
94
|
-
expect(result.associations).to be_empty
|
95
|
-
expect(var_q.fresh?(env)).to be_truthy
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'should fail for one right literal and one composite arguments' do
|
99
|
-
# Reasoned S2, frame 1:44
|
100
|
-
# (== '(pea) 'pea) ;; => #u
|
101
|
-
result = solve_for(sample_cons, pea)
|
102
|
-
|
103
|
-
expect(result.resultant).to eq(:"#u")
|
104
|
-
expect(result.associations).to be_empty
|
105
|
-
expect(var_q.fresh?(env)).to be_truthy
|
106
|
-
end
|
107
|
-
|
108
|
-
it 'should succeed for a right-handed fresh argument' do
|
109
|
-
result = solve_for(pea, ref_q)
|
110
|
-
|
111
|
-
expect(result).to be_success
|
112
|
-
expect(env.associations.size).to eq(1)
|
113
|
-
expect(env.associations['q'].first.value).to eq(pea)
|
114
|
-
expect(var_q.fresh?(result)).to be_falsey
|
115
|
-
expect(ref_q.values(result).first).to eq(pea)
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'should succeed for a left-handed fresh argument' do
|
119
|
-
result = solve_for(ref_q, pea)
|
120
|
-
|
121
|
-
expect(result).to be_success
|
122
|
-
expect(env.associations.size).to eq(1)
|
123
|
-
expect(env.associations['q'].first.value).to eq(pea)
|
124
|
-
expect(var_q.fresh?(result)).to be_falsey
|
125
|
-
expect(ref_q.values(result).first).to eq(pea)
|
126
|
-
end
|
127
|
-
|
128
|
-
it 'should succeed for a right-handed bound argument equal constant' do
|
129
|
-
ref_q.associate(pod, env)
|
130
|
-
|
131
|
-
result = solve_for(pod, ref_q)
|
132
|
-
expect(result).to be_success
|
133
|
-
expect(env.associations.size).to eq(1) # No new association
|
134
|
-
expect(ref_q.fresh?(result)).not_to be_truthy
|
135
|
-
expect(ref_q.values(result).first).to eq(pod)
|
136
|
-
end
|
137
|
-
|
138
|
-
it 'should succeed for a left-handed bound argument equal constant' do
|
139
|
-
ref_q.associate(pod, env)
|
140
|
-
|
141
|
-
result = solve_for(ref_q, pod)
|
142
|
-
expect(result).to be_success
|
143
|
-
expect(result.associations).to be_empty
|
144
|
-
expect(ref_q.fresh?(result)).to be_falsey
|
145
|
-
expect(ref_q.values(result).first).to eq(pod)
|
146
|
-
end
|
147
|
-
|
148
|
-
it 'should fail for a right-handed bound argument to a distinct literal' do
|
149
|
-
ref_q.associate(pod, env)
|
150
|
-
|
151
|
-
result = solve_for(pea, ref_q)
|
152
|
-
expect(result).not_to be_success
|
153
|
-
expect(result.associations).to be_empty
|
154
|
-
expect(ref_q.fresh?(result)).to be_falsey
|
155
|
-
expect(ref_q.values(result).first).to eq(pod)
|
156
|
-
end
|
157
|
-
|
158
|
-
it 'should fail for a left-handed bound argument to a distinct literal' do
|
159
|
-
ref_q.associate(pod, env)
|
160
|
-
|
161
|
-
result = solve_for(ref_q, pea)
|
162
|
-
expect(result).not_to be_success
|
163
|
-
expect(result.associations).to be_empty
|
164
|
-
expect(ref_q.fresh?(result)).to be_falsey
|
165
|
-
expect(ref_q.values(result).first).to eq(pod)
|
166
|
-
end
|
167
|
-
|
168
|
-
it 'should succeed for a composite and right-handed fresh argument' do
|
169
|
-
result = solve_for(sample_cons, ref_q)
|
170
|
-
|
171
|
-
expect(result).to be_success
|
172
|
-
expect(env.associations.size).to eq(1)
|
173
|
-
expect(ref_q.fresh?(result)).to be_falsey
|
174
|
-
expect(ref_q.values(result).first).to eq(sample_cons)
|
175
|
-
end
|
176
|
-
|
177
|
-
it 'should succeed for composite and left-handed fresh argument' do
|
178
|
-
result = solve_for(ref_q, sample_cons)
|
179
|
-
|
180
|
-
expect(result).to be_success
|
181
|
-
expect(env.associations.size).to eq(1)
|
182
|
-
expect(ref_q.fresh?(result)).to be_falsey
|
183
|
-
expect(ref_q.values(result).first).to eq(sample_cons)
|
184
|
-
end
|
185
|
-
|
186
|
-
it 'should succeed for a right-handed bound equal argument' do
|
187
|
-
ref_q.associate(sample_cons, env)
|
188
|
-
composite = Composite::ConsCell.new(pea)
|
189
|
-
result = solve_for(composite, ref_q)
|
190
|
-
|
191
|
-
expect(result).to be_success
|
192
|
-
expect(result.associations).to be_empty
|
193
|
-
expect(ref_q.fresh?(result)).not_to be_truthy
|
194
|
-
expect(ref_q.values(result).first).to eq(sample_cons)
|
195
|
-
end
|
196
|
-
|
197
|
-
it 'should succeed for a left-handed bound equal argument' do
|
198
|
-
ref_q.associate(sample_cons, env)
|
199
|
-
composite = Composite::ConsCell.new(pea)
|
200
|
-
result = solve_for(ref_q, composite)
|
201
|
-
|
202
|
-
expect(result).to be_success
|
203
|
-
expect(result.associations).to be_empty
|
204
|
-
expect(ref_q.fresh?(result)).not_to be_truthy
|
205
|
-
expect(ref_q.values(result).first).to eq(sample_cons)
|
206
|
-
end
|
207
|
-
|
208
|
-
it 'should succeed for a right-handed bound unequal argument' do
|
209
|
-
ref_q.associate(sample_cons, env)
|
210
|
-
composite = Composite::ConsCell.new(pod)
|
211
|
-
result = solve_for(composite, ref_q)
|
212
|
-
|
213
|
-
expect(result).to be_failure
|
214
|
-
expect(result.associations).to be_empty
|
215
|
-
expect(ref_q.fresh?(result)).not_to be_truthy
|
216
|
-
expect(ref_q.values(result).first).to eq(sample_cons)
|
217
|
-
end
|
218
|
-
|
219
|
-
it 'should succeed for a left-handed bound unequal argument' do
|
220
|
-
ref_q.associate(sample_cons, env)
|
221
|
-
composite = Composite::ConsCell.new(pod)
|
222
|
-
result = solve_for(ref_q, composite)
|
223
|
-
|
224
|
-
expect(result).not_to be_success
|
225
|
-
expect(result.associations).to be_empty
|
226
|
-
expect(ref_q.fresh?(result)).not_to be_truthy
|
227
|
-
expect(ref_q.values(result).first).to eq(sample_cons)
|
228
|
-
end
|
229
|
-
|
230
|
-
it 'should succeed for both identical fresh arguments' do
|
231
|
-
result = solve_for(ref_q, ref_q)
|
232
|
-
|
233
|
-
expect(result).to be_success
|
234
|
-
expect(result.associations).to be_empty
|
235
|
-
expect(ref_q.fresh?(result)).to be_truthy
|
236
|
-
end
|
237
|
-
|
238
|
-
it 'should succeed for both same fresh arguments' do
|
239
|
-
result = solve_for(ref_q, ref_q_bis)
|
240
|
-
|
241
|
-
expect(result).to be_success
|
242
|
-
expect(result.associations).to be_empty
|
243
|
-
expect(ref_q.fresh?(result)).to be_truthy
|
244
|
-
expect(ref_q_bis.fresh?(result)).to be_truthy
|
245
|
-
end
|
246
|
-
|
247
|
-
it 'should succeed for both distinct fresh arguments' do
|
248
|
-
result = solve_for(ref_x, ref_y)
|
249
|
-
|
250
|
-
expect(result).to be_success
|
251
|
-
expect(env.associations).to be_empty # Symmetric association
|
252
|
-
expect(ref_x.fresh?(result)).to be_truthy
|
253
|
-
expect(ref_y.fresh?(result)).to be_truthy
|
254
|
-
end
|
255
|
-
|
256
|
-
it 'should succeed for arguments bound to equal values' do
|
257
|
-
ref_x.associate(pea, env)
|
258
|
-
ref_y.associate(pea, env)
|
259
|
-
expect(ref_x.fresh?(env)).to be_falsey
|
260
|
-
expect(ref_y.fresh?(env)).to be_falsey
|
261
|
-
|
262
|
-
result = solve_for(ref_x, ref_y)
|
263
|
-
expect(result).to be_success
|
264
|
-
expect(result.associations).to be_empty
|
265
|
-
end
|
266
|
-
|
267
|
-
it 'should fail for arguments bound unequal values' do
|
268
|
-
ref_x.associate(pea, env)
|
269
|
-
ref_y.associate(pod, env)
|
270
|
-
expect(ref_x.fresh?(env)).to be_falsey
|
271
|
-
expect(ref_y.fresh?(env)).to be_falsey
|
272
|
-
|
273
|
-
result = solve_for(ref_x, ref_y)
|
274
|
-
expect(result).not_to be_success
|
275
|
-
expect(result.associations).to be_empty
|
276
|
-
end
|
277
|
-
|
278
|
-
it 'should unify composite terms with variables' do
|
279
|
-
# Reasoned S2, frame 1:36
|
280
|
-
# (run* q (fresh (x) (== '(((,q)) ,x) `(((,x)) pod)))) ;; => ('pod)
|
281
|
-
expr1 = cons(cons(cons(ref_q)), cons(ref_x))
|
282
|
-
expect(expr1.to_s).to eq('(((q)) x)')
|
283
|
-
expr2 = cons(cons(cons(ref_x)), cons(pod))
|
284
|
-
expect(expr2.to_s).to eq('(((x)) :pod)')
|
285
|
-
|
286
|
-
result = solve_for(expr1, expr2)
|
287
|
-
expect(result).to be_success
|
288
|
-
expect(ref_x.fresh?(env)).to be_falsey
|
289
|
-
expect(ref_q.fresh?(env)).to be_falsey
|
290
|
-
end
|
291
|
-
|
292
|
-
it 'should fail for one right literal and one composite arguments' do
|
293
|
-
# Reasoned S2, frame 1:46
|
294
|
-
# x associated to ('pea 'pod)
|
295
|
-
# (== '(,x) x) ;; => #u
|
296
|
-
expr1 = cons(pea, cons(pod))
|
297
|
-
ref_x.associate(expr1, env)
|
298
|
-
expr2 = cons(ref_x)
|
299
|
-
result = solve_for(expr2, ref_x)
|
300
|
-
|
301
|
-
expect(result.resultant).to eq(:"#u")
|
302
|
-
expect(result.associations).to be_empty
|
303
|
-
end
|
304
|
-
|
305
|
-
it 'should unify variables with a list' do
|
306
|
-
# Variant of Reasoned S2, frame 2:3
|
307
|
-
# (== (cons q x) '(a c o r n)) ;; q => :a, x => '(c o r n)
|
308
|
-
expr1 = cons(ref_q, ref_x)
|
309
|
-
acorn = cons(k_symbol(:a), cons(k_symbol(:c),
|
310
|
-
cons(k_symbol(:o), cons(k_symbol(:r), cons(k_symbol(:n))))))
|
311
|
-
result = solve_for(expr1, acorn)
|
312
|
-
expect(result.resultant).to eq(:"#s")
|
313
|
-
q_value = env.associations['q'].first.value
|
314
|
-
expect(q_value).to eq(:a)
|
315
|
-
end
|
316
|
-
end # context
|
317
|
-
end # describe
|
318
|
-
end # module
|
319
|
-
end # module
|