mini_kraken 0.1.13 → 0.2.04
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +334 -0
- data/CHANGELOG.md +55 -0
- data/README.md +95 -13
- data/lib/mini_kraken.rb +7 -1
- data/lib/mini_kraken/atomic/all_atomic.rb +4 -0
- data/lib/mini_kraken/{core → atomic}/atomic_term.rb +24 -9
- data/lib/mini_kraken/atomic/k_boolean.rb +42 -0
- data/lib/mini_kraken/{core → atomic}/k_integer.rb +2 -1
- data/lib/mini_kraken/atomic/k_symbol.rb +29 -0
- data/lib/mini_kraken/{core → composite}/composite_term.rb +6 -4
- data/lib/mini_kraken/composite/cons_cell.rb +132 -0
- data/lib/mini_kraken/composite/cons_cell_visitor.rb +102 -0
- data/lib/mini_kraken/core/any_value.rb +5 -1
- data/lib/mini_kraken/core/association_walker.rb +14 -14
- data/lib/mini_kraken/core/binary_relation.rb +10 -10
- data/lib/mini_kraken/core/conde.rb +1 -1
- data/lib/mini_kraken/core/conj2.rb +3 -3
- data/lib/mini_kraken/core/def_relation.rb +4 -0
- data/lib/mini_kraken/core/disj2.rb +2 -2
- data/lib/mini_kraken/core/environment.rb +2 -2
- data/lib/mini_kraken/core/equals.rb +167 -135
- data/lib/mini_kraken/core/formal_ref.rb +2 -1
- data/lib/mini_kraken/core/goal.rb +4 -2
- data/lib/mini_kraken/core/goal_relation.rb +2 -2
- data/lib/mini_kraken/core/goal_template.rb +44 -2
- data/lib/mini_kraken/core/{variable.rb → log_var.rb} +10 -3
- data/lib/mini_kraken/core/{variable_ref.rb → log_var_ref.rb} +10 -3
- data/lib/mini_kraken/core/outcome.rb +11 -1
- data/lib/mini_kraken/core/tap.rb +46 -0
- data/lib/mini_kraken/core/vocabulary.rb +14 -10
- data/lib/mini_kraken/glue/dsl.rb +240 -0
- data/lib/mini_kraken/glue/fresh_env.rb +37 -4
- data/lib/mini_kraken/glue/fresh_env_factory.rb +83 -0
- data/lib/mini_kraken/glue/run_star_expression.rb +5 -7
- data/lib/mini_kraken/version.rb +1 -1
- data/mini_kraken.gemspec +6 -3
- data/spec/.rubocop.yml +13 -0
- data/spec/atomic/atomic_term_spec.rb +94 -0
- data/spec/{core → atomic}/k_boolean_spec.rb +25 -34
- data/spec/{core → atomic}/k_symbol_spec.rb +7 -11
- data/spec/{core → composite}/cons_cell_spec.rb +44 -7
- data/spec/composite/cons_cell_visitor_spec.rb +145 -0
- data/spec/core/association_spec.rb +6 -4
- data/spec/core/association_walker_spec.rb +8 -6
- data/spec/core/conde_spec.rb +29 -27
- data/spec/core/conj2_spec.rb +17 -15
- data/spec/core/def_relation_spec.rb +15 -12
- data/spec/core/disj2_spec.rb +16 -15
- data/spec/core/duck_fiber_spec.rb +2 -2
- data/spec/core/environment_spec.rb +12 -10
- data/spec/core/equals_spec.rb +46 -31
- data/spec/core/goal_spec.rb +8 -7
- data/spec/core/goal_template_spec.rb +5 -5
- data/spec/core/{variable_ref_spec.rb → log_var_ref_spec.rb} +7 -4
- data/spec/core/{variable_spec.rb → log_var_spec.rb} +4 -4
- data/spec/core/outcome_spec.rb +8 -0
- data/spec/core/vocabulary_spec.rb +12 -11
- data/spec/glue/dsl_chap1_spec.rb +634 -0
- data/spec/glue/dsl_chap2_spec.rb +208 -0
- data/spec/glue/fresh_env_factory_spec.rb +99 -0
- data/spec/glue/fresh_env_spec.rb +3 -3
- data/spec/glue/run_star_expression_spec.rb +22 -20
- data/spec/support/factory_atomic.rb +22 -0
- data/spec/support/factory_methods.rb +11 -26
- metadata +40 -22
- data/lib/mini_kraken/core/cons_cell.rb +0 -54
- data/lib/mini_kraken/core/k_boolean.rb +0 -31
- data/lib/mini_kraken/core/k_symbol.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96c07c3751d0c46963e19f8e25dc429a77dbd630c22b32018914ccf36f471689
|
4
|
+
data.tar.gz: 26efce2c03a89fd4129c756ce64daa80783cc9bfcebc0358b493076f33ef9434
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1df0d6a9397a804e35700aaedb1fcc1ae7fb91a4b3b237ffe138be5cc63b51113d1af5a57f5cd216a41217993dce5dc5186d0690ab992d2eab229995321e7dfb
|
7
|
+
data.tar.gz: d5af2983495e31e77e8947bffeca99a9b88b916208cb511a88e3f0e397ec2f105cd60d333d2663b78c4e943edad9e4c76c8aa7eabd07a94a733e9821d63de305
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,334 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'exp/**/*'
|
4
|
+
|
5
|
+
Layout/ArgumentAlignment:
|
6
|
+
Enabled: false
|
7
|
+
|
8
|
+
Layout/ArrayAlignment:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: with_fixed_indentation
|
11
|
+
|
12
|
+
Layout/CaseIndentation:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Layout/ClosingHeredocIndentation:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Layout/CommentIndentation:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Layout/ElseAlignment:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Layout/EmptyLines:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Layout/EndAlignment:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Layout/EndOfLine:
|
31
|
+
Enabled: true
|
32
|
+
EnforcedStyle: lf
|
33
|
+
|
34
|
+
Layout/FirstArgumentIndentation:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Layout/IndentationWidth:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Layout/IndentationConsistency:
|
41
|
+
Enabled: true
|
42
|
+
|
43
|
+
Layout/HeredocIndentation:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Layout/MultilineHashBraceLayout:
|
47
|
+
Enabled: true
|
48
|
+
|
49
|
+
Layout/MultilineMethodCallBraceLayout:
|
50
|
+
Enabled: true
|
51
|
+
EnforcedStyle: same_line
|
52
|
+
|
53
|
+
Layout/SpaceAroundOperators:
|
54
|
+
Enabled: true
|
55
|
+
|
56
|
+
Layout/SpaceInsideParens:
|
57
|
+
Enabled: true
|
58
|
+
|
59
|
+
Layout/IndentationStyle:
|
60
|
+
Enabled: true
|
61
|
+
|
62
|
+
Layout/SpaceAroundMethodCallOperator:
|
63
|
+
Enabled: true
|
64
|
+
|
65
|
+
Layout/TrailingEmptyLines:
|
66
|
+
Enabled: true
|
67
|
+
|
68
|
+
Layout/TrailingWhitespace:
|
69
|
+
Enabled: true
|
70
|
+
|
71
|
+
Lint/Loop:
|
72
|
+
Enabled: true
|
73
|
+
|
74
|
+
Lint/RaiseException:
|
75
|
+
Enabled: true
|
76
|
+
|
77
|
+
Lint/RescueException:
|
78
|
+
Enabled: true
|
79
|
+
|
80
|
+
Lint/StructNewOverride:
|
81
|
+
Enabled: true
|
82
|
+
|
83
|
+
Lint/UnusedMethodArgument:
|
84
|
+
Enabled: true
|
85
|
+
|
86
|
+
Lint/UselessAccessModifier:
|
87
|
+
Enabled: true
|
88
|
+
|
89
|
+
Lint/Void:
|
90
|
+
Enabled: false
|
91
|
+
|
92
|
+
Lint/UselessAssignment:
|
93
|
+
Enabled: true
|
94
|
+
|
95
|
+
Metrics/AbcSize:
|
96
|
+
Enabled: false
|
97
|
+
|
98
|
+
Metrics/BlockLength:
|
99
|
+
Enabled: true
|
100
|
+
Max: 350
|
101
|
+
|
102
|
+
Metrics/BlockNesting:
|
103
|
+
Enabled: true
|
104
|
+
Max: 5
|
105
|
+
|
106
|
+
Metrics/ClassLength:
|
107
|
+
Enabled: true
|
108
|
+
Max: 350
|
109
|
+
|
110
|
+
Metrics/CyclomaticComplexity:
|
111
|
+
Enabled: false
|
112
|
+
|
113
|
+
Layout/LineLength:
|
114
|
+
Enabled: false
|
115
|
+
Max: 90
|
116
|
+
|
117
|
+
Metrics/MethodLength:
|
118
|
+
Enabled: true
|
119
|
+
Max: 50
|
120
|
+
|
121
|
+
Metrics/ModuleLength:
|
122
|
+
Enabled: true
|
123
|
+
Max: 500
|
124
|
+
|
125
|
+
Metrics/PerceivedComplexity:
|
126
|
+
Enabled: false
|
127
|
+
|
128
|
+
Naming/ConstantName:
|
129
|
+
Enabled: false
|
130
|
+
|
131
|
+
Naming/ClassAndModuleCamelCase:
|
132
|
+
Enabled: false
|
133
|
+
|
134
|
+
Naming/BlockParameterName:
|
135
|
+
Enabled: true
|
136
|
+
|
137
|
+
Naming/MethodParameterName:
|
138
|
+
Enabled: false
|
139
|
+
|
140
|
+
Naming/VariableName:
|
141
|
+
Enabled: false
|
142
|
+
|
143
|
+
Style/Alias:
|
144
|
+
Enabled: true
|
145
|
+
|
146
|
+
Layout/HashAlignment:
|
147
|
+
Enabled: false
|
148
|
+
|
149
|
+
Style/AsciiComments:
|
150
|
+
Enabled: false
|
151
|
+
|
152
|
+
Style/BarePercentLiterals:
|
153
|
+
Enabled: false
|
154
|
+
|
155
|
+
Style/BlockComments:
|
156
|
+
Enabled: false
|
157
|
+
|
158
|
+
Style/CharacterLiteral:
|
159
|
+
Enabled: false
|
160
|
+
|
161
|
+
Style/ClassCheck:
|
162
|
+
Enabled: false
|
163
|
+
|
164
|
+
Style/ClassVars:
|
165
|
+
Enabled: false
|
166
|
+
|
167
|
+
Style/ColonMethodCall:
|
168
|
+
Enabled: false
|
169
|
+
|
170
|
+
Style/CommentAnnotation:
|
171
|
+
Enabled: false
|
172
|
+
|
173
|
+
Style/CommentedKeyword:
|
174
|
+
Enabled: false
|
175
|
+
|
176
|
+
Style/ConditionalAssignment:
|
177
|
+
Enabled: false
|
178
|
+
|
179
|
+
Style/DefWithParentheses:
|
180
|
+
Enabled: true
|
181
|
+
|
182
|
+
Style/Documentation:
|
183
|
+
Enabled: false
|
184
|
+
|
185
|
+
Style/ExpandPathArguments:
|
186
|
+
Enabled: false
|
187
|
+
|
188
|
+
Style/ExponentialNotation:
|
189
|
+
Enabled: true
|
190
|
+
|
191
|
+
Style/GuardClause:
|
192
|
+
Enabled: false
|
193
|
+
|
194
|
+
Style/HashEachMethods:
|
195
|
+
Enabled: true
|
196
|
+
|
197
|
+
Style/HashTransformKeys:
|
198
|
+
Enabled: true
|
199
|
+
|
200
|
+
Style/HashTransformValues:
|
201
|
+
Enabled: true
|
202
|
+
|
203
|
+
Style/IfUnlessModifier:
|
204
|
+
Enabled: false
|
205
|
+
|
206
|
+
Style/InverseMethods:
|
207
|
+
Enabled: true
|
208
|
+
|
209
|
+
Style/MissingRespondToMissing:
|
210
|
+
Enabled: false
|
211
|
+
|
212
|
+
Style/Next:
|
213
|
+
Enabled: false
|
214
|
+
|
215
|
+
Style/NumericLiterals:
|
216
|
+
Enabled: false
|
217
|
+
|
218
|
+
Style/RaiseArgs:
|
219
|
+
Enabled: true
|
220
|
+
|
221
|
+
Style/RedundantReturn:
|
222
|
+
Enabled: false
|
223
|
+
|
224
|
+
Style/RedundantSelf:
|
225
|
+
Enabled: true
|
226
|
+
|
227
|
+
Style/RegexpLiteral:
|
228
|
+
Enabled: false
|
229
|
+
|
230
|
+
Style/PercentLiteralDelimiters:
|
231
|
+
Enabled: false
|
232
|
+
|
233
|
+
Style/StderrPuts:
|
234
|
+
Enabled: false
|
235
|
+
|
236
|
+
Style/StringLiterals:
|
237
|
+
Enabled: true
|
238
|
+
|
239
|
+
Style/TernaryParentheses:
|
240
|
+
Enabled: false
|
241
|
+
|
242
|
+
Style/UnlessElse:
|
243
|
+
Enabled: false
|
244
|
+
|
245
|
+
# Rubocop complains when it doesn't find an explicit setting for the following cops:
|
246
|
+
Layout/EmptyLinesAroundAttributeAccessor:
|
247
|
+
Enabled: true
|
248
|
+
|
249
|
+
Lint/BinaryOperatorWithIdenticalOperands:
|
250
|
+
Enabled: true
|
251
|
+
|
252
|
+
Lint/DeprecatedOpenSSLConstant:
|
253
|
+
Enabled: true
|
254
|
+
|
255
|
+
Lint/DuplicateElsifCondition:
|
256
|
+
Enabled: true
|
257
|
+
|
258
|
+
Lint/DuplicateRescueException:
|
259
|
+
Enabled: true
|
260
|
+
|
261
|
+
Lint/EmptyConditionalBody:
|
262
|
+
Enabled: true
|
263
|
+
|
264
|
+
Lint/FloatComparison:
|
265
|
+
Enabled: true
|
266
|
+
|
267
|
+
Lint/MissingSuper:
|
268
|
+
Enabled: true
|
269
|
+
|
270
|
+
Lint/MixedRegexpCaptureTypes:
|
271
|
+
Enabled: true
|
272
|
+
|
273
|
+
Lint/OutOfRangeRegexpRef:
|
274
|
+
Enabled: true
|
275
|
+
|
276
|
+
Lint/SelfAssignment:
|
277
|
+
Enabled: true
|
278
|
+
|
279
|
+
Lint/TopLevelReturnWithArgument:
|
280
|
+
Enabled: true
|
281
|
+
|
282
|
+
Lint/UnreachableLoop:
|
283
|
+
Enabled: true
|
284
|
+
|
285
|
+
Style/AccessorGrouping:
|
286
|
+
Enabled: true
|
287
|
+
|
288
|
+
Style/ArrayCoercion:
|
289
|
+
Enabled: true
|
290
|
+
|
291
|
+
Style/BisectedAttrAccessor:
|
292
|
+
Enabled: true
|
293
|
+
|
294
|
+
Style/CaseLikeIf:
|
295
|
+
Enabled: true
|
296
|
+
|
297
|
+
Style/ExplicitBlockArgument:
|
298
|
+
Enabled: true
|
299
|
+
|
300
|
+
Style/GlobalStdStream:
|
301
|
+
Enabled: true
|
302
|
+
|
303
|
+
Style/HashAsLastArrayItem:
|
304
|
+
Enabled: true
|
305
|
+
|
306
|
+
Style/HashLikeCase:
|
307
|
+
Enabled: true
|
308
|
+
|
309
|
+
Style/OptionalBooleanParameter:
|
310
|
+
Enabled: true
|
311
|
+
|
312
|
+
Style/RedundantAssignment:
|
313
|
+
Enabled: true
|
314
|
+
|
315
|
+
Style/RedundantFetchBlock:
|
316
|
+
Enabled: true
|
317
|
+
|
318
|
+
Style/RedundantFileExtensionInRequire:
|
319
|
+
Enabled: true
|
320
|
+
|
321
|
+
Style/RedundantRegexpCharacterClass:
|
322
|
+
Enabled: true
|
323
|
+
|
324
|
+
Style/RedundantRegexpEscape:
|
325
|
+
Enabled: true
|
326
|
+
|
327
|
+
Style/SingleArgumentDig:
|
328
|
+
Enabled: true
|
329
|
+
|
330
|
+
Style/SlicingWithRange:
|
331
|
+
Enabled: true
|
332
|
+
|
333
|
+
Style/StringConcatenation:
|
334
|
+
Enabled: true
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,58 @@
|
|
1
|
+
## [0.2.04] - 2020-09-02
|
2
|
+
- Intermediate version before vast code rework.
|
3
|
+
|
4
|
+
### CHANGED
|
5
|
+
- Classes `KBoolean`, `KSymbol`, `KInteger` moved to namespace `Atomic`
|
6
|
+
- Classes `ConsCell`, `ConsCellVisitor` moved to namespace `Composite`
|
7
|
+
- Class `Variable` renamed to `LogVar`
|
8
|
+
- Class `VariableRef` renamed to `LogVarRef`
|
9
|
+
|
10
|
+
## [0.2.03] - 2020-09-02
|
11
|
+
- The DSL (Domain Specific Language) supports the `caro` relation & passes frames up to 2-8 from Chapter 2.
|
12
|
+
|
13
|
+
### NEW
|
14
|
+
- Class `ConsCellVisitor`. Its method `df_visitor` builds a Fiber that walks over a ConsCell (list/graph).
|
15
|
+
- Method `Outcome#failure?`
|
16
|
+
- Method `Outcome#prune!` for removing associations of transient variables.
|
17
|
+
- Method `VariableRef#to_s` for providing a text representation of a variable reference
|
18
|
+
- Method `Vocabulary#prune` for removing associations of transient variables.
|
19
|
+
- Class `FreshEnvFactory` as its name implies, is used to build `FreshEnv` instances.
|
20
|
+
|
21
|
+
### CHANGED
|
22
|
+
- Method `Outcome#successful?` renamed to `Outcome#success?`
|
23
|
+
|
24
|
+
### FIXED
|
25
|
+
- Method `Equals#solver_for` now prunes associations of transient variables.
|
26
|
+
- Method `Equals#unify_composite_terms` now copes with Conscell vs. VariableRef unification.
|
27
|
+
|
28
|
+
## [0.2.02] - 2020-08-08
|
29
|
+
- The DSL (Domain Specific Language) now supports `conde` and passes all examples from Chapter 1.
|
30
|
+
|
31
|
+
### NEW
|
32
|
+
- Method`Glue::DSL#conde` to implement conde goals.
|
33
|
+
- Method `KBoolean#to_s` returns a text representation of a boolean value.
|
34
|
+
|
35
|
+
## [0.2.01] - 2020-08-07
|
36
|
+
- The DSL (Domain Specific Language) now supports `defrel` and boolean literals.
|
37
|
+
|
38
|
+
### CHANGED
|
39
|
+
- Constructor `DefRelation#initialize` now freezes any new class instance.
|
40
|
+
- Constructor `GoalTemplate#initialize` now freezes any new class instance.
|
41
|
+
- Mixin module `Glue::DSL` new method `defrel` to build custom relations.
|
42
|
+
- File `.rubocop.yml` to please Rubocop 0.89
|
43
|
+
|
44
|
+
## [0.2.00] - 2020-07-12
|
45
|
+
- First release of DSL (Domain Specific Language)
|
46
|
+
- Fix defect for fused variables that remain fresh
|
47
|
+
|
48
|
+
### NEW
|
49
|
+
- Mix-in module `Glue::DSL` hosting methods for implementing the DSL.
|
50
|
+
- Method `ConsCell#to_s` uses the Lisp convention for representing lists.
|
51
|
+
|
52
|
+
### CHANGED
|
53
|
+
- File `README.md` Added a couple of examples of DSL use.
|
54
|
+
- Method `AnyValue#==` can compare with symbols with format '_' + integer literal (e.g. :_0).
|
55
|
+
|
1
56
|
## [0.1.13] - 2020-07-01
|
2
57
|
- Cover all frames from Chapter One of "Reasoned Scheme" book.
|
3
58
|
- Fix defect for fused variables that remain fresh
|
data/README.md
CHANGED
@@ -4,31 +4,38 @@
|
|
4
4
|
[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/famished-tiger/mini_kraken/blob/master/LICENSE.txt)
|
5
5
|
|
6
6
|
### What is __mini_kraken__ ?
|
7
|
-
|
7
|
+
A library containing an implementation of the [miniKanren](http://minikanren.org/)
|
8
|
+
relational programming language in Ruby.
|
8
9
|
*miniKanren* is a small language for relational (logic) programming.
|
9
10
|
Based on the reference implementation, in Scheme from the "The Reasoned Schemer" book.
|
10
11
|
Daniel P. Friedman, William E. Byrd, Oleg Kiselyov, and Jason Hemann: "The Reasoned Schemer", Second Edition,
|
11
12
|
ISBN: 9780262535519, (2018), MIT Press.
|
12
13
|
|
13
14
|
### Features
|
15
|
+
- Pure Ruby implementation, not a port from another language
|
16
|
+
- Object-Oriented design
|
17
|
+
- No runtime dependencies
|
18
|
+
|
19
|
+
### miniKanren Features
|
14
20
|
- [X] ==
|
15
21
|
- [X] run\*
|
16
|
-
- [X] fresh
|
22
|
+
- [X] fresh
|
23
|
+
- [X] conde
|
17
24
|
- [X] conj2
|
18
25
|
- [X] disj2
|
19
|
-
- [X] defrel
|
26
|
+
- [X] defrel
|
27
|
+
- [X] caro
|
20
28
|
|
21
29
|
### TODO
|
22
|
-
|
30
|
+
|
23
31
|
- [ ] Occurs check
|
24
32
|
|
25
|
-
List-centric relations from Chapter 2
|
26
|
-
- [ ]
|
27
|
-
- [ ]
|
28
|
-
- [ ]
|
29
|
-
- [ ]
|
30
|
-
- [ ]
|
31
|
-
- [ ] singletono
|
33
|
+
List-centric relations from Chapter 2
|
34
|
+
- [ ] cdro
|
35
|
+
- [ ] conso
|
36
|
+
- [ ] nullo
|
37
|
+
- [ ] pairo
|
38
|
+
- [ ] singletono
|
32
39
|
|
33
40
|
## Installation
|
34
41
|
|
@@ -46,9 +53,84 @@ Or install it yourself as:
|
|
46
53
|
|
47
54
|
$ gem install mini_kraken
|
48
55
|
|
49
|
-
##
|
56
|
+
## Examples
|
57
|
+
|
58
|
+
The following __MiniKraken__ examples use its DSL (Domain Specific Language).
|
59
|
+
|
60
|
+
### Example 1
|
61
|
+
Let's first begin with a rather simplistic example.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
require 'mini_kraken' # Load MiniKraken library
|
65
|
+
|
66
|
+
extend(MiniKraken::Glue::DSL) # Add DSL method to self (object in context)
|
67
|
+
|
68
|
+
result = run_star('q', equals(q, :pea))
|
69
|
+
puts result # => (:pea)
|
70
|
+
```
|
71
|
+
|
72
|
+
The two first lines in the above code snippet are pretty standard:
|
73
|
+
- The first line loads the `mini_kraken` library.
|
74
|
+
- The second line add the DSL methods to the current object.
|
75
|
+
|
76
|
+
The next line constitutes a trivial `miniKanren` program.
|
77
|
+
The aim of a `miniKanren` program is to find one or more solutions involving the given logical variable(s)
|
78
|
+
and satisfying one or more goals to the `run_star method.
|
79
|
+
In our example, the `run_star` method instructs `MiniKraken` to find all solutions,
|
80
|
+
knowing that each successful solution:
|
81
|
+
- binds a value to the provided variable `q` and
|
82
|
+
- meets the goal `equals(q, :pea)`.
|
83
|
+
|
84
|
+
The goal `equals(q, :pea)` succeeds because the logical variable `q` is _fresh_ (that is,
|
85
|
+
not yet bound to a value) and will be bound to the symbol `:pea` as a side effect
|
86
|
+
of the goal `equals`.
|
87
|
+
|
88
|
+
So the above program succeeds and the only found solution is obtained by binding
|
89
|
+
the variable `q` to the value :pea. Hence the result of the `puts` method.
|
90
|
+
|
91
|
+
### Example 2
|
92
|
+
The next example illustrates the behavior of a failing `miniKanren` program.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
require 'mini_kraken' # Load MiniKraken library
|
96
|
+
|
97
|
+
extend(MiniKraken::Glue::DSL) # Add DSL method to self (object in context)
|
98
|
+
|
99
|
+
# Following miniKanren program fails
|
100
|
+
result = run_star('q', [equals(q, :pea), equals(q, :pod)])
|
101
|
+
puts result # => ()
|
102
|
+
```
|
103
|
+
In this example, we learn that `run_star` can take multiple goals placed in an array.
|
104
|
+
The program fails to find a solution since it is not possible to satisfy the two `equals` goals simultaneously.
|
105
|
+
In case of failure, the `run_star` returns an empty list represented as `()` in the output.
|
106
|
+
|
107
|
+
|
108
|
+
### Example 3
|
109
|
+
The next example shows the use two logical variables.
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
# In this example and following, one assumes that DSL is loaded as shown in Example 1
|
113
|
+
|
114
|
+
result = run_star(['x', 'y'], [equals(:hello, x), equals(y, :world)])
|
115
|
+
puts result # => ((:hello :world))
|
116
|
+
```
|
50
117
|
|
51
|
-
|
118
|
+
This time, `run_star` takes two logical variables -`x` and `y`- and successfully finds the solution `x = :hello, y = :world`.
|
119
|
+
|
120
|
+
### Example 4
|
121
|
+
The next example shows the use of `disj2` goals.
|
122
|
+
```ruby
|
123
|
+
result = run_star(['x', 'y'],
|
124
|
+
[
|
125
|
+
disj2(equals(x, :blue), equals(x, :red)),
|
126
|
+
disj2(equals(y, :sea), equals(:mountain, y))
|
127
|
+
])
|
128
|
+
puts result # => ((:blue :sea) (:blue :mountain) (:red :sea) (:red :mountain))
|
129
|
+
```
|
130
|
+
|
131
|
+
Here, `run_star` takes two logical variables and two `disj2` goals.
|
132
|
+
A `disj2` succeeds if any of its arguments succeeds.
|
133
|
+
This program finds four distinct solutions for x, y pairs.
|
52
134
|
|
53
135
|
## Development
|
54
136
|
|